Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 b2b3fce1 2022-10-29 op
67 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
68 86c3caaf 2018-03-09 stsp
69 99724ed4 2018-03-10 stsp static const struct got_error *
70 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
71 99724ed4 2018-03-10 stsp {
72 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 99724ed4 2018-03-10 stsp char *path;
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
77 99724ed4 2018-03-10 stsp
78 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
79 99724ed4 2018-03-10 stsp free(path);
80 507dc3bb 2018-12-29 stsp return err;
81 507dc3bb 2018-12-29 stsp }
82 507dc3bb 2018-12-29 stsp
83 507dc3bb 2018-12-29 stsp static const struct got_error *
84 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
85 507dc3bb 2018-12-29 stsp {
86 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
87 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
88 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
89 507dc3bb 2018-12-29 stsp char *path = NULL;
90 507dc3bb 2018-12-29 stsp
91 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
93 507dc3bb 2018-12-29 stsp path = NULL;
94 507dc3bb 2018-12-29 stsp goto done;
95 507dc3bb 2018-12-29 stsp }
96 507dc3bb 2018-12-29 stsp
97 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 507dc3bb 2018-12-29 stsp if (err)
99 507dc3bb 2018-12-29 stsp goto done;
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp if (content) {
102 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
103 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
105 507dc3bb 2018-12-29 stsp goto done;
106 507dc3bb 2018-12-29 stsp }
107 507dc3bb 2018-12-29 stsp }
108 507dc3bb 2018-12-29 stsp
109 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
111 2a57020b 2019-02-20 stsp unlink(tmppath);
112 507dc3bb 2018-12-29 stsp goto done;
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp
115 507dc3bb 2018-12-29 stsp done:
116 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
117 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
118 230a42bd 2019-05-11 jcs free(tmppath);
119 09fe317a 2018-03-11 stsp return err;
120 09fe317a 2018-03-11 stsp }
121 09fe317a 2018-03-11 stsp
122 024e9686 2019-05-14 stsp static const struct got_error *
123 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
124 024e9686 2019-05-14 stsp {
125 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
126 024e9686 2019-05-14 stsp char *refstr = NULL;
127 024e9686 2019-05-14 stsp
128 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
129 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
132 024e9686 2019-05-14 stsp } else {
133 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
134 024e9686 2019-05-14 stsp if (refstr == NULL)
135 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
136 024e9686 2019-05-14 stsp }
137 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 024e9686 2019-05-14 stsp free(refstr);
139 024e9686 2019-05-14 stsp return err;
140 024e9686 2019-05-14 stsp }
141 024e9686 2019-05-14 stsp
142 86c3caaf 2018-03-09 stsp const struct got_error *
143 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
144 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
145 86c3caaf 2018-03-09 stsp {
146 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
147 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
148 ec22038e 2019-03-10 stsp uuid_t uuid;
149 ec22038e 2019-03-10 stsp uint32_t uuid_status;
150 65596e15 2018-12-24 stsp int obj_type;
151 7ac97322 2018-03-11 stsp char *path_got = NULL;
152 1451e70d 2018-03-10 stsp char *formatstr = NULL;
153 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
154 65596e15 2018-12-24 stsp char *basestr = NULL;
155 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
156 65596e15 2018-12-24 stsp
157 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
159 0c48fee2 2019-03-11 stsp goto done;
160 0c48fee2 2019-03-11 stsp }
161 0c48fee2 2019-03-11 stsp
162 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
163 65596e15 2018-12-24 stsp if (err)
164 65596e15 2018-12-24 stsp return err;
165 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
166 65596e15 2018-12-24 stsp if (err)
167 65596e15 2018-12-24 stsp return err;
168 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
170 86c3caaf 2018-03-09 stsp
171 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
172 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
174 0bb8a95e 2018-03-12 stsp }
175 577ec78f 2018-03-11 stsp
176 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
177 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
179 86c3caaf 2018-03-09 stsp goto done;
180 86c3caaf 2018-03-09 stsp }
181 86c3caaf 2018-03-09 stsp
182 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
183 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
189 86c3caaf 2018-03-09 stsp goto done;
190 86c3caaf 2018-03-09 stsp }
191 86c3caaf 2018-03-09 stsp
192 056e7441 2018-03-11 stsp /* Create an empty lock file. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 056e7441 2018-03-11 stsp if (err)
195 056e7441 2018-03-11 stsp goto done;
196 056e7441 2018-03-11 stsp
197 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
198 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 99724ed4 2018-03-10 stsp if (err)
200 86c3caaf 2018-03-09 stsp goto done;
201 86c3caaf 2018-03-09 stsp
202 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
203 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
204 65596e15 2018-12-24 stsp if (err)
205 65596e15 2018-12-24 stsp goto done;
206 65596e15 2018-12-24 stsp
207 65596e15 2018-12-24 stsp /* Record our base commit. */
208 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
209 65596e15 2018-12-24 stsp if (err)
210 65596e15 2018-12-24 stsp goto done;
211 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 99724ed4 2018-03-10 stsp if (err)
213 86c3caaf 2018-03-09 stsp goto done;
214 86c3caaf 2018-03-09 stsp
215 1451e70d 2018-03-10 stsp /* Store path to repository. */
216 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
218 99724ed4 2018-03-10 stsp if (err)
219 86c3caaf 2018-03-09 stsp goto done;
220 86c3caaf 2018-03-09 stsp
221 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
222 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
224 ec22038e 2019-03-10 stsp if (err)
225 ec22038e 2019-03-10 stsp goto done;
226 ec22038e 2019-03-10 stsp
227 ec22038e 2019-03-10 stsp /* Generate UUID. */
228 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
229 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
230 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
231 ec22038e 2019-03-10 stsp goto done;
232 ec22038e 2019-03-10 stsp }
233 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
235 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
236 ec22038e 2019-03-10 stsp goto done;
237 ec22038e 2019-03-10 stsp }
238 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 577ec78f 2018-03-11 stsp if (err)
240 577ec78f 2018-03-11 stsp goto done;
241 577ec78f 2018-03-11 stsp
242 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
243 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 1451e70d 2018-03-10 stsp goto done;
246 1451e70d 2018-03-10 stsp }
247 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 99724ed4 2018-03-10 stsp if (err)
249 1451e70d 2018-03-10 stsp goto done;
250 1451e70d 2018-03-10 stsp
251 86c3caaf 2018-03-09 stsp done:
252 65596e15 2018-12-24 stsp free(commit_id);
253 7ac97322 2018-03-11 stsp free(path_got);
254 1451e70d 2018-03-10 stsp free(formatstr);
255 0bb8a95e 2018-03-12 stsp free(absprefix);
256 65596e15 2018-12-24 stsp free(basestr);
257 ec22038e 2019-03-10 stsp free(uuidstr);
258 86c3caaf 2018-03-09 stsp return err;
259 86c3caaf 2018-03-09 stsp }
260 86c3caaf 2018-03-09 stsp
261 247140b2 2019-02-05 stsp const struct got_error *
262 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 e5dc7198 2018-12-29 stsp const char *path_prefix)
264 e5dc7198 2018-12-29 stsp {
265 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
266 e5dc7198 2018-12-29 stsp
267 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
268 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
270 e5dc7198 2018-12-29 stsp }
271 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
272 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
273 e5dc7198 2018-12-29 stsp free(absprefix);
274 e5dc7198 2018-12-29 stsp return NULL;
275 86c3caaf 2018-03-09 stsp }
276 86c3caaf 2018-03-09 stsp
277 bc70eb79 2019-05-09 stsp const char *
278 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
279 86c3caaf 2018-03-09 stsp {
280 36a38700 2019-05-10 stsp return worktree->head_ref_name;
281 024e9686 2019-05-14 stsp }
282 024e9686 2019-05-14 stsp
283 024e9686 2019-05-14 stsp const struct got_error *
284 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
285 024e9686 2019-05-14 stsp struct got_reference *head_ref)
286 024e9686 2019-05-14 stsp {
287 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
288 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
289 024e9686 2019-05-14 stsp
290 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
292 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
293 024e9686 2019-05-14 stsp path_got = NULL;
294 024e9686 2019-05-14 stsp goto done;
295 024e9686 2019-05-14 stsp }
296 024e9686 2019-05-14 stsp
297 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
298 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
299 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
300 024e9686 2019-05-14 stsp goto done;
301 024e9686 2019-05-14 stsp }
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
304 024e9686 2019-05-14 stsp if (err)
305 024e9686 2019-05-14 stsp goto done;
306 024e9686 2019-05-14 stsp
307 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
308 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
309 024e9686 2019-05-14 stsp done:
310 024e9686 2019-05-14 stsp free(path_got);
311 024e9686 2019-05-14 stsp if (err)
312 024e9686 2019-05-14 stsp free(head_ref_name);
313 024e9686 2019-05-14 stsp return err;
314 507dc3bb 2018-12-29 stsp }
315 507dc3bb 2018-12-29 stsp
316 b72f483a 2019-02-05 stsp struct got_object_id *
317 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
318 507dc3bb 2018-12-29 stsp {
319 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 507dc3bb 2018-12-29 stsp const struct got_error *
323 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
325 507dc3bb 2018-12-29 stsp {
326 507dc3bb 2018-12-29 stsp const struct got_error *err;
327 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
328 507dc3bb 2018-12-29 stsp char *id_str = NULL;
329 507dc3bb 2018-12-29 stsp char *path_got = NULL;
330 507dc3bb 2018-12-29 stsp
331 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
333 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
334 507dc3bb 2018-12-29 stsp path_got = NULL;
335 507dc3bb 2018-12-29 stsp goto done;
336 507dc3bb 2018-12-29 stsp }
337 507dc3bb 2018-12-29 stsp
338 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
339 507dc3bb 2018-12-29 stsp if (err)
340 507dc3bb 2018-12-29 stsp return err;
341 507dc3bb 2018-12-29 stsp
342 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
344 507dc3bb 2018-12-29 stsp goto done;
345 507dc3bb 2018-12-29 stsp }
346 507dc3bb 2018-12-29 stsp
347 507dc3bb 2018-12-29 stsp /* Record our base commit. */
348 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
349 507dc3bb 2018-12-29 stsp if (err)
350 507dc3bb 2018-12-29 stsp goto done;
351 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 507dc3bb 2018-12-29 stsp if (err)
353 507dc3bb 2018-12-29 stsp goto done;
354 507dc3bb 2018-12-29 stsp
355 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
356 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
357 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp }
361 507dc3bb 2018-12-29 stsp done:
362 507dc3bb 2018-12-29 stsp if (obj)
363 507dc3bb 2018-12-29 stsp got_object_close(obj);
364 507dc3bb 2018-12-29 stsp free(id_str);
365 507dc3bb 2018-12-29 stsp free(path_got);
366 507dc3bb 2018-12-29 stsp return err;
367 50b0790e 2020-09-11 stsp }
368 50b0790e 2020-09-11 stsp
369 50b0790e 2020-09-11 stsp const struct got_gotconfig *
370 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
371 50b0790e 2020-09-11 stsp {
372 50b0790e 2020-09-11 stsp return worktree->gotconfig;
373 507dc3bb 2018-12-29 stsp }
374 507dc3bb 2018-12-29 stsp
375 9d31a1d8 2018-03-11 stsp static const struct got_error *
376 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
377 86c3caaf 2018-03-09 stsp {
378 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
381 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
382 86c3caaf 2018-03-09 stsp return NULL;
383 21908da4 2019-01-13 stsp }
384 21908da4 2019-01-13 stsp
385 21908da4 2019-01-13 stsp static const struct got_error *
386 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
387 4a1ddfc2 2019-01-12 stsp {
388 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
389 4a1ddfc2 2019-01-12 stsp char *abspath;
390 4a1ddfc2 2019-01-12 stsp
391 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
393 4a1ddfc2 2019-01-12 stsp
394 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
395 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 ddcd8544 2019-03-11 stsp struct stat sb;
397 ddcd8544 2019-03-11 stsp err = NULL;
398 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
400 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
401 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
402 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 ddcd8544 2019-03-11 stsp }
404 ddcd8544 2019-03-11 stsp }
405 4a1ddfc2 2019-01-12 stsp free(abspath);
406 68c76935 2019-02-19 stsp return err;
407 68c76935 2019-02-19 stsp }
408 68c76935 2019-02-19 stsp
409 68c76935 2019-02-19 stsp static const struct got_error *
410 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
411 68c76935 2019-02-19 stsp {
412 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
413 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
414 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
415 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
416 68c76935 2019-02-19 stsp
417 68c76935 2019-02-19 stsp *same = 1;
418 68c76935 2019-02-19 stsp
419 230a42bd 2019-05-11 jcs for (;;) {
420 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
423 68c76935 2019-02-19 stsp break;
424 68c76935 2019-02-19 stsp }
425 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
427 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
428 68c76935 2019-02-19 stsp break;
429 68c76935 2019-02-19 stsp }
430 68c76935 2019-02-19 stsp if (flen1 == 0) {
431 68c76935 2019-02-19 stsp if (flen2 != 0)
432 68c76935 2019-02-19 stsp *same = 0;
433 68c76935 2019-02-19 stsp break;
434 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
435 68c76935 2019-02-19 stsp if (flen1 != 0)
436 68c76935 2019-02-19 stsp *same = 0;
437 68c76935 2019-02-19 stsp break;
438 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
439 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 68c76935 2019-02-19 stsp *same = 0;
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp } else {
444 68c76935 2019-02-19 stsp *same = 0;
445 68c76935 2019-02-19 stsp break;
446 68c76935 2019-02-19 stsp }
447 68c76935 2019-02-19 stsp }
448 68c76935 2019-02-19 stsp
449 68c76935 2019-02-19 stsp return err;
450 68c76935 2019-02-19 stsp }
451 68c76935 2019-02-19 stsp
452 68c76935 2019-02-19 stsp static const struct got_error *
453 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
454 68c76935 2019-02-19 stsp {
455 68c76935 2019-02-19 stsp struct stat sb;
456 68c76935 2019-02-19 stsp size_t size1, size2;
457 68c76935 2019-02-19 stsp
458 68c76935 2019-02-19 stsp *same = 1;
459 68c76935 2019-02-19 stsp
460 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size1 = sb.st_size;
463 68c76935 2019-02-19 stsp
464 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
465 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
466 68c76935 2019-02-19 stsp size2 = sb.st_size;
467 68c76935 2019-02-19 stsp
468 68c76935 2019-02-19 stsp if (size1 != size2) {
469 68c76935 2019-02-19 stsp *same = 0;
470 68c76935 2019-02-19 stsp return NULL;
471 68c76935 2019-02-19 stsp }
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
475 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
476 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
477 68c76935 2019-02-19 stsp
478 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
479 0e039681 2021-11-15 stsp }
480 0e039681 2021-11-15 stsp
481 0e039681 2021-11-15 stsp static const struct got_error *
482 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
483 0e039681 2021-11-15 stsp {
484 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
485 0e039681 2021-11-15 stsp size_t flen;
486 0e039681 2021-11-15 stsp ssize_t outlen;
487 0e039681 2021-11-15 stsp
488 0e039681 2021-11-15 stsp *outsize = 0;
489 0e039681 2021-11-15 stsp
490 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
491 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
492 0e039681 2021-11-15 stsp
493 0e039681 2021-11-15 stsp for (;;) {
494 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
495 0e039681 2021-11-15 stsp if (flen == 0) {
496 0e039681 2021-11-15 stsp if (ferror(f))
497 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
498 0e039681 2021-11-15 stsp if (feof(f))
499 0e039681 2021-11-15 stsp break;
500 0e039681 2021-11-15 stsp }
501 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
502 0e039681 2021-11-15 stsp if (outlen == -1)
503 0e039681 2021-11-15 stsp return got_error_from_errno("write");
504 0e039681 2021-11-15 stsp if (outlen != flen)
505 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
506 0e039681 2021-11-15 stsp *outsize += outlen;
507 0e039681 2021-11-15 stsp }
508 0e039681 2021-11-15 stsp
509 0e039681 2021-11-15 stsp return NULL;
510 0e039681 2021-11-15 stsp }
511 0e039681 2021-11-15 stsp
512 0e039681 2021-11-15 stsp static const struct got_error *
513 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
514 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 0e039681 2021-11-15 stsp const char *ondisk_path)
517 0e039681 2021-11-15 stsp {
518 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
519 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
520 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
524 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
525 0e039681 2021-11-15 stsp
526 0e039681 2021-11-15 stsp *overlapcnt = 0;
527 0e039681 2021-11-15 stsp
528 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 0e039681 2021-11-15 stsp if (err)
530 0e039681 2021-11-15 stsp return err;
531 0e039681 2021-11-15 stsp
532 0e039681 2021-11-15 stsp if (same_content)
533 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
534 0e039681 2021-11-15 stsp
535 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
536 0e039681 2021-11-15 stsp if (err)
537 0e039681 2021-11-15 stsp return err;
538 0e039681 2021-11-15 stsp changed_deriv = !same_content;
539 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
540 0e039681 2021-11-15 stsp if (err)
541 0e039681 2021-11-15 stsp return err;
542 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
543 0e039681 2021-11-15 stsp
544 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
545 0e039681 2021-11-15 stsp *overlapcnt = 1;
546 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
548 0e039681 2021-11-15 stsp goto done;
549 0e039681 2021-11-15 stsp }
550 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
552 0e039681 2021-11-15 stsp goto done;
553 0e039681 2021-11-15 stsp }
554 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
556 0e039681 2021-11-15 stsp goto done;
557 0e039681 2021-11-15 stsp }
558 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 b90054ed 2022-11-01 stsp base_path_orig, "");
560 0e039681 2021-11-15 stsp if (err)
561 0e039681 2021-11-15 stsp goto done;
562 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 b90054ed 2022-11-01 stsp base_path_deriv, "");
564 0e039681 2021-11-15 stsp if (err)
565 0e039681 2021-11-15 stsp goto done;
566 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 b90054ed 2022-11-01 stsp base_path_deriv2, "");
568 0e039681 2021-11-15 stsp if (err)
569 0e039681 2021-11-15 stsp goto done;
570 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 0e039681 2021-11-15 stsp if (err)
572 0e039681 2021-11-15 stsp goto done;
573 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 0e039681 2021-11-15 stsp if (err)
575 0e039681 2021-11-15 stsp goto done;
576 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 0e039681 2021-11-15 stsp if (err)
578 0e039681 2021-11-15 stsp goto done;
579 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
580 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
581 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
582 0e039681 2021-11-15 stsp goto done;
583 0e039681 2021-11-15 stsp }
584 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 0e039681 2021-11-15 stsp label_deriv ? " " : "",
587 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
588 0e039681 2021-11-15 stsp path_deriv) < 0) {
589 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
590 0e039681 2021-11-15 stsp goto done;
591 0e039681 2021-11-15 stsp }
592 0e039681 2021-11-15 stsp if (size_orig > 0) {
593 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
595 0e039681 2021-11-15 stsp label_orig ? " " : "",
596 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
597 0e039681 2021-11-15 stsp path_orig) < 0) {
598 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
599 0e039681 2021-11-15 stsp goto done;
600 0e039681 2021-11-15 stsp }
601 0e039681 2021-11-15 stsp }
602 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
604 0e039681 2021-11-15 stsp path_deriv2,
605 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
606 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
607 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
608 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
609 0e039681 2021-11-15 stsp goto done;
610 0e039681 2021-11-15 stsp }
611 0e039681 2021-11-15 stsp } else if (changed_deriv)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 0e039681 2021-11-15 stsp else if (changed_deriv2)
614 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 0e039681 2021-11-15 stsp done:
616 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 0e039681 2021-11-15 stsp err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
619 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
621 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
623 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
625 0e039681 2021-11-15 stsp free(path_orig);
626 0e039681 2021-11-15 stsp free(path_deriv);
627 0e039681 2021-11-15 stsp free(path_deriv2);
628 0e039681 2021-11-15 stsp free(base_path_orig);
629 0e039681 2021-11-15 stsp free(base_path_deriv);
630 0e039681 2021-11-15 stsp free(base_path_deriv2);
631 0e039681 2021-11-15 stsp return err;
632 6353ad76 2019-02-08 stsp }
633 6353ad76 2019-02-08 stsp
634 6353ad76 2019-02-08 stsp /*
635 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
636 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
637 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
638 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
639 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
640 6353ad76 2019-02-08 stsp */
641 6353ad76 2019-02-08 stsp static const struct got_error *
642 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
645 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
648 6353ad76 2019-02-08 stsp {
649 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
650 6353ad76 2019-02-08 stsp int merged_fd = -1;
651 db590691 2021-06-02 stsp FILE *f_merged = NULL;
652 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
653 234035bc 2019-06-01 stsp int overlapcnt = 0;
654 3524bbf9 2020-10-20 stsp char *parent = NULL;
655 6353ad76 2019-02-08 stsp
656 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
657 234035bc 2019-06-01 stsp
658 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
659 3524bbf9 2020-10-20 stsp if (err)
660 3524bbf9 2020-10-20 stsp return err;
661 af54ae4a 2019-02-19 stsp
662 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
664 3524bbf9 2020-10-20 stsp goto done;
665 3524bbf9 2020-10-20 stsp }
666 af54ae4a 2019-02-19 stsp
667 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 6353ad76 2019-02-08 stsp if (err)
669 6353ad76 2019-02-08 stsp goto done;
670 3b9f0f87 2020-07-23 stsp
671 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 0e039681 2021-11-15 stsp if (err) {
674 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
675 0e039681 2021-11-15 stsp goto done;
676 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 0e039681 2021-11-15 stsp ondisk_path);
679 0e039681 2021-11-15 stsp if (err)
680 0e039681 2021-11-15 stsp goto done;
681 0e039681 2021-11-15 stsp }
682 6353ad76 2019-02-08 stsp
683 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
684 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 1ee397ad 2019-07-12 stsp if (err)
686 1ee397ad 2019-07-12 stsp goto done;
687 6353ad76 2019-02-08 stsp
688 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
690 816dc654 2019-02-16 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp
693 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
694 db590691 2021-06-02 stsp if (f_merged == NULL) {
695 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
696 db590691 2021-06-02 stsp goto done;
697 db590691 2021-06-02 stsp }
698 db590691 2021-06-02 stsp merged_fd = -1;
699 db590691 2021-06-02 stsp
700 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
701 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
702 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
703 db590691 2021-06-02 stsp f_merged);
704 68c76935 2019-02-19 stsp if (err)
705 68c76935 2019-02-19 stsp goto done;
706 816dc654 2019-02-16 stsp }
707 6353ad76 2019-02-08 stsp
708 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
710 70a0c8ec 2019-02-20 stsp goto done;
711 70a0c8ec 2019-02-20 stsp }
712 70a0c8ec 2019-02-20 stsp
713 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
714 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
715 230a42bd 2019-05-11 jcs ondisk_path);
716 6353ad76 2019-02-08 stsp goto done;
717 6353ad76 2019-02-08 stsp }
718 6353ad76 2019-02-08 stsp done:
719 fdcb7daf 2019-12-15 stsp if (err) {
720 fdcb7daf 2019-12-15 stsp if (merged_path)
721 fdcb7daf 2019-12-15 stsp unlink(merged_path);
722 3b9f0f87 2020-07-23 stsp }
723 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
725 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
727 6353ad76 2019-02-08 stsp free(merged_path);
728 af54ae4a 2019-02-19 stsp free(base_path);
729 3524bbf9 2020-10-20 stsp free(parent);
730 af57b12a 2020-07-23 stsp return err;
731 af57b12a 2020-07-23 stsp }
732 af57b12a 2020-07-23 stsp
733 af57b12a 2020-07-23 stsp static const struct got_error *
734 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
735 af57b12a 2020-07-23 stsp size_t target_len)
736 af57b12a 2020-07-23 stsp {
737 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
738 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
740 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
741 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
742 af57b12a 2020-07-23 stsp ondisk_path);
743 af57b12a 2020-07-23 stsp return NULL;
744 af57b12a 2020-07-23 stsp }
745 af57b12a 2020-07-23 stsp
746 af57b12a 2020-07-23 stsp /*
747 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
749 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
750 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
751 993e2a1b 2020-07-23 stsp *
752 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
753 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
754 993e2a1b 2020-07-23 stsp *
755 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
756 993e2a1b 2020-07-23 stsp * has deleted this symlink.
757 11cc08c1 2020-07-23 stsp */
758 11cc08c1 2020-07-23 stsp static const struct got_error *
759 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
760 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
762 11cc08c1 2020-07-23 stsp {
763 11cc08c1 2020-07-23 stsp const struct got_error *err;
764 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 11cc08c1 2020-07-23 stsp FILE *f = NULL;
766 11cc08c1 2020-07-23 stsp
767 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
768 11cc08c1 2020-07-23 stsp if (err)
769 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
770 11cc08c1 2020-07-23 stsp
771 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
772 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
774 11cc08c1 2020-07-23 stsp goto done;
775 11cc08c1 2020-07-23 stsp }
776 11cc08c1 2020-07-23 stsp
777 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 11cc08c1 2020-07-23 stsp if (err)
779 3818e3c4 2020-11-01 naddy goto done;
780 3818e3c4 2020-11-01 naddy
781 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
783 11cc08c1 2020-07-23 stsp goto done;
784 3818e3c4 2020-11-01 naddy }
785 11cc08c1 2020-07-23 stsp
786 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
789 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
792 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
793 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
794 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
796 11cc08c1 2020-07-23 stsp goto done;
797 11cc08c1 2020-07-23 stsp }
798 11cc08c1 2020-07-23 stsp
799 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
801 11cc08c1 2020-07-23 stsp goto done;
802 11cc08c1 2020-07-23 stsp }
803 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
805 11cc08c1 2020-07-23 stsp goto done;
806 11cc08c1 2020-07-23 stsp }
807 11cc08c1 2020-07-23 stsp done:
808 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
809 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
810 11cc08c1 2020-07-23 stsp free(path);
811 11cc08c1 2020-07-23 stsp free(id_str);
812 11cc08c1 2020-07-23 stsp free(label_deriv);
813 11cc08c1 2020-07-23 stsp return err;
814 11cc08c1 2020-07-23 stsp }
815 d219f183 2020-07-23 stsp
816 d219f183 2020-07-23 stsp /* forward declaration */
817 d219f183 2020-07-23 stsp static const struct got_error *
818 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
820 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
821 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
822 11cc08c1 2020-07-23 stsp
823 11cc08c1 2020-07-23 stsp /*
824 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
825 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
826 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
827 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
828 af57b12a 2020-07-23 stsp */
829 af57b12a 2020-07-23 stsp static const struct got_error *
830 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
831 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
832 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
833 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
835 af57b12a 2020-07-23 stsp {
836 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
837 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
838 af57b12a 2020-07-23 stsp struct stat sb;
839 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
840 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
841 11cc08c1 2020-07-23 stsp int have_local_change = 0;
842 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
843 af57b12a 2020-07-23 stsp
844 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
845 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
846 af57b12a 2020-07-23 stsp
847 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
848 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
849 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
850 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
851 af57b12a 2020-07-23 stsp ondisk_path);
852 af57b12a 2020-07-23 stsp goto done;
853 af57b12a 2020-07-23 stsp }
854 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
855 af57b12a 2020-07-23 stsp
856 526a746f 2020-07-23 stsp if (blob_orig) {
857 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 526a746f 2020-07-23 stsp if (err)
859 526a746f 2020-07-23 stsp goto done;
860 526a746f 2020-07-23 stsp }
861 af57b12a 2020-07-23 stsp
862 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
863 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
864 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 11cc08c1 2020-07-23 stsp have_local_change = 1;
866 11cc08c1 2020-07-23 stsp
867 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
868 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
869 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
870 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
872 11cc08c1 2020-07-23 stsp
873 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
874 11cc08c1 2020-07-23 stsp if (ancestor_target) {
875 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
876 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 11cc08c1 2020-07-23 stsp path);
878 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
879 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
881 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 11cc08c1 2020-07-23 stsp path);
883 11cc08c1 2020-07-23 stsp } else {
884 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
885 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
886 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
887 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
888 11cc08c1 2020-07-23 stsp if (err)
889 11cc08c1 2020-07-23 stsp goto done;
890 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 11cc08c1 2020-07-23 stsp path);
892 11cc08c1 2020-07-23 stsp }
893 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
894 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
895 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
896 af57b12a 2020-07-23 stsp strlen(deriv_target));
897 af57b12a 2020-07-23 stsp if (err)
898 af57b12a 2020-07-23 stsp goto done;
899 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
901 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
902 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
904 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 ea7786be 2020-07-23 stsp path);
906 ea7786be 2020-07-23 stsp } else {
907 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
908 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
909 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
910 ea7786be 2020-07-23 stsp if (err)
911 ea7786be 2020-07-23 stsp goto done;
912 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 ea7786be 2020-07-23 stsp path);
914 ea7786be 2020-07-23 stsp }
915 6353ad76 2019-02-08 stsp }
916 11cc08c1 2020-07-23 stsp
917 af57b12a 2020-07-23 stsp done:
918 af57b12a 2020-07-23 stsp free(ancestor_target);
919 eec2f5a9 2021-06-03 stsp return err;
920 eec2f5a9 2021-06-03 stsp }
921 eec2f5a9 2021-06-03 stsp
922 eec2f5a9 2021-06-03 stsp static const struct got_error *
923 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
924 eec2f5a9 2021-06-03 stsp {
925 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
926 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
927 eec2f5a9 2021-06-03 stsp ssize_t target_len;
928 eec2f5a9 2021-06-03 stsp size_t n;
929 eec2f5a9 2021-06-03 stsp FILE *f;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp *outfile = NULL;
932 eec2f5a9 2021-06-03 stsp
933 eec2f5a9 2021-06-03 stsp f = got_opentemp();
934 eec2f5a9 2021-06-03 stsp if (f == NULL)
935 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
936 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
938 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
939 eec2f5a9 2021-06-03 stsp goto done;
940 eec2f5a9 2021-06-03 stsp }
941 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
942 eec2f5a9 2021-06-03 stsp if (n != target_len) {
943 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
944 eec2f5a9 2021-06-03 stsp goto done;
945 eec2f5a9 2021-06-03 stsp }
946 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
947 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
948 eec2f5a9 2021-06-03 stsp goto done;
949 eec2f5a9 2021-06-03 stsp }
950 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
952 eec2f5a9 2021-06-03 stsp goto done;
953 eec2f5a9 2021-06-03 stsp }
954 eec2f5a9 2021-06-03 stsp done:
955 eec2f5a9 2021-06-03 stsp if (err)
956 eec2f5a9 2021-06-03 stsp fclose(f);
957 eec2f5a9 2021-06-03 stsp else
958 eec2f5a9 2021-06-03 stsp *outfile = f;
959 14c901f1 2019-08-08 stsp return err;
960 14c901f1 2019-08-08 stsp }
961 14c901f1 2019-08-08 stsp
962 14c901f1 2019-08-08 stsp /*
963 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
965 14c901f1 2019-08-08 stsp * acts as the second derived version.
966 14c901f1 2019-08-08 stsp */
967 14c901f1 2019-08-08 stsp static const struct got_error *
968 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
970 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
971 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
972 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
974 14c901f1 2019-08-08 stsp {
975 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
976 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
978 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
980 14c901f1 2019-08-08 stsp
981 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
982 14c901f1 2019-08-08 stsp
983 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
984 ed6b5030 2020-10-20 stsp if (err)
985 ed6b5030 2020-10-20 stsp return err;
986 14c901f1 2019-08-08 stsp
987 67a66647 2021-05-31 stsp if (blob_orig) {
988 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 67a66647 2021-05-31 stsp parent) == -1) {
990 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
991 67a66647 2021-05-31 stsp base_path = NULL;
992 67a66647 2021-05-31 stsp goto done;
993 67a66647 2021-05-31 stsp }
994 67a66647 2021-05-31 stsp
995 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
996 b90054ed 2022-11-01 stsp base_path, "");
997 67a66647 2021-05-31 stsp if (err)
998 67a66647 2021-05-31 stsp goto done;
999 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 67a66647 2021-05-31 stsp blob_orig);
1001 67a66647 2021-05-31 stsp if (err)
1002 67a66647 2021-05-31 stsp goto done;
1003 67a66647 2021-05-31 stsp free(base_path);
1004 db590691 2021-06-02 stsp } else {
1005 db590691 2021-06-02 stsp /*
1006 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1007 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1008 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1009 db590691 2021-06-02 stsp */
1010 db590691 2021-06-02 stsp f_orig = got_opentemp();
1011 db590691 2021-06-02 stsp if (f_orig == NULL) {
1012 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1013 db590691 2021-06-02 stsp goto done;
1014 db590691 2021-06-02 stsp }
1015 67a66647 2021-05-31 stsp }
1016 67a66647 2021-05-31 stsp
1017 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1019 14c901f1 2019-08-08 stsp base_path = NULL;
1020 14c901f1 2019-08-08 stsp goto done;
1021 14c901f1 2019-08-08 stsp }
1022 14c901f1 2019-08-08 stsp
1023 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 14c901f1 2019-08-08 stsp if (err)
1025 14c901f1 2019-08-08 stsp goto done;
1026 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 14c901f1 2019-08-08 stsp blob_deriv);
1028 14c901f1 2019-08-08 stsp if (err)
1029 14c901f1 2019-08-08 stsp goto done;
1030 14c901f1 2019-08-08 stsp
1031 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 14c901f1 2019-08-08 stsp if (err)
1033 14c901f1 2019-08-08 stsp goto done;
1034 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1035 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1037 14c901f1 2019-08-08 stsp goto done;
1038 eec2f5a9 2021-06-03 stsp }
1039 eec2f5a9 2021-06-03 stsp
1040 5e91dae4 2022-08-30 stsp /*
1041 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1042 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1043 eec2f5a9 2021-06-03 stsp */
1044 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1045 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 eec2f5a9 2021-06-03 stsp if (err)
1047 eec2f5a9 2021-06-03 stsp goto done;
1048 eec2f5a9 2021-06-03 stsp } else {
1049 eec2f5a9 2021-06-03 stsp int fd;
1050 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp }
1055 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1056 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1057 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1058 eec2f5a9 2021-06-03 stsp close(fd);
1059 eec2f5a9 2021-06-03 stsp goto done;
1060 eec2f5a9 2021-06-03 stsp }
1061 14c901f1 2019-08-08 stsp }
1062 14c901f1 2019-08-08 stsp
1063 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 14c901f1 2019-08-08 stsp done:
1067 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1069 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1071 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1073 14c901f1 2019-08-08 stsp free(base_path);
1074 67a66647 2021-05-31 stsp if (blob_orig_path) {
1075 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1076 67a66647 2021-05-31 stsp free(blob_orig_path);
1077 67a66647 2021-05-31 stsp }
1078 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1079 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1080 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1081 14c901f1 2019-08-08 stsp }
1082 6353ad76 2019-02-08 stsp free(id_str);
1083 818c7501 2019-07-11 stsp free(label_deriv);
1084 ed6b5030 2020-10-20 stsp free(parent);
1085 4a1ddfc2 2019-01-12 stsp return err;
1086 4a1ddfc2 2019-01-12 stsp }
1087 4a1ddfc2 2019-01-12 stsp
1088 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1089 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1092 13d9040b 2019-03-27 stsp {
1093 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1094 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1095 13d9040b 2019-03-27 stsp
1096 65b05cec 2020-07-23 stsp *new_iep = NULL;
1097 65b05cec 2020-07-23 stsp
1098 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1099 054041d0 2020-06-24 stsp if (err)
1100 054041d0 2020-06-24 stsp return err;
1101 054041d0 2020-06-24 stsp
1102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1104 054041d0 2020-06-24 stsp if (err)
1105 054041d0 2020-06-24 stsp goto done;
1106 054041d0 2020-06-24 stsp
1107 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1108 054041d0 2020-06-24 stsp done:
1109 054041d0 2020-06-24 stsp if (err)
1110 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1111 65b05cec 2020-07-23 stsp else
1112 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1113 13d9040b 2019-03-27 stsp return err;
1114 1ebedb77 2019-10-19 stsp }
1115 1ebedb77 2019-10-19 stsp
1116 1ebedb77 2019-10-19 stsp static mode_t
1117 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1118 1ebedb77 2019-10-19 stsp {
1119 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1120 1ebedb77 2019-10-19 stsp
1121 1ebedb77 2019-10-19 stsp if (executable) {
1122 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1125 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1126 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1127 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1128 1ebedb77 2019-10-19 stsp }
1129 1ebedb77 2019-10-19 stsp
1130 b2b3fce1 2022-10-29 op return st_mode;
1131 b2b3fce1 2022-10-29 op }
1132 b2b3fce1 2022-10-29 op
1133 b2b3fce1 2022-10-29 op static mode_t
1134 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1135 b2b3fce1 2022-10-29 op {
1136 b2b3fce1 2022-10-29 op mode_t um;
1137 b2b3fce1 2022-10-29 op
1138 b2b3fce1 2022-10-29 op um = umask(000);
1139 b2b3fce1 2022-10-29 op umask(um);
1140 b2b3fce1 2022-10-29 op return mode & ~um;
1141 13d9040b 2019-03-27 stsp }
1142 13d9040b 2019-03-27 stsp
1143 8ba819a3 2020-07-23 stsp /* forward declaration */
1144 13d9040b 2019-03-27 stsp static const struct got_error *
1145 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1147 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1148 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1149 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1150 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 8ba819a3 2020-07-23 stsp
1152 5a1fbc73 2020-07-23 stsp /*
1153 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1154 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1155 5a1fbc73 2020-07-23 stsp */
1156 8ba819a3 2020-07-23 stsp static const struct got_error *
1157 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1159 5a1fbc73 2020-07-23 stsp {
1160 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1161 5a1fbc73 2020-07-23 stsp ssize_t elen;
1162 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1163 5a1fbc73 2020-07-23 stsp int fd;
1164 5a1fbc73 2020-07-23 stsp
1165 c6e8a826 2021-04-05 stsp *did_something = 0;
1166 c6e8a826 2021-04-05 stsp
1167 5a1fbc73 2020-07-23 stsp /*
1168 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1169 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1170 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1171 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1172 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1173 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1174 5a1fbc73 2020-07-23 stsp */
1175 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1177 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1178 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp
1180 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1181 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 5a1fbc73 2020-07-23 stsp if (elen == -1)
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1184 5a1fbc73 2020-07-23 stsp
1185 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1186 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1187 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1188 5a1fbc73 2020-07-23 stsp }
1189 5a1fbc73 2020-07-23 stsp
1190 c6e8a826 2021-04-05 stsp *did_something = 1;
1191 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1192 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1193 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1194 5a1fbc73 2020-07-23 stsp return err;
1195 3c1ec782 2020-07-23 stsp }
1196 3c1ec782 2020-07-23 stsp
1197 3c1ec782 2020-07-23 stsp static const struct got_error *
1198 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 3c1ec782 2020-07-23 stsp {
1201 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1202 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1203 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1206 3c1ec782 2020-07-23 stsp
1207 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1209 3c1ec782 2020-07-23 stsp return NULL;
1210 3c1ec782 2020-07-23 stsp }
1211 3c1ec782 2020-07-23 stsp
1212 3c1ec782 2020-07-23 stsp /*
1213 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1214 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1215 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1216 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1217 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1218 3c1ec782 2020-07-23 stsp */
1219 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1220 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1221 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1222 ce031e9e 2020-10-20 stsp if (err)
1223 ce031e9e 2020-10-20 stsp return err;
1224 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 ce031e9e 2020-10-20 stsp free(parent);
1226 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1227 ce031e9e 2020-10-20 stsp }
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1230 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 3c1ec782 2020-07-23 stsp free(abspath);
1232 3c1ec782 2020-07-23 stsp return err;
1233 3c1ec782 2020-07-23 stsp }
1234 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 3c1ec782 2020-07-23 stsp free(abspath);
1236 3c1ec782 2020-07-23 stsp if (err)
1237 3c1ec782 2020-07-23 stsp return err;
1238 3c1ec782 2020-07-23 stsp } else {
1239 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 3c1ec782 2020-07-23 stsp if (err)
1241 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp }
1243 3c1ec782 2020-07-23 stsp
1244 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1245 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1247 3c1ec782 2020-07-23 stsp return NULL;
1248 3c1ec782 2020-07-23 stsp }
1249 3c1ec782 2020-07-23 stsp
1250 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1251 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1253 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1254 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp
1257 3c1ec782 2020-07-23 stsp free(path_got);
1258 3c1ec782 2020-07-23 stsp return NULL;
1259 5a1fbc73 2020-07-23 stsp }
1260 5a1fbc73 2020-07-23 stsp
1261 5a1fbc73 2020-07-23 stsp static const struct got_error *
1262 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1265 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1266 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1267 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 9d31a1d8 2018-03-11 stsp {
1269 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1270 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1271 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1272 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 8ba819a3 2020-07-23 stsp
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1276 2e1fa222 2020-07-23 stsp
1277 3c29341b 2022-07-21 florian /*
1278 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1279 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1280 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1281 8ba819a3 2020-07-23 stsp * in the blob object.
1282 8ba819a3 2020-07-23 stsp */
1283 8ba819a3 2020-07-23 stsp do {
1284 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1285 538b6881 2022-07-21 florian if (err)
1286 538b6881 2022-07-21 florian return err;
1287 538b6881 2022-07-21 florian
1288 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1289 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1290 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1291 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1292 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1293 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1295 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1296 3b9f0f87 2020-07-23 stsp progress_arg);
1297 8ba819a3 2020-07-23 stsp }
1298 8ba819a3 2020-07-23 stsp if (len > 0) {
1299 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1300 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1301 8ba819a3 2020-07-23 stsp len - hdrlen);
1302 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1303 8ba819a3 2020-07-23 stsp hdrlen = 0;
1304 8ba819a3 2020-07-23 stsp }
1305 8ba819a3 2020-07-23 stsp } while (len != 0);
1306 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1307 8ba819a3 2020-07-23 stsp
1308 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1310 3c1ec782 2020-07-23 stsp if (err)
1311 3c1ec782 2020-07-23 stsp return err;
1312 8ba819a3 2020-07-23 stsp
1313 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1314 906c123b 2020-07-23 stsp /* install as a regular file */
1315 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1316 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1317 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1319 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1320 3c29341b 2022-07-21 florian return err;
1321 906c123b 2020-07-23 stsp }
1322 906c123b 2020-07-23 stsp
1323 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1324 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1325 c6e8a826 2021-04-05 stsp int symlink_replaced;
1326 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1327 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1328 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1329 3c29341b 2022-07-21 florian return err;
1330 c90c8ce3 2020-07-23 stsp }
1331 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1332 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1333 5a1fbc73 2020-07-23 stsp if (err)
1334 3c29341b 2022-07-21 florian return err;
1335 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1336 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1337 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1338 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1341 c6e8a826 2021-04-05 stsp } else {
1342 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1344 c6e8a826 2021-04-05 stsp }
1345 f35fa46a 2020-07-23 stsp }
1346 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1347 f35fa46a 2020-07-23 stsp }
1348 f35fa46a 2020-07-23 stsp
1349 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1350 f4994adc 2020-10-20 stsp char *parent;
1351 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1352 f4994adc 2020-10-20 stsp if (err)
1353 3c29341b 2022-07-21 florian return err;
1354 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1355 f4994adc 2020-10-20 stsp free(parent);
1356 f35fa46a 2020-07-23 stsp if (err)
1357 3c29341b 2022-07-21 florian return err;
1358 f35fa46a 2020-07-23 stsp /*
1359 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1360 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1361 f35fa46a 2020-07-23 stsp */
1362 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1363 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1364 3c29341b 2022-07-21 florian return err;
1365 f35fa46a 2020-07-23 stsp }
1366 f35fa46a 2020-07-23 stsp }
1367 f35fa46a 2020-07-23 stsp
1368 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1369 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1370 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1371 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1372 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1373 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1374 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1376 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1377 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1378 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1379 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1380 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1381 8ba819a3 2020-07-23 stsp } else {
1382 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1383 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1384 8ba819a3 2020-07-23 stsp }
1385 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1386 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 8ba819a3 2020-07-23 stsp return err;
1389 8ba819a3 2020-07-23 stsp }
1390 8ba819a3 2020-07-23 stsp
1391 8ba819a3 2020-07-23 stsp static const struct got_error *
1392 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1394 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1395 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1396 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1397 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 8ba819a3 2020-07-23 stsp {
1399 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1400 507dc3bb 2018-12-29 stsp int fd = -1;
1401 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1402 507dc3bb 2018-12-29 stsp int update = 0;
1403 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1404 b2b3fce1 2022-10-29 op mode_t mode;
1405 8ba819a3 2020-07-23 stsp
1406 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1409 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1410 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1411 f5375317 2020-10-20 stsp char *parent;
1412 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1413 f5375317 2020-10-20 stsp if (err)
1414 f5375317 2020-10-20 stsp return err;
1415 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1416 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp if (err)
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 b2b3fce1 2022-10-29 op mode);
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1430 3b9f0f87 2020-07-23 stsp }
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1437 d70b8e30 2018-12-27 stsp } else {
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 b90054ed 2022-11-01 stsp ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp if (err)
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1443 b2b3fce1 2022-10-29 op
1444 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1445 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1446 b2b3fce1 2022-10-29 op tmppath);
1447 b2b3fce1 2022-10-29 op goto done;
1448 b2b3fce1 2022-10-29 op }
1449 9d31a1d8 2018-03-11 stsp }
1450 507dc3bb 2018-12-29 stsp } else
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 3818e3c4 2020-11-01 naddy }
1453 3818e3c4 2020-11-01 naddy
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp if (err)
1465 bd6aa359 2020-07-23 stsp goto done;
1466 bd6aa359 2020-07-23 stsp }
1467 d7b62c98 2018-12-27 stsp
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp do {
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (err)
1473 9d31a1d8 2018-03-11 stsp break;
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1483 9d31a1d8 2018-03-11 stsp }
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp }
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 9d31a1d8 2018-03-11 stsp
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1491 816dc654 2019-02-16 stsp }
1492 9d31a1d8 2018-03-11 stsp
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1497 f6d8c0ac 2020-11-09 stsp }
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1502 68ed9ba5 2019-02-10 stsp }
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1505 507dc3bb 2018-12-29 stsp }
1506 507dc3bb 2018-12-29 stsp
1507 9d31a1d8 2018-03-11 stsp done:
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1514 6353ad76 2019-02-08 stsp }
1515 6353ad76 2019-02-08 stsp
1516 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1517 6353ad76 2019-02-08 stsp static const struct got_error *
1518 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1519 7154f6ce 2019-03-27 stsp {
1520 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1521 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1522 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1523 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1524 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1525 7154f6ce 2019-03-27 stsp };
1526 7154f6ce 2019-03-27 stsp int i = 0;
1527 9bdd68dd 2021-01-02 naddy char *line = NULL;
1528 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1529 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1530 7154f6ce 2019-03-27 stsp
1531 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1532 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1533 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1534 7154f6ce 2019-03-27 stsp if (feof(f))
1535 7154f6ce 2019-03-27 stsp break;
1536 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1537 7154f6ce 2019-03-27 stsp break;
1538 7154f6ce 2019-03-27 stsp }
1539 7154f6ce 2019-03-27 stsp
1540 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1541 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1542 19332e6d 2019-05-13 stsp == 0)
1543 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1544 7154f6ce 2019-03-27 stsp else
1545 7154f6ce 2019-03-27 stsp i++;
1546 7154f6ce 2019-03-27 stsp }
1547 7154f6ce 2019-03-27 stsp }
1548 9bdd68dd 2021-01-02 naddy free(line);
1549 7154f6ce 2019-03-27 stsp
1550 7154f6ce 2019-03-27 stsp return err;
1551 e2b1e152 2019-07-13 stsp }
1552 e2b1e152 2019-07-13 stsp
1553 e2b1e152 2019-07-13 stsp static int
1554 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1555 1ebedb77 2019-10-19 stsp {
1556 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1557 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1558 1ebedb77 2019-10-19 stsp }
1559 1ebedb77 2019-10-19 stsp
1560 1ebedb77 2019-10-19 stsp static int
1561 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1562 e2b1e152 2019-07-13 stsp {
1563 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1564 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1565 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1566 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1567 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1568 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1569 c363b2c1 2019-08-03 stsp }
1570 c363b2c1 2019-08-03 stsp
1571 c363b2c1 2019-08-03 stsp static unsigned char
1572 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1573 c363b2c1 2019-08-03 stsp {
1574 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1575 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1576 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1577 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1578 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1579 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1580 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1581 c363b2c1 2019-08-03 stsp default:
1582 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1583 a919d5c4 2020-07-23 stsp }
1584 a919d5c4 2020-07-23 stsp }
1585 a919d5c4 2020-07-23 stsp
1586 a919d5c4 2020-07-23 stsp static const struct got_error *
1587 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1588 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1589 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1590 a919d5c4 2020-07-23 stsp {
1591 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1592 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1593 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1594 a919d5c4 2020-07-23 stsp ssize_t elen;
1595 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1596 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1597 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1598 a919d5c4 2020-07-23 stsp
1599 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1600 a919d5c4 2020-07-23 stsp
1601 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1602 a919d5c4 2020-07-23 stsp do {
1603 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1604 a919d5c4 2020-07-23 stsp if (err)
1605 a919d5c4 2020-07-23 stsp return err;
1606 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1607 a919d5c4 2020-07-23 stsp /*
1608 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1609 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1610 a919d5c4 2020-07-23 stsp */
1611 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1612 a919d5c4 2020-07-23 stsp }
1613 a919d5c4 2020-07-23 stsp if (len > 0) {
1614 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1615 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1616 a919d5c4 2020-07-23 stsp len - hdrlen);
1617 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1618 a919d5c4 2020-07-23 stsp hdrlen = 0;
1619 a919d5c4 2020-07-23 stsp }
1620 a919d5c4 2020-07-23 stsp } while (len != 0);
1621 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1622 a919d5c4 2020-07-23 stsp
1623 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1624 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1625 a919d5c4 2020-07-23 stsp if (elen == -1)
1626 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1627 a919d5c4 2020-07-23 stsp } else {
1628 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1629 a919d5c4 2020-07-23 stsp if (elen == -1)
1630 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1631 c363b2c1 2019-08-03 stsp }
1632 a919d5c4 2020-07-23 stsp
1633 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1634 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1635 a919d5c4 2020-07-23 stsp
1636 a919d5c4 2020-07-23 stsp return NULL;
1637 7154f6ce 2019-03-27 stsp }
1638 7154f6ce 2019-03-27 stsp
1639 7154f6ce 2019-03-27 stsp static const struct got_error *
1640 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1641 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1642 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1643 6353ad76 2019-02-08 stsp {
1644 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1645 6353ad76 2019-02-08 stsp struct got_object_id id;
1646 6353ad76 2019-02-08 stsp size_t hdrlen;
1647 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1648 6353ad76 2019-02-08 stsp FILE *f = NULL;
1649 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1650 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1651 6353ad76 2019-02-08 stsp size_t flen, blen;
1652 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1653 6353ad76 2019-02-08 stsp
1654 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1655 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1656 6353ad76 2019-02-08 stsp
1657 7f91a133 2019-12-13 stsp /*
1658 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1659 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1660 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1661 7f91a133 2019-12-13 stsp */
1662 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1663 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1664 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1665 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1666 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1667 882ef1b9 2019-12-13 stsp else
1668 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1669 882ef1b9 2019-12-13 stsp goto done;
1670 882ef1b9 2019-12-13 stsp }
1671 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1672 3d35a492 2019-12-13 stsp goto done;
1673 3d35a492 2019-12-13 stsp }
1674 7f91a133 2019-12-13 stsp } else {
1675 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1676 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1677 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1678 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1679 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1680 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1681 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1682 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1683 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1684 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1685 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1686 3d35a492 2019-12-13 stsp else
1687 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1688 3d35a492 2019-12-13 stsp goto done;
1689 3d35a492 2019-12-13 stsp }
1690 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1691 1338848f 2019-12-13 stsp goto done;
1692 a378724f 2019-02-10 stsp }
1693 a378724f 2019-02-10 stsp }
1694 6353ad76 2019-02-08 stsp
1695 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1696 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1697 1338848f 2019-12-13 stsp goto done;
1698 3f148bc6 2019-07-27 stsp }
1699 efdd40df 2019-07-27 stsp
1700 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1701 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1702 1338848f 2019-12-13 stsp goto done;
1703 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1704 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1705 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1706 1338848f 2019-12-13 stsp goto done;
1707 d00136be 2019-03-26 stsp }
1708 b8f41171 2019-02-10 stsp
1709 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1710 f179e66d 2020-07-23 stsp goto done;
1711 f179e66d 2020-07-23 stsp
1712 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1713 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1714 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1715 1338848f 2019-12-13 stsp goto done;
1716 f179e66d 2020-07-23 stsp }
1717 6353ad76 2019-02-08 stsp
1718 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1719 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1720 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1721 c363b2c1 2019-08-03 stsp else
1722 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1723 c363b2c1 2019-08-03 stsp
1724 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1725 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1726 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1727 eb81bc23 2022-06-28 tracey goto done;
1728 eb81bc23 2022-06-28 tracey }
1729 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1730 6353ad76 2019-02-08 stsp if (err)
1731 1338848f 2019-12-13 stsp goto done;
1732 6353ad76 2019-02-08 stsp
1733 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1734 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1735 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1736 a919d5c4 2020-07-23 stsp goto done;
1737 a919d5c4 2020-07-23 stsp }
1738 a919d5c4 2020-07-23 stsp
1739 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1740 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1741 ab0d4361 2019-12-13 stsp if (fd == -1) {
1742 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1743 ab0d4361 2019-12-13 stsp goto done;
1744 ab0d4361 2019-12-13 stsp }
1745 3d35a492 2019-12-13 stsp }
1746 3d35a492 2019-12-13 stsp
1747 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1748 6353ad76 2019-02-08 stsp if (f == NULL) {
1749 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1750 6353ad76 2019-02-08 stsp goto done;
1751 6353ad76 2019-02-08 stsp }
1752 1338848f 2019-12-13 stsp fd = -1;
1753 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1754 656b1f76 2019-05-11 jcs for (;;) {
1755 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1756 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1757 6353ad76 2019-02-08 stsp if (err)
1758 7154f6ce 2019-03-27 stsp goto done;
1759 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1760 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1761 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1762 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1763 7154f6ce 2019-03-27 stsp goto done;
1764 80c5c120 2019-02-19 stsp }
1765 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1766 6353ad76 2019-02-08 stsp if (flen != 0)
1767 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1768 6353ad76 2019-02-08 stsp break;
1769 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1770 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1771 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1772 6353ad76 2019-02-08 stsp break;
1773 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1774 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1775 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1776 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1777 6353ad76 2019-02-08 stsp break;
1778 6353ad76 2019-02-08 stsp }
1779 6353ad76 2019-02-08 stsp } else {
1780 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1781 6353ad76 2019-02-08 stsp break;
1782 6353ad76 2019-02-08 stsp }
1783 6353ad76 2019-02-08 stsp hdrlen = 0;
1784 6353ad76 2019-02-08 stsp }
1785 7154f6ce 2019-03-27 stsp
1786 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1787 7154f6ce 2019-03-27 stsp rewind(f);
1788 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1789 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1790 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1791 6353ad76 2019-02-08 stsp done:
1792 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1793 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1794 6353ad76 2019-02-08 stsp if (blob)
1795 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1796 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1797 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1798 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1799 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1800 9d31a1d8 2018-03-11 stsp return err;
1801 e2b1e152 2019-07-13 stsp }
1802 e2b1e152 2019-07-13 stsp
1803 e2b1e152 2019-07-13 stsp /*
1804 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1805 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1806 e2b1e152 2019-07-13 stsp */
1807 e2b1e152 2019-07-13 stsp static const struct got_error *
1808 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1809 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1810 e2b1e152 2019-07-13 stsp {
1811 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1813 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1814 e2b1e152 2019-07-13 stsp
1815 e2b1e152 2019-07-13 stsp return NULL;
1816 9d31a1d8 2018-03-11 stsp }
1817 9d31a1d8 2018-03-11 stsp
1818 9d31a1d8 2018-03-11 stsp static const struct got_error *
1819 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1820 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1822 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 0584f854 2019-04-06 stsp void *progress_arg)
1824 9d31a1d8 2018-03-11 stsp {
1825 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1826 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1827 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1828 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1829 b8f41171 2019-02-10 stsp struct stat sb;
1830 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1831 9d31a1d8 2018-03-11 stsp
1832 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1833 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1834 6353ad76 2019-02-08 stsp
1835 abb4604f 2019-07-27 stsp if (ie) {
1836 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1837 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1838 a76c42e6 2019-08-03 stsp goto done;
1839 a76c42e6 2019-08-03 stsp }
1840 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1841 7f91a133 2019-12-13 stsp repo);
1842 abb4604f 2019-07-27 stsp if (err)
1843 abb4604f 2019-07-27 stsp goto done;
1844 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1845 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1846 c90c8ce3 2020-07-23 stsp } else {
1847 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1848 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1849 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1850 f6764181 2021-09-24 stsp ondisk_path);
1851 f6764181 2021-09-24 stsp goto done;
1852 f6764181 2021-09-24 stsp }
1853 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1854 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1855 f6764181 2021-09-24 stsp } else {
1856 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1857 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1858 f6764181 2021-09-24 stsp else
1859 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1860 f6764181 2021-09-24 stsp }
1861 c90c8ce3 2020-07-23 stsp }
1862 6353ad76 2019-02-08 stsp
1863 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1864 2c41dce7 2021-06-27 stsp if (ie)
1865 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1866 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1867 b8f41171 2019-02-10 stsp goto done;
1868 b8f41171 2019-02-10 stsp }
1869 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1870 a769b60b 2021-06-27 stsp if (ie)
1871 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1872 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1873 5036ab18 2020-04-18 stsp path);
1874 5036ab18 2020-04-18 stsp goto done;
1875 5036ab18 2020-04-18 stsp }
1876 b8f41171 2019-02-10 stsp
1877 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1878 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1879 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1880 c6e8a826 2021-04-05 stsp /*
1881 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1882 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1883 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1884 c6e8a826 2021-04-05 stsp * updating contents of this file.
1885 c6e8a826 2021-04-05 stsp */
1886 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1887 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1888 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1889 c6e8a826 2021-04-05 stsp /* Same commit. */
1890 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1891 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1892 e2b1e152 2019-07-13 stsp if (err)
1893 e2b1e152 2019-07-13 stsp goto done;
1894 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1895 b8f41171 2019-02-10 stsp path);
1896 6353ad76 2019-02-08 stsp goto done;
1897 a378724f 2019-02-10 stsp }
1898 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1899 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1900 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1901 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1902 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1903 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1904 0f58026f 2021-04-05 stsp if (err)
1905 0f58026f 2021-04-05 stsp goto done;
1906 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1907 0f58026f 2021-04-05 stsp path);
1908 b8f41171 2019-02-10 stsp goto done;
1909 e2b1e152 2019-07-13 stsp }
1910 9d31a1d8 2018-03-11 stsp }
1911 9d31a1d8 2018-03-11 stsp
1912 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1913 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1914 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1915 eb81bc23 2022-06-28 tracey goto done;
1916 eb81bc23 2022-06-28 tracey }
1917 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1918 8da9e5f4 2019-01-12 stsp if (err)
1919 6353ad76 2019-02-08 stsp goto done;
1920 512f0d0e 2019-01-02 stsp
1921 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1922 234035bc 2019-06-01 stsp int update_timestamps;
1923 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1924 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1925 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1926 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1927 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1928 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1929 eb81bc23 2022-06-28 tracey goto done;
1930 eb81bc23 2022-06-28 tracey }
1931 234035bc 2019-06-01 stsp struct got_object_id id2;
1932 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1933 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1934 eb81bc23 2022-06-28 tracey fd2);
1935 234035bc 2019-06-01 stsp if (err)
1936 f69721c3 2019-10-21 stsp goto done;
1937 f69721c3 2019-10-21 stsp }
1938 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1939 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1940 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1941 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1942 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1943 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1944 f69721c3 2019-10-21 stsp goto done;
1945 f69721c3 2019-10-21 stsp }
1946 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1947 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1948 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1949 234035bc 2019-06-01 stsp goto done;
1950 f69721c3 2019-10-21 stsp }
1951 234035bc 2019-06-01 stsp }
1952 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1953 36bf999c 2020-07-23 stsp char *link_target;
1954 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1955 36bf999c 2020-07-23 stsp if (err)
1956 36bf999c 2020-07-23 stsp goto done;
1957 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1958 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1959 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1960 36bf999c 2020-07-23 stsp free(link_target);
1961 993e2a1b 2020-07-23 stsp } else {
1962 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1963 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1964 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1965 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1966 993e2a1b 2020-07-23 stsp }
1967 f69721c3 2019-10-21 stsp free(label_orig);
1968 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1969 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1970 eb81bc23 2022-06-28 tracey goto done;
1971 eb81bc23 2022-06-28 tracey }
1972 234035bc 2019-06-01 stsp if (blob2)
1973 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1974 909d120e 2019-09-22 stsp if (err)
1975 909d120e 2019-09-22 stsp goto done;
1976 234035bc 2019-06-01 stsp /*
1977 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1978 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1979 234035bc 2019-06-01 stsp * unmodified files again.
1980 234035bc 2019-06-01 stsp */
1981 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1982 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1983 234035bc 2019-06-01 stsp update_timestamps);
1984 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1985 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1986 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1987 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1988 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1989 1ee397ad 2019-07-12 stsp if (err)
1990 1ee397ad 2019-07-12 stsp goto done;
1991 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1992 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1993 13d9040b 2019-03-27 stsp if (err)
1994 13d9040b 2019-03-27 stsp goto done;
1995 13d9040b 2019-03-27 stsp } else {
1996 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1997 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1998 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1999 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2000 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2001 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2002 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2003 2e1fa222 2020-07-23 stsp } else {
2004 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2005 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2006 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2007 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2008 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2009 2e1fa222 2020-07-23 stsp }
2010 13d9040b 2019-03-27 stsp if (err)
2011 13d9040b 2019-03-27 stsp goto done;
2012 65b05cec 2020-07-23 stsp
2013 054041d0 2020-06-24 stsp if (ie) {
2014 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2015 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2016 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2017 054041d0 2020-06-24 stsp } else {
2018 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2019 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2020 054041d0 2020-06-24 stsp &blob->id);
2021 054041d0 2020-06-24 stsp }
2022 13d9040b 2019-03-27 stsp if (err)
2023 13d9040b 2019-03-27 stsp goto done;
2024 65b05cec 2020-07-23 stsp
2025 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2026 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2027 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2028 2e1fa222 2020-07-23 stsp }
2029 13d9040b 2019-03-27 stsp }
2030 eb81bc23 2022-06-28 tracey
2031 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2032 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2033 eb81bc23 2022-06-28 tracey goto done;
2034 eb81bc23 2022-06-28 tracey }
2035 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2036 6353ad76 2019-02-08 stsp done:
2037 6353ad76 2019-02-08 stsp free(ondisk_path);
2038 8da9e5f4 2019-01-12 stsp return err;
2039 90285c3b 2019-01-08 stsp }
2040 90285c3b 2019-01-08 stsp
2041 90285c3b 2019-01-08 stsp static const struct got_error *
2042 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2043 90285c3b 2019-01-08 stsp {
2044 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2045 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2046 90285c3b 2019-01-08 stsp
2047 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2048 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2049 90285c3b 2019-01-08 stsp
2050 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2051 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2052 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2053 c97665ae 2019-01-13 stsp } else {
2054 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2055 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2056 1c4cdd89 2021-06-20 stsp if (err)
2057 1c4cdd89 2021-06-20 stsp goto done;
2058 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2059 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2060 fddefe3b 2020-10-20 stsp free(ondisk_path);
2061 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2062 1c4cdd89 2021-06-20 stsp parent = NULL;
2063 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2064 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2065 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2066 fddefe3b 2020-10-20 stsp ondisk_path);
2067 90285c3b 2019-01-08 stsp break;
2068 90285c3b 2019-01-08 stsp }
2069 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2070 1c4cdd89 2021-06-20 stsp if (err)
2071 1c4cdd89 2021-06-20 stsp break;
2072 1c4cdd89 2021-06-20 stsp }
2073 90285c3b 2019-01-08 stsp }
2074 1c4cdd89 2021-06-20 stsp done:
2075 90285c3b 2019-01-08 stsp free(ondisk_path);
2076 1c4cdd89 2021-06-20 stsp free(parent);
2077 90285c3b 2019-01-08 stsp return err;
2078 512f0d0e 2019-01-02 stsp }
2079 512f0d0e 2019-01-02 stsp
2080 708d8e67 2019-03-27 stsp static const struct got_error *
2081 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2083 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 708d8e67 2019-03-27 stsp {
2085 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2086 708d8e67 2019-03-27 stsp unsigned char status;
2087 708d8e67 2019-03-27 stsp struct stat sb;
2088 708d8e67 2019-03-27 stsp char *ondisk_path;
2089 708d8e67 2019-03-27 stsp
2090 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2091 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 a76c42e6 2019-08-03 stsp
2093 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 708d8e67 2019-03-27 stsp == -1)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2096 708d8e67 2019-03-27 stsp
2097 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2098 708d8e67 2019-03-27 stsp if (err)
2099 5a58a424 2020-06-23 stsp goto done;
2100 708d8e67 2019-03-27 stsp
2101 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2102 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2103 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2104 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2105 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2106 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2107 993e2a1b 2020-07-23 stsp goto done;
2108 993e2a1b 2020-07-23 stsp }
2109 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2110 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2111 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2112 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2113 993e2a1b 2020-07-23 stsp if (err)
2114 993e2a1b 2020-07-23 stsp goto done;
2115 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2116 993e2a1b 2020-07-23 stsp ie->path);
2117 993e2a1b 2020-07-23 stsp goto done;
2118 993e2a1b 2020-07-23 stsp }
2119 993e2a1b 2020-07-23 stsp
2120 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2121 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2122 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2123 1ee397ad 2019-07-12 stsp if (err)
2124 5a58a424 2020-06-23 stsp goto done;
2125 fc6346c4 2019-03-27 stsp /*
2126 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2127 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2128 fc6346c4 2019-03-27 stsp */
2129 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2130 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2131 fc6346c4 2019-03-27 stsp } else {
2132 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2133 1ee397ad 2019-07-12 stsp if (err)
2134 5a58a424 2020-06-23 stsp goto done;
2135 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2136 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2137 fc6346c4 2019-03-27 stsp if (err)
2138 5a58a424 2020-06-23 stsp goto done;
2139 fc6346c4 2019-03-27 stsp }
2140 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2141 708d8e67 2019-03-27 stsp }
2142 5a58a424 2020-06-23 stsp done:
2143 5a58a424 2020-06-23 stsp free(ondisk_path);
2144 fc6346c4 2019-03-27 stsp return err;
2145 708d8e67 2019-03-27 stsp }
2146 708d8e67 2019-03-27 stsp
2147 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2148 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2149 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2150 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2151 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2152 8da9e5f4 2019-01-12 stsp void *progress_arg;
2153 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2154 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2155 8da9e5f4 2019-01-12 stsp };
2156 8da9e5f4 2019-01-12 stsp
2157 512f0d0e 2019-01-02 stsp static const struct got_error *
2158 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2159 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2160 512f0d0e 2019-01-02 stsp {
2161 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2162 0584f854 2019-04-06 stsp
2163 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2164 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2165 512f0d0e 2019-01-02 stsp
2166 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2167 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2168 8da9e5f4 2019-01-12 stsp }
2169 512f0d0e 2019-01-02 stsp
2170 8da9e5f4 2019-01-12 stsp static const struct got_error *
2171 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 8da9e5f4 2019-01-12 stsp {
2173 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2174 7a9df742 2019-01-08 stsp
2175 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2177 0584f854 2019-04-06 stsp
2178 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2179 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2180 9d31a1d8 2018-03-11 stsp }
2181 9d31a1d8 2018-03-11 stsp
2182 9d31a1d8 2018-03-11 stsp static const struct got_error *
2183 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 9d31a1d8 2018-03-11 stsp {
2185 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2186 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2187 8da9e5f4 2019-01-12 stsp char *path;
2188 9d31a1d8 2018-03-11 stsp
2189 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2191 0584f854 2019-04-06 stsp
2192 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2193 63c5ca5d 2019-08-24 stsp return NULL;
2194 63c5ca5d 2019-08-24 stsp
2195 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2196 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2197 8da9e5f4 2019-01-12 stsp == -1)
2198 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2199 9d31a1d8 2018-03-11 stsp
2200 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2201 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2202 8da9e5f4 2019-01-12 stsp else
2203 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2204 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2205 9d31a1d8 2018-03-11 stsp
2206 8da9e5f4 2019-01-12 stsp free(path);
2207 0cd1c46a 2019-03-11 stsp return err;
2208 0cd1c46a 2019-03-11 stsp }
2209 0cd1c46a 2019-03-11 stsp
2210 b2118c49 2020-07-28 stsp const struct got_error *
2211 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 b2118c49 2020-07-28 stsp {
2213 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2214 b2118c49 2020-07-28 stsp
2215 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2216 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2217 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2218 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2219 b2118c49 2020-07-28 stsp }
2220 b2118c49 2020-07-28 stsp
2221 b2118c49 2020-07-28 stsp return NULL;
2222 b2118c49 2020-07-28 stsp }
2223 b2118c49 2020-07-28 stsp
2224 818c7501 2019-07-11 stsp static const struct got_error *
2225 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 0cd1c46a 2019-03-11 stsp {
2227 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2228 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2229 0cd1c46a 2019-03-11 stsp
2230 517bab73 2019-03-11 stsp *refname = NULL;
2231 517bab73 2019-03-11 stsp
2232 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2233 b2118c49 2020-07-28 stsp if (err)
2234 b2118c49 2020-07-28 stsp return err;
2235 0cd1c46a 2019-03-11 stsp
2236 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2237 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2238 0647c563 2019-03-11 stsp *refname = NULL;
2239 0cd1c46a 2019-03-11 stsp }
2240 517bab73 2019-03-11 stsp free(uuidstr);
2241 517bab73 2019-03-11 stsp return err;
2242 517bab73 2019-03-11 stsp }
2243 517bab73 2019-03-11 stsp
2244 818c7501 2019-07-11 stsp const struct got_error *
2245 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2246 818c7501 2019-07-11 stsp {
2247 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2248 818c7501 2019-07-11 stsp }
2249 818c7501 2019-07-11 stsp
2250 818c7501 2019-07-11 stsp static const struct got_error *
2251 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2252 818c7501 2019-07-11 stsp {
2253 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2254 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2255 818c7501 2019-07-11 stsp }
2256 818c7501 2019-07-11 stsp
2257 818c7501 2019-07-11 stsp static const struct got_error *
2258 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2259 818c7501 2019-07-11 stsp {
2260 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2261 818c7501 2019-07-11 stsp }
2262 818c7501 2019-07-11 stsp
2263 818c7501 2019-07-11 stsp static const struct got_error *
2264 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2265 818c7501 2019-07-11 stsp {
2266 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2267 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2268 818c7501 2019-07-11 stsp }
2269 818c7501 2019-07-11 stsp
2270 818c7501 2019-07-11 stsp static const struct got_error *
2271 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2272 818c7501 2019-07-11 stsp {
2273 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2274 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2275 0ebf8283 2019-07-24 stsp }
2276 0ebf8283 2019-07-24 stsp
2277 0ebf8283 2019-07-24 stsp static const struct got_error *
2278 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2279 0ebf8283 2019-07-24 stsp {
2280 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2281 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2282 0ebf8283 2019-07-24 stsp }
2283 0ebf8283 2019-07-24 stsp
2284 0ebf8283 2019-07-24 stsp static const struct got_error *
2285 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2286 0ebf8283 2019-07-24 stsp {
2287 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2288 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2289 0ebf8283 2019-07-24 stsp }
2290 0ebf8283 2019-07-24 stsp
2291 0ebf8283 2019-07-24 stsp static const struct got_error *
2292 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 0ebf8283 2019-07-24 stsp {
2294 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2295 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2296 818c7501 2019-07-11 stsp }
2297 818c7501 2019-07-11 stsp
2298 0ebf8283 2019-07-24 stsp static const struct got_error *
2299 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 0ebf8283 2019-07-24 stsp {
2301 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2302 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2303 0ebf8283 2019-07-24 stsp }
2304 818c7501 2019-07-11 stsp
2305 0ebf8283 2019-07-24 stsp const struct got_error *
2306 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2307 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2308 0ebf8283 2019-07-24 stsp {
2309 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2310 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2311 0ebf8283 2019-07-24 stsp *path = NULL;
2312 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2313 0ebf8283 2019-07-24 stsp }
2314 0ebf8283 2019-07-24 stsp return NULL;
2315 f259c4c1 2021-09-24 stsp }
2316 f259c4c1 2021-09-24 stsp
2317 f259c4c1 2021-09-24 stsp static const struct got_error *
2318 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2319 f259c4c1 2021-09-24 stsp {
2320 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2321 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2322 0ebf8283 2019-07-24 stsp }
2323 0ebf8283 2019-07-24 stsp
2324 f259c4c1 2021-09-24 stsp static const struct got_error *
2325 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2326 f259c4c1 2021-09-24 stsp {
2327 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2328 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2329 f259c4c1 2021-09-24 stsp }
2330 f259c4c1 2021-09-24 stsp
2331 517bab73 2019-03-11 stsp /*
2332 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2333 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2334 517bab73 2019-03-11 stsp */
2335 517bab73 2019-03-11 stsp static const struct got_error *
2336 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2337 517bab73 2019-03-11 stsp {
2338 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2339 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2340 517bab73 2019-03-11 stsp char *refname;
2341 517bab73 2019-03-11 stsp
2342 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2343 517bab73 2019-03-11 stsp if (err)
2344 517bab73 2019-03-11 stsp return err;
2345 0cd1c46a 2019-03-11 stsp
2346 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2347 0cd1c46a 2019-03-11 stsp if (err)
2348 0cd1c46a 2019-03-11 stsp goto done;
2349 0cd1c46a 2019-03-11 stsp
2350 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2351 0cd1c46a 2019-03-11 stsp done:
2352 0cd1c46a 2019-03-11 stsp free(refname);
2353 0cd1c46a 2019-03-11 stsp if (ref)
2354 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2355 3e3a69f1 2019-07-25 stsp return err;
2356 3e3a69f1 2019-07-25 stsp }
2357 3e3a69f1 2019-07-25 stsp
2358 3e3a69f1 2019-07-25 stsp static const struct got_error *
2359 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2360 3e3a69f1 2019-07-25 stsp {
2361 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2362 3e3a69f1 2019-07-25 stsp
2363 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2364 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2365 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2366 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2367 3e3a69f1 2019-07-25 stsp }
2368 9d31a1d8 2018-03-11 stsp return err;
2369 9d31a1d8 2018-03-11 stsp }
2370 9d31a1d8 2018-03-11 stsp
2371 3e3a69f1 2019-07-25 stsp
2372 ebf99748 2019-05-09 stsp static const struct got_error *
2373 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2374 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2375 ebf99748 2019-05-09 stsp {
2376 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2377 ebf99748 2019-05-09 stsp FILE *index = NULL;
2378 0cd1c46a 2019-03-11 stsp
2379 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2380 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2381 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2382 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2383 ebf99748 2019-05-09 stsp
2384 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2385 3e3a69f1 2019-07-25 stsp if (err)
2386 ebf99748 2019-05-09 stsp goto done;
2387 ebf99748 2019-05-09 stsp
2388 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2389 ebf99748 2019-05-09 stsp if (index == NULL) {
2390 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2391 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2392 ebf99748 2019-05-09 stsp } else {
2393 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2394 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2395 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2396 ebf99748 2019-05-09 stsp }
2397 ebf99748 2019-05-09 stsp done:
2398 ebf99748 2019-05-09 stsp if (err) {
2399 ebf99748 2019-05-09 stsp free(*fileindex_path);
2400 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2401 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2402 ebf99748 2019-05-09 stsp *fileindex = NULL;
2403 ebf99748 2019-05-09 stsp }
2404 ebf99748 2019-05-09 stsp return err;
2405 ebf99748 2019-05-09 stsp }
2406 c932eeeb 2019-05-22 stsp
2407 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2408 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2409 c932eeeb 2019-05-22 stsp const char *path;
2410 c932eeeb 2019-05-22 stsp size_t path_len;
2411 c932eeeb 2019-05-22 stsp const char *entry_name;
2412 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2413 a484d721 2019-06-10 stsp void *progress_arg;
2414 c932eeeb 2019-05-22 stsp };
2415 ebf99748 2019-05-09 stsp
2416 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2417 c932eeeb 2019-05-22 stsp static const struct got_error *
2418 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2419 c932eeeb 2019-05-22 stsp {
2420 1ee397ad 2019-07-12 stsp const struct got_error *err;
2421 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2422 c932eeeb 2019-05-22 stsp
2423 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2424 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2425 c932eeeb 2019-05-22 stsp return NULL;
2426 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2427 102ff934 2019-06-11 stsp return NULL;
2428 102ff934 2019-06-11 stsp
2429 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2430 a769b60b 2021-06-27 stsp return NULL;
2431 a769b60b 2021-06-27 stsp
2432 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2433 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2434 c932eeeb 2019-05-22 stsp return NULL;
2435 c932eeeb 2019-05-22 stsp
2436 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2437 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2438 edd02c5e 2019-07-11 stsp ie->path);
2439 1ee397ad 2019-07-12 stsp if (err)
2440 1ee397ad 2019-07-12 stsp return err;
2441 1ee397ad 2019-07-12 stsp }
2442 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2443 c932eeeb 2019-05-22 stsp return NULL;
2444 a615e0e7 2020-12-16 stsp }
2445 a615e0e7 2020-12-16 stsp
2446 a615e0e7 2020-12-16 stsp static const struct got_error *
2447 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2448 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2449 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 a615e0e7 2020-12-16 stsp {
2451 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2452 a615e0e7 2020-12-16 stsp
2453 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2454 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2455 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2456 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2457 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2458 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2459 a615e0e7 2020-12-16 stsp
2460 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2461 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2462 9c6338c4 2019-06-02 stsp }
2463 9c6338c4 2019-06-02 stsp
2464 9c6338c4 2019-06-02 stsp static const struct got_error *
2465 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 9c6338c4 2019-06-02 stsp {
2467 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2468 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2469 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2470 867630bb 2020-01-17 stsp struct timespec timeout;
2471 9c6338c4 2019-06-02 stsp
2472 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2473 b90054ed 2022-11-01 stsp fileindex_path, "");
2474 9c6338c4 2019-06-02 stsp if (err)
2475 9c6338c4 2019-06-02 stsp goto done;
2476 9c6338c4 2019-06-02 stsp
2477 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2478 9c6338c4 2019-06-02 stsp if (err)
2479 9c6338c4 2019-06-02 stsp goto done;
2480 9c6338c4 2019-06-02 stsp
2481 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2482 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2483 9c6338c4 2019-06-02 stsp fileindex_path);
2484 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2485 9c6338c4 2019-06-02 stsp }
2486 867630bb 2020-01-17 stsp
2487 867630bb 2020-01-17 stsp /*
2488 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2489 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2490 867630bb 2020-01-17 stsp * was recorded in the file index.
2491 867630bb 2020-01-17 stsp */
2492 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2493 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2494 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2495 9c6338c4 2019-06-02 stsp done:
2496 9c6338c4 2019-06-02 stsp if (new_index)
2497 9c6338c4 2019-06-02 stsp fclose(new_index);
2498 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2499 9c6338c4 2019-06-02 stsp return err;
2500 c932eeeb 2019-05-22 stsp }
2501 6ced4176 2019-07-12 stsp
2502 6ced4176 2019-07-12 stsp static const struct got_error *
2503 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2504 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2505 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2506 a44927cc 2022-04-07 stsp struct got_repository *repo)
2507 6ced4176 2019-07-12 stsp {
2508 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2509 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2510 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2511 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2512 6ced4176 2019-07-12 stsp
2513 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2514 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2515 6ced4176 2019-07-12 stsp *tree_id = NULL;
2516 6ced4176 2019-07-12 stsp
2517 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2518 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2519 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2520 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2521 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2522 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2523 6ced4176 2019-07-12 stsp goto done;
2524 6ced4176 2019-07-12 stsp }
2525 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2526 a44927cc 2022-04-07 stsp worktree->path_prefix);
2527 6ced4176 2019-07-12 stsp if (err)
2528 6ced4176 2019-07-12 stsp goto done;
2529 6ced4176 2019-07-12 stsp return NULL;
2530 6ced4176 2019-07-12 stsp }
2531 6ced4176 2019-07-12 stsp
2532 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2533 6ced4176 2019-07-12 stsp
2534 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2535 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2536 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2537 6ced4176 2019-07-12 stsp goto done;
2538 6ced4176 2019-07-12 stsp }
2539 6ced4176 2019-07-12 stsp
2540 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2541 6ced4176 2019-07-12 stsp if (err)
2542 6ced4176 2019-07-12 stsp goto done;
2543 6ced4176 2019-07-12 stsp
2544 6ced4176 2019-07-12 stsp free(in_repo_path);
2545 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2546 6ced4176 2019-07-12 stsp
2547 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2548 6ced4176 2019-07-12 stsp if (err)
2549 6ced4176 2019-07-12 stsp goto done;
2550 c932eeeb 2019-05-22 stsp
2551 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2552 6ced4176 2019-07-12 stsp /* Check out a single file. */
2553 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2554 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2555 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2556 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2557 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2558 6ced4176 2019-07-12 stsp goto done;
2559 6ced4176 2019-07-12 stsp }
2560 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2561 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2562 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2563 6ced4176 2019-07-12 stsp goto done;
2564 6ced4176 2019-07-12 stsp }
2565 6ced4176 2019-07-12 stsp } else {
2566 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2567 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2568 6ced4176 2019-07-12 stsp if (err)
2569 6ced4176 2019-07-12 stsp return err;
2570 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2571 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2572 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2573 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2574 6ced4176 2019-07-12 stsp goto done;
2575 6ced4176 2019-07-12 stsp }
2576 6ced4176 2019-07-12 stsp }
2577 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2578 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2579 6ced4176 2019-07-12 stsp } else {
2580 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2581 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2582 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2583 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2584 6ced4176 2019-07-12 stsp goto done;
2585 6ced4176 2019-07-12 stsp }
2586 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2587 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2588 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2589 6ced4176 2019-07-12 stsp goto done;
2590 6ced4176 2019-07-12 stsp }
2591 6ced4176 2019-07-12 stsp }
2592 6ced4176 2019-07-12 stsp done:
2593 6ced4176 2019-07-12 stsp free(id);
2594 6ced4176 2019-07-12 stsp free(in_repo_path);
2595 6ced4176 2019-07-12 stsp if (err) {
2596 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2597 6ced4176 2019-07-12 stsp free(*tree_relpath);
2598 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2599 6ced4176 2019-07-12 stsp free(*tree_id);
2600 6ced4176 2019-07-12 stsp *tree_id = NULL;
2601 6ced4176 2019-07-12 stsp }
2602 07ed472d 2019-07-12 stsp return err;
2603 07ed472d 2019-07-12 stsp }
2604 07ed472d 2019-07-12 stsp
2605 07ed472d 2019-07-12 stsp static const struct got_error *
2606 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2607 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2608 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2609 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 07ed472d 2019-07-12 stsp {
2611 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2612 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2613 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2614 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2615 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2616 07ed472d 2019-07-12 stsp
2617 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2618 7f47418f 2019-12-20 stsp if (err) {
2619 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2620 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2621 7f47418f 2019-12-20 stsp goto done;
2622 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2623 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2624 7f47418f 2019-12-20 stsp if (err)
2625 7f47418f 2019-12-20 stsp return err;
2626 7f47418f 2019-12-20 stsp }
2627 07ed472d 2019-07-12 stsp
2628 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2629 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2630 07ed472d 2019-07-12 stsp if (err)
2631 07ed472d 2019-07-12 stsp goto done;
2632 07ed472d 2019-07-12 stsp
2633 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2634 07ed472d 2019-07-12 stsp if (err)
2635 07ed472d 2019-07-12 stsp goto done;
2636 07ed472d 2019-07-12 stsp
2637 07ed472d 2019-07-12 stsp if (entry_name &&
2638 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2639 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2640 07ed472d 2019-07-12 stsp goto done;
2641 07ed472d 2019-07-12 stsp }
2642 07ed472d 2019-07-12 stsp
2643 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2644 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2645 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2646 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2647 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2648 07ed472d 2019-07-12 stsp arg.repo = repo;
2649 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2650 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2651 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2652 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2653 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2654 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2655 07ed472d 2019-07-12 stsp done:
2656 07ed472d 2019-07-12 stsp if (tree)
2657 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2658 07ed472d 2019-07-12 stsp if (commit)
2659 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2660 6ced4176 2019-07-12 stsp return err;
2661 6ced4176 2019-07-12 stsp }
2662 6ced4176 2019-07-12 stsp
2663 9d31a1d8 2018-03-11 stsp const struct got_error *
2664 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2665 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2666 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2667 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2668 9d31a1d8 2018-03-11 stsp {
2669 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2670 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2671 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2672 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2673 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2674 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2675 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2676 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2677 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2678 f2ea84fa 2019-07-27 stsp int entry_type;
2679 f2ea84fa 2019-07-27 stsp char *relpath;
2680 f2ea84fa 2019-07-27 stsp char *entry_name;
2681 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2682 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 9d31a1d8 2018-03-11 stsp
2684 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2685 f2ea84fa 2019-07-27 stsp
2686 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2687 9d31a1d8 2018-03-11 stsp if (err)
2688 9d31a1d8 2018-03-11 stsp return err;
2689 a44927cc 2022-04-07 stsp
2690 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2691 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2692 a44927cc 2022-04-07 stsp if (err)
2693 a44927cc 2022-04-07 stsp goto done;
2694 93a30277 2018-12-24 stsp
2695 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2696 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2697 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2698 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2699 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2700 f2ea84fa 2019-07-27 stsp goto done;
2701 f2ea84fa 2019-07-27 stsp }
2702 6ced4176 2019-07-12 stsp
2703 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2704 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2705 a44927cc 2022-04-07 stsp worktree, repo);
2706 f2ea84fa 2019-07-27 stsp if (err) {
2707 f2ea84fa 2019-07-27 stsp free(tpd);
2708 6ced4176 2019-07-12 stsp goto done;
2709 6ced4176 2019-07-12 stsp }
2710 f2ea84fa 2019-07-27 stsp
2711 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2712 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2713 f2ea84fa 2019-07-27 stsp if (err) {
2714 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2715 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2716 f2ea84fa 2019-07-27 stsp free(tpd);
2717 f2ea84fa 2019-07-27 stsp goto done;
2718 f2ea84fa 2019-07-27 stsp }
2719 f2ea84fa 2019-07-27 stsp } else
2720 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2721 f2ea84fa 2019-07-27 stsp
2722 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2723 6ced4176 2019-07-12 stsp }
2724 6ced4176 2019-07-12 stsp
2725 51514078 2018-12-25 stsp /*
2726 51514078 2018-12-25 stsp * Read the file index.
2727 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2728 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2729 51514078 2018-12-25 stsp */
2730 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2731 8da9e5f4 2019-01-12 stsp if (err)
2732 c4cdcb68 2019-04-03 stsp goto done;
2733 c4cdcb68 2019-04-03 stsp
2734 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2735 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2736 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2737 f2ea84fa 2019-07-27 stsp
2738 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2739 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2740 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2741 f2ea84fa 2019-07-27 stsp if (err)
2742 f2ea84fa 2019-07-27 stsp break;
2743 f2ea84fa 2019-07-27 stsp
2744 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2745 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2746 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2747 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2748 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2749 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2750 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2751 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2752 f2ea84fa 2019-07-27 stsp if (err)
2753 f2ea84fa 2019-07-27 stsp break;
2754 f2ea84fa 2019-07-27 stsp
2755 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2756 711d9cd9 2019-07-12 stsp }
2757 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2758 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2759 af12c6b9 2019-06-04 stsp err = sync_err;
2760 9d31a1d8 2018-03-11 stsp done:
2761 9c6338c4 2019-06-02 stsp free(fileindex_path);
2762 edfa7d7f 2018-09-11 stsp if (tree)
2763 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2764 9d31a1d8 2018-03-11 stsp if (commit)
2765 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2766 5ade8233 2019-07-12 stsp if (fileindex)
2767 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2768 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2769 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2770 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2771 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2772 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2773 f2ea84fa 2019-07-27 stsp free(tpd);
2774 f2ea84fa 2019-07-27 stsp }
2775 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2776 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2777 9d31a1d8 2018-03-11 stsp err = unlockerr;
2778 f8d1f275 2019-02-04 stsp return err;
2779 f8d1f275 2019-02-04 stsp }
2780 f8d1f275 2019-02-04 stsp
2781 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2782 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2783 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2784 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2785 234035bc 2019-06-01 stsp void *progress_arg;
2786 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2787 234035bc 2019-06-01 stsp void *cancel_arg;
2788 f69721c3 2019-10-21 stsp const char *label_orig;
2789 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2790 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2791 234035bc 2019-06-01 stsp };
2792 234035bc 2019-06-01 stsp
2793 234035bc 2019-06-01 stsp static const struct got_error *
2794 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2795 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2796 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2797 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2798 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2799 234035bc 2019-06-01 stsp {
2800 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2801 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2802 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2803 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2804 234035bc 2019-06-01 stsp struct stat sb;
2805 234035bc 2019-06-01 stsp unsigned char status;
2806 234035bc 2019-06-01 stsp int local_changes_subsumed;
2807 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2808 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2809 234035bc 2019-06-01 stsp
2810 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2811 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2812 d6c87207 2019-08-02 stsp strlen(path2));
2813 1ee397ad 2019-07-12 stsp if (ie == NULL)
2814 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2815 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2816 234035bc 2019-06-01 stsp
2817 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2818 234035bc 2019-06-01 stsp path2) == -1)
2819 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2820 234035bc 2019-06-01 stsp
2821 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2822 7f91a133 2019-12-13 stsp repo);
2823 234035bc 2019-06-01 stsp if (err)
2824 234035bc 2019-06-01 stsp goto done;
2825 234035bc 2019-06-01 stsp
2826 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2827 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2828 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2829 234035bc 2019-06-01 stsp goto done;
2830 234035bc 2019-06-01 stsp }
2831 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2832 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2833 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2834 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2835 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2836 234035bc 2019-06-01 stsp goto done;
2837 234035bc 2019-06-01 stsp }
2838 234035bc 2019-06-01 stsp
2839 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2840 36bf999c 2020-07-23 stsp char *link_target2;
2841 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2842 36bf999c 2020-07-23 stsp if (err)
2843 36bf999c 2020-07-23 stsp goto done;
2844 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2845 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2846 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2847 36bf999c 2020-07-23 stsp free(link_target2);
2848 af57b12a 2020-07-23 stsp } else {
2849 1af628f4 2021-06-03 stsp int fd;
2850 1af628f4 2021-06-03 stsp
2851 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2852 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2853 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2854 1af628f4 2021-06-03 stsp goto done;
2855 1af628f4 2021-06-03 stsp }
2856 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2857 1af628f4 2021-06-03 stsp f_orig, blob1);
2858 1af628f4 2021-06-03 stsp if (err)
2859 1af628f4 2021-06-03 stsp goto done;
2860 1af628f4 2021-06-03 stsp
2861 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2862 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2863 1af628f4 2021-06-03 stsp goto done;
2864 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2865 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2866 1af628f4 2021-06-03 stsp if (err)
2867 1af628f4 2021-06-03 stsp goto done;
2868 1af628f4 2021-06-03 stsp
2869 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2870 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2871 1af628f4 2021-06-03 stsp if (fd == -1) {
2872 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2873 1af628f4 2021-06-03 stsp ondisk_path);
2874 1af628f4 2021-06-03 stsp goto done;
2875 1af628f4 2021-06-03 stsp }
2876 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2877 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2878 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2879 1af628f4 2021-06-03 stsp ondisk_path);
2880 1af628f4 2021-06-03 stsp close(fd);
2881 1af628f4 2021-06-03 stsp goto done;
2882 1af628f4 2021-06-03 stsp }
2883 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2884 1af628f4 2021-06-03 stsp if (err)
2885 1af628f4 2021-06-03 stsp goto done;
2886 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2887 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2888 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2889 1af628f4 2021-06-03 stsp goto done;
2890 1af628f4 2021-06-03 stsp }
2891 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2892 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2893 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2894 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2895 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2896 af57b12a 2020-07-23 stsp }
2897 234035bc 2019-06-01 stsp } else if (blob1) {
2898 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2899 d6c87207 2019-08-02 stsp strlen(path1));
2900 1ee397ad 2019-07-12 stsp if (ie == NULL)
2901 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2902 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2903 2b92fad7 2019-06-02 stsp
2904 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2905 2b92fad7 2019-06-02 stsp path1) == -1)
2906 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2907 2b92fad7 2019-06-02 stsp
2908 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2909 7f91a133 2019-12-13 stsp repo);
2910 2b92fad7 2019-06-02 stsp if (err)
2911 2b92fad7 2019-06-02 stsp goto done;
2912 2b92fad7 2019-06-02 stsp
2913 2b92fad7 2019-06-02 stsp switch (status) {
2914 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2915 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2916 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2917 1ee397ad 2019-07-12 stsp if (err)
2918 1ee397ad 2019-07-12 stsp goto done;
2919 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2920 2b92fad7 2019-06-02 stsp if (err)
2921 2b92fad7 2019-06-02 stsp goto done;
2922 2b92fad7 2019-06-02 stsp if (ie)
2923 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2924 2b92fad7 2019-06-02 stsp break;
2925 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2926 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2927 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2928 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2929 1ee397ad 2019-07-12 stsp if (err)
2930 1ee397ad 2019-07-12 stsp goto done;
2931 2b92fad7 2019-06-02 stsp if (ie)
2932 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2933 2b92fad7 2019-06-02 stsp break;
2934 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2935 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2936 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2937 72840534 2022-01-19 stsp off_t blob1_size;
2938 0a22ca1a 2020-09-23 stsp /*
2939 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2940 0a22ca1a 2020-09-23 stsp * exists in the repository.
2941 0a22ca1a 2020-09-23 stsp */
2942 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2943 72840534 2022-01-19 stsp &blob1_size, path1);
2944 0a22ca1a 2020-09-23 stsp if (err)
2945 0a22ca1a 2020-09-23 stsp goto done;
2946 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2947 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2948 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2949 0a22ca1a 2020-09-23 stsp if (err)
2950 0a22ca1a 2020-09-23 stsp goto done;
2951 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2952 0a22ca1a 2020-09-23 stsp path1);
2953 0a22ca1a 2020-09-23 stsp if (err)
2954 0a22ca1a 2020-09-23 stsp goto done;
2955 0a22ca1a 2020-09-23 stsp if (ie)
2956 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2957 0a22ca1a 2020-09-23 stsp ie);
2958 0a22ca1a 2020-09-23 stsp } else {
2959 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2960 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2961 0a22ca1a 2020-09-23 stsp }
2962 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2963 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2964 0a22ca1a 2020-09-23 stsp free(id);
2965 0a22ca1a 2020-09-23 stsp if (err)
2966 0a22ca1a 2020-09-23 stsp goto done;
2967 0a22ca1a 2020-09-23 stsp break;
2968 0a22ca1a 2020-09-23 stsp }
2969 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2970 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2971 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2972 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2973 1ee397ad 2019-07-12 stsp if (err)
2974 1ee397ad 2019-07-12 stsp goto done;
2975 2b92fad7 2019-06-02 stsp break;
2976 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2977 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2978 1ee397ad 2019-07-12 stsp if (err)
2979 1ee397ad 2019-07-12 stsp goto done;
2980 2b92fad7 2019-06-02 stsp break;
2981 2b92fad7 2019-06-02 stsp default:
2982 2b92fad7 2019-06-02 stsp break;
2983 2b92fad7 2019-06-02 stsp }
2984 234035bc 2019-06-01 stsp } else if (blob2) {
2985 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 234035bc 2019-06-01 stsp path2) == -1)
2987 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2988 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2989 d6c87207 2019-08-02 stsp strlen(path2));
2990 234035bc 2019-06-01 stsp if (ie) {
2991 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2992 7f91a133 2019-12-13 stsp -1, NULL, repo);
2993 234035bc 2019-06-01 stsp if (err)
2994 234035bc 2019-06-01 stsp goto done;
2995 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2996 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2997 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2998 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2999 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3000 1ee397ad 2019-07-12 stsp status, path2);
3001 234035bc 2019-06-01 stsp goto done;
3002 234035bc 2019-06-01 stsp }
3003 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3004 36bf999c 2020-07-23 stsp char *link_target2;
3005 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3006 36bf999c 2020-07-23 stsp blob2);
3007 36bf999c 2020-07-23 stsp if (err)
3008 36bf999c 2020-07-23 stsp goto done;
3009 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3010 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3011 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3012 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3013 36bf999c 2020-07-23 stsp free(link_target2);
3014 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3015 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3016 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3017 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3018 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3019 526a746f 2020-07-23 stsp a->progress_arg);
3020 dfe9fba0 2020-07-23 stsp } else {
3021 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3022 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3023 526a746f 2020-07-23 stsp }
3024 dfe9fba0 2020-07-23 stsp if (err)
3025 dfe9fba0 2020-07-23 stsp goto done;
3026 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3027 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3028 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3029 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3030 234035bc 2019-06-01 stsp if (err)
3031 234035bc 2019-06-01 stsp goto done;
3032 234035bc 2019-06-01 stsp }
3033 234035bc 2019-06-01 stsp } else {
3034 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3035 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3036 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3037 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3038 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3039 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3040 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3041 2e1fa222 2020-07-23 stsp } else {
3042 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3043 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3044 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3045 2e1fa222 2020-07-23 stsp }
3046 234035bc 2019-06-01 stsp if (err)
3047 234035bc 2019-06-01 stsp goto done;
3048 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3049 234035bc 2019-06-01 stsp if (err)
3050 234035bc 2019-06-01 stsp goto done;
3051 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3052 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3053 3969253a 2020-03-07 stsp if (err) {
3054 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3055 3969253a 2020-03-07 stsp goto done;
3056 3969253a 2020-03-07 stsp }
3057 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3058 2b92fad7 2019-06-02 stsp if (err) {
3059 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3060 2b92fad7 2019-06-02 stsp goto done;
3061 2b92fad7 2019-06-02 stsp }
3062 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3063 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3064 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3065 2e1fa222 2020-07-23 stsp }
3066 234035bc 2019-06-01 stsp }
3067 234035bc 2019-06-01 stsp }
3068 234035bc 2019-06-01 stsp done:
3069 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3070 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3071 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3072 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3073 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3074 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3075 1af628f4 2021-06-03 stsp free(id_str);
3076 54d5be07 2021-06-03 stsp free(label_deriv2);
3077 234035bc 2019-06-01 stsp free(ondisk_path);
3078 234035bc 2019-06-01 stsp return err;
3079 234035bc 2019-06-01 stsp }
3080 234035bc 2019-06-01 stsp
3081 69de9dd4 2021-09-03 stsp static const struct got_error *
3082 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3083 69de9dd4 2021-09-03 stsp {
3084 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3085 69de9dd4 2021-09-03 stsp
3086 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3087 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3088 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3089 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3090 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3091 69de9dd4 2021-09-03 stsp
3092 69de9dd4 2021-09-03 stsp return NULL;
3093 69de9dd4 2021-09-03 stsp }
3094 69de9dd4 2021-09-03 stsp
3095 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3096 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3097 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3098 234035bc 2019-06-01 stsp struct got_repository *repo;
3099 234035bc 2019-06-01 stsp };
3100 234035bc 2019-06-01 stsp
3101 234035bc 2019-06-01 stsp static const struct got_error *
3102 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3103 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3104 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3105 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3106 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3107 234035bc 2019-06-01 stsp {
3108 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3109 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3110 234035bc 2019-06-01 stsp unsigned char status;
3111 234035bc 2019-06-01 stsp struct stat sb;
3112 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3113 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3114 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3115 234035bc 2019-06-01 stsp char *ondisk_path;
3116 234035bc 2019-06-01 stsp
3117 69de9dd4 2021-09-03 stsp if (id == NULL)
3118 69de9dd4 2021-09-03 stsp return NULL;
3119 234035bc 2019-06-01 stsp
3120 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3121 69de9dd4 2021-09-03 stsp if (ie == NULL)
3122 69de9dd4 2021-09-03 stsp return NULL;
3123 69de9dd4 2021-09-03 stsp
3124 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3125 234035bc 2019-06-01 stsp == -1)
3126 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3127 234035bc 2019-06-01 stsp
3128 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3129 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3130 5546d466 2021-09-02 stsp free(ondisk_path);
3131 234035bc 2019-06-01 stsp if (err)
3132 234035bc 2019-06-01 stsp return err;
3133 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3134 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3135 234035bc 2019-06-01 stsp
3136 234035bc 2019-06-01 stsp return NULL;
3137 234035bc 2019-06-01 stsp }
3138 234035bc 2019-06-01 stsp
3139 818c7501 2019-07-11 stsp static const struct got_error *
3140 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3141 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3142 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3143 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3144 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3145 234035bc 2019-06-01 stsp {
3146 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3147 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3148 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3149 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3150 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3151 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3152 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3153 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3154 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3155 234035bc 2019-06-01 stsp
3156 03415a1a 2019-06-02 stsp if (commit_id1) {
3157 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3158 a44927cc 2022-04-07 stsp if (err)
3159 a44927cc 2022-04-07 stsp goto done;
3160 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3161 03415a1a 2019-06-02 stsp worktree->path_prefix);
3162 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3163 03415a1a 2019-06-02 stsp goto done;
3164 69d57f3d 2020-07-31 stsp }
3165 69d57f3d 2020-07-31 stsp if (tree_id1) {
3166 69d57f3d 2020-07-31 stsp char *id_str;
3167 03415a1a 2019-06-02 stsp
3168 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3169 03415a1a 2019-06-02 stsp if (err)
3170 03415a1a 2019-06-02 stsp goto done;
3171 f69721c3 2019-10-21 stsp
3172 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3173 f69721c3 2019-10-21 stsp if (err)
3174 f69721c3 2019-10-21 stsp goto done;
3175 f69721c3 2019-10-21 stsp
3176 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3177 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3178 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3179 f69721c3 2019-10-21 stsp free(id_str);
3180 f69721c3 2019-10-21 stsp goto done;
3181 f69721c3 2019-10-21 stsp }
3182 f69721c3 2019-10-21 stsp free(id_str);
3183 b72706c3 2022-06-01 stsp
3184 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3185 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3186 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3187 b72706c3 2022-06-01 stsp goto done;
3188 b72706c3 2022-06-01 stsp }
3189 03415a1a 2019-06-02 stsp }
3190 234035bc 2019-06-01 stsp
3191 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3192 a44927cc 2022-04-07 stsp if (err)
3193 a44927cc 2022-04-07 stsp goto done;
3194 a44927cc 2022-04-07 stsp
3195 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3196 234035bc 2019-06-01 stsp worktree->path_prefix);
3197 234035bc 2019-06-01 stsp if (err)
3198 234035bc 2019-06-01 stsp goto done;
3199 234035bc 2019-06-01 stsp
3200 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3201 234035bc 2019-06-01 stsp if (err)
3202 234035bc 2019-06-01 stsp goto done;
3203 234035bc 2019-06-01 stsp
3204 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3205 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3206 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3207 f9d37699 2022-06-28 stsp goto done;
3208 f9d37699 2022-06-28 stsp }
3209 f9d37699 2022-06-28 stsp
3210 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3211 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3212 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3213 b72706c3 2022-06-01 stsp goto done;
3214 b72706c3 2022-06-01 stsp }
3215 b72706c3 2022-06-01 stsp
3216 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3217 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3218 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3219 f9d37699 2022-06-28 stsp goto done;
3220 f9d37699 2022-06-28 stsp }
3221 f9d37699 2022-06-28 stsp
3222 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3223 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3224 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3225 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3226 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3227 69de9dd4 2021-09-03 stsp if (err)
3228 69de9dd4 2021-09-03 stsp goto done;
3229 69de9dd4 2021-09-03 stsp
3230 234035bc 2019-06-01 stsp arg.worktree = worktree;
3231 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3232 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3233 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3234 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3235 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3236 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3237 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3238 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3239 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3240 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3241 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3242 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3243 af12c6b9 2019-06-04 stsp err = sync_err;
3244 234035bc 2019-06-01 stsp done:
3245 a44927cc 2022-04-07 stsp if (commit1)
3246 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3247 a44927cc 2022-04-07 stsp if (commit2)
3248 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3249 234035bc 2019-06-01 stsp if (tree1)
3250 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3251 234035bc 2019-06-01 stsp if (tree2)
3252 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3253 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3254 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3255 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3256 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3257 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3258 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3259 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3260 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3261 f69721c3 2019-10-21 stsp free(label_orig);
3262 818c7501 2019-07-11 stsp return err;
3263 818c7501 2019-07-11 stsp }
3264 234035bc 2019-06-01 stsp
3265 818c7501 2019-07-11 stsp const struct got_error *
3266 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3267 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3268 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3269 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3270 818c7501 2019-07-11 stsp {
3271 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3272 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3273 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3274 818c7501 2019-07-11 stsp
3275 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3276 818c7501 2019-07-11 stsp if (err)
3277 818c7501 2019-07-11 stsp return err;
3278 818c7501 2019-07-11 stsp
3279 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3280 818c7501 2019-07-11 stsp if (err)
3281 818c7501 2019-07-11 stsp goto done;
3282 818c7501 2019-07-11 stsp
3283 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3284 69de9dd4 2021-09-03 stsp worktree);
3285 818c7501 2019-07-11 stsp if (err)
3286 818c7501 2019-07-11 stsp goto done;
3287 818c7501 2019-07-11 stsp
3288 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3289 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3290 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3291 818c7501 2019-07-11 stsp done:
3292 818c7501 2019-07-11 stsp if (fileindex)
3293 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3294 818c7501 2019-07-11 stsp free(fileindex_path);
3295 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3296 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3297 234035bc 2019-06-01 stsp err = unlockerr;
3298 234035bc 2019-06-01 stsp return err;
3299 234035bc 2019-06-01 stsp }
3300 234035bc 2019-06-01 stsp
3301 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3302 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3303 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3304 927df6b7 2019-02-10 stsp const char *status_path;
3305 927df6b7 2019-02-10 stsp size_t status_path_len;
3306 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3307 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3308 f8d1f275 2019-02-04 stsp void *status_arg;
3309 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3310 f8d1f275 2019-02-04 stsp void *cancel_arg;
3311 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3312 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3313 f2a9dc41 2019-12-13 tracey int report_unchanged;
3314 3143d852 2020-06-25 stsp int no_ignores;
3315 f8d1f275 2019-02-04 stsp };
3316 88d0e355 2019-08-03 stsp
3317 f8d1f275 2019-02-04 stsp static const struct got_error *
3318 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3319 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3320 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3321 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3322 927df6b7 2019-02-10 stsp {
3323 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3324 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3325 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3326 927df6b7 2019-02-10 stsp struct stat sb;
3327 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3328 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3329 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3330 927df6b7 2019-02-10 stsp
3331 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3332 98eaaa12 2019-08-03 stsp if (err)
3333 98eaaa12 2019-08-03 stsp return err;
3334 98eaaa12 2019-08-03 stsp
3335 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3336 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3337 98eaaa12 2019-08-03 stsp return NULL;
3338 98eaaa12 2019-08-03 stsp
3339 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3340 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3341 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3342 98eaaa12 2019-08-03 stsp }
3343 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3344 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3345 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3346 927df6b7 2019-02-10 stsp }
3347 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3348 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3349 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3350 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3351 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3352 98eaaa12 2019-08-03 stsp }
3353 98eaaa12 2019-08-03 stsp
3354 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3355 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3356 927df6b7 2019-02-10 stsp }
3357 927df6b7 2019-02-10 stsp
3358 927df6b7 2019-02-10 stsp static const struct got_error *
3359 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3360 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3361 f8d1f275 2019-02-04 stsp {
3362 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3363 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3364 f8d1f275 2019-02-04 stsp char *abspath;
3365 f8d1f275 2019-02-04 stsp
3366 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3367 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3368 0584f854 2019-04-06 stsp
3369 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3370 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3371 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3372 927df6b7 2019-02-10 stsp return NULL;
3373 927df6b7 2019-02-10 stsp
3374 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3375 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3376 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3377 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3378 f8d1f275 2019-02-04 stsp } else {
3379 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3380 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3381 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3382 f8d1f275 2019-02-04 stsp }
3383 f8d1f275 2019-02-04 stsp
3384 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3385 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3386 f8d1f275 2019-02-04 stsp free(abspath);
3387 9d31a1d8 2018-03-11 stsp return err;
3388 9d31a1d8 2018-03-11 stsp }
3389 f8d1f275 2019-02-04 stsp
3390 f8d1f275 2019-02-04 stsp static const struct got_error *
3391 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3392 f8d1f275 2019-02-04 stsp {
3393 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3394 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3395 2ec1f75b 2019-03-26 stsp unsigned char status;
3396 927df6b7 2019-02-10 stsp
3397 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3398 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3399 0584f854 2019-04-06 stsp
3400 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3401 927df6b7 2019-02-10 stsp return NULL;
3402 927df6b7 2019-02-10 stsp
3403 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3404 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3405 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3406 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3407 2ec1f75b 2019-03-26 stsp else
3408 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3409 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3410 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3411 6841da00 2019-08-08 stsp }
3412 6841da00 2019-08-08 stsp
3413 336075a4 2022-06-25 op static void
3414 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3415 6841da00 2019-08-08 stsp {
3416 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3417 6841da00 2019-08-08 stsp
3418 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3419 6841da00 2019-08-08 stsp free((char *)pe->path);
3420 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3421 6841da00 2019-08-08 stsp }
3422 6841da00 2019-08-08 stsp
3423 336075a4 2022-06-25 op static void
3424 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3425 6841da00 2019-08-08 stsp {
3426 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3427 6841da00 2019-08-08 stsp
3428 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3429 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3430 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3431 6841da00 2019-08-08 stsp free((char *)pe->path);
3432 6841da00 2019-08-08 stsp }
3433 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3434 6841da00 2019-08-08 stsp }
3435 6841da00 2019-08-08 stsp
3436 6841da00 2019-08-08 stsp static const struct got_error *
3437 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 6841da00 2019-08-08 stsp {
3439 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3440 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3441 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3442 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3443 6841da00 2019-08-08 stsp size_t linesize = 0;
3444 6841da00 2019-08-08 stsp ssize_t linelen;
3445 6841da00 2019-08-08 stsp
3446 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3447 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3448 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3449 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3450 6841da00 2019-08-08 stsp
3451 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3452 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3453 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3454 bd8de430 2019-10-04 stsp
3455 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3456 bd8de430 2019-10-04 stsp if (line[0] == '#')
3457 bd8de430 2019-10-04 stsp continue;
3458 bd8de430 2019-10-04 stsp
3459 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3460 bd8de430 2019-10-04 stsp if (line[0] == '!')
3461 bd8de430 2019-10-04 stsp continue;
3462 bd8de430 2019-10-04 stsp
3463 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3464 6841da00 2019-08-08 stsp line) == -1) {
3465 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3466 6841da00 2019-08-08 stsp goto done;
3467 6841da00 2019-08-08 stsp }
3468 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3469 6841da00 2019-08-08 stsp if (err)
3470 6841da00 2019-08-08 stsp goto done;
3471 6841da00 2019-08-08 stsp }
3472 6841da00 2019-08-08 stsp if (ferror(f)) {
3473 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3474 6841da00 2019-08-08 stsp goto done;
3475 6841da00 2019-08-08 stsp }
3476 6841da00 2019-08-08 stsp
3477 6841da00 2019-08-08 stsp dirpath = strdup(path);
3478 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3479 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3480 6841da00 2019-08-08 stsp goto done;
3481 6841da00 2019-08-08 stsp }
3482 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3483 6841da00 2019-08-08 stsp done:
3484 6841da00 2019-08-08 stsp free(line);
3485 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3486 6841da00 2019-08-08 stsp free(dirpath);
3487 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3488 6841da00 2019-08-08 stsp }
3489 6841da00 2019-08-08 stsp return err;
3490 6841da00 2019-08-08 stsp }
3491 6841da00 2019-08-08 stsp
3492 336075a4 2022-06-25 op static int
3493 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 6841da00 2019-08-08 stsp {
3495 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3496 bd8de430 2019-10-04 stsp
3497 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3498 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3499 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3500 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3501 bd8de430 2019-10-04 stsp
3502 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3503 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3504 6841da00 2019-08-08 stsp
3505 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3506 bd8de430 2019-10-04 stsp continue;
3507 bd8de430 2019-10-04 stsp pattern += 3;
3508 bd8de430 2019-10-04 stsp p = path;
3509 bd8de430 2019-10-04 stsp while (*p) {
3510 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3511 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3512 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3513 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3514 bd8de430 2019-10-04 stsp p++;
3515 bd8de430 2019-10-04 stsp while (*p == '/')
3516 bd8de430 2019-10-04 stsp p++;
3517 bd8de430 2019-10-04 stsp continue;
3518 bd8de430 2019-10-04 stsp }
3519 bd8de430 2019-10-04 stsp return 1;
3520 bd8de430 2019-10-04 stsp }
3521 bd8de430 2019-10-04 stsp }
3522 bd8de430 2019-10-04 stsp }
3523 bd8de430 2019-10-04 stsp
3524 6841da00 2019-08-08 stsp /*
3525 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3526 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3527 6841da00 2019-08-08 stsp * ignores backwards.
3528 6841da00 2019-08-08 stsp */
3529 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3530 6841da00 2019-08-08 stsp while (pe) {
3531 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3532 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3533 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3534 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3535 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3536 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3537 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3538 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3539 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3540 6841da00 2019-08-08 stsp continue;
3541 6841da00 2019-08-08 stsp return 1;
3542 6841da00 2019-08-08 stsp }
3543 6841da00 2019-08-08 stsp }
3544 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3545 6841da00 2019-08-08 stsp }
3546 6841da00 2019-08-08 stsp
3547 6841da00 2019-08-08 stsp return 0;
3548 6841da00 2019-08-08 stsp }
3549 6841da00 2019-08-08 stsp
3550 6841da00 2019-08-08 stsp static const struct got_error *
3551 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3552 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3553 6841da00 2019-08-08 stsp {
3554 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3555 6841da00 2019-08-08 stsp char *ignorespath;
3556 886cec17 2019-12-15 stsp int fd = -1;
3557 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3558 6841da00 2019-08-08 stsp
3559 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3560 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3561 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3562 6841da00 2019-08-08 stsp
3563 886cec17 2019-12-15 stsp if (dirfd != -1) {
3564 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3565 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3566 886cec17 2019-12-15 stsp if (fd == -1) {
3567 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3568 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3569 886cec17 2019-12-15 stsp ignorespath);
3570 886cec17 2019-12-15 stsp } else {
3571 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3572 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3573 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3574 886cec17 2019-12-15 stsp ignorespath);
3575 886cec17 2019-12-15 stsp else {
3576 886cec17 2019-12-15 stsp fd = -1;
3577 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3578 886cec17 2019-12-15 stsp }
3579 886cec17 2019-12-15 stsp }
3580 886cec17 2019-12-15 stsp } else {
3581 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3582 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3583 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3584 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3585 886cec17 2019-12-15 stsp ignorespath);
3586 886cec17 2019-12-15 stsp } else
3587 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3588 886cec17 2019-12-15 stsp }
3589 6841da00 2019-08-08 stsp
3590 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3591 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3592 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3593 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3594 6841da00 2019-08-08 stsp free(ignorespath);
3595 6841da00 2019-08-08 stsp return err;
3596 f8d1f275 2019-02-04 stsp }
3597 f8d1f275 2019-02-04 stsp
3598 f8d1f275 2019-02-04 stsp static const struct got_error *
3599 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3600 62da3196 2021-10-01 stsp int dirfd)
3601 f8d1f275 2019-02-04 stsp {
3602 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3603 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3604 f8d1f275 2019-02-04 stsp char *path = NULL;
3605 62da3196 2021-10-01 stsp
3606 62da3196 2021-10-01 stsp if (ignore != NULL)
3607 62da3196 2021-10-01 stsp *ignore = 0;
3608 f8d1f275 2019-02-04 stsp
3609 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3610 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3611 0584f854 2019-04-06 stsp
3612 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3613 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3614 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3615 f8d1f275 2019-02-04 stsp } else {
3616 f8d1f275 2019-02-04 stsp path = de->d_name;
3617 f8d1f275 2019-02-04 stsp }
3618 f8d1f275 2019-02-04 stsp
3619 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3620 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3621 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3622 62da3196 2021-10-01 stsp *ignore = 1;
3623 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3624 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3625 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3626 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3627 f8d1f275 2019-02-04 stsp if (parent_path[0])
3628 f8d1f275 2019-02-04 stsp free(path);
3629 b72f483a 2019-02-05 stsp return err;
3630 f8d1f275 2019-02-04 stsp }
3631 f8d1f275 2019-02-04 stsp
3632 347d1d3e 2019-07-12 stsp static const struct got_error *
3633 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3634 3143d852 2020-06-25 stsp {
3635 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3636 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3637 3143d852 2020-06-25 stsp
3638 3143d852 2020-06-25 stsp if (a->no_ignores)
3639 3143d852 2020-06-25 stsp return NULL;
3640 3143d852 2020-06-25 stsp
3641 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3642 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3643 3143d852 2020-06-25 stsp if (err)
3644 3143d852 2020-06-25 stsp return err;
3645 3143d852 2020-06-25 stsp
3646 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3647 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3648 3143d852 2020-06-25 stsp
3649 3143d852 2020-06-25 stsp return err;
3650 3143d852 2020-06-25 stsp }
3651 3143d852 2020-06-25 stsp
3652 3143d852 2020-06-25 stsp static const struct got_error *
3653 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3654 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3655 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3656 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3657 abb4604f 2019-07-27 stsp {
3658 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3659 abb4604f 2019-07-27 stsp struct stat sb;
3660 ff56836b 2021-07-08 stsp
3661 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 abb4604f 2019-07-27 stsp if (ie)
3663 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3664 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3665 abb4604f 2019-07-27 stsp
3666 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3667 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3668 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3669 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3670 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3671 abb4604f 2019-07-27 stsp }
3672 abb4604f 2019-07-27 stsp
3673 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3674 916237f3 2022-02-11 stsp return NULL;
3675 916237f3 2022-02-11 stsp
3676 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3677 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3678 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3679 abb4604f 2019-07-27 stsp
3680 abb4604f 2019-07-27 stsp return NULL;
3681 3143d852 2020-06-25 stsp }
3682 3143d852 2020-06-25 stsp
3683 3143d852 2020-06-25 stsp static const struct got_error *
3684 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3685 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3686 3143d852 2020-06-25 stsp {
3687 3143d852 2020-06-25 stsp const struct got_error *err;
3688 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3689 3143d852 2020-06-25 stsp
3690 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3691 3143d852 2020-06-25 stsp ".cvsignore");
3692 3143d852 2020-06-25 stsp if (err)
3693 3143d852 2020-06-25 stsp return err;
3694 3143d852 2020-06-25 stsp
3695 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3696 3143d852 2020-06-25 stsp ".gitignore");
3697 3143d852 2020-06-25 stsp if (err)
3698 3143d852 2020-06-25 stsp return err;
3699 3143d852 2020-06-25 stsp
3700 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3701 3143d852 2020-06-25 stsp if (err) {
3702 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3703 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3704 3143d852 2020-06-25 stsp return err;
3705 3143d852 2020-06-25 stsp }
3706 3143d852 2020-06-25 stsp for (;;) {
3707 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3708 3143d852 2020-06-25 stsp ".cvsignore");
3709 3143d852 2020-06-25 stsp if (err)
3710 3143d852 2020-06-25 stsp break;
3711 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3712 3143d852 2020-06-25 stsp ".gitignore");
3713 3143d852 2020-06-25 stsp if (err)
3714 3143d852 2020-06-25 stsp break;
3715 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3716 3143d852 2020-06-25 stsp if (err) {
3717 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3718 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3719 3143d852 2020-06-25 stsp break;
3720 3143d852 2020-06-25 stsp }
3721 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3722 ff56836b 2021-07-08 stsp break;
3723 b737c85a 2020-06-26 stsp free(parent_path);
3724 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3725 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3726 3143d852 2020-06-25 stsp }
3727 3143d852 2020-06-25 stsp
3728 b737c85a 2020-06-26 stsp free(parent_path);
3729 b737c85a 2020-06-26 stsp free(next_parent_path);
3730 3143d852 2020-06-25 stsp return err;
3731 abb4604f 2019-07-27 stsp }
3732 abb4604f 2019-07-27 stsp
3733 abb4604f 2019-07-27 stsp static const struct got_error *
3734 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3735 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3736 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3737 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3738 f2a9dc41 2019-12-13 tracey int report_unchanged)
3739 f8d1f275 2019-02-04 stsp {
3740 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3741 6fc93f37 2019-12-13 stsp int fd = -1;
3742 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3743 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3744 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3745 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3746 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3747 3143d852 2020-06-25 stsp
3748 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3749 f8d1f275 2019-02-04 stsp
3750 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3751 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3752 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3753 8dc303cc 2019-07-27 stsp
3754 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3755 a84c0d30 2022-03-12 stsp if (ie) {
3756 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3757 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3758 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3759 a84c0d30 2022-03-12 stsp goto done;
3760 a84c0d30 2022-03-12 stsp }
3761 a84c0d30 2022-03-12 stsp
3762 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3763 6fc93f37 2019-12-13 stsp if (fd == -1) {
3764 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3765 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3766 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3767 ff56836b 2021-07-08 stsp else {
3768 ff56836b 2021-07-08 stsp if (!no_ignores) {
3769 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3770 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3771 ff56836b 2021-07-08 stsp if (err)
3772 ff56836b 2021-07-08 stsp goto done;
3773 ff56836b 2021-07-08 stsp }
3774 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3775 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3776 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3777 ff56836b 2021-07-08 stsp }
3778 abb4604f 2019-07-27 stsp } else {
3779 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3780 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3781 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3782 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3783 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3784 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3785 abb4604f 2019-07-27 stsp arg.status_path = path;
3786 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3787 abb4604f 2019-07-27 stsp arg.repo = repo;
3788 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3789 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3790 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3791 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3792 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3793 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3794 022fae89 2019-12-06 tracey if (!no_ignores) {
3795 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3796 3143d852 2020-06-25 stsp worktree->root_path, path);
3797 3143d852 2020-06-25 stsp if (err)
3798 3143d852 2020-06-25 stsp goto done;
3799 022fae89 2019-12-06 tracey }
3800 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3801 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3802 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3803 927df6b7 2019-02-10 stsp }
3804 3143d852 2020-06-25 stsp done:
3805 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3806 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3807 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3808 927df6b7 2019-02-10 stsp free(ondisk_path);
3809 347d1d3e 2019-07-12 stsp return err;
3810 347d1d3e 2019-07-12 stsp }
3811 347d1d3e 2019-07-12 stsp
3812 347d1d3e 2019-07-12 stsp const struct got_error *
3813 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3814 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3815 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3816 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3817 347d1d3e 2019-07-12 stsp {
3818 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3819 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3820 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3821 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3822 347d1d3e 2019-07-12 stsp
3823 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3824 347d1d3e 2019-07-12 stsp if (err)
3825 347d1d3e 2019-07-12 stsp return err;
3826 347d1d3e 2019-07-12 stsp
3827 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3828 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3829 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3830 f6343036 2021-06-22 stsp no_ignores, 0);
3831 72ea6654 2019-07-27 stsp if (err)
3832 72ea6654 2019-07-27 stsp break;
3833 72ea6654 2019-07-27 stsp }
3834 f8d1f275 2019-02-04 stsp free(fileindex_path);
3835 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3836 f8d1f275 2019-02-04 stsp return err;
3837 f8d1f275 2019-02-04 stsp }
3838 6c7ab921 2019-03-18 stsp
3839 6c7ab921 2019-03-18 stsp const struct got_error *
3840 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3841 6c7ab921 2019-03-18 stsp const char *arg)
3842 6c7ab921 2019-03-18 stsp {
3843 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3844 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3845 6c7ab921 2019-03-18 stsp size_t len;
3846 00bb5ea0 2020-07-23 stsp struct stat sb;
3847 01740607 2020-11-04 stsp char *abspath = NULL;
3848 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3849 6c7ab921 2019-03-18 stsp
3850 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3851 6c7ab921 2019-03-18 stsp
3852 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3853 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3854 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3855 00bb5ea0 2020-07-23 stsp
3856 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3857 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3858 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3859 00bb5ea0 2020-07-23 stsp goto done;
3860 00bb5ea0 2020-07-23 stsp }
3861 727173c3 2020-11-06 stsp sb.st_mode = 0;
3862 00bb5ea0 2020-07-23 stsp }
3863 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3864 00bb5ea0 2020-07-23 stsp /*
3865 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3866 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3867 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3868 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3869 00bb5ea0 2020-07-23 stsp */
3870 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3871 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3872 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3873 00bb5ea0 2020-07-23 stsp goto done;
3874 00bb5ea0 2020-07-23 stsp }
3875 00bb5ea0 2020-07-23 stsp
3876 00bb5ea0 2020-07-23 stsp }
3877 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3878 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3879 00bb5ea0 2020-07-23 stsp if (err)
3880 d0710d08 2019-07-22 stsp goto done;
3881 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3882 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3883 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3884 00bb5ea0 2020-07-23 stsp goto done;
3885 00bb5ea0 2020-07-23 stsp }
3886 00bb5ea0 2020-07-23 stsp } else {
3887 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3888 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3889 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3890 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3891 00bb5ea0 2020-07-23 stsp goto done;
3892 00bb5ea0 2020-07-23 stsp }
3893 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3894 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3895 01740607 2020-11-04 stsp goto done;
3896 01740607 2020-11-04 stsp }
3897 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3898 01740607 2020-11-04 stsp sizeof(canonpath));
3899 01740607 2020-11-04 stsp if (err)
3900 00bb5ea0 2020-07-23 stsp goto done;
3901 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3902 01740607 2020-11-04 stsp if (resolved == NULL) {
3903 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3904 01740607 2020-11-04 stsp goto done;
3905 00bb5ea0 2020-07-23 stsp }
3906 d0710d08 2019-07-22 stsp }
3907 d0710d08 2019-07-22 stsp }
3908 6c7ab921 2019-03-18 stsp
3909 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3910 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3911 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3912 6c7ab921 2019-03-18 stsp goto done;
3913 6c7ab921 2019-03-18 stsp }
3914 6c7ab921 2019-03-18 stsp
3915 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3916 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3917 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3918 f6d88e1a 2019-05-29 stsp if (err)
3919 f6d88e1a 2019-05-29 stsp goto done;
3920 f6d88e1a 2019-05-29 stsp } else {
3921 f6d88e1a 2019-05-29 stsp path = strdup("");
3922 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3923 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3924 f6d88e1a 2019-05-29 stsp goto done;
3925 f6d88e1a 2019-05-29 stsp }
3926 6c7ab921 2019-03-18 stsp }
3927 6c7ab921 2019-03-18 stsp
3928 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3929 6c7ab921 2019-03-18 stsp len = strlen(path);
3930 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3931 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3932 6c7ab921 2019-03-18 stsp len--;
3933 6c7ab921 2019-03-18 stsp }
3934 6c7ab921 2019-03-18 stsp done:
3935 01740607 2020-11-04 stsp free(abspath);
3936 6c7ab921 2019-03-18 stsp free(resolved);
3937 d0710d08 2019-07-22 stsp free(cwd);
3938 6c7ab921 2019-03-18 stsp if (err == NULL)
3939 6c7ab921 2019-03-18 stsp *wt_path = path;
3940 6c7ab921 2019-03-18 stsp else
3941 6c7ab921 2019-03-18 stsp free(path);
3942 d00136be 2019-03-26 stsp return err;
3943 d00136be 2019-03-26 stsp }
3944 d00136be 2019-03-26 stsp
3945 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3946 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3947 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3948 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3949 4e68cba3 2019-11-23 stsp void *progress_arg;
3950 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3951 4e68cba3 2019-11-23 stsp };
3952 4e68cba3 2019-11-23 stsp
3953 4e68cba3 2019-11-23 stsp static const struct got_error *
3954 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3955 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3956 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3957 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3958 07f5b47a 2019-06-02 stsp {
3959 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3960 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3961 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3962 6d022e97 2019-08-04 stsp struct stat sb;
3963 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3964 07f5b47a 2019-06-02 stsp
3965 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3966 dbb83fbd 2019-12-12 stsp relpath) == -1)
3967 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3968 dbb83fbd 2019-12-12 stsp
3969 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 6d022e97 2019-08-04 stsp if (ie) {
3971 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3972 12463d8b 2019-12-13 stsp de_name, a->repo);
3973 6d022e97 2019-08-04 stsp if (err)
3974 dbb83fbd 2019-12-12 stsp goto done;
3975 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3976 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3977 dbb83fbd 2019-12-12 stsp goto done;
3978 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3979 dbb83fbd 2019-12-12 stsp if (err)
3980 dbb83fbd 2019-12-12 stsp goto done;
3981 6d022e97 2019-08-04 stsp }
3982 07f5b47a 2019-06-02 stsp
3983 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3984 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3985 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3986 9b4603c0 2022-01-31 stsp else
3987 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3988 dbb83fbd 2019-12-12 stsp goto done;
3989 4e68cba3 2019-11-23 stsp }
3990 07f5b47a 2019-06-02 stsp
3991 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3992 4e68cba3 2019-11-23 stsp if (err)
3993 4e68cba3 2019-11-23 stsp goto done;
3994 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3995 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3996 3969253a 2020-03-07 stsp if (err) {
3997 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3998 3969253a 2020-03-07 stsp goto done;
3999 3969253a 2020-03-07 stsp }
4000 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4001 07f5b47a 2019-06-02 stsp if (err) {
4002 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4003 4e68cba3 2019-11-23 stsp goto done;
4004 07f5b47a 2019-06-02 stsp }
4005 4e68cba3 2019-11-23 stsp done:
4006 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4007 dbb83fbd 2019-12-12 stsp if (err)
4008 dbb83fbd 2019-12-12 stsp return err;
4009 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4010 dbb83fbd 2019-12-12 stsp return NULL;
4011 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4012 07f5b47a 2019-06-02 stsp }
4013 07f5b47a 2019-06-02 stsp
4014 d00136be 2019-03-26 stsp const struct got_error *
4015 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4016 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4017 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4018 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4019 d00136be 2019-03-26 stsp {
4020 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4021 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4022 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4023 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4024 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4025 d00136be 2019-03-26 stsp
4026 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4027 d00136be 2019-03-26 stsp if (err)
4028 d00136be 2019-03-26 stsp return err;
4029 d00136be 2019-03-26 stsp
4030 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4031 d00136be 2019-03-26 stsp if (err)
4032 d00136be 2019-03-26 stsp goto done;
4033 d00136be 2019-03-26 stsp
4034 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4035 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4036 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4037 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4038 4e68cba3 2019-11-23 stsp saa.repo = repo;
4039 4e68cba3 2019-11-23 stsp
4040 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4041 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4042 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4043 1dd54920 2019-05-11 stsp if (err)
4044 af12c6b9 2019-06-04 stsp break;
4045 1dd54920 2019-05-11 stsp }
4046 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4047 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4048 af12c6b9 2019-06-04 stsp err = sync_err;
4049 d00136be 2019-03-26 stsp done:
4050 fb399478 2019-07-12 stsp free(fileindex_path);
4051 d00136be 2019-03-26 stsp if (fileindex)
4052 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4053 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4054 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4055 d00136be 2019-03-26 stsp err = unlockerr;
4056 6c7ab921 2019-03-18 stsp return err;
4057 6c7ab921 2019-03-18 stsp }
4058 17ed4618 2019-06-02 stsp
4059 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4060 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4061 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4062 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4063 f2a9dc41 2019-12-13 tracey void *progress_arg;
4064 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4065 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4066 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4067 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4068 766841c2 2020-08-13 stsp const char *status_codes;
4069 f2a9dc41 2019-12-13 tracey };
4070 f2a9dc41 2019-12-13 tracey
4071 f2a9dc41 2019-12-13 tracey static const struct got_error *
4072 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4073 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4074 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4075 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 17ed4618 2019-06-02 stsp {
4077 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4078 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4079 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4080 17ed4618 2019-06-02 stsp struct stat sb;
4081 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4082 4e12cd97 2022-01-25 stsp
4083 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4084 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4085 4e12cd97 2022-01-25 stsp return NULL;
4086 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4087 4e12cd97 2022-01-25 stsp }
4088 17ed4618 2019-06-02 stsp
4089 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4090 17ed4618 2019-06-02 stsp if (ie == NULL)
4091 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 2ec1f75b 2019-03-26 stsp
4093 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4094 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4095 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4096 9acbc4fa 2019-08-03 stsp return NULL;
4097 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4098 9acbc4fa 2019-08-03 stsp }
4099 9acbc4fa 2019-08-03 stsp
4100 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4101 f2a9dc41 2019-12-13 tracey relpath) == -1)
4102 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4103 f2a9dc41 2019-12-13 tracey
4104 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4105 12463d8b 2019-12-13 stsp a->repo);
4106 17ed4618 2019-06-02 stsp if (err)
4107 f2a9dc41 2019-12-13 tracey goto done;
4108 17ed4618 2019-06-02 stsp
4109 766841c2 2020-08-13 stsp if (a->status_codes) {
4110 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4111 766841c2 2020-08-13 stsp int i;
4112 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4113 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4114 766841c2 2020-08-13 stsp break;
4115 766841c2 2020-08-13 stsp }
4116 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4117 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4118 766841c2 2020-08-13 stsp free(ondisk_path);
4119 766841c2 2020-08-13 stsp return NULL;
4120 766841c2 2020-08-13 stsp }
4121 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4122 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4123 766841c2 2020-08-13 stsp static char msg[64];
4124 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4125 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4126 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4127 766841c2 2020-08-13 stsp goto done;
4128 766841c2 2020-08-13 stsp }
4129 766841c2 2020-08-13 stsp }
4130 766841c2 2020-08-13 stsp
4131 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4132 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4133 f2a9dc41 2019-12-13 tracey goto done;
4134 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4135 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4136 f2a9dc41 2019-12-13 tracey goto done;
4137 f2a9dc41 2019-12-13 tracey }
4138 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4139 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4140 4e12cd97 2022-01-25 stsp goto done;
4141 4e12cd97 2022-01-25 stsp }
4142 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4143 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4144 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4145 f2a9dc41 2019-12-13 tracey goto done;
4146 f2a9dc41 2019-12-13 tracey }
4147 17ed4618 2019-06-02 stsp }
4148 17ed4618 2019-06-02 stsp
4149 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4150 20a2ad1c 2020-10-20 stsp size_t root_len;
4151 20a2ad1c 2020-10-20 stsp
4152 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4153 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4154 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4155 12463d8b 2019-12-13 stsp ondisk_path);
4156 12463d8b 2019-12-13 stsp goto done;
4157 12463d8b 2019-12-13 stsp }
4158 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4159 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4160 12463d8b 2019-12-13 stsp goto done;
4161 15341bfd 2020-03-05 tracey }
4162 15341bfd 2020-03-05 tracey
4163 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4164 20a2ad1c 2020-10-20 stsp do {
4165 20a2ad1c 2020-10-20 stsp char *parent;
4166 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4167 20a2ad1c 2020-10-20 stsp if (err)
4168 2513f20a 2020-10-20 stsp goto done;
4169 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4170 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4171 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4172 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4173 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4174 20a2ad1c 2020-10-20 stsp ondisk_path);
4175 15341bfd 2020-03-05 tracey break;
4176 15341bfd 2020-03-05 tracey }
4177 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4178 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4179 f2a9dc41 2019-12-13 tracey }
4180 17ed4618 2019-06-02 stsp
4181 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4182 f2a9dc41 2019-12-13 tracey done:
4183 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4184 f2a9dc41 2019-12-13 tracey if (err)
4185 f2a9dc41 2019-12-13 tracey return err;
4186 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4187 f2a9dc41 2019-12-13 tracey return NULL;
4188 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4189 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4190 17ed4618 2019-06-02 stsp }
4191 17ed4618 2019-06-02 stsp
4192 2ec1f75b 2019-03-26 stsp const struct got_error *
4193 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4194 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4195 766841c2 2020-08-13 stsp const char *status_codes,
4196 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4197 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 2ec1f75b 2019-03-26 stsp {
4199 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4200 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4201 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4202 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4203 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4204 2ec1f75b 2019-03-26 stsp
4205 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4206 2ec1f75b 2019-03-26 stsp if (err)
4207 2ec1f75b 2019-03-26 stsp return err;
4208 2ec1f75b 2019-03-26 stsp
4209 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4210 2ec1f75b 2019-03-26 stsp if (err)
4211 2ec1f75b 2019-03-26 stsp goto done;
4212 f2a9dc41 2019-12-13 tracey
4213 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4214 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4215 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4216 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4217 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4218 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4219 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4220 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4221 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4222 2ec1f75b 2019-03-26 stsp
4223 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4224 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4225 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4226 17ed4618 2019-06-02 stsp if (err)
4227 af12c6b9 2019-06-04 stsp break;
4228 2ec1f75b 2019-03-26 stsp }
4229 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4230 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4231 af12c6b9 2019-06-04 stsp err = sync_err;
4232 2ec1f75b 2019-03-26 stsp done:
4233 fb399478 2019-07-12 stsp free(fileindex_path);
4234 2ec1f75b 2019-03-26 stsp if (fileindex)
4235 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4236 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4237 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4238 2ec1f75b 2019-03-26 stsp err = unlockerr;
4239 33aa809d 2019-08-08 stsp return err;
4240 33aa809d 2019-08-08 stsp }
4241 33aa809d 2019-08-08 stsp
4242 33aa809d 2019-08-08 stsp static const struct got_error *
4243 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 33aa809d 2019-08-08 stsp {
4245 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4246 33aa809d 2019-08-08 stsp char *line = NULL;
4247 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4248 33aa809d 2019-08-08 stsp ssize_t linelen;
4249 33aa809d 2019-08-08 stsp
4250 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4251 33aa809d 2019-08-08 stsp if (linelen == -1) {
4252 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4253 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4254 33aa809d 2019-08-08 stsp goto done;
4255 33aa809d 2019-08-08 stsp }
4256 33aa809d 2019-08-08 stsp return NULL;
4257 33aa809d 2019-08-08 stsp }
4258 33aa809d 2019-08-08 stsp if (outfile) {
4259 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4260 33aa809d 2019-08-08 stsp if (n != linelen) {
4261 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4262 33aa809d 2019-08-08 stsp goto done;
4263 33aa809d 2019-08-08 stsp }
4264 33aa809d 2019-08-08 stsp }
4265 33aa809d 2019-08-08 stsp if (rejectfile) {
4266 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4267 33aa809d 2019-08-08 stsp if (n != linelen)
4268 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4269 33aa809d 2019-08-08 stsp }
4270 33aa809d 2019-08-08 stsp done:
4271 33aa809d 2019-08-08 stsp free(line);
4272 2ec1f75b 2019-03-26 stsp return err;
4273 2ec1f75b 2019-03-26 stsp }
4274 1f1abb7e 2019-08-08 stsp
4275 33aa809d 2019-08-08 stsp static const struct got_error *
4276 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4277 33aa809d 2019-08-08 stsp {
4278 33aa809d 2019-08-08 stsp char *line = NULL;
4279 33aa809d 2019-08-08 stsp size_t linesize = 0;
4280 33aa809d 2019-08-08 stsp ssize_t linelen;
4281 33aa809d 2019-08-08 stsp
4282 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4283 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4284 500467ff 2019-09-25 hiltjo if (ferror(f))
4285 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4286 500467ff 2019-09-25 hiltjo return NULL;
4287 500467ff 2019-09-25 hiltjo }
4288 33aa809d 2019-08-08 stsp free(line);
4289 33aa809d 2019-08-08 stsp return NULL;
4290 33aa809d 2019-08-08 stsp }
4291 33aa809d 2019-08-08 stsp
4292 33aa809d 2019-08-08 stsp static const struct got_error *
4293 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4294 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4295 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4296 abc59930 2021-09-05 naddy {
4297 33aa809d 2019-08-08 stsp const struct got_error *err;
4298 33aa809d 2019-08-08 stsp
4299 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4300 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4301 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4302 33aa809d 2019-08-08 stsp if (err)
4303 33aa809d 2019-08-08 stsp return err;
4304 33aa809d 2019-08-08 stsp (*line_cur1)++;
4305 33aa809d 2019-08-08 stsp }
4306 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4307 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4308 33aa809d 2019-08-08 stsp if (rejectfile)
4309 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4310 33aa809d 2019-08-08 stsp else
4311 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4312 33aa809d 2019-08-08 stsp if (err)
4313 33aa809d 2019-08-08 stsp return err;
4314 33aa809d 2019-08-08 stsp (*line_cur2)++;
4315 33aa809d 2019-08-08 stsp }
4316 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4317 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4318 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4319 33aa809d 2019-08-08 stsp if (err)
4320 33aa809d 2019-08-08 stsp return err;
4321 33aa809d 2019-08-08 stsp (*line_cur2)++;
4322 33aa809d 2019-08-08 stsp }
4323 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4324 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4325 33aa809d 2019-08-08 stsp if (rejectfile)
4326 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4327 33aa809d 2019-08-08 stsp else
4328 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4329 33aa809d 2019-08-08 stsp if (err)
4330 33aa809d 2019-08-08 stsp return err;
4331 33aa809d 2019-08-08 stsp (*line_cur1)++;
4332 33aa809d 2019-08-08 stsp }
4333 f1e81a05 2019-08-10 stsp
4334 f1e81a05 2019-08-10 stsp return NULL;
4335 f1e81a05 2019-08-10 stsp }
4336 f1e81a05 2019-08-10 stsp
4337 f1e81a05 2019-08-10 stsp static const struct got_error *
4338 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4339 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4340 f1e81a05 2019-08-10 stsp {
4341 f1e81a05 2019-08-10 stsp const struct got_error *err;
4342 f1e81a05 2019-08-10 stsp
4343 f1e81a05 2019-08-10 stsp if (outfile) {
4344 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4345 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4346 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4347 f1e81a05 2019-08-10 stsp if (err)
4348 f1e81a05 2019-08-10 stsp return err;
4349 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4350 f1e81a05 2019-08-10 stsp }
4351 f1e81a05 2019-08-10 stsp }
4352 f1e81a05 2019-08-10 stsp if (rejectfile) {
4353 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4354 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4355 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4356 f1e81a05 2019-08-10 stsp if (err)
4357 f1e81a05 2019-08-10 stsp return err;
4358 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4359 f1e81a05 2019-08-10 stsp }
4360 33aa809d 2019-08-08 stsp }
4361 33aa809d 2019-08-08 stsp
4362 33aa809d 2019-08-08 stsp return NULL;
4363 33aa809d 2019-08-08 stsp }
4364 33aa809d 2019-08-08 stsp
4365 33aa809d 2019-08-08 stsp static const struct got_error *
4366 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4367 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4368 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4369 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4370 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4371 33aa809d 2019-08-08 stsp {
4372 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4373 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4374 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4375 33aa809d 2019-08-08 stsp FILE *hunkfile;
4376 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4377 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4378 fe621944 2020-11-10 stsp int rc;
4379 33aa809d 2019-08-08 stsp
4380 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4381 33aa809d 2019-08-08 stsp
4382 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4383 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4384 fe621944 2020-11-10 stsp start_old = cc.left.start;
4385 fe621944 2020-11-10 stsp end_old = cc.left.end;
4386 fe621944 2020-11-10 stsp start_new = cc.right.start;
4387 fe621944 2020-11-10 stsp end_new = cc.right.end;
4388 33aa809d 2019-08-08 stsp
4389 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4390 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4391 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 33aa809d 2019-08-08 stsp
4393 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4394 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4395 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4396 33aa809d 2019-08-08 stsp
4397 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4398 fe621944 2020-11-10 stsp if (diff_state == NULL)
4399 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4400 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4401 fe621944 2020-11-10 stsp
4402 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4403 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4404 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4405 33aa809d 2019-08-08 stsp goto done;
4406 33aa809d 2019-08-08 stsp }
4407 fe621944 2020-11-10 stsp
4408 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4409 fe621944 2020-11-10 stsp diff_result, &cc);
4410 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4411 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4412 33aa809d 2019-08-08 stsp goto done;
4413 33aa809d 2019-08-08 stsp }
4414 fe621944 2020-11-10 stsp
4415 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4416 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4417 33aa809d 2019-08-08 stsp goto done;
4418 33aa809d 2019-08-08 stsp }
4419 33aa809d 2019-08-08 stsp
4420 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4421 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4422 33aa809d 2019-08-08 stsp if (err)
4423 33aa809d 2019-08-08 stsp goto done;
4424 33aa809d 2019-08-08 stsp
4425 33aa809d 2019-08-08 stsp switch (*choice) {
4426 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4427 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4428 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4429 33aa809d 2019-08-08 stsp break;
4430 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4431 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4432 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4433 33aa809d 2019-08-08 stsp break;
4434 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4435 33aa809d 2019-08-08 stsp break;
4436 33aa809d 2019-08-08 stsp default:
4437 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4438 33aa809d 2019-08-08 stsp break;
4439 33aa809d 2019-08-08 stsp }
4440 33aa809d 2019-08-08 stsp done:
4441 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4442 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4443 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4444 33aa809d 2019-08-08 stsp return err;
4445 33aa809d 2019-08-08 stsp }
4446 33aa809d 2019-08-08 stsp
4447 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4448 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4449 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4450 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4451 1f1abb7e 2019-08-08 stsp void *progress_arg;
4452 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4453 33aa809d 2019-08-08 stsp void *patch_arg;
4454 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4455 f259c4c1 2021-09-24 stsp int unlink_added_files;
4456 1f1abb7e 2019-08-08 stsp };
4457 a129376b 2019-03-28 stsp
4458 e20a8b6f 2019-06-04 stsp static const struct got_error *
4459 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4460 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4461 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4462 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4463 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4464 33aa809d 2019-08-08 stsp {
4465 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4466 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4467 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4468 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4469 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4470 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4471 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4472 fe621944 2020-11-10 stsp struct stat sb2;
4473 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4474 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4475 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 33aa809d 2019-08-08 stsp
4477 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4478 33aa809d 2019-08-08 stsp
4479 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4480 33aa809d 2019-08-08 stsp if (err)
4481 33aa809d 2019-08-08 stsp return err;
4482 33aa809d 2019-08-08 stsp
4483 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4484 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4485 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4486 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4487 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4488 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4489 fa3cef63 2020-07-23 stsp goto done;
4490 fa3cef63 2020-07-23 stsp }
4491 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4492 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4493 c0df5966 2021-12-31 stsp if (link_len == -1) {
4494 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4495 c0df5966 2021-12-31 stsp path2);
4496 c0df5966 2021-12-31 stsp }
4497 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4498 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4499 12463d8b 2019-12-13 stsp }
4500 12463d8b 2019-12-13 stsp } else {
4501 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4502 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4503 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4504 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4505 fa3cef63 2020-07-23 stsp goto done;
4506 fa3cef63 2020-07-23 stsp }
4507 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4508 fa3cef63 2020-07-23 stsp sizeof(link_target));
4509 fa3cef63 2020-07-23 stsp if (link_len == -1)
4510 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4511 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4512 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4513 12463d8b 2019-12-13 stsp }
4514 1ebedb77 2019-10-19 stsp }
4515 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4516 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4517 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4518 fa3cef63 2020-07-23 stsp goto done;
4519 fa3cef63 2020-07-23 stsp }
4520 1ebedb77 2019-10-19 stsp
4521 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4522 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4523 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4524 fa3cef63 2020-07-23 stsp goto done;
4525 fa3cef63 2020-07-23 stsp }
4526 fa3cef63 2020-07-23 stsp fd2 = -1;
4527 fa3cef63 2020-07-23 stsp } else {
4528 fa3cef63 2020-07-23 stsp size_t n;
4529 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4530 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4531 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4532 fa3cef63 2020-07-23 stsp goto done;
4533 fa3cef63 2020-07-23 stsp }
4534 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4535 fa3cef63 2020-07-23 stsp if (n != link_len) {
4536 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4537 fa3cef63 2020-07-23 stsp goto done;
4538 fa3cef63 2020-07-23 stsp }
4539 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4540 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4541 fa3cef63 2020-07-23 stsp goto done;
4542 fa3cef63 2020-07-23 stsp }
4543 fa3cef63 2020-07-23 stsp rewind(f2);
4544 33aa809d 2019-08-08 stsp }
4545 33aa809d 2019-08-08 stsp
4546 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4547 eb81bc23 2022-06-28 tracey if (fd == -1) {
4548 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4549 eb81bc23 2022-06-28 tracey goto done;
4550 eb81bc23 2022-06-28 tracey }
4551 eb81bc23 2022-06-28 tracey
4552 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4553 33aa809d 2019-08-08 stsp if (err)
4554 33aa809d 2019-08-08 stsp goto done;
4555 33aa809d 2019-08-08 stsp
4556 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4557 33aa809d 2019-08-08 stsp if (err)
4558 33aa809d 2019-08-08 stsp goto done;
4559 33aa809d 2019-08-08 stsp
4560 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4561 33aa809d 2019-08-08 stsp if (err)
4562 33aa809d 2019-08-08 stsp goto done;
4563 33aa809d 2019-08-08 stsp
4564 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4565 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4566 33aa809d 2019-08-08 stsp if (err)
4567 33aa809d 2019-08-08 stsp goto done;
4568 33aa809d 2019-08-08 stsp
4569 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4570 b90054ed 2022-11-01 stsp "");
4571 33aa809d 2019-08-08 stsp if (err)
4572 33aa809d 2019-08-08 stsp goto done;
4573 33aa809d 2019-08-08 stsp
4574 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4575 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4576 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4577 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4578 fe621944 2020-11-10 stsp
4579 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4580 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4581 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4582 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4583 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4584 fe621944 2020-11-10 stsp nchanges++;
4585 fe621944 2020-11-10 stsp }
4586 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4587 33aa809d 2019-08-08 stsp int choice;
4588 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4589 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4590 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4591 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4592 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4593 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4594 33aa809d 2019-08-08 stsp if (err)
4595 33aa809d 2019-08-08 stsp goto done;
4596 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4597 33aa809d 2019-08-08 stsp have_content = 1;
4598 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4599 33aa809d 2019-08-08 stsp break;
4600 33aa809d 2019-08-08 stsp }
4601 1ebedb77 2019-10-19 stsp if (have_content) {
4602 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4603 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4604 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4605 1ebedb77 2019-10-19 stsp if (err)
4606 1ebedb77 2019-10-19 stsp goto done;
4607 1ebedb77 2019-10-19 stsp
4608 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4609 b2b3fce1 2022-10-29 op mode_t mode;
4610 b2b3fce1 2022-10-29 op
4611 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4612 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4613 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4614 fa3cef63 2020-07-23 stsp goto done;
4615 fa3cef63 2020-07-23 stsp }
4616 1ebedb77 2019-10-19 stsp }
4617 1ebedb77 2019-10-19 stsp }
4618 33aa809d 2019-08-08 stsp done:
4619 33aa809d 2019-08-08 stsp free(id_str);
4620 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4621 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4622 33aa809d 2019-08-08 stsp if (blob)
4623 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4624 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4625 fe621944 2020-11-10 stsp if (err == NULL)
4626 fe621944 2020-11-10 stsp err = free_err;
4627 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4628 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4629 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4630 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4631 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4632 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4633 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4634 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4635 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4636 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4637 33aa809d 2019-08-08 stsp if (err || !have_content) {
4638 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4639 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4640 33aa809d 2019-08-08 stsp free(*path_outfile);
4641 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4642 33aa809d 2019-08-08 stsp }
4643 33aa809d 2019-08-08 stsp free(path1);
4644 33aa809d 2019-08-08 stsp return err;
4645 33aa809d 2019-08-08 stsp }
4646 33aa809d 2019-08-08 stsp
4647 33aa809d 2019-08-08 stsp static const struct got_error *
4648 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4649 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4650 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4651 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4652 a129376b 2019-03-28 stsp {
4653 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4654 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4655 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4656 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4657 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4658 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4659 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4660 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4661 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4662 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4663 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4664 eb81bc23 2022-06-28 tracey int fd = -1;
4665 a129376b 2019-03-28 stsp
4666 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4667 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4668 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4669 d3bcc3d1 2019-08-08 stsp return NULL;
4670 3d69ad8d 2019-08-17 semarie
4671 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4672 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4673 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4674 d3bcc3d1 2019-08-08 stsp
4675 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4676 65084dad 2019-08-08 stsp if (ie == NULL)
4677 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4678 a129376b 2019-03-28 stsp
4679 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4680 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4681 a129376b 2019-03-28 stsp if (err) {
4682 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4683 a129376b 2019-03-28 stsp goto done;
4684 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4685 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4686 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4687 e20a8b6f 2019-06-04 stsp goto done;
4688 e20a8b6f 2019-06-04 stsp }
4689 a129376b 2019-03-28 stsp }
4690 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4691 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4692 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4693 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4694 a129376b 2019-03-28 stsp goto done;
4695 a129376b 2019-03-28 stsp }
4696 a129376b 2019-03-28 stsp } else {
4697 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4698 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4699 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4700 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4701 a129376b 2019-03-28 stsp goto done;
4702 a129376b 2019-03-28 stsp }
4703 a129376b 2019-03-28 stsp } else {
4704 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4705 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4706 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4707 a129376b 2019-03-28 stsp goto done;
4708 a129376b 2019-03-28 stsp }
4709 a129376b 2019-03-28 stsp }
4710 a129376b 2019-03-28 stsp }
4711 a129376b 2019-03-28 stsp
4712 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4713 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4714 a44927cc 2022-04-07 stsp if (err)
4715 a44927cc 2022-04-07 stsp goto done;
4716 a44927cc 2022-04-07 stsp
4717 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4718 a9fa2909 2019-07-27 stsp if (err) {
4719 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4720 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4721 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4722 a9fa2909 2019-07-27 stsp goto done;
4723 a9fa2909 2019-07-27 stsp } else {
4724 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4725 a9fa2909 2019-07-27 stsp if (err)
4726 a9fa2909 2019-07-27 stsp goto done;
4727 a9fa2909 2019-07-27 stsp
4728 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4729 1233e6b6 2020-10-19 stsp if (err)
4730 a9fa2909 2019-07-27 stsp goto done;
4731 a9fa2909 2019-07-27 stsp
4732 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4733 1233e6b6 2020-10-19 stsp free(te_name);
4734 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4735 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4736 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4737 a9fa2909 2019-07-27 stsp goto done;
4738 a9fa2909 2019-07-27 stsp }
4739 a129376b 2019-03-28 stsp }
4740 a129376b 2019-03-28 stsp
4741 a129376b 2019-03-28 stsp switch (status) {
4742 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4743 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4744 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4745 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4746 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4747 33aa809d 2019-08-08 stsp if (err)
4748 33aa809d 2019-08-08 stsp goto done;
4749 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4750 33aa809d 2019-08-08 stsp break;
4751 33aa809d 2019-08-08 stsp }
4752 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4753 1f1abb7e 2019-08-08 stsp ie->path);
4754 1ee397ad 2019-07-12 stsp if (err)
4755 1ee397ad 2019-07-12 stsp goto done;
4756 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4757 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4758 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4759 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4760 f259c4c1 2021-09-24 stsp relpath) == -1) {
4761 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4762 f259c4c1 2021-09-24 stsp goto done;
4763 f259c4c1 2021-09-24 stsp }
4764 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4765 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4766 f259c4c1 2021-09-24 stsp ondisk_path);
4767 f259c4c1 2021-09-24 stsp break;
4768 f259c4c1 2021-09-24 stsp }
4769 f259c4c1 2021-09-24 stsp }
4770 a129376b 2019-03-28 stsp break;
4771 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4772 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4773 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4774 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4775 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4776 33aa809d 2019-08-08 stsp if (err)
4777 33aa809d 2019-08-08 stsp goto done;
4778 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4779 33aa809d 2019-08-08 stsp break;
4780 33aa809d 2019-08-08 stsp }
4781 33aa809d 2019-08-08 stsp /* fall through */
4782 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4783 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4784 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4785 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4786 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4787 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4788 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4789 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4790 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4791 24278f30 2019-08-03 stsp } else
4792 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4793 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4794 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4795 eb81bc23 2022-06-28 tracey if (fd == -1) {
4796 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4797 eb81bc23 2022-06-28 tracey goto done;
4798 eb81bc23 2022-06-28 tracey }
4799 eb81bc23 2022-06-28 tracey
4800 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4801 a129376b 2019-03-28 stsp if (err)
4802 65084dad 2019-08-08 stsp goto done;
4803 65084dad 2019-08-08 stsp
4804 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4805 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4806 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4807 a129376b 2019-03-28 stsp goto done;
4808 65084dad 2019-08-08 stsp }
4809 65084dad 2019-08-08 stsp
4810 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4811 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4812 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4813 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4814 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4815 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4816 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4817 33aa809d 2019-08-08 stsp break;
4818 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4819 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4820 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4821 369fd7e5 2020-07-23 stsp path_content);
4822 369fd7e5 2020-07-23 stsp break;
4823 369fd7e5 2020-07-23 stsp }
4824 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4825 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4826 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4827 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4828 369fd7e5 2020-07-23 stsp } else {
4829 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4830 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4831 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4832 369fd7e5 2020-07-23 stsp goto done;
4833 369fd7e5 2020-07-23 stsp }
4834 33aa809d 2019-08-08 stsp }
4835 33aa809d 2019-08-08 stsp } else {
4836 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4837 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4838 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4839 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4840 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4841 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4842 2e1fa222 2020-07-23 stsp } else {
4843 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4844 2e1fa222 2020-07-23 stsp ie->path,
4845 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4846 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4847 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4848 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4849 2e1fa222 2020-07-23 stsp }
4850 a129376b 2019-03-28 stsp if (err)
4851 a129376b 2019-03-28 stsp goto done;
4852 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4853 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4854 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4855 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4856 437adc9d 2020-12-10 yzhong blob->id.sha1,
4857 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4858 2e1fa222 2020-07-23 stsp if (err)
4859 2e1fa222 2020-07-23 stsp goto done;
4860 2e1fa222 2020-07-23 stsp }
4861 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4862 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4863 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4864 33aa809d 2019-08-08 stsp }
4865 a129376b 2019-03-28 stsp }
4866 a129376b 2019-03-28 stsp break;
4867 e20a8b6f 2019-06-04 stsp }
4868 a129376b 2019-03-28 stsp default:
4869 1f1abb7e 2019-08-08 stsp break;
4870 a129376b 2019-03-28 stsp }
4871 a129376b 2019-03-28 stsp done:
4872 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4873 33aa809d 2019-08-08 stsp free(path_content);
4874 e20a8b6f 2019-06-04 stsp free(parent_path);
4875 a129376b 2019-03-28 stsp free(tree_path);
4876 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4877 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4878 a129376b 2019-03-28 stsp if (blob)
4879 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4880 a129376b 2019-03-28 stsp if (tree)
4881 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4882 a129376b 2019-03-28 stsp free(tree_id);
4883 a44927cc 2022-04-07 stsp if (base_commit)
4884 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4885 e20a8b6f 2019-06-04 stsp return err;
4886 e20a8b6f 2019-06-04 stsp }
4887 e20a8b6f 2019-06-04 stsp
4888 e20a8b6f 2019-06-04 stsp const struct got_error *
4889 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4890 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4891 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4892 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4893 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4894 e20a8b6f 2019-06-04 stsp {
4895 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4896 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4897 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4898 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4899 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4900 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4901 e20a8b6f 2019-06-04 stsp
4902 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4903 e20a8b6f 2019-06-04 stsp if (err)
4904 e20a8b6f 2019-06-04 stsp return err;
4905 e20a8b6f 2019-06-04 stsp
4906 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4907 e20a8b6f 2019-06-04 stsp if (err)
4908 e20a8b6f 2019-06-04 stsp goto done;
4909 e20a8b6f 2019-06-04 stsp
4910 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4911 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4912 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4913 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4914 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4915 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4916 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4917 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4918 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4919 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4920 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4921 e20a8b6f 2019-06-04 stsp if (err)
4922 af12c6b9 2019-06-04 stsp break;
4923 e20a8b6f 2019-06-04 stsp }
4924 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4925 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4926 e20a8b6f 2019-06-04 stsp err = sync_err;
4927 af12c6b9 2019-06-04 stsp done:
4928 fb399478 2019-07-12 stsp free(fileindex_path);
4929 a129376b 2019-03-28 stsp if (fileindex)
4930 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4931 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4932 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4933 a129376b 2019-03-28 stsp err = unlockerr;
4934 c4296144 2019-05-09 stsp return err;
4935 c4296144 2019-05-09 stsp }
4936 c4296144 2019-05-09 stsp
4937 cf066bf8 2019-05-09 stsp static void
4938 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4939 cf066bf8 2019-05-09 stsp {
4940 24519714 2019-05-09 stsp free(ct->path);
4941 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4942 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4943 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4944 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4945 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4946 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4947 cf066bf8 2019-05-09 stsp free(ct);
4948 cf066bf8 2019-05-09 stsp }
4949 24519714 2019-05-09 stsp
4950 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4951 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4952 24519714 2019-05-09 stsp struct got_repository *repo;
4953 24519714 2019-05-09 stsp struct got_worktree *worktree;
4954 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4955 5f8a88c6 2019-08-03 stsp int have_staged_files;
4956 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4957 2a47b1e5 2022-11-01 stsp int diff_header_shown;
4958 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
4959 2a47b1e5 2022-11-01 stsp FILE *f1;
4960 2a47b1e5 2022-11-01 stsp FILE *f2;
4961 24519714 2019-05-09 stsp };
4962 2a47b1e5 2022-11-01 stsp
4963 2a47b1e5 2022-11-01 stsp /*
4964 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
4965 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
4966 2a47b1e5 2022-11-01 stsp */
4967 2a47b1e5 2022-11-01 stsp static const struct got_error *
4968 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4969 2a47b1e5 2022-11-01 stsp const char *abspath)
4970 2a47b1e5 2022-11-01 stsp {
4971 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
4972 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
4973 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
4974 2a47b1e5 2022-11-01 stsp
4975 2a47b1e5 2022-11-01 stsp *fd = -1;
4976 2a47b1e5 2022-11-01 stsp
4977 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
4978 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4979 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4980 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
4981 2a47b1e5 2022-11-01 stsp } else {
4982 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4983 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4984 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
4985 2a47b1e5 2022-11-01 stsp }
4986 2a47b1e5 2022-11-01 stsp
4987 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
4988 2a47b1e5 2022-11-01 stsp if (*fd == -1)
4989 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
4990 2a47b1e5 2022-11-01 stsp
4991 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
4992 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
4993 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
4994 2a47b1e5 2022-11-01 stsp goto done;
4995 2a47b1e5 2022-11-01 stsp }
4996 2a47b1e5 2022-11-01 stsp
4997 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4998 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
4999 2a47b1e5 2022-11-01 stsp goto done;
5000 2a47b1e5 2022-11-01 stsp }
5001 2a47b1e5 2022-11-01 stsp done:
5002 2a47b1e5 2022-11-01 stsp if (err) {
5003 2a47b1e5 2022-11-01 stsp close(*fd);
5004 2a47b1e5 2022-11-01 stsp *fd = -1;
5005 2a47b1e5 2022-11-01 stsp }
5006 2a47b1e5 2022-11-01 stsp return err;
5007 2a47b1e5 2022-11-01 stsp }
5008 2a47b1e5 2022-11-01 stsp
5009 2a47b1e5 2022-11-01 stsp static const struct got_error *
5010 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5011 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5012 2a47b1e5 2022-11-01 stsp struct got_repository *repo, struct got_worktree *worktree)
5013 2a47b1e5 2022-11-01 stsp {
5014 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5015 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5016 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5017 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5018 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5019 2a47b1e5 2022-11-01 stsp struct stat sb;
5020 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5021 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5022 2a47b1e5 2022-11-01 stsp int diff_staged = (ct->staged_status != GOT_STATUS_NO_CHANGE);
5023 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5024 2a47b1e5 2022-11-01 stsp
5025 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5026 2a47b1e5 2022-11-01 stsp
5027 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5028 2a47b1e5 2022-11-01 stsp if (err)
5029 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5030 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5031 2a47b1e5 2022-11-01 stsp if (err)
5032 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5033 2a47b1e5 2022-11-01 stsp
5034 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5035 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5036 2a47b1e5 2022-11-01 stsp if (err)
5037 2a47b1e5 2022-11-01 stsp return err;
5038 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5039 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5040 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5041 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5042 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5043 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5044 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5045 2a47b1e5 2022-11-01 stsp }
5046 2a47b1e5 2022-11-01 stsp
5047 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5048 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5049 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5050 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5051 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5052 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5053 2a47b1e5 2022-11-01 stsp break;
5054 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5055 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5056 2a47b1e5 2022-11-01 stsp break;
5057 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5058 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5059 2a47b1e5 2022-11-01 stsp break;
5060 2a47b1e5 2022-11-01 stsp default:
5061 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5062 2a47b1e5 2022-11-01 stsp }
5063 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5064 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5065 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5066 2a47b1e5 2022-11-01 stsp goto done;
5067 2a47b1e5 2022-11-01 stsp }
5068 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5069 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5070 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5071 2a47b1e5 2022-11-01 stsp goto done;
5072 2a47b1e5 2022-11-01 stsp }
5073 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5074 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5075 2a47b1e5 2022-11-01 stsp label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5076 2a47b1e5 2022-11-01 stsp repo, diff_outfile);
5077 2a47b1e5 2022-11-01 stsp goto done;
5078 2a47b1e5 2022-11-01 stsp }
5079 2a47b1e5 2022-11-01 stsp
5080 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5081 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5082 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5083 2a47b1e5 2022-11-01 stsp goto done;
5084 2a47b1e5 2022-11-01 stsp }
5085 2a47b1e5 2022-11-01 stsp
5086 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5087 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5088 2a47b1e5 2022-11-01 stsp 8192, fd1);
5089 2a47b1e5 2022-11-01 stsp if (err)
5090 2a47b1e5 2022-11-01 stsp goto done;
5091 2a47b1e5 2022-11-01 stsp }
5092 2a47b1e5 2022-11-01 stsp
5093 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5094 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5095 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5096 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5097 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5098 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5099 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5100 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5101 2a47b1e5 2022-11-01 stsp goto done;
5102 2a47b1e5 2022-11-01 stsp }
5103 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5104 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5105 2a47b1e5 2022-11-01 stsp if (err)
5106 2a47b1e5 2022-11-01 stsp goto done;
5107 2a47b1e5 2022-11-01 stsp }
5108 2a47b1e5 2022-11-01 stsp } else {
5109 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5110 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5111 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5112 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5113 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5114 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5115 2a47b1e5 2022-11-01 stsp goto done;
5116 2a47b1e5 2022-11-01 stsp }
5117 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5118 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5119 2a47b1e5 2022-11-01 stsp if (err)
5120 2a47b1e5 2022-11-01 stsp goto done;
5121 2a47b1e5 2022-11-01 stsp }
5122 2a47b1e5 2022-11-01 stsp }
5123 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5124 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5125 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5126 2a47b1e5 2022-11-01 stsp goto done;
5127 2a47b1e5 2022-11-01 stsp }
5128 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5129 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5130 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5131 2a47b1e5 2022-11-01 stsp goto done;
5132 2a47b1e5 2022-11-01 stsp }
5133 2a47b1e5 2022-11-01 stsp fd = -1;
5134 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5135 2a47b1e5 2022-11-01 stsp }
5136 2a47b1e5 2022-11-01 stsp
5137 2a47b1e5 2022-11-01 stsp if (blob1) {
5138 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5139 2a47b1e5 2022-11-01 stsp f1, blob1);
5140 2a47b1e5 2022-11-01 stsp if (err)
5141 2a47b1e5 2022-11-01 stsp goto done;
5142 2a47b1e5 2022-11-01 stsp }
5143 2a47b1e5 2022-11-01 stsp
5144 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5145 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5146 2a47b1e5 2022-11-01 stsp GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, diff_outfile);
5147 2a47b1e5 2022-11-01 stsp done:
5148 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5149 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5150 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5151 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5152 2a47b1e5 2022-11-01 stsp if (blob1)
5153 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5154 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5155 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5156 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5157 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5158 2a47b1e5 2022-11-01 stsp return err;
5159 2a47b1e5 2022-11-01 stsp }
5160 cf066bf8 2019-05-09 stsp
5161 c4296144 2019-05-09 stsp static const struct got_error *
5162 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5163 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5164 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5165 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5166 c4296144 2019-05-09 stsp {
5167 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5168 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5169 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5170 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5171 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5172 768aea60 2019-05-09 stsp struct stat sb;
5173 c4296144 2019-05-09 stsp
5174 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5175 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5176 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5177 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5178 5f8a88c6 2019-08-03 stsp return NULL;
5179 5f8a88c6 2019-08-03 stsp } else {
5180 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
5181 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5182 c4296144 2019-05-09 stsp
5183 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5184 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5185 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5186 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
5187 5f8a88c6 2019-08-03 stsp return NULL;
5188 5f8a88c6 2019-08-03 stsp }
5189 0b5cc0d6 2019-05-09 stsp
5190 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5191 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5192 036813ee 2019-05-09 stsp goto done;
5193 036813ee 2019-05-09 stsp }
5194 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5195 036813ee 2019-05-09 stsp parent_path = strdup("");
5196 69960a46 2019-05-09 stsp if (parent_path == NULL)
5197 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5198 69960a46 2019-05-09 stsp } else {
5199 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5200 69960a46 2019-05-09 stsp if (err)
5201 69960a46 2019-05-09 stsp return err;
5202 69960a46 2019-05-09 stsp }
5203 c4296144 2019-05-09 stsp
5204 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5205 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5206 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5207 c4296144 2019-05-09 stsp goto done;
5208 768aea60 2019-05-09 stsp }
5209 768aea60 2019-05-09 stsp
5210 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5211 768aea60 2019-05-09 stsp relpath) == -1) {
5212 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5213 768aea60 2019-05-09 stsp goto done;
5214 768aea60 2019-05-09 stsp }
5215 0aeb8099 2020-07-23 stsp
5216 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5217 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5218 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5219 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5220 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5221 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5222 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5223 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5224 0aeb8099 2020-07-23 stsp break;
5225 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5226 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5227 0aeb8099 2020-07-23 stsp break;
5228 0aeb8099 2020-07-23 stsp default:
5229 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5230 0aeb8099 2020-07-23 stsp goto done;
5231 0aeb8099 2020-07-23 stsp }
5232 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5233 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5234 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5235 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5236 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5237 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5238 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5239 12463d8b 2019-12-13 stsp ct->ondisk_path);
5240 12463d8b 2019-12-13 stsp goto done;
5241 12463d8b 2019-12-13 stsp }
5242 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5243 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5244 768aea60 2019-05-09 stsp goto done;
5245 768aea60 2019-05-09 stsp }
5246 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5247 c4296144 2019-05-09 stsp }
5248 c4296144 2019-05-09 stsp
5249 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5250 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5251 44d03001 2019-05-09 stsp relpath) == -1) {
5252 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5253 44d03001 2019-05-09 stsp goto done;
5254 44d03001 2019-05-09 stsp }
5255 44d03001 2019-05-09 stsp
5256 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5257 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5258 35213c7c 2020-07-23 stsp int is_bad_symlink;
5259 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5260 35213c7c 2020-07-23 stsp ssize_t target_len;
5261 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5262 35213c7c 2020-07-23 stsp sizeof(target_path));
5263 35213c7c 2020-07-23 stsp if (target_len == -1) {
5264 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5265 35213c7c 2020-07-23 stsp ct->ondisk_path);
5266 35213c7c 2020-07-23 stsp goto done;
5267 35213c7c 2020-07-23 stsp }
5268 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5269 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5270 35213c7c 2020-07-23 stsp if (err)
5271 35213c7c 2020-07-23 stsp goto done;
5272 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5273 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5274 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5275 35213c7c 2020-07-23 stsp goto done;
5276 35213c7c 2020-07-23 stsp }
5277 35213c7c 2020-07-23 stsp }
5278 35213c7c 2020-07-23 stsp
5279 35213c7c 2020-07-23 stsp
5280 cf066bf8 2019-05-09 stsp ct->status = status;
5281 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5282 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5283 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5284 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5285 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5286 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5287 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5288 b416585c 2019-05-13 stsp goto done;
5289 b416585c 2019-05-13 stsp }
5290 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5291 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5292 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5293 f0b75401 2019-08-03 stsp goto done;
5294 f0b75401 2019-08-03 stsp }
5295 f0b75401 2019-08-03 stsp }
5296 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5297 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5298 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5299 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5300 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5301 036813ee 2019-05-09 stsp goto done;
5302 036813ee 2019-05-09 stsp }
5303 ca2503ea 2019-05-09 stsp }
5304 24519714 2019-05-09 stsp ct->path = strdup(path);
5305 24519714 2019-05-09 stsp if (ct->path == NULL) {
5306 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5307 24519714 2019-05-09 stsp goto done;
5308 24519714 2019-05-09 stsp }
5309 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5310 2a47b1e5 2022-11-01 stsp if (err)
5311 2a47b1e5 2022-11-01 stsp goto done;
5312 2a47b1e5 2022-11-01 stsp
5313 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5314 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5315 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5316 2a47b1e5 2022-11-01 stsp a->repo, a->worktree);
5317 2a47b1e5 2022-11-01 stsp if (err)
5318 2a47b1e5 2022-11-01 stsp goto done;
5319 2a47b1e5 2022-11-01 stsp }
5320 c4296144 2019-05-09 stsp done:
5321 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5322 ed175427 2019-05-09 stsp free_commitable(ct);
5323 24519714 2019-05-09 stsp free(parent_path);
5324 036813ee 2019-05-09 stsp free(path);
5325 a129376b 2019-03-28 stsp return err;
5326 a129376b 2019-03-28 stsp }
5327 c4296144 2019-05-09 stsp
5328 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5329 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5330 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5331 036813ee 2019-05-09 stsp struct got_repository *);
5332 ed175427 2019-05-09 stsp
5333 ed175427 2019-05-09 stsp static const struct got_error *
5334 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5335 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5336 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5337 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5338 afa376bf 2019-05-09 stsp struct got_repository *repo)
5339 ed175427 2019-05-09 stsp {
5340 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5341 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5342 036813ee 2019-05-09 stsp char *subpath;
5343 ed175427 2019-05-09 stsp
5344 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5345 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5346 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5347 ed175427 2019-05-09 stsp
5348 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5349 ed175427 2019-05-09 stsp if (err)
5350 ed175427 2019-05-09 stsp return err;
5351 ed175427 2019-05-09 stsp
5352 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5353 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5354 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5355 036813ee 2019-05-09 stsp free(subpath);
5356 036813ee 2019-05-09 stsp return err;
5357 036813ee 2019-05-09 stsp }
5358 ed175427 2019-05-09 stsp
5359 036813ee 2019-05-09 stsp static const struct got_error *
5360 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5361 036813ee 2019-05-09 stsp {
5362 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5363 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5364 ed175427 2019-05-09 stsp
5365 036813ee 2019-05-09 stsp *match = 0;
5366 ed175427 2019-05-09 stsp
5367 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5368 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5369 0f63689d 2019-05-10 stsp return NULL;
5370 036813ee 2019-05-09 stsp }
5371 0b5cc0d6 2019-05-09 stsp
5372 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5373 0f63689d 2019-05-10 stsp if (err)
5374 0f63689d 2019-05-10 stsp return err;
5375 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5376 036813ee 2019-05-09 stsp free(ct_parent_path);
5377 036813ee 2019-05-09 stsp return err;
5378 036813ee 2019-05-09 stsp }
5379 0b5cc0d6 2019-05-09 stsp
5380 768aea60 2019-05-09 stsp static mode_t
5381 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5382 768aea60 2019-05-09 stsp {
5383 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5384 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5385 3d9a4ec4 2020-07-23 stsp
5386 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5387 768aea60 2019-05-09 stsp }
5388 768aea60 2019-05-09 stsp
5389 036813ee 2019-05-09 stsp static const struct got_error *
5390 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5391 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5392 036813ee 2019-05-09 stsp {
5393 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5394 ca2503ea 2019-05-09 stsp
5395 036813ee 2019-05-09 stsp *new_te = NULL;
5396 0b5cc0d6 2019-05-09 stsp
5397 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5398 036813ee 2019-05-09 stsp if (err)
5399 036813ee 2019-05-09 stsp goto done;
5400 0b5cc0d6 2019-05-09 stsp
5401 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5402 036813ee 2019-05-09 stsp
5403 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5404 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5405 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5406 5f8a88c6 2019-08-03 stsp else
5407 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5408 036813ee 2019-05-09 stsp done:
5409 036813ee 2019-05-09 stsp if (err && *new_te) {
5410 56e0773d 2019-11-28 stsp free(*new_te);
5411 036813ee 2019-05-09 stsp *new_te = NULL;
5412 036813ee 2019-05-09 stsp }
5413 036813ee 2019-05-09 stsp return err;
5414 036813ee 2019-05-09 stsp }
5415 036813ee 2019-05-09 stsp
5416 036813ee 2019-05-09 stsp static const struct got_error *
5417 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5418 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5419 036813ee 2019-05-09 stsp {
5420 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5421 102b254e 2020-10-19 stsp char *ct_name = NULL;
5422 036813ee 2019-05-09 stsp
5423 036813ee 2019-05-09 stsp *new_te = NULL;
5424 036813ee 2019-05-09 stsp
5425 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5426 036813ee 2019-05-09 stsp if (*new_te == NULL)
5427 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5428 036813ee 2019-05-09 stsp
5429 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5430 102b254e 2020-10-19 stsp if (err)
5431 036813ee 2019-05-09 stsp goto done;
5432 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5433 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5434 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5435 036813ee 2019-05-09 stsp goto done;
5436 036813ee 2019-05-09 stsp }
5437 036813ee 2019-05-09 stsp
5438 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5439 036813ee 2019-05-09 stsp
5440 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5441 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5442 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5443 5f8a88c6 2019-08-03 stsp else
5444 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5445 036813ee 2019-05-09 stsp done:
5446 102b254e 2020-10-19 stsp free(ct_name);
5447 036813ee 2019-05-09 stsp if (err && *new_te) {
5448 56e0773d 2019-11-28 stsp free(*new_te);
5449 036813ee 2019-05-09 stsp *new_te = NULL;
5450 036813ee 2019-05-09 stsp }
5451 036813ee 2019-05-09 stsp return err;
5452 036813ee 2019-05-09 stsp }
5453 036813ee 2019-05-09 stsp
5454 036813ee 2019-05-09 stsp static const struct got_error *
5455 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5456 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5457 036813ee 2019-05-09 stsp {
5458 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5459 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5460 036813ee 2019-05-09 stsp
5461 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5462 036813ee 2019-05-09 stsp if (err)
5463 036813ee 2019-05-09 stsp return err;
5464 036813ee 2019-05-09 stsp if (new_pe == NULL)
5465 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5466 036813ee 2019-05-09 stsp return NULL;
5467 afa376bf 2019-05-09 stsp }
5468 afa376bf 2019-05-09 stsp
5469 afa376bf 2019-05-09 stsp static const struct got_error *
5470 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5471 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5472 afa376bf 2019-05-09 stsp {
5473 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5474 5f8a88c6 2019-08-03 stsp unsigned char status;
5475 0ff8d236 2021-09-28 stsp
5476 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5477 0ff8d236 2021-09-28 stsp return NULL;
5478 5f8a88c6 2019-08-03 stsp
5479 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5480 afa376bf 2019-05-09 stsp ct_path++;
5481 5f8a88c6 2019-08-03 stsp
5482 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5483 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5484 5f8a88c6 2019-08-03 stsp else
5485 5f8a88c6 2019-08-03 stsp status = ct->status;
5486 5f8a88c6 2019-08-03 stsp
5487 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5488 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5489 036813ee 2019-05-09 stsp }
5490 036813ee 2019-05-09 stsp
5491 036813ee 2019-05-09 stsp static const struct got_error *
5492 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5493 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5494 44d03001 2019-05-09 stsp {
5495 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5496 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5497 44d03001 2019-05-09 stsp char *te_path;
5498 44d03001 2019-05-09 stsp
5499 44d03001 2019-05-09 stsp *modified = 0;
5500 44d03001 2019-05-09 stsp
5501 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5502 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5503 44d03001 2019-05-09 stsp te->name) == -1)
5504 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5505 44d03001 2019-05-09 stsp
5506 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5507 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5508 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5509 44d03001 2019-05-09 stsp strlen(te_path));
5510 62d463ca 2020-10-20 naddy if (*modified)
5511 44d03001 2019-05-09 stsp break;
5512 44d03001 2019-05-09 stsp }
5513 44d03001 2019-05-09 stsp
5514 44d03001 2019-05-09 stsp free(te_path);
5515 44d03001 2019-05-09 stsp return err;
5516 44d03001 2019-05-09 stsp }
5517 44d03001 2019-05-09 stsp
5518 44d03001 2019-05-09 stsp static const struct got_error *
5519 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5520 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5521 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5522 036813ee 2019-05-09 stsp {
5523 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5524 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5525 036813ee 2019-05-09 stsp
5526 036813ee 2019-05-09 stsp *ctp = NULL;
5527 036813ee 2019-05-09 stsp
5528 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5529 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5530 036813ee 2019-05-09 stsp char *ct_name = NULL;
5531 036813ee 2019-05-09 stsp int path_matches;
5532 036813ee 2019-05-09 stsp
5533 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5534 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5535 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5536 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5537 5f8a88c6 2019-08-03 stsp continue;
5538 5f8a88c6 2019-08-03 stsp } else {
5539 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5540 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5541 5f8a88c6 2019-08-03 stsp continue;
5542 5f8a88c6 2019-08-03 stsp }
5543 036813ee 2019-05-09 stsp
5544 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5545 036813ee 2019-05-09 stsp continue;
5546 036813ee 2019-05-09 stsp
5547 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5548 62d463ca 2020-10-20 naddy if (err)
5549 036813ee 2019-05-09 stsp return err;
5550 036813ee 2019-05-09 stsp if (!path_matches)
5551 036813ee 2019-05-09 stsp continue;
5552 036813ee 2019-05-09 stsp
5553 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5554 d34b633e 2020-10-19 stsp if (err)
5555 d34b633e 2020-10-19 stsp return err;
5556 d34b633e 2020-10-19 stsp
5557 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5558 d34b633e 2020-10-19 stsp free(ct_name);
5559 036813ee 2019-05-09 stsp continue;
5560 d34b633e 2020-10-19 stsp }
5561 d34b633e 2020-10-19 stsp free(ct_name);
5562 036813ee 2019-05-09 stsp
5563 036813ee 2019-05-09 stsp *ctp = ct;
5564 036813ee 2019-05-09 stsp break;
5565 036813ee 2019-05-09 stsp }
5566 2b496619 2019-07-10 stsp
5567 2b496619 2019-07-10 stsp return err;
5568 2b496619 2019-07-10 stsp }
5569 2b496619 2019-07-10 stsp
5570 2b496619 2019-07-10 stsp static const struct got_error *
5571 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5572 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5573 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5574 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5575 2b496619 2019-07-10 stsp struct got_repository *repo)
5576 2b496619 2019-07-10 stsp {
5577 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5578 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5579 2b496619 2019-07-10 stsp char *subtree_path;
5580 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5581 ba580f68 2020-03-22 stsp int nentries;
5582 2b496619 2019-07-10 stsp
5583 2b496619 2019-07-10 stsp *new_tep = NULL;
5584 2b496619 2019-07-10 stsp
5585 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5586 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5587 2b496619 2019-07-10 stsp child_path) == -1)
5588 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5589 036813ee 2019-05-09 stsp
5590 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5591 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5592 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5593 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5594 56e0773d 2019-11-28 stsp
5595 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5596 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5597 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5598 2b496619 2019-07-10 stsp goto done;
5599 2b496619 2019-07-10 stsp }
5600 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5601 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5602 2b496619 2019-07-10 stsp if (err) {
5603 56e0773d 2019-11-28 stsp free(new_te);
5604 2b496619 2019-07-10 stsp goto done;
5605 2b496619 2019-07-10 stsp }
5606 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5607 2b496619 2019-07-10 stsp done:
5608 56e0773d 2019-11-28 stsp free(id);
5609 2b496619 2019-07-10 stsp free(subtree_path);
5610 2b496619 2019-07-10 stsp if (err == NULL)
5611 2b496619 2019-07-10 stsp *new_tep = new_te;
5612 036813ee 2019-05-09 stsp return err;
5613 036813ee 2019-05-09 stsp }
5614 036813ee 2019-05-09 stsp
5615 036813ee 2019-05-09 stsp static const struct got_error *
5616 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5617 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5618 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5619 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5620 036813ee 2019-05-09 stsp struct got_repository *repo)
5621 036813ee 2019-05-09 stsp {
5622 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5623 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5624 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5625 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5626 036813ee 2019-05-09 stsp
5627 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5628 ba580f68 2020-03-22 stsp *nentries = 0;
5629 036813ee 2019-05-09 stsp
5630 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5631 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5632 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5633 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5634 036813ee 2019-05-09 stsp
5635 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5636 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5637 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5638 036813ee 2019-05-09 stsp continue;
5639 036813ee 2019-05-09 stsp
5640 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5641 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5642 036813ee 2019-05-09 stsp continue;
5643 036813ee 2019-05-09 stsp
5644 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5645 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5646 036813ee 2019-05-09 stsp if (err)
5647 036813ee 2019-05-09 stsp goto done;
5648 036813ee 2019-05-09 stsp
5649 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5650 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5651 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5652 036813ee 2019-05-09 stsp if (err)
5653 036813ee 2019-05-09 stsp goto done;
5654 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5655 afa376bf 2019-05-09 stsp if (err)
5656 afa376bf 2019-05-09 stsp goto done;
5657 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5658 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5659 2b496619 2019-07-10 stsp if (err)
5660 2f51b5b3 2019-05-09 stsp goto done;
5661 ba580f68 2020-03-22 stsp (*nentries)++;
5662 2b496619 2019-07-10 stsp } else {
5663 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5664 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5665 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5666 2b496619 2019-07-10 stsp == NULL) {
5667 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5668 2b496619 2019-07-10 stsp child_path, path_base_tree,
5669 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5670 2b496619 2019-07-10 stsp repo);
5671 2b496619 2019-07-10 stsp if (err)
5672 2b496619 2019-07-10 stsp goto done;
5673 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5674 2b496619 2019-07-10 stsp if (err)
5675 2b496619 2019-07-10 stsp goto done;
5676 ba580f68 2020-03-22 stsp (*nentries)++;
5677 9ba0479c 2019-05-10 stsp }
5678 ed175427 2019-05-09 stsp }
5679 2f51b5b3 2019-05-09 stsp }
5680 2f51b5b3 2019-05-09 stsp
5681 2f51b5b3 2019-05-09 stsp if (base_tree) {
5682 56e0773d 2019-11-28 stsp int i, nbase_entries;
5683 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5684 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5685 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5686 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5687 63c5ca5d 2019-08-24 stsp
5688 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5689 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5690 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5691 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5692 63c5ca5d 2019-08-24 stsp if (err)
5693 63c5ca5d 2019-08-24 stsp goto done;
5694 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5695 63c5ca5d 2019-08-24 stsp if (err)
5696 63c5ca5d 2019-08-24 stsp goto done;
5697 ba580f68 2020-03-22 stsp (*nentries)++;
5698 63c5ca5d 2019-08-24 stsp continue;
5699 63c5ca5d 2019-08-24 stsp }
5700 2f51b5b3 2019-05-09 stsp
5701 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5702 44d03001 2019-05-09 stsp int modified;
5703 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5704 036813ee 2019-05-09 stsp if (err)
5705 036813ee 2019-05-09 stsp goto done;
5706 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5707 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5708 2f51b5b3 2019-05-09 stsp if (err)
5709 2f51b5b3 2019-05-09 stsp goto done;
5710 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5711 44d03001 2019-05-09 stsp if (modified) {
5712 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5713 ba580f68 2020-03-22 stsp int nsubentries;
5714 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5715 ba580f68 2020-03-22 stsp &nsubentries, te,
5716 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5717 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5718 44d03001 2019-05-09 stsp if (err)
5719 44d03001 2019-05-09 stsp goto done;
5720 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5721 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5722 ba580f68 2020-03-22 stsp free(new_id);
5723 ba580f68 2020-03-22 stsp continue;
5724 ba580f68 2020-03-22 stsp }
5725 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5726 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5727 56e0773d 2019-11-28 stsp free(new_id);
5728 44d03001 2019-05-09 stsp }
5729 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5730 036813ee 2019-05-09 stsp if (err)
5731 036813ee 2019-05-09 stsp goto done;
5732 ba580f68 2020-03-22 stsp (*nentries)++;
5733 2f51b5b3 2019-05-09 stsp continue;
5734 036813ee 2019-05-09 stsp }
5735 2f51b5b3 2019-05-09 stsp
5736 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5737 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5738 f66c734c 2019-09-22 stsp if (err)
5739 f66c734c 2019-09-22 stsp goto done;
5740 2f51b5b3 2019-05-09 stsp if (ct) {
5741 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5742 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5743 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5744 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5745 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5746 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5747 2f51b5b3 2019-05-09 stsp if (err)
5748 2f51b5b3 2019-05-09 stsp goto done;
5749 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5750 2f51b5b3 2019-05-09 stsp if (err)
5751 2f51b5b3 2019-05-09 stsp goto done;
5752 ba580f68 2020-03-22 stsp (*nentries)++;
5753 2f51b5b3 2019-05-09 stsp }
5754 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5755 b416585c 2019-05-13 stsp status_arg);
5756 afa376bf 2019-05-09 stsp if (err)
5757 afa376bf 2019-05-09 stsp goto done;
5758 2f51b5b3 2019-05-09 stsp } else {
5759 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5760 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5761 2f51b5b3 2019-05-09 stsp if (err)
5762 2f51b5b3 2019-05-09 stsp goto done;
5763 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5764 2f51b5b3 2019-05-09 stsp if (err)
5765 2f51b5b3 2019-05-09 stsp goto done;
5766 ba580f68 2020-03-22 stsp (*nentries)++;
5767 2f51b5b3 2019-05-09 stsp }
5768 ca2503ea 2019-05-09 stsp }
5769 ed175427 2019-05-09 stsp }
5770 0b5cc0d6 2019-05-09 stsp
5771 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5772 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5773 ed175427 2019-05-09 stsp done:
5774 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5775 2e1fa222 2020-07-23 stsp return err;
5776 2e1fa222 2020-07-23 stsp }
5777 2e1fa222 2020-07-23 stsp
5778 2e1fa222 2020-07-23 stsp static const struct got_error *
5779 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5780 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5781 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5782 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5783 ebf99748 2019-05-09 stsp {
5784 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5785 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5786 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5787 ebf99748 2019-05-09 stsp
5788 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5789 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5790 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5791 ebf99748 2019-05-09 stsp
5792 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5793 437adc9d 2020-12-10 yzhong
5794 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5795 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5796 437adc9d 2020-12-10 yzhong if (err)
5797 437adc9d 2020-12-10 yzhong goto done;
5798 437adc9d 2020-12-10 yzhong
5799 ebf99748 2019-05-09 stsp if (ie) {
5800 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5801 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5802 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5803 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5804 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5805 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5806 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5807 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5808 437adc9d 2020-12-10 yzhong
5809 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5810 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5811 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5812 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5813 72fd46fa 2019-09-06 stsp !have_staged_files);
5814 ebf99748 2019-05-09 stsp } else
5815 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5816 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5817 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5818 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5819 72fd46fa 2019-09-06 stsp !have_staged_files);
5820 ebf99748 2019-05-09 stsp } else {
5821 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5822 ebf99748 2019-05-09 stsp if (err)
5823 437adc9d 2020-12-10 yzhong goto done;
5824 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5825 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5826 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5827 3969253a 2020-03-07 stsp if (err) {
5828 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5829 437adc9d 2020-12-10 yzhong goto done;
5830 3969253a 2020-03-07 stsp }
5831 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5832 3969253a 2020-03-07 stsp if (err) {
5833 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5834 437adc9d 2020-12-10 yzhong goto done;
5835 3969253a 2020-03-07 stsp }
5836 ebf99748 2019-05-09 stsp }
5837 437adc9d 2020-12-10 yzhong free(relpath);
5838 437adc9d 2020-12-10 yzhong relpath = NULL;
5839 ebf99748 2019-05-09 stsp }
5840 437adc9d 2020-12-10 yzhong done:
5841 437adc9d 2020-12-10 yzhong free(relpath);
5842 d56d26ce 2019-05-10 stsp return err;
5843 d56d26ce 2019-05-10 stsp }
5844 735ef5ac 2019-08-03 stsp
5845 d56d26ce 2019-05-10 stsp
5846 d56d26ce 2019-05-10 stsp static const struct got_error *
5847 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5848 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5849 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5850 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5851 735ef5ac 2019-08-03 stsp int ood_errcode)
5852 d56d26ce 2019-05-10 stsp {
5853 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5854 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5855 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5856 1a36436d 2019-06-10 stsp
5857 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5858 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5859 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5860 1a36436d 2019-06-10 stsp return NULL;
5861 9bead371 2019-07-28 stsp /*
5862 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5863 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5864 9bead371 2019-07-28 stsp */
5865 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5866 a44927cc 2022-04-07 stsp if (err)
5867 a44927cc 2022-04-07 stsp goto done;
5868 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5869 9bead371 2019-07-28 stsp if (err) {
5870 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5871 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5872 1a36436d 2019-06-10 stsp goto done;
5873 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5874 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5875 1a36436d 2019-06-10 stsp } else {
5876 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5877 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5878 a44927cc 2022-04-07 stsp if (err)
5879 a44927cc 2022-04-07 stsp goto done;
5880 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5881 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5882 1a36436d 2019-06-10 stsp goto done;
5883 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5884 1a36436d 2019-06-10 stsp }
5885 1a36436d 2019-06-10 stsp done:
5886 1a36436d 2019-06-10 stsp free(id);
5887 a44927cc 2022-04-07 stsp if (commit)
5888 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5889 1a36436d 2019-06-10 stsp return err;
5890 ebf99748 2019-05-09 stsp }
5891 ebf99748 2019-05-09 stsp
5892 336075a4 2022-06-25 op static const struct got_error *
5893 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5894 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5895 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5896 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5897 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5898 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5899 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5900 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5901 afa376bf 2019-05-09 stsp struct got_repository *repo)
5902 c4296144 2019-05-09 stsp {
5903 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5904 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5905 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5906 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5907 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5908 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5909 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5910 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5911 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5912 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5913 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5914 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5915 f8399b8f 2022-07-22 stsp time_t timestamp;
5916 c4296144 2019-05-09 stsp
5917 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5918 c4296144 2019-05-09 stsp
5919 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5920 675c7539 2019-05-09 stsp
5921 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5922 588edf97 2019-05-10 stsp if (err)
5923 588edf97 2019-05-10 stsp goto done;
5924 de18fc63 2019-05-09 stsp
5925 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5926 588edf97 2019-05-10 stsp if (err)
5927 588edf97 2019-05-10 stsp goto done;
5928 588edf97 2019-05-10 stsp
5929 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5930 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
5931 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
5932 33ad4cbe 2019-05-12 jcs if (err)
5933 33ad4cbe 2019-05-12 jcs goto done;
5934 33ad4cbe 2019-05-12 jcs }
5935 c4296144 2019-05-09 stsp
5936 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5937 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5938 33ad4cbe 2019-05-12 jcs goto done;
5939 33ad4cbe 2019-05-12 jcs }
5940 33ad4cbe 2019-05-12 jcs
5941 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5942 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5943 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5944 cf066bf8 2019-05-09 stsp char *ondisk_path;
5945 cf066bf8 2019-05-09 stsp
5946 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5947 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5948 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5949 5f8a88c6 2019-08-03 stsp continue;
5950 5f8a88c6 2019-08-03 stsp
5951 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5952 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5953 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5954 cf066bf8 2019-05-09 stsp continue;
5955 cf066bf8 2019-05-09 stsp
5956 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5957 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5958 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5959 cf066bf8 2019-05-09 stsp goto done;
5960 cf066bf8 2019-05-09 stsp }
5961 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5962 2e1fa222 2020-07-23 stsp free(ondisk_path);
5963 2e1fa222 2020-07-23 stsp if (err)
5964 cf066bf8 2019-05-09 stsp goto done;
5965 cf066bf8 2019-05-09 stsp }
5966 036813ee 2019-05-09 stsp
5967 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5968 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5969 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5970 036813ee 2019-05-09 stsp if (err)
5971 036813ee 2019-05-09 stsp goto done;
5972 036813ee 2019-05-09 stsp
5973 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5974 de18fc63 2019-05-09 stsp if (err)
5975 de18fc63 2019-05-09 stsp goto done;
5976 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5977 f259c4c1 2021-09-24 stsp nparents++;
5978 f259c4c1 2021-09-24 stsp if (parent_id2) {
5979 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5980 f259c4c1 2021-09-24 stsp if (err)
5981 f259c4c1 2021-09-24 stsp goto done;
5982 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5983 f259c4c1 2021-09-24 stsp nparents++;
5984 f259c4c1 2021-09-24 stsp }
5985 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
5986 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5987 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
5988 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5989 33ad4cbe 2019-05-12 jcs free(logmsg);
5990 9d40349a 2019-05-09 stsp if (err)
5991 9d40349a 2019-05-09 stsp goto done;
5992 9d40349a 2019-05-09 stsp
5993 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5994 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5995 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5996 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5997 09f5bd90 2019-05-09 stsp goto done;
5998 09f5bd90 2019-05-09 stsp }
5999 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6000 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6001 09f5bd90 2019-05-09 stsp if (err)
6002 09f5bd90 2019-05-09 stsp goto done;
6003 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6004 ebf99748 2019-05-09 stsp if (err)
6005 ebf99748 2019-05-09 stsp goto done;
6006 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6007 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6008 09f5bd90 2019-05-09 stsp goto done;
6009 09f5bd90 2019-05-09 stsp }
6010 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6011 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6012 f2c16586 2019-05-09 stsp if (err)
6013 f2c16586 2019-05-09 stsp goto done;
6014 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6015 f2c16586 2019-05-09 stsp if (err)
6016 f2c16586 2019-05-09 stsp goto done;
6017 f2c16586 2019-05-09 stsp
6018 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6019 09f5bd90 2019-05-09 stsp if (err)
6020 09f5bd90 2019-05-09 stsp goto done;
6021 09f5bd90 2019-05-09 stsp
6022 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6023 ebf99748 2019-05-09 stsp if (err)
6024 ebf99748 2019-05-09 stsp goto done;
6025 c4296144 2019-05-09 stsp done:
6026 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6027 588edf97 2019-05-10 stsp if (head_tree)
6028 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6029 588edf97 2019-05-10 stsp if (head_commit)
6030 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6031 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6032 2f17228e 2019-05-12 stsp if (head_ref2) {
6033 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6034 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6035 2f17228e 2019-05-12 stsp err = unlockerr;
6036 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6037 39cd0ff6 2019-07-12 stsp }
6038 39cd0ff6 2019-07-12 stsp return err;
6039 39cd0ff6 2019-07-12 stsp }
6040 5c1e53bc 2019-07-28 stsp
6041 5c1e53bc 2019-07-28 stsp static const struct got_error *
6042 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6043 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6044 5c1e53bc 2019-07-28 stsp {
6045 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6046 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6047 39cd0ff6 2019-07-12 stsp
6048 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6049 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6050 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6051 5c1e53bc 2019-07-28 stsp
6052 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6053 5c1e53bc 2019-07-28 stsp ct_path++;
6054 5c1e53bc 2019-07-28 stsp
6055 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6056 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6057 5c1e53bc 2019-07-28 stsp break;
6058 5c1e53bc 2019-07-28 stsp }
6059 5c1e53bc 2019-07-28 stsp
6060 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6061 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6062 5c1e53bc 2019-07-28 stsp
6063 5c1e53bc 2019-07-28 stsp return NULL;
6064 5c1e53bc 2019-07-28 stsp }
6065 5c1e53bc 2019-07-28 stsp
6066 f0b75401 2019-08-03 stsp static const struct got_error *
6067 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6068 f0b75401 2019-08-03 stsp {
6069 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6070 f0b75401 2019-08-03 stsp
6071 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6072 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6073 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6074 f0b75401 2019-08-03 stsp }
6075 f0b75401 2019-08-03 stsp
6076 f0b75401 2019-08-03 stsp return NULL;
6077 f0b75401 2019-08-03 stsp }
6078 f0b75401 2019-08-03 stsp
6079 5f8a88c6 2019-08-03 stsp static const struct got_error *
6080 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6081 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6082 5f8a88c6 2019-08-03 stsp {
6083 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6084 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6085 5f8a88c6 2019-08-03 stsp
6086 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6087 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6088 5f8a88c6 2019-08-03 stsp continue;
6089 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6090 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6091 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6092 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6093 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6094 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6095 5f8a88c6 2019-08-03 stsp }
6096 5f8a88c6 2019-08-03 stsp
6097 5f8a88c6 2019-08-03 stsp return NULL;
6098 5f8a88c6 2019-08-03 stsp }
6099 5f8a88c6 2019-08-03 stsp
6100 39cd0ff6 2019-07-12 stsp const struct got_error *
6101 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6102 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6103 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6104 2a47b1e5 2022-11-01 stsp int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6105 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6106 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6107 39cd0ff6 2019-07-12 stsp {
6108 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6109 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6110 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6111 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6112 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6113 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6114 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6115 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6116 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6117 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6118 39cd0ff6 2019-07-12 stsp
6119 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6120 39cd0ff6 2019-07-12 stsp
6121 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6122 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6123 39cd0ff6 2019-07-12 stsp
6124 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6125 39cd0ff6 2019-07-12 stsp if (err)
6126 39cd0ff6 2019-07-12 stsp goto done;
6127 39cd0ff6 2019-07-12 stsp
6128 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6129 39cd0ff6 2019-07-12 stsp if (err)
6130 39cd0ff6 2019-07-12 stsp goto done;
6131 39cd0ff6 2019-07-12 stsp
6132 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6133 39cd0ff6 2019-07-12 stsp if (err)
6134 39cd0ff6 2019-07-12 stsp goto done;
6135 39cd0ff6 2019-07-12 stsp
6136 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6137 39cd0ff6 2019-07-12 stsp if (err)
6138 39cd0ff6 2019-07-12 stsp goto done;
6139 39cd0ff6 2019-07-12 stsp
6140 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6141 5f8a88c6 2019-08-03 stsp &have_staged_files);
6142 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6143 5f8a88c6 2019-08-03 stsp goto done;
6144 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6145 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6146 5f8a88c6 2019-08-03 stsp if (err)
6147 5f8a88c6 2019-08-03 stsp goto done;
6148 5f8a88c6 2019-08-03 stsp }
6149 5f8a88c6 2019-08-03 stsp
6150 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6151 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6152 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6153 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6154 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6155 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6156 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6157 2a47b1e5 2022-11-01 stsp if (show_diff) {
6158 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6159 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6160 2a47b1e5 2022-11-01 stsp if (err)
6161 2a47b1e5 2022-11-01 stsp goto done;
6162 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6163 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6164 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6165 2a47b1e5 2022-11-01 stsp goto done;
6166 2a47b1e5 2022-11-01 stsp }
6167 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6168 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6169 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6170 2a47b1e5 2022-11-01 stsp goto done;
6171 2a47b1e5 2022-11-01 stsp }
6172 2a47b1e5 2022-11-01 stsp }
6173 2a47b1e5 2022-11-01 stsp
6174 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6175 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6176 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6177 5c1e53bc 2019-07-28 stsp if (err)
6178 5c1e53bc 2019-07-28 stsp goto done;
6179 5c1e53bc 2019-07-28 stsp }
6180 39cd0ff6 2019-07-12 stsp
6181 2a47b1e5 2022-11-01 stsp if (show_diff) {
6182 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6183 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6184 2a47b1e5 2022-11-01 stsp goto done;
6185 2a47b1e5 2022-11-01 stsp }
6186 2a47b1e5 2022-11-01 stsp }
6187 2a47b1e5 2022-11-01 stsp
6188 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6189 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6190 39cd0ff6 2019-07-12 stsp goto done;
6191 5c1e53bc 2019-07-28 stsp }
6192 5c1e53bc 2019-07-28 stsp
6193 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6194 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6195 5c1e53bc 2019-07-28 stsp if (err)
6196 5c1e53bc 2019-07-28 stsp goto done;
6197 39cd0ff6 2019-07-12 stsp }
6198 f0b75401 2019-08-03 stsp
6199 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6200 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6201 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6202 f0b75401 2019-08-03 stsp
6203 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6204 f0b75401 2019-08-03 stsp ct_path++;
6205 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6206 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6207 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6208 b50cabdf 2019-07-12 stsp if (err)
6209 b50cabdf 2019-07-12 stsp goto done;
6210 f0b75401 2019-08-03 stsp
6211 b50cabdf 2019-07-12 stsp }
6212 b50cabdf 2019-07-12 stsp
6213 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6214 2a47b1e5 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer, diff_path,
6215 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6216 39cd0ff6 2019-07-12 stsp if (err)
6217 39cd0ff6 2019-07-12 stsp goto done;
6218 39cd0ff6 2019-07-12 stsp
6219 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6220 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6221 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6222 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6223 39cd0ff6 2019-07-12 stsp err = sync_err;
6224 39cd0ff6 2019-07-12 stsp done:
6225 39cd0ff6 2019-07-12 stsp if (fileindex)
6226 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6227 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6228 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6229 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6230 39cd0ff6 2019-07-12 stsp err = unlockerr;
6231 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6232 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6233 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6234 39cd0ff6 2019-07-12 stsp }
6235 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
6236 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6237 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6238 2a47b1e5 2022-11-01 stsp free(diff_path);
6239 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6240 2a47b1e5 2022-11-01 stsp err == NULL)
6241 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6242 c4296144 2019-05-09 stsp return err;
6243 8656d6c4 2019-05-20 stsp }
6244 8656d6c4 2019-05-20 stsp
6245 8656d6c4 2019-05-20 stsp const char *
6246 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6247 8656d6c4 2019-05-20 stsp {
6248 8656d6c4 2019-05-20 stsp return ct->path;
6249 8656d6c4 2019-05-20 stsp }
6250 8656d6c4 2019-05-20 stsp
6251 8656d6c4 2019-05-20 stsp unsigned int
6252 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6253 8656d6c4 2019-05-20 stsp {
6254 8656d6c4 2019-05-20 stsp return ct->status;
6255 818c7501 2019-07-11 stsp }
6256 818c7501 2019-07-11 stsp
6257 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6258 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6259 818c7501 2019-07-11 stsp struct got_repository *repo;
6260 818c7501 2019-07-11 stsp };
6261 818c7501 2019-07-11 stsp
6262 818c7501 2019-07-11 stsp static const struct got_error *
6263 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6264 818c7501 2019-07-11 stsp {
6265 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6266 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6267 818c7501 2019-07-11 stsp unsigned char status;
6268 818c7501 2019-07-11 stsp struct stat sb;
6269 818c7501 2019-07-11 stsp char *ondisk_path;
6270 818c7501 2019-07-11 stsp
6271 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6272 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6273 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6274 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6275 818c7501 2019-07-11 stsp
6276 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6277 818c7501 2019-07-11 stsp == -1)
6278 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6279 818c7501 2019-07-11 stsp
6280 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6281 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6282 818c7501 2019-07-11 stsp free(ondisk_path);
6283 818c7501 2019-07-11 stsp if (err)
6284 818c7501 2019-07-11 stsp return err;
6285 818c7501 2019-07-11 stsp
6286 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6287 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6288 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6289 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6290 818c7501 2019-07-11 stsp
6291 818c7501 2019-07-11 stsp return NULL;
6292 818c7501 2019-07-11 stsp }
6293 818c7501 2019-07-11 stsp
6294 818c7501 2019-07-11 stsp const struct got_error *
6295 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6296 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6297 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6298 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6299 818c7501 2019-07-11 stsp {
6300 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6301 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6302 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6303 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6304 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6305 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6306 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6307 818c7501 2019-07-11 stsp
6308 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6309 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6310 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6311 818c7501 2019-07-11 stsp
6312 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6313 818c7501 2019-07-11 stsp if (err)
6314 818c7501 2019-07-11 stsp return err;
6315 818c7501 2019-07-11 stsp
6316 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6317 818c7501 2019-07-11 stsp if (err)
6318 818c7501 2019-07-11 stsp goto done;
6319 818c7501 2019-07-11 stsp
6320 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6321 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6322 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6323 818c7501 2019-07-11 stsp &ok_arg);
6324 818c7501 2019-07-11 stsp if (err)
6325 818c7501 2019-07-11 stsp goto done;
6326 818c7501 2019-07-11 stsp
6327 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6328 818c7501 2019-07-11 stsp if (err)
6329 818c7501 2019-07-11 stsp goto done;
6330 818c7501 2019-07-11 stsp
6331 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6332 818c7501 2019-07-11 stsp if (err)
6333 818c7501 2019-07-11 stsp goto done;
6334 818c7501 2019-07-11 stsp
6335 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6336 818c7501 2019-07-11 stsp if (err)
6337 818c7501 2019-07-11 stsp goto done;
6338 818c7501 2019-07-11 stsp
6339 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6340 818c7501 2019-07-11 stsp 0);
6341 e51d7b55 2020-01-04 stsp if (err)
6342 e51d7b55 2020-01-04 stsp goto done;
6343 e51d7b55 2020-01-04 stsp
6344 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6345 818c7501 2019-07-11 stsp if (err)
6346 818c7501 2019-07-11 stsp goto done;
6347 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6348 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6349 e51d7b55 2020-01-04 stsp goto done;
6350 e51d7b55 2020-01-04 stsp }
6351 818c7501 2019-07-11 stsp
6352 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6353 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6354 818c7501 2019-07-11 stsp if (err)
6355 818c7501 2019-07-11 stsp goto done;
6356 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6357 818c7501 2019-07-11 stsp if (err)
6358 818c7501 2019-07-11 stsp goto done;
6359 818c7501 2019-07-11 stsp
6360 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6361 818c7501 2019-07-11 stsp
6362 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6363 818c7501 2019-07-11 stsp if (err)
6364 818c7501 2019-07-11 stsp goto done;
6365 818c7501 2019-07-11 stsp
6366 818c7501 2019-07-11 stsp err = got_ref_write(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 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6371 818c7501 2019-07-11 stsp worktree->base_commit_id);
6372 818c7501 2019-07-11 stsp if (err)
6373 818c7501 2019-07-11 stsp goto done;
6374 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6375 818c7501 2019-07-11 stsp if (err)
6376 818c7501 2019-07-11 stsp goto done;
6377 818c7501 2019-07-11 stsp
6378 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6379 818c7501 2019-07-11 stsp if (err)
6380 818c7501 2019-07-11 stsp goto done;
6381 818c7501 2019-07-11 stsp done:
6382 818c7501 2019-07-11 stsp free(fileindex_path);
6383 818c7501 2019-07-11 stsp free(tmp_branch_name);
6384 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6385 818c7501 2019-07-11 stsp free(branch_ref_name);
6386 818c7501 2019-07-11 stsp if (branch_ref)
6387 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6388 818c7501 2019-07-11 stsp if (wt_branch)
6389 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6390 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6391 818c7501 2019-07-11 stsp if (err) {
6392 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6393 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6394 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6395 818c7501 2019-07-11 stsp }
6396 818c7501 2019-07-11 stsp if (*tmp_branch) {
6397 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6398 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6399 818c7501 2019-07-11 stsp }
6400 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6401 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6402 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6403 3e3a69f1 2019-07-25 stsp }
6404 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6405 818c7501 2019-07-11 stsp }
6406 818c7501 2019-07-11 stsp return err;
6407 818c7501 2019-07-11 stsp }
6408 818c7501 2019-07-11 stsp
6409 818c7501 2019-07-11 stsp const struct got_error *
6410 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6411 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6412 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6413 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6414 818c7501 2019-07-11 stsp {
6415 818c7501 2019-07-11 stsp const struct got_error *err;
6416 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6417 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6418 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6419 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6420 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6421 818c7501 2019-07-11 stsp
6422 818c7501 2019-07-11 stsp *commit_id = NULL;
6423 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6424 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6425 3e3a69f1 2019-07-25 stsp *branch = NULL;
6426 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6427 3e3a69f1 2019-07-25 stsp
6428 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6429 3e3a69f1 2019-07-25 stsp if (err)
6430 3e3a69f1 2019-07-25 stsp return err;
6431 818c7501 2019-07-11 stsp
6432 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6433 3e3a69f1 2019-07-25 stsp if (err)
6434 f032f1f7 2019-08-04 stsp goto done;
6435 f032f1f7 2019-08-04 stsp
6436 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6437 f032f1f7 2019-08-04 stsp &have_staged_files);
6438 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6439 f032f1f7 2019-08-04 stsp goto done;
6440 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6441 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6442 3e3a69f1 2019-07-25 stsp goto done;
6443 f032f1f7 2019-08-04 stsp }
6444 3e3a69f1 2019-07-25 stsp
6445 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6446 818c7501 2019-07-11 stsp if (err)
6447 f032f1f7 2019-08-04 stsp goto done;
6448 818c7501 2019-07-11 stsp
6449 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6450 818c7501 2019-07-11 stsp if (err)
6451 818c7501 2019-07-11 stsp goto done;
6452 818c7501 2019-07-11 stsp
6453 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6454 818c7501 2019-07-11 stsp if (err)
6455 818c7501 2019-07-11 stsp goto done;
6456 818c7501 2019-07-11 stsp
6457 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6458 818c7501 2019-07-11 stsp if (err)
6459 818c7501 2019-07-11 stsp goto done;
6460 818c7501 2019-07-11 stsp
6461 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6462 818c7501 2019-07-11 stsp if (err)
6463 818c7501 2019-07-11 stsp goto done;
6464 818c7501 2019-07-11 stsp
6465 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6466 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6467 818c7501 2019-07-11 stsp if (err)
6468 818c7501 2019-07-11 stsp goto done;
6469 818c7501 2019-07-11 stsp
6470 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6471 818c7501 2019-07-11 stsp if (err)
6472 818c7501 2019-07-11 stsp goto done;
6473 818c7501 2019-07-11 stsp
6474 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6475 818c7501 2019-07-11 stsp if (err)
6476 818c7501 2019-07-11 stsp goto done;
6477 818c7501 2019-07-11 stsp
6478 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6479 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6480 818c7501 2019-07-11 stsp if (err)
6481 818c7501 2019-07-11 stsp goto done;
6482 818c7501 2019-07-11 stsp
6483 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6484 818c7501 2019-07-11 stsp if (err)
6485 818c7501 2019-07-11 stsp goto done;
6486 818c7501 2019-07-11 stsp done:
6487 818c7501 2019-07-11 stsp free(commit_ref_name);
6488 818c7501 2019-07-11 stsp free(branch_ref_name);
6489 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6490 818c7501 2019-07-11 stsp if (commit_ref)
6491 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6492 818c7501 2019-07-11 stsp if (branch_ref)
6493 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6494 818c7501 2019-07-11 stsp if (err) {
6495 818c7501 2019-07-11 stsp free(*commit_id);
6496 818c7501 2019-07-11 stsp *commit_id = NULL;
6497 818c7501 2019-07-11 stsp if (*tmp_branch) {
6498 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6499 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6500 818c7501 2019-07-11 stsp }
6501 818c7501 2019-07-11 stsp if (*new_base_branch) {
6502 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6503 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6504 818c7501 2019-07-11 stsp }
6505 818c7501 2019-07-11 stsp if (*branch) {
6506 818c7501 2019-07-11 stsp got_ref_close(*branch);
6507 818c7501 2019-07-11 stsp *branch = NULL;
6508 818c7501 2019-07-11 stsp }
6509 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6510 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6511 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6512 3e3a69f1 2019-07-25 stsp }
6513 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6514 818c7501 2019-07-11 stsp }
6515 818c7501 2019-07-11 stsp return err;
6516 c4296144 2019-05-09 stsp }
6517 818c7501 2019-07-11 stsp
6518 818c7501 2019-07-11 stsp const struct got_error *
6519 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6520 818c7501 2019-07-11 stsp {
6521 818c7501 2019-07-11 stsp const struct got_error *err;
6522 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6523 818c7501 2019-07-11 stsp
6524 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6525 818c7501 2019-07-11 stsp if (err)
6526 818c7501 2019-07-11 stsp return err;
6527 818c7501 2019-07-11 stsp
6528 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6529 818c7501 2019-07-11 stsp free(tmp_branch_name);
6530 818c7501 2019-07-11 stsp return NULL;
6531 818c7501 2019-07-11 stsp }
6532 818c7501 2019-07-11 stsp
6533 818c7501 2019-07-11 stsp static const struct got_error *
6534 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6535 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6536 818c7501 2019-07-11 stsp {
6537 0ebf8283 2019-07-24 stsp *logmsg = arg;
6538 818c7501 2019-07-11 stsp return NULL;
6539 818c7501 2019-07-11 stsp }
6540 818c7501 2019-07-11 stsp
6541 818c7501 2019-07-11 stsp static const struct got_error *
6542 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6543 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6544 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6545 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6546 818c7501 2019-07-11 stsp {
6547 818c7501 2019-07-11 stsp return NULL;
6548 01757395 2019-07-12 stsp }
6549 01757395 2019-07-12 stsp
6550 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6551 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6552 01757395 2019-07-12 stsp void *progress_arg;
6553 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6554 01757395 2019-07-12 stsp };
6555 01757395 2019-07-12 stsp
6556 01757395 2019-07-12 stsp static const struct got_error *
6557 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6558 01757395 2019-07-12 stsp {
6559 01757395 2019-07-12 stsp const struct got_error *err;
6560 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6561 01757395 2019-07-12 stsp char *p;
6562 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6563 01757395 2019-07-12 stsp
6564 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6565 01757395 2019-07-12 stsp if (err)
6566 01757395 2019-07-12 stsp return err;
6567 01757395 2019-07-12 stsp
6568 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6569 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6570 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6571 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6572 01757395 2019-07-12 stsp return NULL;
6573 01757395 2019-07-12 stsp
6574 01757395 2019-07-12 stsp p = strdup(path);
6575 01757395 2019-07-12 stsp if (p == NULL)
6576 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6577 01757395 2019-07-12 stsp
6578 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6579 01757395 2019-07-12 stsp if (err || new == NULL)
6580 01757395 2019-07-12 stsp free(p);
6581 01757395 2019-07-12 stsp return err;
6582 01757395 2019-07-12 stsp }
6583 01757395 2019-07-12 stsp
6584 01757395 2019-07-12 stsp void
6585 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6586 01757395 2019-07-12 stsp {
6587 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6588 01757395 2019-07-12 stsp
6589 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6590 01757395 2019-07-12 stsp free((char *)pe->path);
6591 01757395 2019-07-12 stsp
6592 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6593 818c7501 2019-07-11 stsp }
6594 818c7501 2019-07-11 stsp
6595 0ebf8283 2019-07-24 stsp static const struct got_error *
6596 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6597 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6598 818c7501 2019-07-11 stsp {
6599 818c7501 2019-07-11 stsp const struct got_error *err;
6600 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6601 818c7501 2019-07-11 stsp
6602 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6603 818c7501 2019-07-11 stsp if (err) {
6604 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6605 818c7501 2019-07-11 stsp goto done;
6606 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6607 818c7501 2019-07-11 stsp if (err)
6608 818c7501 2019-07-11 stsp goto done;
6609 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6610 818c7501 2019-07-11 stsp if (err)
6611 818c7501 2019-07-11 stsp goto done;
6612 de05890f 2020-03-05 stsp } else if (is_rebase) {
6613 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6614 818c7501 2019-07-11 stsp int cmp;
6615 818c7501 2019-07-11 stsp
6616 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6617 818c7501 2019-07-11 stsp if (err)
6618 818c7501 2019-07-11 stsp goto done;
6619 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6620 818c7501 2019-07-11 stsp free(stored_id);
6621 818c7501 2019-07-11 stsp if (cmp != 0) {
6622 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6623 818c7501 2019-07-11 stsp goto done;
6624 818c7501 2019-07-11 stsp }
6625 818c7501 2019-07-11 stsp }
6626 0ebf8283 2019-07-24 stsp done:
6627 0ebf8283 2019-07-24 stsp if (commit_ref)
6628 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6629 0ebf8283 2019-07-24 stsp return err;
6630 0ebf8283 2019-07-24 stsp }
6631 0ebf8283 2019-07-24 stsp
6632 0ebf8283 2019-07-24 stsp static const struct got_error *
6633 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6634 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6635 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6636 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6637 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6638 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6639 0ebf8283 2019-07-24 stsp {
6640 0ebf8283 2019-07-24 stsp const struct got_error *err;
6641 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6642 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6643 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6644 818c7501 2019-07-11 stsp
6645 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6646 0ebf8283 2019-07-24 stsp
6647 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6648 0ebf8283 2019-07-24 stsp if (err)
6649 0ebf8283 2019-07-24 stsp return err;
6650 0ebf8283 2019-07-24 stsp
6651 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6652 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6653 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6654 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6655 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6656 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6657 818c7501 2019-07-11 stsp if (commit_ref)
6658 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6659 818c7501 2019-07-11 stsp return err;
6660 818c7501 2019-07-11 stsp }
6661 818c7501 2019-07-11 stsp
6662 818c7501 2019-07-11 stsp const struct got_error *
6663 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6664 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6665 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6666 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6667 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6668 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6669 818c7501 2019-07-11 stsp {
6670 0ebf8283 2019-07-24 stsp const struct got_error *err;
6671 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6672 0ebf8283 2019-07-24 stsp
6673 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6674 0ebf8283 2019-07-24 stsp if (err)
6675 0ebf8283 2019-07-24 stsp return err;
6676 0ebf8283 2019-07-24 stsp
6677 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6678 0ebf8283 2019-07-24 stsp if (err)
6679 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp
6681 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6682 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6683 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6684 0ebf8283 2019-07-24 stsp done:
6685 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6686 0ebf8283 2019-07-24 stsp return err;
6687 0ebf8283 2019-07-24 stsp }
6688 0ebf8283 2019-07-24 stsp
6689 0ebf8283 2019-07-24 stsp const struct got_error *
6690 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6691 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6692 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6693 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6694 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6695 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6696 0ebf8283 2019-07-24 stsp {
6697 0ebf8283 2019-07-24 stsp const struct got_error *err;
6698 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6699 0ebf8283 2019-07-24 stsp
6700 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6701 0ebf8283 2019-07-24 stsp if (err)
6702 0ebf8283 2019-07-24 stsp return err;
6703 0ebf8283 2019-07-24 stsp
6704 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6705 0ebf8283 2019-07-24 stsp if (err)
6706 0ebf8283 2019-07-24 stsp goto done;
6707 0ebf8283 2019-07-24 stsp
6708 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6709 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6710 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6711 0ebf8283 2019-07-24 stsp done:
6712 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6713 0ebf8283 2019-07-24 stsp return err;
6714 0ebf8283 2019-07-24 stsp }
6715 0ebf8283 2019-07-24 stsp
6716 0ebf8283 2019-07-24 stsp static const struct got_error *
6717 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6718 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6719 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6720 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6721 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6722 598eac43 2022-07-22 stsp struct got_repository *repo)
6723 0ebf8283 2019-07-24 stsp {
6724 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6725 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6726 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6727 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6728 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6729 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6730 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6731 818c7501 2019-07-11 stsp
6732 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6733 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6734 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6735 a0e95631 2019-07-12 stsp
6736 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6737 818c7501 2019-07-11 stsp
6738 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6739 a0e95631 2019-07-12 stsp if (err)
6740 3e3a69f1 2019-07-25 stsp return err;
6741 a0e95631 2019-07-12 stsp
6742 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6743 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6744 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6745 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6746 01757395 2019-07-12 stsp /*
6747 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6748 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6749 6c13b005 2021-09-02 stsp *
6750 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6751 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6752 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6753 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6754 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6755 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6756 01757395 2019-07-12 stsp */
6757 01757395 2019-07-12 stsp if (merged_paths) {
6758 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6759 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6760 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6761 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6762 f2a9dc41 2019-12-13 tracey 0);
6763 01757395 2019-07-12 stsp if (err)
6764 01757395 2019-07-12 stsp goto done;
6765 01757395 2019-07-12 stsp }
6766 01757395 2019-07-12 stsp } else {
6767 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6768 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6769 01757395 2019-07-12 stsp if (err)
6770 01757395 2019-07-12 stsp goto done;
6771 01757395 2019-07-12 stsp }
6772 a0e95631 2019-07-12 stsp
6773 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6774 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6775 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6776 a0e95631 2019-07-12 stsp if (err)
6777 a0e95631 2019-07-12 stsp goto done;
6778 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6779 a0e95631 2019-07-12 stsp goto done;
6780 ff0d2220 2019-07-11 stsp }
6781 818c7501 2019-07-11 stsp
6782 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6783 edd02c5e 2019-07-11 stsp if (err)
6784 edd02c5e 2019-07-11 stsp goto done;
6785 edd02c5e 2019-07-11 stsp
6786 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6787 818c7501 2019-07-11 stsp if (err)
6788 818c7501 2019-07-11 stsp goto done;
6789 818c7501 2019-07-11 stsp
6790 5943eee2 2019-08-13 stsp if (new_logmsg) {
6791 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6792 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6793 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6794 5943eee2 2019-08-13 stsp goto done;
6795 5943eee2 2019-08-13 stsp }
6796 5943eee2 2019-08-13 stsp } else {
6797 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6798 5943eee2 2019-08-13 stsp if (err)
6799 5943eee2 2019-08-13 stsp goto done;
6800 5943eee2 2019-08-13 stsp }
6801 0ebf8283 2019-07-24 stsp
6802 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6803 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6804 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6805 50e7a649 2022-07-22 stsp committer ? committer :
6806 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6807 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6808 a0e95631 2019-07-12 stsp if (err)
6809 a0e95631 2019-07-12 stsp goto done;
6810 a0e95631 2019-07-12 stsp
6811 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6812 a0e95631 2019-07-12 stsp if (err)
6813 a0e95631 2019-07-12 stsp goto done;
6814 a0e95631 2019-07-12 stsp
6815 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6816 a0e95631 2019-07-12 stsp if (err)
6817 a0e95631 2019-07-12 stsp goto done;
6818 a0e95631 2019-07-12 stsp
6819 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6820 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6821 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6822 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6823 a0e95631 2019-07-12 stsp err = sync_err;
6824 818c7501 2019-07-11 stsp done:
6825 a0e95631 2019-07-12 stsp free(fileindex_path);
6826 a0e95631 2019-07-12 stsp free(head_commit_id);
6827 a0e95631 2019-07-12 stsp if (head_ref)
6828 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6829 818c7501 2019-07-11 stsp if (err) {
6830 818c7501 2019-07-11 stsp free(*new_commit_id);
6831 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6832 818c7501 2019-07-11 stsp }
6833 818c7501 2019-07-11 stsp return err;
6834 818c7501 2019-07-11 stsp }
6835 818c7501 2019-07-11 stsp
6836 818c7501 2019-07-11 stsp const struct got_error *
6837 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6838 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6839 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6840 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6841 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6842 0ebf8283 2019-07-24 stsp {
6843 0ebf8283 2019-07-24 stsp const struct got_error *err;
6844 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6845 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6846 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6847 0ebf8283 2019-07-24 stsp
6848 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6849 0ebf8283 2019-07-24 stsp if (err)
6850 0ebf8283 2019-07-24 stsp return err;
6851 0ebf8283 2019-07-24 stsp
6852 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6853 0ebf8283 2019-07-24 stsp if (err)
6854 0ebf8283 2019-07-24 stsp goto done;
6855 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6856 0ebf8283 2019-07-24 stsp if (err)
6857 0ebf8283 2019-07-24 stsp goto done;
6858 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6859 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6860 0ebf8283 2019-07-24 stsp goto done;
6861 0ebf8283 2019-07-24 stsp }
6862 0ebf8283 2019-07-24 stsp
6863 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6864 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6865 598eac43 2022-07-22 stsp NULL, repo);
6866 0ebf8283 2019-07-24 stsp done:
6867 0ebf8283 2019-07-24 stsp if (commit_ref)
6868 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6869 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6870 0ebf8283 2019-07-24 stsp free(commit_id);
6871 0ebf8283 2019-07-24 stsp return err;
6872 0ebf8283 2019-07-24 stsp }
6873 0ebf8283 2019-07-24 stsp
6874 0ebf8283 2019-07-24 stsp const struct got_error *
6875 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6876 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6877 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6878 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6879 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6880 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6881 0ebf8283 2019-07-24 stsp {
6882 0ebf8283 2019-07-24 stsp const struct got_error *err;
6883 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6884 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6885 0ebf8283 2019-07-24 stsp
6886 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6887 0ebf8283 2019-07-24 stsp if (err)
6888 0ebf8283 2019-07-24 stsp return err;
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6891 0ebf8283 2019-07-24 stsp if (err)
6892 0ebf8283 2019-07-24 stsp goto done;
6893 0ebf8283 2019-07-24 stsp
6894 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6895 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6896 598eac43 2022-07-22 stsp new_logmsg, repo);
6897 0ebf8283 2019-07-24 stsp done:
6898 0ebf8283 2019-07-24 stsp if (commit_ref)
6899 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6900 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6901 0ebf8283 2019-07-24 stsp return err;
6902 0ebf8283 2019-07-24 stsp }
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp const struct got_error *
6905 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6906 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6907 818c7501 2019-07-11 stsp {
6908 3e3a69f1 2019-07-25 stsp if (fileindex)
6909 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6910 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6911 69844fba 2019-07-11 stsp }
6912 69844fba 2019-07-11 stsp
6913 69844fba 2019-07-11 stsp static const struct got_error *
6914 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6915 69844fba 2019-07-11 stsp {
6916 69844fba 2019-07-11 stsp const struct got_error *err;
6917 69844fba 2019-07-11 stsp struct got_reference *ref;
6918 69844fba 2019-07-11 stsp
6919 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6920 69844fba 2019-07-11 stsp if (err) {
6921 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6922 69844fba 2019-07-11 stsp return NULL;
6923 69844fba 2019-07-11 stsp return err;
6924 69844fba 2019-07-11 stsp }
6925 69844fba 2019-07-11 stsp
6926 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6927 69844fba 2019-07-11 stsp got_ref_close(ref);
6928 69844fba 2019-07-11 stsp return err;
6929 818c7501 2019-07-11 stsp }
6930 818c7501 2019-07-11 stsp
6931 69844fba 2019-07-11 stsp static const struct got_error *
6932 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6933 69844fba 2019-07-11 stsp {
6934 69844fba 2019-07-11 stsp const struct got_error *err;
6935 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6936 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6937 69844fba 2019-07-11 stsp
6938 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6939 69844fba 2019-07-11 stsp if (err)
6940 69844fba 2019-07-11 stsp goto done;
6941 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6942 69844fba 2019-07-11 stsp if (err)
6943 69844fba 2019-07-11 stsp goto done;
6944 69844fba 2019-07-11 stsp
6945 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6946 69844fba 2019-07-11 stsp if (err)
6947 69844fba 2019-07-11 stsp goto done;
6948 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6949 69844fba 2019-07-11 stsp if (err)
6950 69844fba 2019-07-11 stsp goto done;
6951 69844fba 2019-07-11 stsp
6952 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6953 69844fba 2019-07-11 stsp if (err)
6954 69844fba 2019-07-11 stsp goto done;
6955 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6956 69844fba 2019-07-11 stsp if (err)
6957 69844fba 2019-07-11 stsp goto done;
6958 69844fba 2019-07-11 stsp
6959 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6960 69844fba 2019-07-11 stsp if (err)
6961 69844fba 2019-07-11 stsp goto done;
6962 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6963 69844fba 2019-07-11 stsp if (err)
6964 69844fba 2019-07-11 stsp goto done;
6965 69844fba 2019-07-11 stsp
6966 69844fba 2019-07-11 stsp done:
6967 69844fba 2019-07-11 stsp free(tmp_branch_name);
6968 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6969 69844fba 2019-07-11 stsp free(branch_ref_name);
6970 69844fba 2019-07-11 stsp free(commit_ref_name);
6971 e600f124 2021-03-21 stsp return err;
6972 e600f124 2021-03-21 stsp }
6973 e600f124 2021-03-21 stsp
6974 336075a4 2022-06-25 op static const struct got_error *
6975 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6976 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6977 e600f124 2021-03-21 stsp {
6978 e600f124 2021-03-21 stsp const struct got_error *err;
6979 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6980 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6981 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6982 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6983 e600f124 2021-03-21 stsp char *refname = NULL;
6984 e600f124 2021-03-21 stsp
6985 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6986 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6987 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6988 e600f124 2021-03-21 stsp branch_name += 11;
6989 e600f124 2021-03-21 stsp
6990 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6991 e600f124 2021-03-21 stsp if (err)
6992 e600f124 2021-03-21 stsp return err;
6993 e600f124 2021-03-21 stsp
6994 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6995 e600f124 2021-03-21 stsp new_id_str) == -1) {
6996 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6997 e600f124 2021-03-21 stsp goto done;
6998 e600f124 2021-03-21 stsp }
6999 e600f124 2021-03-21 stsp
7000 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7001 e600f124 2021-03-21 stsp if (err)
7002 e600f124 2021-03-21 stsp goto done;
7003 e600f124 2021-03-21 stsp
7004 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7005 e600f124 2021-03-21 stsp if (err)
7006 e600f124 2021-03-21 stsp goto done;
7007 e600f124 2021-03-21 stsp
7008 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7009 e600f124 2021-03-21 stsp done:
7010 e600f124 2021-03-21 stsp free(new_id_str);
7011 e600f124 2021-03-21 stsp free(refname);
7012 e600f124 2021-03-21 stsp free(old_commit_id);
7013 e600f124 2021-03-21 stsp if (ref)
7014 e600f124 2021-03-21 stsp got_ref_close(ref);
7015 69844fba 2019-07-11 stsp return err;
7016 69844fba 2019-07-11 stsp }
7017 69844fba 2019-07-11 stsp
7018 818c7501 2019-07-11 stsp const struct got_error *
7019 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7020 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7021 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7022 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
7023 818c7501 2019-07-11 stsp {
7024 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7025 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7026 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7027 818c7501 2019-07-11 stsp
7028 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7029 818c7501 2019-07-11 stsp if (err)
7030 818c7501 2019-07-11 stsp return err;
7031 e600f124 2021-03-21 stsp
7032 e600f124 2021-03-21 stsp if (create_backup) {
7033 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7034 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7035 e600f124 2021-03-21 stsp if (err)
7036 e600f124 2021-03-21 stsp goto done;
7037 e600f124 2021-03-21 stsp }
7038 818c7501 2019-07-11 stsp
7039 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7040 818c7501 2019-07-11 stsp if (err)
7041 818c7501 2019-07-11 stsp goto done;
7042 818c7501 2019-07-11 stsp
7043 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7044 818c7501 2019-07-11 stsp if (err)
7045 818c7501 2019-07-11 stsp goto done;
7046 818c7501 2019-07-11 stsp
7047 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7048 818c7501 2019-07-11 stsp if (err)
7049 818c7501 2019-07-11 stsp goto done;
7050 818c7501 2019-07-11 stsp
7051 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7052 a615e0e7 2020-12-16 stsp if (err)
7053 a615e0e7 2020-12-16 stsp goto done;
7054 a615e0e7 2020-12-16 stsp
7055 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7056 a615e0e7 2020-12-16 stsp if (err)
7057 a615e0e7 2020-12-16 stsp goto done;
7058 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7059 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7060 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7061 a615e0e7 2020-12-16 stsp err = sync_err;
7062 818c7501 2019-07-11 stsp done:
7063 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7064 a615e0e7 2020-12-16 stsp free(fileindex_path);
7065 818c7501 2019-07-11 stsp free(new_head_commit_id);
7066 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7067 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7068 818c7501 2019-07-11 stsp err = unlockerr;
7069 818c7501 2019-07-11 stsp return err;
7070 818c7501 2019-07-11 stsp }
7071 818c7501 2019-07-11 stsp
7072 818c7501 2019-07-11 stsp const struct got_error *
7073 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7074 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7075 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7076 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7077 818c7501 2019-07-11 stsp {
7078 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7079 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7080 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7081 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7082 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7083 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7084 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7085 818c7501 2019-07-11 stsp
7086 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7087 818c7501 2019-07-11 stsp if (err)
7088 818c7501 2019-07-11 stsp return err;
7089 818c7501 2019-07-11 stsp
7090 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7091 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7092 a44927cc 2022-04-07 stsp if (err)
7093 a44927cc 2022-04-07 stsp goto done;
7094 a44927cc 2022-04-07 stsp
7095 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7096 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7097 818c7501 2019-07-11 stsp if (err)
7098 818c7501 2019-07-11 stsp goto done;
7099 818c7501 2019-07-11 stsp
7100 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7101 818c7501 2019-07-11 stsp if (err)
7102 818c7501 2019-07-11 stsp goto done;
7103 818c7501 2019-07-11 stsp
7104 818c7501 2019-07-11 stsp /*
7105 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7106 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7107 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7108 818c7501 2019-07-11 stsp */
7109 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7110 818c7501 2019-07-11 stsp if (err)
7111 818c7501 2019-07-11 stsp goto done;
7112 818c7501 2019-07-11 stsp
7113 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7114 818c7501 2019-07-11 stsp if (err)
7115 818c7501 2019-07-11 stsp goto done;
7116 818c7501 2019-07-11 stsp
7117 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7118 a44927cc 2022-04-07 stsp worktree->path_prefix);
7119 ca355955 2019-07-12 stsp if (err)
7120 ca355955 2019-07-12 stsp goto done;
7121 ca355955 2019-07-12 stsp
7122 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7123 ca355955 2019-07-12 stsp if (err)
7124 ca355955 2019-07-12 stsp goto done;
7125 ca355955 2019-07-12 stsp
7126 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7127 818c7501 2019-07-11 stsp if (err)
7128 818c7501 2019-07-11 stsp goto done;
7129 818c7501 2019-07-11 stsp
7130 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7131 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7132 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7133 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7134 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7135 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7136 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7137 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7138 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7139 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7140 55bd499d 2019-07-12 stsp if (err)
7141 1f1abb7e 2019-08-08 stsp goto sync;
7142 55bd499d 2019-07-12 stsp
7143 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7144 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7145 ca355955 2019-07-12 stsp sync:
7146 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7147 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7148 ca355955 2019-07-12 stsp err = sync_err;
7149 818c7501 2019-07-11 stsp done:
7150 818c7501 2019-07-11 stsp got_ref_close(resolved);
7151 a3a2faf2 2019-07-12 stsp free(tree_id);
7152 818c7501 2019-07-11 stsp free(commit_id);
7153 a44927cc 2022-04-07 stsp if (commit)
7154 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7155 0ebf8283 2019-07-24 stsp if (fileindex)
7156 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7157 0ebf8283 2019-07-24 stsp free(fileindex_path);
7158 0ebf8283 2019-07-24 stsp
7159 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7160 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7161 0ebf8283 2019-07-24 stsp err = unlockerr;
7162 0ebf8283 2019-07-24 stsp return err;
7163 0ebf8283 2019-07-24 stsp }
7164 0ebf8283 2019-07-24 stsp
7165 0ebf8283 2019-07-24 stsp const struct got_error *
7166 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7167 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7168 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7169 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7170 0ebf8283 2019-07-24 stsp {
7171 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7172 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7173 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7174 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7175 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7176 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7177 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7178 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7179 0ebf8283 2019-07-24 stsp
7180 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7181 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7182 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7183 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7184 0ebf8283 2019-07-24 stsp
7185 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7186 0ebf8283 2019-07-24 stsp if (err)
7187 0ebf8283 2019-07-24 stsp return err;
7188 0ebf8283 2019-07-24 stsp
7189 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7190 0ebf8283 2019-07-24 stsp if (err)
7191 0ebf8283 2019-07-24 stsp goto done;
7192 0ebf8283 2019-07-24 stsp
7193 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7194 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7195 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7196 0ebf8283 2019-07-24 stsp &ok_arg);
7197 0ebf8283 2019-07-24 stsp if (err)
7198 0ebf8283 2019-07-24 stsp goto done;
7199 0ebf8283 2019-07-24 stsp
7200 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7201 0ebf8283 2019-07-24 stsp if (err)
7202 0ebf8283 2019-07-24 stsp goto done;
7203 0ebf8283 2019-07-24 stsp
7204 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7205 0ebf8283 2019-07-24 stsp if (err)
7206 0ebf8283 2019-07-24 stsp goto done;
7207 0ebf8283 2019-07-24 stsp
7208 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7209 0ebf8283 2019-07-24 stsp worktree);
7210 0ebf8283 2019-07-24 stsp if (err)
7211 0ebf8283 2019-07-24 stsp goto done;
7212 0ebf8283 2019-07-24 stsp
7213 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7214 0ebf8283 2019-07-24 stsp 0);
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 = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7219 0ebf8283 2019-07-24 stsp if (err)
7220 0ebf8283 2019-07-24 stsp goto done;
7221 0ebf8283 2019-07-24 stsp
7222 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7223 0ebf8283 2019-07-24 stsp if (err)
7224 0ebf8283 2019-07-24 stsp goto done;
7225 0ebf8283 2019-07-24 stsp
7226 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7227 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7228 0ebf8283 2019-07-24 stsp if (err)
7229 0ebf8283 2019-07-24 stsp goto done;
7230 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7231 0ebf8283 2019-07-24 stsp if (err)
7232 0ebf8283 2019-07-24 stsp goto done;
7233 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7234 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7235 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7236 0ebf8283 2019-07-24 stsp goto done;
7237 0ebf8283 2019-07-24 stsp }
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7240 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7241 0ebf8283 2019-07-24 stsp if (err)
7242 0ebf8283 2019-07-24 stsp goto done;
7243 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7244 0ebf8283 2019-07-24 stsp if (err)
7245 0ebf8283 2019-07-24 stsp goto done;
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7248 0ebf8283 2019-07-24 stsp if (err)
7249 0ebf8283 2019-07-24 stsp goto done;
7250 0ebf8283 2019-07-24 stsp done:
7251 0ebf8283 2019-07-24 stsp free(fileindex_path);
7252 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7253 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7254 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7255 0ebf8283 2019-07-24 stsp if (wt_branch)
7256 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7257 0ebf8283 2019-07-24 stsp if (err) {
7258 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7259 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7260 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7261 0ebf8283 2019-07-24 stsp }
7262 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7263 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7264 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7265 0ebf8283 2019-07-24 stsp }
7266 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7267 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7268 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7269 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7270 3e3a69f1 2019-07-25 stsp }
7271 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7272 0ebf8283 2019-07-24 stsp }
7273 0ebf8283 2019-07-24 stsp return err;
7274 0ebf8283 2019-07-24 stsp }
7275 0ebf8283 2019-07-24 stsp
7276 0ebf8283 2019-07-24 stsp const struct got_error *
7277 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7278 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7279 0ebf8283 2019-07-24 stsp {
7280 3e3a69f1 2019-07-25 stsp if (fileindex)
7281 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7282 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7283 0ebf8283 2019-07-24 stsp }
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp const struct got_error *
7286 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7287 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7288 0ebf8283 2019-07-24 stsp {
7289 0ebf8283 2019-07-24 stsp const struct got_error *err;
7290 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7291 0ebf8283 2019-07-24 stsp
7292 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7293 0ebf8283 2019-07-24 stsp if (err)
7294 0ebf8283 2019-07-24 stsp return err;
7295 0ebf8283 2019-07-24 stsp
7296 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7297 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7298 0ebf8283 2019-07-24 stsp return NULL;
7299 0ebf8283 2019-07-24 stsp }
7300 0ebf8283 2019-07-24 stsp
7301 0ebf8283 2019-07-24 stsp const struct got_error *
7302 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7303 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7304 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7305 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7306 0ebf8283 2019-07-24 stsp {
7307 0ebf8283 2019-07-24 stsp const struct got_error *err;
7308 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7309 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7310 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7311 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7312 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7313 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7314 0ebf8283 2019-07-24 stsp
7315 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7316 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7317 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7318 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7319 0ebf8283 2019-07-24 stsp
7320 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7321 3e3a69f1 2019-07-25 stsp if (err)
7322 3e3a69f1 2019-07-25 stsp return err;
7323 3e3a69f1 2019-07-25 stsp
7324 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7325 3e3a69f1 2019-07-25 stsp if (err)
7326 f032f1f7 2019-08-04 stsp goto done;
7327 f032f1f7 2019-08-04 stsp
7328 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7329 f032f1f7 2019-08-04 stsp &have_staged_files);
7330 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7331 f032f1f7 2019-08-04 stsp goto done;
7332 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7333 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7334 3e3a69f1 2019-07-25 stsp goto done;
7335 f032f1f7 2019-08-04 stsp }
7336 3e3a69f1 2019-07-25 stsp
7337 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7338 0ebf8283 2019-07-24 stsp if (err)
7339 f032f1f7 2019-08-04 stsp goto done;
7340 0ebf8283 2019-07-24 stsp
7341 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7342 0ebf8283 2019-07-24 stsp if (err)
7343 0ebf8283 2019-07-24 stsp goto done;
7344 0ebf8283 2019-07-24 stsp
7345 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7346 0ebf8283 2019-07-24 stsp if (err)
7347 0ebf8283 2019-07-24 stsp goto done;
7348 0ebf8283 2019-07-24 stsp
7349 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7350 0ebf8283 2019-07-24 stsp worktree);
7351 0ebf8283 2019-07-24 stsp if (err)
7352 0ebf8283 2019-07-24 stsp goto done;
7353 0ebf8283 2019-07-24 stsp
7354 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7355 0ebf8283 2019-07-24 stsp if (err)
7356 0ebf8283 2019-07-24 stsp goto done;
7357 0ebf8283 2019-07-24 stsp
7358 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7359 0ebf8283 2019-07-24 stsp if (err)
7360 0ebf8283 2019-07-24 stsp goto done;
7361 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7362 0ebf8283 2019-07-24 stsp if (err)
7363 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp
7365 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7366 0ebf8283 2019-07-24 stsp if (err)
7367 0ebf8283 2019-07-24 stsp goto done;
7368 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7369 0ebf8283 2019-07-24 stsp if (err)
7370 0ebf8283 2019-07-24 stsp goto done;
7371 0ebf8283 2019-07-24 stsp
7372 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7373 0ebf8283 2019-07-24 stsp if (err)
7374 0ebf8283 2019-07-24 stsp goto done;
7375 0ebf8283 2019-07-24 stsp done:
7376 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7377 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7378 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7379 0ebf8283 2019-07-24 stsp if (commit_ref)
7380 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7381 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7382 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7383 0ebf8283 2019-07-24 stsp if (err) {
7384 0ebf8283 2019-07-24 stsp free(*commit_id);
7385 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7386 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7387 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7388 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7389 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7390 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7391 0ebf8283 2019-07-24 stsp }
7392 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7393 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7394 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7395 3e3a69f1 2019-07-25 stsp }
7396 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7397 0ebf8283 2019-07-24 stsp }
7398 0ebf8283 2019-07-24 stsp return err;
7399 0ebf8283 2019-07-24 stsp }
7400 0ebf8283 2019-07-24 stsp
7401 0ebf8283 2019-07-24 stsp static const struct got_error *
7402 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7403 0ebf8283 2019-07-24 stsp {
7404 0ebf8283 2019-07-24 stsp const struct got_error *err;
7405 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7406 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7407 0ebf8283 2019-07-24 stsp
7408 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7409 0ebf8283 2019-07-24 stsp if (err)
7410 0ebf8283 2019-07-24 stsp goto done;
7411 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7412 0ebf8283 2019-07-24 stsp if (err)
7413 0ebf8283 2019-07-24 stsp goto done;
7414 0ebf8283 2019-07-24 stsp
7415 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7416 0ebf8283 2019-07-24 stsp worktree);
7417 0ebf8283 2019-07-24 stsp if (err)
7418 0ebf8283 2019-07-24 stsp goto done;
7419 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7420 0ebf8283 2019-07-24 stsp if (err)
7421 0ebf8283 2019-07-24 stsp goto done;
7422 0ebf8283 2019-07-24 stsp
7423 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7424 0ebf8283 2019-07-24 stsp if (err)
7425 0ebf8283 2019-07-24 stsp goto done;
7426 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7427 0ebf8283 2019-07-24 stsp if (err)
7428 0ebf8283 2019-07-24 stsp goto done;
7429 0ebf8283 2019-07-24 stsp
7430 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7431 0ebf8283 2019-07-24 stsp if (err)
7432 0ebf8283 2019-07-24 stsp goto done;
7433 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7434 0ebf8283 2019-07-24 stsp if (err)
7435 0ebf8283 2019-07-24 stsp goto done;
7436 0ebf8283 2019-07-24 stsp done:
7437 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7438 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7439 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7440 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7441 0ebf8283 2019-07-24 stsp return err;
7442 0ebf8283 2019-07-24 stsp }
7443 0ebf8283 2019-07-24 stsp
7444 0ebf8283 2019-07-24 stsp const struct got_error *
7445 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7446 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7447 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7448 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7449 0ebf8283 2019-07-24 stsp {
7450 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7451 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7452 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7453 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7454 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7455 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7456 0ebf8283 2019-07-24 stsp
7457 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7458 0ebf8283 2019-07-24 stsp if (err)
7459 0ebf8283 2019-07-24 stsp return err;
7460 0ebf8283 2019-07-24 stsp
7461 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7462 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7463 a44927cc 2022-04-07 stsp if (err)
7464 a44927cc 2022-04-07 stsp goto done;
7465 a44927cc 2022-04-07 stsp
7466 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7467 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7468 0ebf8283 2019-07-24 stsp if (err)
7469 0ebf8283 2019-07-24 stsp goto done;
7470 0ebf8283 2019-07-24 stsp
7471 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7472 0ebf8283 2019-07-24 stsp if (err)
7473 0ebf8283 2019-07-24 stsp goto done;
7474 0ebf8283 2019-07-24 stsp
7475 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7476 0ebf8283 2019-07-24 stsp if (err)
7477 0ebf8283 2019-07-24 stsp goto done;
7478 0ebf8283 2019-07-24 stsp
7479 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7480 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7481 0ebf8283 2019-07-24 stsp if (err)
7482 0ebf8283 2019-07-24 stsp goto done;
7483 0ebf8283 2019-07-24 stsp
7484 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7485 0ebf8283 2019-07-24 stsp if (err)
7486 0ebf8283 2019-07-24 stsp goto done;
7487 0ebf8283 2019-07-24 stsp
7488 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7489 0ebf8283 2019-07-24 stsp if (err)
7490 0ebf8283 2019-07-24 stsp goto done;
7491 0ebf8283 2019-07-24 stsp
7492 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7493 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7494 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7495 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7496 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7497 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7498 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7499 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7500 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7501 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7502 0ebf8283 2019-07-24 stsp if (err)
7503 1f1abb7e 2019-08-08 stsp goto sync;
7504 0ebf8283 2019-07-24 stsp
7505 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7506 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7507 0ebf8283 2019-07-24 stsp sync:
7508 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7509 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7510 0ebf8283 2019-07-24 stsp err = sync_err;
7511 0ebf8283 2019-07-24 stsp done:
7512 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7513 0ebf8283 2019-07-24 stsp free(tree_id);
7514 818c7501 2019-07-11 stsp free(fileindex_path);
7515 818c7501 2019-07-11 stsp
7516 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7517 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7518 818c7501 2019-07-11 stsp err = unlockerr;
7519 818c7501 2019-07-11 stsp return err;
7520 818c7501 2019-07-11 stsp }
7521 0ebf8283 2019-07-24 stsp
7522 0ebf8283 2019-07-24 stsp const struct got_error *
7523 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7524 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7525 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7526 0ebf8283 2019-07-24 stsp {
7527 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7528 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7529 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7530 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7531 0ebf8283 2019-07-24 stsp
7532 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7533 0ebf8283 2019-07-24 stsp if (err)
7534 0ebf8283 2019-07-24 stsp return err;
7535 0ebf8283 2019-07-24 stsp
7536 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7537 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7538 e600f124 2021-03-21 stsp if (err)
7539 e600f124 2021-03-21 stsp goto done;
7540 e600f124 2021-03-21 stsp
7541 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7542 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7543 0ebf8283 2019-07-24 stsp if (err)
7544 0ebf8283 2019-07-24 stsp goto done;
7545 0ebf8283 2019-07-24 stsp
7546 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7547 0ebf8283 2019-07-24 stsp if (err)
7548 0ebf8283 2019-07-24 stsp goto done;
7549 0ebf8283 2019-07-24 stsp
7550 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7551 0ebf8283 2019-07-24 stsp if (err)
7552 0ebf8283 2019-07-24 stsp goto done;
7553 0ebf8283 2019-07-24 stsp
7554 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7555 0ebf8283 2019-07-24 stsp if (err)
7556 0ebf8283 2019-07-24 stsp goto done;
7557 0ebf8283 2019-07-24 stsp
7558 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7559 a615e0e7 2020-12-16 stsp if (err)
7560 a615e0e7 2020-12-16 stsp goto done;
7561 a615e0e7 2020-12-16 stsp
7562 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7563 a615e0e7 2020-12-16 stsp if (err)
7564 a615e0e7 2020-12-16 stsp goto done;
7565 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7566 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7567 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7568 a615e0e7 2020-12-16 stsp err = sync_err;
7569 0ebf8283 2019-07-24 stsp done:
7570 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7571 a615e0e7 2020-12-16 stsp free(fileindex_path);
7572 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7573 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7574 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7575 0ebf8283 2019-07-24 stsp err = unlockerr;
7576 0ebf8283 2019-07-24 stsp return err;
7577 0ebf8283 2019-07-24 stsp }
7578 0ebf8283 2019-07-24 stsp
7579 0ebf8283 2019-07-24 stsp const struct got_error *
7580 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7581 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7582 0ebf8283 2019-07-24 stsp {
7583 0ebf8283 2019-07-24 stsp const struct got_error *err;
7584 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7585 0ebf8283 2019-07-24 stsp
7586 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7587 0ebf8283 2019-07-24 stsp if (err)
7588 0ebf8283 2019-07-24 stsp return err;
7589 0ebf8283 2019-07-24 stsp
7590 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7591 0ebf8283 2019-07-24 stsp if (err)
7592 0ebf8283 2019-07-24 stsp goto done;
7593 0ebf8283 2019-07-24 stsp
7594 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7595 0ebf8283 2019-07-24 stsp done:
7596 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7597 2822a352 2019-10-15 stsp return err;
7598 2822a352 2019-10-15 stsp }
7599 2822a352 2019-10-15 stsp
7600 2822a352 2019-10-15 stsp const struct got_error *
7601 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7602 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7603 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7604 2822a352 2019-10-15 stsp struct got_repository *repo)
7605 2822a352 2019-10-15 stsp {
7606 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7607 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7608 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7609 2822a352 2019-10-15 stsp
7610 2822a352 2019-10-15 stsp *fileindex = NULL;
7611 2822a352 2019-10-15 stsp *branch_ref = NULL;
7612 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7613 2822a352 2019-10-15 stsp
7614 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7615 2822a352 2019-10-15 stsp if (err)
7616 2822a352 2019-10-15 stsp return err;
7617 2822a352 2019-10-15 stsp
7618 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7619 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7620 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7621 2822a352 2019-10-15 stsp "update -b or different branch name required");
7622 2822a352 2019-10-15 stsp goto done;
7623 2822a352 2019-10-15 stsp }
7624 2822a352 2019-10-15 stsp
7625 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7626 2822a352 2019-10-15 stsp if (err)
7627 2822a352 2019-10-15 stsp goto done;
7628 2822a352 2019-10-15 stsp
7629 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7630 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7631 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7632 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7633 2822a352 2019-10-15 stsp &ok_arg);
7634 2822a352 2019-10-15 stsp if (err)
7635 2822a352 2019-10-15 stsp goto done;
7636 2822a352 2019-10-15 stsp
7637 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7638 2822a352 2019-10-15 stsp if (err)
7639 2822a352 2019-10-15 stsp goto done;
7640 2822a352 2019-10-15 stsp
7641 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7642 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7643 2822a352 2019-10-15 stsp done:
7644 2822a352 2019-10-15 stsp if (err) {
7645 2822a352 2019-10-15 stsp if (*branch_ref) {
7646 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7647 2822a352 2019-10-15 stsp *branch_ref = NULL;
7648 2822a352 2019-10-15 stsp }
7649 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7650 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7651 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7652 2822a352 2019-10-15 stsp }
7653 2822a352 2019-10-15 stsp if (*fileindex) {
7654 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7655 2822a352 2019-10-15 stsp *fileindex = NULL;
7656 2822a352 2019-10-15 stsp }
7657 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7658 2822a352 2019-10-15 stsp }
7659 0cb83759 2019-08-03 stsp return err;
7660 0cb83759 2019-08-03 stsp }
7661 0cb83759 2019-08-03 stsp
7662 2822a352 2019-10-15 stsp const struct got_error *
7663 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7664 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7665 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7666 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7667 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7668 2822a352 2019-10-15 stsp {
7669 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7670 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7671 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7672 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7673 2822a352 2019-10-15 stsp
7674 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7675 2822a352 2019-10-15 stsp if (err)
7676 2822a352 2019-10-15 stsp goto done;
7677 2822a352 2019-10-15 stsp
7678 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7679 2822a352 2019-10-15 stsp if (err)
7680 2822a352 2019-10-15 stsp goto done;
7681 2822a352 2019-10-15 stsp
7682 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7683 a44927cc 2022-04-07 stsp if (err)
7684 a44927cc 2022-04-07 stsp goto done;
7685 a44927cc 2022-04-07 stsp
7686 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7687 2822a352 2019-10-15 stsp worktree->path_prefix);
7688 2822a352 2019-10-15 stsp if (err)
7689 2822a352 2019-10-15 stsp goto done;
7690 2822a352 2019-10-15 stsp
7691 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7692 2822a352 2019-10-15 stsp if (err)
7693 2822a352 2019-10-15 stsp goto done;
7694 2822a352 2019-10-15 stsp
7695 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7696 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7697 2822a352 2019-10-15 stsp if (err)
7698 2822a352 2019-10-15 stsp goto sync;
7699 2822a352 2019-10-15 stsp
7700 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7701 2822a352 2019-10-15 stsp if (err)
7702 2822a352 2019-10-15 stsp goto sync;
7703 2822a352 2019-10-15 stsp
7704 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7705 6e210706 2021-01-22 stsp if (err)
7706 6e210706 2021-01-22 stsp goto sync;
7707 6e210706 2021-01-22 stsp
7708 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7709 2822a352 2019-10-15 stsp sync:
7710 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7711 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7712 2822a352 2019-10-15 stsp err = sync_err;
7713 2822a352 2019-10-15 stsp
7714 2822a352 2019-10-15 stsp done:
7715 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7716 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7717 2822a352 2019-10-15 stsp err = unlockerr;
7718 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7719 2822a352 2019-10-15 stsp
7720 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7721 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7722 2822a352 2019-10-15 stsp err = unlockerr;
7723 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7724 2822a352 2019-10-15 stsp
7725 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7726 2822a352 2019-10-15 stsp free(fileindex_path);
7727 2822a352 2019-10-15 stsp free(tree_id);
7728 a44927cc 2022-04-07 stsp if (commit)
7729 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7730 2822a352 2019-10-15 stsp
7731 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7732 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7733 2822a352 2019-10-15 stsp err = unlockerr;
7734 2822a352 2019-10-15 stsp return err;
7735 2822a352 2019-10-15 stsp }
7736 2822a352 2019-10-15 stsp
7737 2822a352 2019-10-15 stsp const struct got_error *
7738 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7739 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7740 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7741 2822a352 2019-10-15 stsp {
7742 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7743 8b692cd0 2019-10-21 stsp
7744 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7745 8b692cd0 2019-10-21 stsp
7746 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7747 8b692cd0 2019-10-21 stsp
7748 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7749 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7750 8b692cd0 2019-10-21 stsp err = unlockerr;
7751 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7752 8b692cd0 2019-10-21 stsp
7753 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7754 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7755 8b692cd0 2019-10-21 stsp err = unlockerr;
7756 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7757 f259c4c1 2021-09-24 stsp
7758 f259c4c1 2021-09-24 stsp return err;
7759 f259c4c1 2021-09-24 stsp }
7760 f259c4c1 2021-09-24 stsp
7761 f259c4c1 2021-09-24 stsp const struct got_error *
7762 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7763 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7764 f259c4c1 2021-09-24 stsp {
7765 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7766 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7767 f259c4c1 2021-09-24 stsp
7768 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7769 f259c4c1 2021-09-24 stsp if (err)
7770 f259c4c1 2021-09-24 stsp goto done;
7771 8b692cd0 2019-10-21 stsp
7772 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7773 f259c4c1 2021-09-24 stsp
7774 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7775 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7776 f259c4c1 2021-09-24 stsp err = sync_err;
7777 f259c4c1 2021-09-24 stsp done:
7778 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7779 f259c4c1 2021-09-24 stsp free(fileindex_path);
7780 8b692cd0 2019-10-21 stsp return err;
7781 2822a352 2019-10-15 stsp }
7782 2822a352 2019-10-15 stsp
7783 f259c4c1 2021-09-24 stsp static const struct got_error *
7784 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7785 f259c4c1 2021-09-24 stsp {
7786 f259c4c1 2021-09-24 stsp const struct got_error *err;
7787 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7788 f259c4c1 2021-09-24 stsp
7789 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7790 f259c4c1 2021-09-24 stsp if (err)
7791 f259c4c1 2021-09-24 stsp goto done;
7792 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7793 f259c4c1 2021-09-24 stsp if (err)
7794 f259c4c1 2021-09-24 stsp goto done;
7795 f259c4c1 2021-09-24 stsp
7796 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7797 f259c4c1 2021-09-24 stsp if (err)
7798 f259c4c1 2021-09-24 stsp goto done;
7799 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7800 f259c4c1 2021-09-24 stsp if (err)
7801 f259c4c1 2021-09-24 stsp goto done;
7802 f259c4c1 2021-09-24 stsp
7803 f259c4c1 2021-09-24 stsp done:
7804 f259c4c1 2021-09-24 stsp free(branch_refname);
7805 f259c4c1 2021-09-24 stsp free(commit_refname);
7806 f259c4c1 2021-09-24 stsp return err;
7807 f259c4c1 2021-09-24 stsp }
7808 f259c4c1 2021-09-24 stsp
7809 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7810 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7811 f259c4c1 2021-09-24 stsp const char *branch_name;
7812 f259c4c1 2021-09-24 stsp };
7813 f259c4c1 2021-09-24 stsp
7814 f259c4c1 2021-09-24 stsp static const struct got_error *
7815 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7816 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7817 f259c4c1 2021-09-24 stsp {
7818 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7819 f259c4c1 2021-09-24 stsp
7820 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7821 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7822 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7823 f259c4c1 2021-09-24 stsp
7824 f259c4c1 2021-09-24 stsp return NULL;
7825 f259c4c1 2021-09-24 stsp }
7826 f259c4c1 2021-09-24 stsp
7827 f259c4c1 2021-09-24 stsp
7828 f259c4c1 2021-09-24 stsp const struct got_error *
7829 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7830 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7831 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7832 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7833 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7834 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7835 f259c4c1 2021-09-24 stsp {
7836 f259c4c1 2021-09-24 stsp const struct got_error *err;
7837 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7838 f259c4c1 2021-09-24 stsp
7839 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7840 f259c4c1 2021-09-24 stsp if (err)
7841 f259c4c1 2021-09-24 stsp goto done;
7842 f259c4c1 2021-09-24 stsp
7843 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7844 f259c4c1 2021-09-24 stsp worktree);
7845 f259c4c1 2021-09-24 stsp if (err)
7846 f259c4c1 2021-09-24 stsp goto done;
7847 f259c4c1 2021-09-24 stsp
7848 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7849 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7850 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7851 f259c4c1 2021-09-24 stsp done:
7852 f259c4c1 2021-09-24 stsp free(fileindex_path);
7853 f259c4c1 2021-09-24 stsp return err;
7854 f259c4c1 2021-09-24 stsp }
7855 f259c4c1 2021-09-24 stsp
7856 f259c4c1 2021-09-24 stsp const struct got_error *
7857 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7858 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7859 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7860 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7861 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7862 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7863 0ff8d236 2021-09-28 stsp
7864 f259c4c1 2021-09-24 stsp {
7865 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7866 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7867 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7868 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7869 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7870 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7871 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7872 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7873 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7874 f259c4c1 2021-09-24 stsp
7875 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7876 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7877 f259c4c1 2021-09-24 stsp
7878 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7879 f259c4c1 2021-09-24 stsp
7880 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7881 f259c4c1 2021-09-24 stsp if (err)
7882 f259c4c1 2021-09-24 stsp goto done;
7883 f259c4c1 2021-09-24 stsp
7884 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7885 f259c4c1 2021-09-24 stsp if (err)
7886 f259c4c1 2021-09-24 stsp goto done;
7887 f259c4c1 2021-09-24 stsp
7888 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7889 f259c4c1 2021-09-24 stsp if (err)
7890 f259c4c1 2021-09-24 stsp goto done;
7891 f259c4c1 2021-09-24 stsp
7892 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7893 f259c4c1 2021-09-24 stsp &have_staged_files);
7894 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7895 f259c4c1 2021-09-24 stsp goto done;
7896 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7897 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7898 f259c4c1 2021-09-24 stsp goto done;
7899 f259c4c1 2021-09-24 stsp }
7900 f259c4c1 2021-09-24 stsp
7901 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7902 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7903 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7904 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7905 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7906 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7907 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7908 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7909 f259c4c1 2021-09-24 stsp if (err)
7910 f259c4c1 2021-09-24 stsp goto done;
7911 f259c4c1 2021-09-24 stsp
7912 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7913 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7914 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7915 f259c4c1 2021-09-24 stsp goto done;
7916 f259c4c1 2021-09-24 stsp }
7917 f259c4c1 2021-09-24 stsp
7918 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7919 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7920 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7921 f259c4c1 2021-09-24 stsp
7922 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7923 f259c4c1 2021-09-24 stsp ct_path++;
7924 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7925 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7926 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7927 f259c4c1 2021-09-24 stsp if (err)
7928 f259c4c1 2021-09-24 stsp goto done;
7929 f259c4c1 2021-09-24 stsp
7930 f259c4c1 2021-09-24 stsp }
7931 f259c4c1 2021-09-24 stsp
7932 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7933 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7934 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7935 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
7936 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7937 f259c4c1 2021-09-24 stsp if (err)
7938 f259c4c1 2021-09-24 stsp goto done;
7939 f259c4c1 2021-09-24 stsp
7940 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7941 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7942 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7943 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7944 f259c4c1 2021-09-24 stsp err = sync_err;
7945 f259c4c1 2021-09-24 stsp done:
7946 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7947 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7948 f259c4c1 2021-09-24 stsp free_commitable(ct);
7949 f259c4c1 2021-09-24 stsp }
7950 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7951 f259c4c1 2021-09-24 stsp free(fileindex_path);
7952 f259c4c1 2021-09-24 stsp return err;
7953 f259c4c1 2021-09-24 stsp }
7954 f259c4c1 2021-09-24 stsp
7955 f259c4c1 2021-09-24 stsp const struct got_error *
7956 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7957 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7958 f259c4c1 2021-09-24 stsp {
7959 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7960 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7961 f259c4c1 2021-09-24 stsp
7962 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7963 f259c4c1 2021-09-24 stsp if (err)
7964 f259c4c1 2021-09-24 stsp goto done;
7965 f259c4c1 2021-09-24 stsp
7966 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7967 f259c4c1 2021-09-24 stsp if (err)
7968 f259c4c1 2021-09-24 stsp goto done;
7969 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7970 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7971 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7972 f259c4c1 2021-09-24 stsp err = sync_err;
7973 f259c4c1 2021-09-24 stsp done:
7974 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7975 f259c4c1 2021-09-24 stsp free(fileindex_path);
7976 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7977 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7978 f259c4c1 2021-09-24 stsp err = unlockerr;
7979 f259c4c1 2021-09-24 stsp return err;
7980 f259c4c1 2021-09-24 stsp }
7981 f259c4c1 2021-09-24 stsp
7982 f259c4c1 2021-09-24 stsp const struct got_error *
7983 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7984 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7985 f259c4c1 2021-09-24 stsp {
7986 f259c4c1 2021-09-24 stsp const struct got_error *err;
7987 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7988 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7989 f259c4c1 2021-09-24 stsp
7990 f259c4c1 2021-09-24 stsp *in_progress = 0;
7991 f259c4c1 2021-09-24 stsp
7992 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7993 f259c4c1 2021-09-24 stsp if (err)
7994 f259c4c1 2021-09-24 stsp return err;
7995 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7996 793fcac3 2021-09-24 stsp free(branch_refname);
7997 f259c4c1 2021-09-24 stsp if (err) {
7998 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7999 f259c4c1 2021-09-24 stsp return err;
8000 f259c4c1 2021-09-24 stsp } else
8001 f259c4c1 2021-09-24 stsp *in_progress = 1;
8002 f259c4c1 2021-09-24 stsp
8003 f259c4c1 2021-09-24 stsp return NULL;
8004 f259c4c1 2021-09-24 stsp }
8005 f259c4c1 2021-09-24 stsp
8006 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8007 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8008 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8009 f259c4c1 2021-09-24 stsp {
8010 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8011 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8012 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8013 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8014 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8015 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8016 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8017 f259c4c1 2021-09-24 stsp
8018 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8019 f259c4c1 2021-09-24 stsp
8020 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8021 f259c4c1 2021-09-24 stsp if (err)
8022 f259c4c1 2021-09-24 stsp return err;
8023 f259c4c1 2021-09-24 stsp
8024 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8025 f259c4c1 2021-09-24 stsp if (err)
8026 f259c4c1 2021-09-24 stsp goto done;
8027 f259c4c1 2021-09-24 stsp
8028 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8029 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8030 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8031 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8032 f259c4c1 2021-09-24 stsp &ok_arg);
8033 f259c4c1 2021-09-24 stsp if (err)
8034 f259c4c1 2021-09-24 stsp goto done;
8035 f259c4c1 2021-09-24 stsp
8036 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8037 f259c4c1 2021-09-24 stsp if (err)
8038 f259c4c1 2021-09-24 stsp return err;
8039 f259c4c1 2021-09-24 stsp
8040 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8041 f259c4c1 2021-09-24 stsp if (err)
8042 f259c4c1 2021-09-24 stsp return err;
8043 f259c4c1 2021-09-24 stsp
8044 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8045 f259c4c1 2021-09-24 stsp 0);
8046 f259c4c1 2021-09-24 stsp if (err)
8047 f259c4c1 2021-09-24 stsp goto done;
8048 f259c4c1 2021-09-24 stsp
8049 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8050 f259c4c1 2021-09-24 stsp if (err)
8051 f259c4c1 2021-09-24 stsp goto done;
8052 f259c4c1 2021-09-24 stsp
8053 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8054 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8055 f259c4c1 2021-09-24 stsp goto done;
8056 f259c4c1 2021-09-24 stsp }
8057 f259c4c1 2021-09-24 stsp
8058 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8059 f259c4c1 2021-09-24 stsp if (err)
8060 f259c4c1 2021-09-24 stsp goto done;
8061 f259c4c1 2021-09-24 stsp
8062 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8063 f259c4c1 2021-09-24 stsp if (err)
8064 f259c4c1 2021-09-24 stsp goto done;
8065 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8066 f259c4c1 2021-09-24 stsp if (err)
8067 f259c4c1 2021-09-24 stsp goto done;
8068 f259c4c1 2021-09-24 stsp
8069 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8070 f259c4c1 2021-09-24 stsp if (err)
8071 f259c4c1 2021-09-24 stsp goto done;
8072 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8073 f259c4c1 2021-09-24 stsp if (err)
8074 f259c4c1 2021-09-24 stsp goto done;
8075 f259c4c1 2021-09-24 stsp
8076 f259c4c1 2021-09-24 stsp done:
8077 f259c4c1 2021-09-24 stsp free(branch_refname);
8078 f259c4c1 2021-09-24 stsp free(commit_refname);
8079 f259c4c1 2021-09-24 stsp free(fileindex_path);
8080 f259c4c1 2021-09-24 stsp if (branch_ref)
8081 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8082 f259c4c1 2021-09-24 stsp if (commit_ref)
8083 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8084 f259c4c1 2021-09-24 stsp if (wt_branch)
8085 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8086 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8087 f259c4c1 2021-09-24 stsp if (err) {
8088 f259c4c1 2021-09-24 stsp if (*fileindex) {
8089 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8090 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8091 f259c4c1 2021-09-24 stsp }
8092 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8093 f259c4c1 2021-09-24 stsp }
8094 f259c4c1 2021-09-24 stsp return err;
8095 f259c4c1 2021-09-24 stsp }
8096 f259c4c1 2021-09-24 stsp
8097 f259c4c1 2021-09-24 stsp const struct got_error *
8098 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8099 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8100 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8101 f259c4c1 2021-09-24 stsp {
8102 f259c4c1 2021-09-24 stsp const struct got_error *err;
8103 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8104 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8105 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8106 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8107 f259c4c1 2021-09-24 stsp
8108 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8109 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8110 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8111 f259c4c1 2021-09-24 stsp
8112 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8113 f259c4c1 2021-09-24 stsp if (err)
8114 f259c4c1 2021-09-24 stsp return err;
8115 f259c4c1 2021-09-24 stsp
8116 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8117 f259c4c1 2021-09-24 stsp if (err)
8118 f259c4c1 2021-09-24 stsp goto done;
8119 f259c4c1 2021-09-24 stsp
8120 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8121 f259c4c1 2021-09-24 stsp &have_staged_files);
8122 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8123 f259c4c1 2021-09-24 stsp goto done;
8124 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8125 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8126 f259c4c1 2021-09-24 stsp goto done;
8127 f259c4c1 2021-09-24 stsp }
8128 f259c4c1 2021-09-24 stsp
8129 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8130 f259c4c1 2021-09-24 stsp if (err)
8131 f259c4c1 2021-09-24 stsp goto done;
8132 f259c4c1 2021-09-24 stsp
8133 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8134 f259c4c1 2021-09-24 stsp if (err)
8135 f259c4c1 2021-09-24 stsp goto done;
8136 f259c4c1 2021-09-24 stsp
8137 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8138 f259c4c1 2021-09-24 stsp if (err)
8139 f259c4c1 2021-09-24 stsp goto done;
8140 f259c4c1 2021-09-24 stsp
8141 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8142 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8143 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8144 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8145 f259c4c1 2021-09-24 stsp goto done;
8146 f259c4c1 2021-09-24 stsp }
8147 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8148 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8149 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8150 f259c4c1 2021-09-24 stsp goto done;
8151 f259c4c1 2021-09-24 stsp }
8152 f259c4c1 2021-09-24 stsp
8153 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8154 f259c4c1 2021-09-24 stsp if (err)
8155 f259c4c1 2021-09-24 stsp goto done;
8156 f259c4c1 2021-09-24 stsp
8157 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8158 f259c4c1 2021-09-24 stsp if (err)
8159 f259c4c1 2021-09-24 stsp goto done;
8160 f259c4c1 2021-09-24 stsp done:
8161 f259c4c1 2021-09-24 stsp free(commit_refname);
8162 f259c4c1 2021-09-24 stsp free(branch_refname);
8163 f259c4c1 2021-09-24 stsp free(fileindex_path);
8164 f259c4c1 2021-09-24 stsp if (commit_ref)
8165 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8166 f259c4c1 2021-09-24 stsp if (branch_ref)
8167 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8168 f259c4c1 2021-09-24 stsp if (err) {
8169 f259c4c1 2021-09-24 stsp if (*branch_name) {
8170 f259c4c1 2021-09-24 stsp free(*branch_name);
8171 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8172 f259c4c1 2021-09-24 stsp }
8173 f259c4c1 2021-09-24 stsp free(*branch_tip);
8174 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8175 f259c4c1 2021-09-24 stsp if (*fileindex) {
8176 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8177 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8178 f259c4c1 2021-09-24 stsp }
8179 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8180 f259c4c1 2021-09-24 stsp }
8181 f259c4c1 2021-09-24 stsp return err;
8182 f259c4c1 2021-09-24 stsp }
8183 f259c4c1 2021-09-24 stsp
8184 f259c4c1 2021-09-24 stsp const struct got_error *
8185 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8186 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8187 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8188 f259c4c1 2021-09-24 stsp {
8189 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8190 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8191 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8192 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8193 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8194 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8195 f259c4c1 2021-09-24 stsp
8196 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8197 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8198 a44927cc 2022-04-07 stsp if (err)
8199 a44927cc 2022-04-07 stsp goto done;
8200 a44927cc 2022-04-07 stsp
8201 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8202 a44927cc 2022-04-07 stsp worktree->path_prefix);
8203 f259c4c1 2021-09-24 stsp if (err)
8204 f259c4c1 2021-09-24 stsp goto done;
8205 f259c4c1 2021-09-24 stsp
8206 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8207 f259c4c1 2021-09-24 stsp if (err)
8208 f259c4c1 2021-09-24 stsp goto done;
8209 f259c4c1 2021-09-24 stsp
8210 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8211 f259c4c1 2021-09-24 stsp if (err)
8212 f259c4c1 2021-09-24 stsp goto done;
8213 f259c4c1 2021-09-24 stsp
8214 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8215 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8216 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8217 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8218 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8219 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8220 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8221 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8222 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8223 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8224 f259c4c1 2021-09-24 stsp if (err)
8225 f259c4c1 2021-09-24 stsp goto sync;
8226 f259c4c1 2021-09-24 stsp
8227 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8228 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8229 f259c4c1 2021-09-24 stsp sync:
8230 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8231 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8232 f259c4c1 2021-09-24 stsp err = sync_err;
8233 f259c4c1 2021-09-24 stsp done:
8234 f259c4c1 2021-09-24 stsp free(tree_id);
8235 f259c4c1 2021-09-24 stsp free(commit_id);
8236 a44927cc 2022-04-07 stsp if (commit)
8237 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8238 f259c4c1 2021-09-24 stsp if (fileindex)
8239 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8240 f259c4c1 2021-09-24 stsp free(fileindex_path);
8241 f259c4c1 2021-09-24 stsp
8242 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8243 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8244 f259c4c1 2021-09-24 stsp err = unlockerr;
8245 f259c4c1 2021-09-24 stsp return err;
8246 f259c4c1 2021-09-24 stsp }
8247 f259c4c1 2021-09-24 stsp
8248 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8249 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8250 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8251 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8252 2db2652d 2019-08-07 stsp struct got_repository *repo;
8253 2db2652d 2019-08-07 stsp int have_changes;
8254 2db2652d 2019-08-07 stsp };
8255 2db2652d 2019-08-07 stsp
8256 336075a4 2022-06-25 op static const struct got_error *
8257 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8258 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8259 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8260 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8261 735ef5ac 2019-08-03 stsp {
8262 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8263 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8264 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8265 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8266 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8267 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8268 8b13ce36 2019-08-08 stsp
8269 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8270 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8271 8b13ce36 2019-08-08 stsp return NULL;
8272 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8273 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8274 735ef5ac 2019-08-03 stsp
8275 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8276 735ef5ac 2019-08-03 stsp if (ie == NULL)
8277 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8278 735ef5ac 2019-08-03 stsp
8279 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8280 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8281 735ef5ac 2019-08-03 stsp relpath) == -1)
8282 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8283 735ef5ac 2019-08-03 stsp
8284 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8285 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8286 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8287 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8288 735ef5ac 2019-08-03 stsp }
8289 735ef5ac 2019-08-03 stsp
8290 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8291 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8292 3aa5969e 2019-08-06 stsp goto done;
8293 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8294 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8295 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8296 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8297 735ef5ac 2019-08-03 stsp goto done;
8298 3aa5969e 2019-08-06 stsp }
8299 735ef5ac 2019-08-03 stsp
8300 2db2652d 2019-08-07 stsp a->have_changes = 1;
8301 2db2652d 2019-08-07 stsp
8302 735ef5ac 2019-08-03 stsp p = in_repo_path;
8303 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8304 735ef5ac 2019-08-03 stsp p++;
8305 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8306 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8307 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8308 735ef5ac 2019-08-03 stsp done:
8309 735ef5ac 2019-08-03 stsp free(in_repo_path);
8310 dc424a06 2019-08-07 stsp return err;
8311 dc424a06 2019-08-07 stsp }
8312 dc424a06 2019-08-07 stsp
8313 2db2652d 2019-08-07 stsp struct stage_path_arg {
8314 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8315 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8316 2db2652d 2019-08-07 stsp struct got_repository *repo;
8317 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8318 2db2652d 2019-08-07 stsp void *status_arg;
8319 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8320 2db2652d 2019-08-07 stsp void *patch_arg;
8321 7b5dc508 2019-10-28 stsp int staged_something;
8322 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8323 2db2652d 2019-08-07 stsp };
8324 2db2652d 2019-08-07 stsp
8325 2db2652d 2019-08-07 stsp static const struct got_error *
8326 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8327 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8328 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8329 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8330 0cb83759 2019-08-03 stsp {
8331 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8332 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8333 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8334 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8335 0cb83759 2019-08-03 stsp uint32_t stage;
8336 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8337 0aeb8099 2020-07-23 stsp struct stat sb;
8338 8b13ce36 2019-08-08 stsp
8339 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8340 8b13ce36 2019-08-08 stsp return NULL;
8341 0cb83759 2019-08-03 stsp
8342 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8343 d3e7c587 2019-08-03 stsp if (ie == NULL)
8344 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8345 0cb83759 2019-08-03 stsp
8346 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8347 2db2652d 2019-08-07 stsp relpath)== -1)
8348 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8349 0cb83759 2019-08-03 stsp
8350 0cb83759 2019-08-03 stsp switch (status) {
8351 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8352 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8353 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8354 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8355 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8356 0aeb8099 2020-07-23 stsp break;
8357 0aeb8099 2020-07-23 stsp }
8358 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8359 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8360 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8361 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8362 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8363 dc424a06 2019-08-07 stsp if (err)
8364 dc424a06 2019-08-07 stsp break;
8365 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8366 dc424a06 2019-08-07 stsp break;
8367 dc424a06 2019-08-07 stsp } else {
8368 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8369 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8370 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8371 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8372 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8373 dc424a06 2019-08-07 stsp break;
8374 dc424a06 2019-08-07 stsp }
8375 dc424a06 2019-08-07 stsp }
8376 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8377 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8378 0cb83759 2019-08-03 stsp if (err)
8379 dc424a06 2019-08-07 stsp break;
8380 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8381 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8382 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8383 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8384 0cb83759 2019-08-03 stsp else
8385 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8386 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8387 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8388 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8389 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8390 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8391 35213c7c 2020-07-23 stsp ssize_t target_len;
8392 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8393 35213c7c 2020-07-23 stsp sizeof(target_path));
8394 35213c7c 2020-07-23 stsp if (target_len == -1) {
8395 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8396 35213c7c 2020-07-23 stsp ondisk_path);
8397 35213c7c 2020-07-23 stsp break;
8398 35213c7c 2020-07-23 stsp }
8399 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8400 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8401 35213c7c 2020-07-23 stsp a->worktree->root_path);
8402 35213c7c 2020-07-23 stsp if (err)
8403 35213c7c 2020-07-23 stsp break;
8404 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8405 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8406 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8407 35213c7c 2020-07-23 stsp break;
8408 35213c7c 2020-07-23 stsp }
8409 35213c7c 2020-07-23 stsp }
8410 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8411 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8412 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8413 35213c7c 2020-07-23 stsp else
8414 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8415 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8416 0aeb8099 2020-07-23 stsp } else {
8417 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8418 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8419 0aeb8099 2020-07-23 stsp }
8420 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8421 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8422 dc424a06 2019-08-07 stsp break;
8423 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8424 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8425 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8426 9fdde394 2022-06-04 op if (err)
8427 9fdde394 2022-06-04 op break;
8428 9fdde394 2022-06-04 op /*
8429 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8430 9fdde394 2022-06-04 op * implicitly unstage the file.
8431 9fdde394 2022-06-04 op */
8432 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8433 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8434 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8435 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8436 9fdde394 2022-06-04 op }
8437 0cb83759 2019-08-03 stsp break;
8438 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8439 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8440 d3e7c587 2019-08-03 stsp break;
8441 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8442 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8443 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8444 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8445 dc424a06 2019-08-07 stsp if (err)
8446 dc424a06 2019-08-07 stsp break;
8447 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8448 88f33a19 2019-08-08 stsp break;
8449 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8450 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8451 dc424a06 2019-08-07 stsp break;
8452 88f33a19 2019-08-08 stsp }
8453 dc424a06 2019-08-07 stsp }
8454 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8455 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8456 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8457 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8458 dc424a06 2019-08-07 stsp break;
8459 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8460 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8461 12463d8b 2019-12-13 stsp de_name);
8462 0cb83759 2019-08-03 stsp break;
8463 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8464 d3e7c587 2019-08-03 stsp break;
8465 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8466 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8467 ebf48fd5 2019-08-03 stsp break;
8468 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8469 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8470 2a06fe5f 2019-08-24 stsp break;
8471 0cb83759 2019-08-03 stsp default:
8472 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8473 537ac44b 2019-08-03 stsp break;
8474 0cb83759 2019-08-03 stsp }
8475 dc424a06 2019-08-07 stsp
8476 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8477 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8478 dc424a06 2019-08-07 stsp free(path_content);
8479 2db2652d 2019-08-07 stsp free(ondisk_path);
8480 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8481 0ebf8283 2019-07-24 stsp return err;
8482 0ebf8283 2019-07-24 stsp }
8483 0cb83759 2019-08-03 stsp
8484 0cb83759 2019-08-03 stsp const struct got_error *
8485 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8486 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8487 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8488 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8489 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8490 0cb83759 2019-08-03 stsp {
8491 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8492 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8493 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8494 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8495 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8496 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8497 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8498 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8499 0cb83759 2019-08-03 stsp
8500 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8501 0cb83759 2019-08-03 stsp if (err)
8502 0cb83759 2019-08-03 stsp return err;
8503 0cb83759 2019-08-03 stsp
8504 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8505 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8506 735ef5ac 2019-08-03 stsp if (err)
8507 735ef5ac 2019-08-03 stsp goto done;
8508 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8509 735ef5ac 2019-08-03 stsp if (err)
8510 735ef5ac 2019-08-03 stsp goto done;
8511 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8512 0cb83759 2019-08-03 stsp if (err)
8513 0cb83759 2019-08-03 stsp goto done;
8514 0cb83759 2019-08-03 stsp
8515 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8516 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8517 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8518 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8519 2db2652d 2019-08-07 stsp oka.repo = repo;
8520 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8521 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8522 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8523 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8524 735ef5ac 2019-08-03 stsp if (err)
8525 735ef5ac 2019-08-03 stsp goto done;
8526 735ef5ac 2019-08-03 stsp }
8527 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8528 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8529 2db2652d 2019-08-07 stsp goto done;
8530 2db2652d 2019-08-07 stsp }
8531 735ef5ac 2019-08-03 stsp
8532 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8533 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8534 2db2652d 2019-08-07 stsp spa.repo = repo;
8535 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8536 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8537 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8538 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8539 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8540 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8541 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8542 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8543 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8544 42005733 2019-08-03 stsp if (err)
8545 2db2652d 2019-08-07 stsp goto done;
8546 7b5dc508 2019-10-28 stsp }
8547 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8548 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8549 7b5dc508 2019-10-28 stsp goto done;
8550 0cb83759 2019-08-03 stsp }
8551 0cb83759 2019-08-03 stsp
8552 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8553 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8554 0cb83759 2019-08-03 stsp err = sync_err;
8555 0cb83759 2019-08-03 stsp done:
8556 735ef5ac 2019-08-03 stsp if (head_ref)
8557 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8558 735ef5ac 2019-08-03 stsp free(head_commit_id);
8559 0cb83759 2019-08-03 stsp free(fileindex_path);
8560 0cb83759 2019-08-03 stsp if (fileindex)
8561 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8562 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8563 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8564 0cb83759 2019-08-03 stsp err = unlockerr;
8565 0cb83759 2019-08-03 stsp return err;
8566 0cb83759 2019-08-03 stsp }
8567 ad493afc 2019-08-03 stsp
8568 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8569 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8570 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8571 ad493afc 2019-08-03 stsp struct got_repository *repo;
8572 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8573 ad493afc 2019-08-03 stsp void *progress_arg;
8574 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8575 2e1f37b0 2019-08-08 stsp void *patch_arg;
8576 ad493afc 2019-08-03 stsp };
8577 2e1f37b0 2019-08-08 stsp
8578 2e1f37b0 2019-08-08 stsp static const struct got_error *
8579 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8580 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8581 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8582 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8583 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8584 2e1f37b0 2019-08-08 stsp {
8585 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8586 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8587 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8588 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8589 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8590 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8591 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8592 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8593 2e1f37b0 2019-08-08 stsp
8594 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8595 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8596 2e1f37b0 2019-08-08 stsp
8597 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8598 2e1f37b0 2019-08-08 stsp if (err)
8599 2e1f37b0 2019-08-08 stsp return err;
8600 eb81bc23 2022-06-28 tracey
8601 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8602 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8603 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8604 eb81bc23 2022-06-28 tracey goto done;
8605 eb81bc23 2022-06-28 tracey }
8606 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8607 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8608 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8609 eb81bc23 2022-06-28 tracey goto done;
8610 eb81bc23 2022-06-28 tracey }
8611 eb81bc23 2022-06-28 tracey
8612 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8613 2e1f37b0 2019-08-08 stsp if (err)
8614 2e1f37b0 2019-08-08 stsp goto done;
8615 2e1f37b0 2019-08-08 stsp
8616 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8617 2e1f37b0 2019-08-08 stsp if (err)
8618 2e1f37b0 2019-08-08 stsp goto done;
8619 2e1f37b0 2019-08-08 stsp
8620 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8621 2e1f37b0 2019-08-08 stsp if (err)
8622 2e1f37b0 2019-08-08 stsp goto done;
8623 2e1f37b0 2019-08-08 stsp
8624 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8625 eb81bc23 2022-06-28 tracey fd2);
8626 2e1f37b0 2019-08-08 stsp if (err)
8627 2e1f37b0 2019-08-08 stsp goto done;
8628 2e1f37b0 2019-08-08 stsp
8629 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8630 2e1f37b0 2019-08-08 stsp if (err)
8631 2e1f37b0 2019-08-08 stsp goto done;
8632 ad493afc 2019-08-03 stsp
8633 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8634 2e1f37b0 2019-08-08 stsp if (err)
8635 2e1f37b0 2019-08-08 stsp goto done;
8636 2e1f37b0 2019-08-08 stsp
8637 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8638 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8639 2e1f37b0 2019-08-08 stsp if (err)
8640 2e1f37b0 2019-08-08 stsp goto done;
8641 2e1f37b0 2019-08-08 stsp
8642 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8643 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8644 2e1f37b0 2019-08-08 stsp if (err)
8645 2e1f37b0 2019-08-08 stsp goto done;
8646 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8647 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8648 2e1f37b0 2019-08-08 stsp if (err)
8649 2e1f37b0 2019-08-08 stsp goto done;
8650 2e1f37b0 2019-08-08 stsp
8651 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8652 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8653 2e1f37b0 2019-08-08 stsp goto done;
8654 2e1f37b0 2019-08-08 stsp }
8655 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8656 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8657 2e1f37b0 2019-08-08 stsp goto done;
8658 2e1f37b0 2019-08-08 stsp }
8659 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8660 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8661 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8662 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8663 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8664 fe621944 2020-11-10 stsp nchanges++;
8665 fe621944 2020-11-10 stsp }
8666 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8667 2e1f37b0 2019-08-08 stsp int choice;
8668 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8669 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8670 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8671 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8672 2e1f37b0 2019-08-08 stsp if (err)
8673 2e1f37b0 2019-08-08 stsp goto done;
8674 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8675 2e1f37b0 2019-08-08 stsp have_content = 1;
8676 19e4b907 2019-08-08 stsp else
8677 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8678 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8679 2e1f37b0 2019-08-08 stsp break;
8680 2e1f37b0 2019-08-08 stsp }
8681 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8682 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8683 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8684 2e1f37b0 2019-08-08 stsp done:
8685 2e1f37b0 2019-08-08 stsp free(label1);
8686 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8687 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8688 2e1f37b0 2019-08-08 stsp if (blob)
8689 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8690 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8691 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8692 2e1f37b0 2019-08-08 stsp if (staged_blob)
8693 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8694 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8695 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8696 fe621944 2020-11-10 stsp err = free_err;
8697 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8698 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8699 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8700 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8701 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8702 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8703 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8704 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8705 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8706 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8707 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8708 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8709 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8710 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8711 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8712 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8713 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8714 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8715 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8716 2e1f37b0 2019-08-08 stsp }
8717 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8718 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8719 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8720 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8721 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8722 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8723 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8724 2e1f37b0 2019-08-08 stsp }
8725 2e1f37b0 2019-08-08 stsp free(path1);
8726 2e1f37b0 2019-08-08 stsp free(path2);
8727 fda8017d 2020-07-23 stsp return err;
8728 fda8017d 2020-07-23 stsp }
8729 fda8017d 2020-07-23 stsp
8730 fda8017d 2020-07-23 stsp static const struct got_error *
8731 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8732 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8733 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8734 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8735 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8736 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8737 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8738 fda8017d 2020-07-23 stsp {
8739 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8740 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8741 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8742 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8743 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8744 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8745 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8746 fda8017d 2020-07-23 stsp struct stat sb;
8747 fda8017d 2020-07-23 stsp
8748 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8749 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8750 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8751 fda8017d 2020-07-23 stsp if (err)
8752 fda8017d 2020-07-23 stsp return err;
8753 fda8017d 2020-07-23 stsp
8754 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8755 fda8017d 2020-07-23 stsp return NULL;
8756 fda8017d 2020-07-23 stsp
8757 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8758 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8759 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8760 fda8017d 2020-07-23 stsp if (err)
8761 fda8017d 2020-07-23 stsp goto done;
8762 fda8017d 2020-07-23 stsp }
8763 fda8017d 2020-07-23 stsp
8764 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8765 fda8017d 2020-07-23 stsp if (f == NULL) {
8766 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8767 fda8017d 2020-07-23 stsp path_unstaged_content);
8768 fda8017d 2020-07-23 stsp goto done;
8769 fda8017d 2020-07-23 stsp }
8770 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8771 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8772 fda8017d 2020-07-23 stsp goto done;
8773 fda8017d 2020-07-23 stsp }
8774 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8775 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8776 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8777 fda8017d 2020-07-23 stsp size_t r;
8778 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8779 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8780 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8781 fda8017d 2020-07-23 stsp goto done;
8782 fda8017d 2020-07-23 stsp }
8783 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8784 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8785 fda8017d 2020-07-23 stsp goto done;
8786 fda8017d 2020-07-23 stsp }
8787 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8788 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8789 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8790 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8791 fda8017d 2020-07-23 stsp progress_arg);
8792 fda8017d 2020-07-23 stsp } else {
8793 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8794 67a66647 2021-05-31 stsp
8795 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8796 67a66647 2021-05-31 stsp if (err)
8797 67a66647 2021-05-31 stsp return err;
8798 67a66647 2021-05-31 stsp
8799 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8800 67a66647 2021-05-31 stsp parent) == -1) {
8801 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8802 67a66647 2021-05-31 stsp base_path = NULL;
8803 67a66647 2021-05-31 stsp goto done;
8804 67a66647 2021-05-31 stsp }
8805 67a66647 2021-05-31 stsp
8806 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8807 b90054ed 2022-11-01 stsp base_path, "");
8808 67a66647 2021-05-31 stsp if (err)
8809 67a66647 2021-05-31 stsp goto done;
8810 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8811 67a66647 2021-05-31 stsp blob_base);
8812 67a66647 2021-05-31 stsp if (err)
8813 67a66647 2021-05-31 stsp goto done;
8814 67a66647 2021-05-31 stsp
8815 5e91dae4 2022-08-30 stsp /*
8816 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8817 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8818 eec2f5a9 2021-06-03 stsp */
8819 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8820 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8821 eec2f5a9 2021-06-03 stsp ondisk_path);
8822 eec2f5a9 2021-06-03 stsp if (err)
8823 eec2f5a9 2021-06-03 stsp goto done;
8824 eec2f5a9 2021-06-03 stsp } else {
8825 eec2f5a9 2021-06-03 stsp int fd;
8826 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8827 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8828 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8829 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8830 eec2f5a9 2021-06-03 stsp goto done;
8831 eec2f5a9 2021-06-03 stsp }
8832 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8833 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8834 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8835 eec2f5a9 2021-06-03 stsp close(fd);
8836 eec2f5a9 2021-06-03 stsp goto done;
8837 eec2f5a9 2021-06-03 stsp }
8838 eec2f5a9 2021-06-03 stsp }
8839 eec2f5a9 2021-06-03 stsp
8840 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8841 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8842 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8843 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8844 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8845 fda8017d 2020-07-23 stsp }
8846 fda8017d 2020-07-23 stsp if (err)
8847 fda8017d 2020-07-23 stsp goto done;
8848 fda8017d 2020-07-23 stsp
8849 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8850 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8851 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8852 2ac8aa02 2020-11-09 stsp } else {
8853 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8854 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8855 2ac8aa02 2020-11-09 stsp }
8856 fda8017d 2020-07-23 stsp done:
8857 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8858 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8859 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8860 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8861 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8862 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8863 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8864 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8865 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8866 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8867 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8868 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8869 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8870 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8871 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8872 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8873 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8874 67a66647 2021-05-31 stsp free(blob_base_path);
8875 67a66647 2021-05-31 stsp free(parent);
8876 67a66647 2021-05-31 stsp free(base_path);
8877 2e1f37b0 2019-08-08 stsp return err;
8878 2e1f37b0 2019-08-08 stsp }
8879 2e1f37b0 2019-08-08 stsp
8880 ad493afc 2019-08-03 stsp static const struct got_error *
8881 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8882 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8883 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8884 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8885 ad493afc 2019-08-03 stsp {
8886 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8887 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8888 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8889 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8890 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8891 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8892 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8893 9bc94a15 2019-08-03 stsp struct stat sb;
8894 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8895 ad493afc 2019-08-03 stsp
8896 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8897 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8898 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8899 2e1f37b0 2019-08-08 stsp return NULL;
8900 2e1f37b0 2019-08-08 stsp
8901 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8902 ad493afc 2019-08-03 stsp if (ie == NULL)
8903 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8904 9bc94a15 2019-08-03 stsp
8905 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8906 9bc94a15 2019-08-03 stsp == -1)
8907 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8908 ad493afc 2019-08-03 stsp
8909 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8910 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8911 f69721c3 2019-10-21 stsp if (err)
8912 f69721c3 2019-10-21 stsp goto done;
8913 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8914 f69721c3 2019-10-21 stsp id_str) == -1) {
8915 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8916 eb81bc23 2022-06-28 tracey goto done;
8917 eb81bc23 2022-06-28 tracey }
8918 eb81bc23 2022-06-28 tracey
8919 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8920 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8921 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8922 eb81bc23 2022-06-28 tracey goto done;
8923 eb81bc23 2022-06-28 tracey }
8924 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8925 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8926 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8927 f69721c3 2019-10-21 stsp goto done;
8928 f69721c3 2019-10-21 stsp }
8929 f69721c3 2019-10-21 stsp
8930 ad493afc 2019-08-03 stsp switch (staged_status) {
8931 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8932 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8933 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8934 ad493afc 2019-08-03 stsp if (err)
8935 ad493afc 2019-08-03 stsp break;
8936 ad493afc 2019-08-03 stsp /* fall through */
8937 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8938 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8939 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8940 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8941 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8942 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8943 2e1f37b0 2019-08-08 stsp if (err)
8944 2e1f37b0 2019-08-08 stsp break;
8945 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8946 2e1f37b0 2019-08-08 stsp break;
8947 2e1f37b0 2019-08-08 stsp } else {
8948 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8949 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8950 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8951 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8952 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8953 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8954 2e1f37b0 2019-08-08 stsp }
8955 2e1f37b0 2019-08-08 stsp }
8956 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8957 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8958 ad493afc 2019-08-03 stsp if (err)
8959 ad493afc 2019-08-03 stsp break;
8960 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8961 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8962 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8963 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8964 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8965 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8966 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8967 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8968 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8969 ea7786be 2020-07-23 stsp break;
8970 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8971 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8972 36bf999c 2020-07-23 stsp char *staged_target;
8973 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8974 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8975 36bf999c 2020-07-23 stsp if (err)
8976 36bf999c 2020-07-23 stsp goto done;
8977 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8978 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8979 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8980 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8981 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8982 36bf999c 2020-07-23 stsp free(staged_target);
8983 dfe9fba0 2020-07-23 stsp } else {
8984 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8985 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8986 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8987 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8988 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8989 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8990 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8991 dfe9fba0 2020-07-23 stsp }
8992 ea7786be 2020-07-23 stsp break;
8993 ea7786be 2020-07-23 stsp default:
8994 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8995 ea7786be 2020-07-23 stsp break;
8996 ea7786be 2020-07-23 stsp }
8997 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8998 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8999 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9000 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9001 2ac8aa02 2020-11-09 stsp }
9002 ad493afc 2019-08-03 stsp break;
9003 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9004 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9005 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9006 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9007 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9008 2e1f37b0 2019-08-08 stsp if (err)
9009 2e1f37b0 2019-08-08 stsp break;
9010 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9011 2e1f37b0 2019-08-08 stsp break;
9012 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9013 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9014 2e1f37b0 2019-08-08 stsp break;
9015 2e1f37b0 2019-08-08 stsp }
9016 2e1f37b0 2019-08-08 stsp }
9017 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9018 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9019 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9020 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9021 9bc94a15 2019-08-03 stsp if (err)
9022 9bc94a15 2019-08-03 stsp break;
9023 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9024 ad493afc 2019-08-03 stsp break;
9025 ad493afc 2019-08-03 stsp }
9026 f69721c3 2019-10-21 stsp done:
9027 ad493afc 2019-08-03 stsp free(ondisk_path);
9028 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9029 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9030 ad493afc 2019-08-03 stsp if (blob_base)
9031 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9032 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9033 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9034 ad493afc 2019-08-03 stsp if (blob_staged)
9035 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9036 f69721c3 2019-10-21 stsp free(id_str);
9037 f69721c3 2019-10-21 stsp free(label_orig);
9038 ad493afc 2019-08-03 stsp return err;
9039 ad493afc 2019-08-03 stsp }
9040 ad493afc 2019-08-03 stsp
9041 ad493afc 2019-08-03 stsp const struct got_error *
9042 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9043 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9044 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9045 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9046 ad493afc 2019-08-03 stsp struct got_repository *repo)
9047 ad493afc 2019-08-03 stsp {
9048 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9049 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9050 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9051 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9052 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9053 ad493afc 2019-08-03 stsp
9054 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9055 ad493afc 2019-08-03 stsp if (err)
9056 ad493afc 2019-08-03 stsp return err;
9057 ad493afc 2019-08-03 stsp
9058 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9059 ad493afc 2019-08-03 stsp if (err)
9060 ad493afc 2019-08-03 stsp goto done;
9061 ad493afc 2019-08-03 stsp
9062 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9063 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9064 ad493afc 2019-08-03 stsp upa.repo = repo;
9065 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9066 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9067 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9068 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9069 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9070 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9071 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9072 ad493afc 2019-08-03 stsp if (err)
9073 ad493afc 2019-08-03 stsp goto done;
9074 ad493afc 2019-08-03 stsp }
9075 ad493afc 2019-08-03 stsp
9076 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9077 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9078 ad493afc 2019-08-03 stsp err = sync_err;
9079 ad493afc 2019-08-03 stsp done:
9080 ad493afc 2019-08-03 stsp free(fileindex_path);
9081 ad493afc 2019-08-03 stsp if (fileindex)
9082 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9083 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9084 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9085 ad493afc 2019-08-03 stsp err = unlockerr;
9086 ad493afc 2019-08-03 stsp return err;
9087 ad493afc 2019-08-03 stsp }
9088 b2118c49 2020-07-28 stsp
9089 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9090 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9091 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9092 b2118c49 2020-07-28 stsp void *info_arg;
9093 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9094 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9095 b2118c49 2020-07-28 stsp void *cancel_arg;
9096 b2118c49 2020-07-28 stsp };
9097 b2118c49 2020-07-28 stsp
9098 b2118c49 2020-07-28 stsp static const struct got_error *
9099 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9100 b2118c49 2020-07-28 stsp {
9101 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9102 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9103 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9104 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9105 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9106 b2118c49 2020-07-28 stsp int stage;
9107 b2118c49 2020-07-28 stsp
9108 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9109 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9110 b2118c49 2020-07-28 stsp
9111 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9112 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9113 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9114 b2118c49 2020-07-28 stsp break;
9115 b2118c49 2020-07-28 stsp }
9116 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9117 b2118c49 2020-07-28 stsp return NULL;
9118 b2118c49 2020-07-28 stsp
9119 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
9120 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9121 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
9122 b2118c49 2020-07-28 stsp }
9123 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9124 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9125 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9126 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9127 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
9128 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
9129 b2118c49 2020-07-28 stsp }
9130 b2118c49 2020-07-28 stsp
9131 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
9132 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9133 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
9134 b2118c49 2020-07-28 stsp }
9135 b2118c49 2020-07-28 stsp
9136 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9137 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9138 b2118c49 2020-07-28 stsp }
9139 b2118c49 2020-07-28 stsp
9140 b2118c49 2020-07-28 stsp const struct got_error *
9141 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9142 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9143 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9144 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9145 b2118c49 2020-07-28 stsp
9146 b2118c49 2020-07-28 stsp {
9147 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9148 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9149 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9150 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9151 b2118c49 2020-07-28 stsp
9152 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9153 b2118c49 2020-07-28 stsp if (err)
9154 b2118c49 2020-07-28 stsp return err;
9155 b2118c49 2020-07-28 stsp
9156 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9157 b2118c49 2020-07-28 stsp if (err)
9158 b2118c49 2020-07-28 stsp goto done;
9159 b2118c49 2020-07-28 stsp
9160 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9161 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9162 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9163 b2118c49 2020-07-28 stsp arg.paths = paths;
9164 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9165 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9166 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9167 b2118c49 2020-07-28 stsp &arg);
9168 b2118c49 2020-07-28 stsp done:
9169 b2118c49 2020-07-28 stsp free(fileindex_path);
9170 b2118c49 2020-07-28 stsp if (fileindex)
9171 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9172 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9173 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9174 b2118c49 2020-07-28 stsp err = unlockerr;
9175 b2118c49 2020-07-28 stsp return err;
9176 b2118c49 2020-07-28 stsp }
9177 78f5ac24 2022-03-19 op
9178 78f5ac24 2022-03-19 op static const struct got_error *
9179 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9180 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9181 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9182 78f5ac24 2022-03-19 op {
9183 78f5ac24 2022-03-19 op const struct got_error *err;
9184 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9185 78f5ac24 2022-03-19 op struct stat sb;
9186 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9187 78f5ac24 2022-03-19 op
9188 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9189 78f5ac24 2022-03-19 op if (err)
9190 78f5ac24 2022-03-19 op return err;
9191 78f5ac24 2022-03-19 op
9192 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9193 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9194 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9195 78f5ac24 2022-03-19 op
9196 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9197 78f5ac24 2022-03-19 op if (ie) {
9198 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9199 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9200 a05fb460 2022-04-23 op repo);
9201 78f5ac24 2022-03-19 op if (err)
9202 78f5ac24 2022-03-19 op goto done;
9203 78f5ac24 2022-03-19 op } else {
9204 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9205 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9206 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9207 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9208 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9209 78f5ac24 2022-03-19 op ondisk_path);
9210 78f5ac24 2022-03-19 op goto done;
9211 78f5ac24 2022-03-19 op }
9212 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9213 78f5ac24 2022-03-19 op }
9214 78f5ac24 2022-03-19 op }
9215 78f5ac24 2022-03-19 op
9216 78f5ac24 2022-03-19 op done:
9217 78f5ac24 2022-03-19 op free(ondisk_path);
9218 78f5ac24 2022-03-19 op return err;
9219 78f5ac24 2022-03-19 op }
9220 78f5ac24 2022-03-19 op
9221 78f5ac24 2022-03-19 op static const struct got_error *
9222 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9223 78f5ac24 2022-03-19 op unsigned char staged_status)
9224 78f5ac24 2022-03-19 op {
9225 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9226 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9227 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9228 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9229 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9230 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9231 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9232 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9233 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 78f5ac24 2022-03-19 op return NULL;
9235 78f5ac24 2022-03-19 op }
9236 78f5ac24 2022-03-19 op
9237 78f5ac24 2022-03-19 op static const struct got_error *
9238 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9239 78f5ac24 2022-03-19 op {
9240 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9241 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9242 78f5ac24 2022-03-19 op return NULL;
9243 78f5ac24 2022-03-19 op }
9244 78f5ac24 2022-03-19 op
9245 78f5ac24 2022-03-19 op static const struct got_error *
9246 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9247 78f5ac24 2022-03-19 op unsigned char staged_status)
9248 78f5ac24 2022-03-19 op {
9249 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9250 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9251 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9252 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9253 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9254 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9255 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9256 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9257 78f5ac24 2022-03-19 op return NULL;
9258 78f5ac24 2022-03-19 op }
9259 78f5ac24 2022-03-19 op
9260 78f5ac24 2022-03-19 op const struct got_error *
9261 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9262 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9263 78f5ac24 2022-03-19 op {
9264 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9265 78f5ac24 2022-03-19 op }
9266 78f5ac24 2022-03-19 op
9267 78f5ac24 2022-03-19 op const struct got_error *
9268 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9269 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9270 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9271 78f5ac24 2022-03-19 op {
9272 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9273 78f5ac24 2022-03-19 op int file_renamed = 0;
9274 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9275 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9276 78f5ac24 2022-03-19 op
9277 78f5ac24 2022-03-19 op *oldpath = NULL;
9278 78f5ac24 2022-03-19 op *newpath = NULL;
9279 78f5ac24 2022-03-19 op
9280 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9281 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9282 78f5ac24 2022-03-19 op if (err)
9283 78f5ac24 2022-03-19 op goto done;
9284 78f5ac24 2022-03-19 op
9285 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9286 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9287 78f5ac24 2022-03-19 op if (err)
9288 78f5ac24 2022-03-19 op goto done;
9289 78f5ac24 2022-03-19 op
9290 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9291 78f5ac24 2022-03-19 op file_renamed = 1;
9292 78f5ac24 2022-03-19 op
9293 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9294 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9295 78f5ac24 2022-03-19 op else if (file_renamed) {
9296 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9297 78f5ac24 2022-03-19 op if (err == NULL)
9298 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9299 78f5ac24 2022-03-19 op } else if (old == NULL)
9300 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9301 78f5ac24 2022-03-19 op else
9302 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9303 78f5ac24 2022-03-19 op
9304 78f5ac24 2022-03-19 op done:
9305 78f5ac24 2022-03-19 op if (err) {
9306 78f5ac24 2022-03-19 op free(*oldpath);
9307 78f5ac24 2022-03-19 op *oldpath = NULL;
9308 78f5ac24 2022-03-19 op free(*newpath);
9309 78f5ac24 2022-03-19 op *newpath = NULL;
9310 78f5ac24 2022-03-19 op }
9311 78f5ac24 2022-03-19 op return err;
9312 78f5ac24 2022-03-19 op }
9313 78f5ac24 2022-03-19 op
9314 78f5ac24 2022-03-19 op const struct got_error *
9315 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9316 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9317 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9318 78f5ac24 2022-03-19 op {
9319 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9320 f2dd7807 2022-05-17 op
9321 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9322 f2dd7807 2022-05-17 op saa.worktree = worktree;
9323 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9324 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9325 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9326 f2dd7807 2022-05-17 op saa.repo = repo;
9327 f2dd7807 2022-05-17 op
9328 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9329 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9330 78f5ac24 2022-03-19 op }
9331 f2dd7807 2022-05-17 op
9332 f2dd7807 2022-05-17 op const struct got_error *
9333 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9334 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9335 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9336 f2dd7807 2022-05-17 op {
9337 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9338 f2dd7807 2022-05-17 op
9339 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9340 f2dd7807 2022-05-17 op sda.worktree = worktree;
9341 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9342 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9343 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9344 f2dd7807 2022-05-17 op sda.repo = repo;
9345 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9346 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9347 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9348 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9349 f2dd7807 2022-05-17 op
9350 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9351 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9352 f2dd7807 2022-05-17 op }
9353 f2dd7807 2022-05-17 op
9354 f2dd7807 2022-05-17 op const struct got_error *
9355 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9356 4bcdc895 2022-05-17 op const char *fileindex_path)
9357 f2dd7807 2022-05-17 op {
9358 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9359 f2dd7807 2022-05-17 op
9360 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9361 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9362 f2dd7807 2022-05-17 op
9363 f2dd7807 2022-05-17 op return err;
9364 f2dd7807 2022-05-17 op }