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 507dc3bb 2018-12-29 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 0e039681 2021-11-15 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 0e039681 2021-11-15 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 0e039681 2021-11-15 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 af54ae4a 2019-02-19 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 11cc08c1 2020-07-23 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 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
996 67a66647 2021-05-31 stsp if (err)
997 67a66647 2021-05-31 stsp goto done;
998 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
999 67a66647 2021-05-31 stsp blob_orig);
1000 67a66647 2021-05-31 stsp if (err)
1001 67a66647 2021-05-31 stsp goto done;
1002 67a66647 2021-05-31 stsp free(base_path);
1003 db590691 2021-06-02 stsp } else {
1004 db590691 2021-06-02 stsp /*
1005 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1006 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1007 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1008 db590691 2021-06-02 stsp */
1009 db590691 2021-06-02 stsp f_orig = got_opentemp();
1010 db590691 2021-06-02 stsp if (f_orig == NULL) {
1011 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1012 db590691 2021-06-02 stsp goto done;
1013 db590691 2021-06-02 stsp }
1014 67a66647 2021-05-31 stsp }
1015 67a66647 2021-05-31 stsp
1016 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1017 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1018 14c901f1 2019-08-08 stsp base_path = NULL;
1019 14c901f1 2019-08-08 stsp goto done;
1020 14c901f1 2019-08-08 stsp }
1021 14c901f1 2019-08-08 stsp
1022 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1023 14c901f1 2019-08-08 stsp if (err)
1024 14c901f1 2019-08-08 stsp goto done;
1025 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1026 14c901f1 2019-08-08 stsp blob_deriv);
1027 14c901f1 2019-08-08 stsp if (err)
1028 14c901f1 2019-08-08 stsp goto done;
1029 14c901f1 2019-08-08 stsp
1030 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1031 14c901f1 2019-08-08 stsp if (err)
1032 14c901f1 2019-08-08 stsp goto done;
1033 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1034 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1035 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1036 14c901f1 2019-08-08 stsp goto done;
1037 eec2f5a9 2021-06-03 stsp }
1038 eec2f5a9 2021-06-03 stsp
1039 5e91dae4 2022-08-30 stsp /*
1040 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1041 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1042 eec2f5a9 2021-06-03 stsp */
1043 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1044 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1045 eec2f5a9 2021-06-03 stsp if (err)
1046 eec2f5a9 2021-06-03 stsp goto done;
1047 eec2f5a9 2021-06-03 stsp } else {
1048 eec2f5a9 2021-06-03 stsp int fd;
1049 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1050 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1051 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1052 eec2f5a9 2021-06-03 stsp goto done;
1053 eec2f5a9 2021-06-03 stsp }
1054 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1055 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1056 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1057 eec2f5a9 2021-06-03 stsp close(fd);
1058 eec2f5a9 2021-06-03 stsp goto done;
1059 eec2f5a9 2021-06-03 stsp }
1060 14c901f1 2019-08-08 stsp }
1061 14c901f1 2019-08-08 stsp
1062 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1063 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1064 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1065 14c901f1 2019-08-08 stsp done:
1066 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1067 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1068 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1069 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1070 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1071 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1072 14c901f1 2019-08-08 stsp free(base_path);
1073 67a66647 2021-05-31 stsp if (blob_orig_path) {
1074 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1075 67a66647 2021-05-31 stsp free(blob_orig_path);
1076 67a66647 2021-05-31 stsp }
1077 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1078 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1079 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1080 14c901f1 2019-08-08 stsp }
1081 6353ad76 2019-02-08 stsp free(id_str);
1082 818c7501 2019-07-11 stsp free(label_deriv);
1083 ed6b5030 2020-10-20 stsp free(parent);
1084 4a1ddfc2 2019-01-12 stsp return err;
1085 4a1ddfc2 2019-01-12 stsp }
1086 4a1ddfc2 2019-01-12 stsp
1087 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1088 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1089 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1090 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1091 13d9040b 2019-03-27 stsp {
1092 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1093 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1094 13d9040b 2019-03-27 stsp
1095 65b05cec 2020-07-23 stsp *new_iep = NULL;
1096 65b05cec 2020-07-23 stsp
1097 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1098 054041d0 2020-06-24 stsp if (err)
1099 054041d0 2020-06-24 stsp return err;
1100 054041d0 2020-06-24 stsp
1101 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1102 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1103 054041d0 2020-06-24 stsp if (err)
1104 054041d0 2020-06-24 stsp goto done;
1105 054041d0 2020-06-24 stsp
1106 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1107 054041d0 2020-06-24 stsp done:
1108 054041d0 2020-06-24 stsp if (err)
1109 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1110 65b05cec 2020-07-23 stsp else
1111 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1112 13d9040b 2019-03-27 stsp return err;
1113 1ebedb77 2019-10-19 stsp }
1114 1ebedb77 2019-10-19 stsp
1115 1ebedb77 2019-10-19 stsp static mode_t
1116 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1117 1ebedb77 2019-10-19 stsp {
1118 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1119 1ebedb77 2019-10-19 stsp
1120 1ebedb77 2019-10-19 stsp if (executable) {
1121 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1124 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1125 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1126 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1127 1ebedb77 2019-10-19 stsp }
1128 1ebedb77 2019-10-19 stsp
1129 b2b3fce1 2022-10-29 op return st_mode;
1130 b2b3fce1 2022-10-29 op }
1131 b2b3fce1 2022-10-29 op
1132 b2b3fce1 2022-10-29 op static mode_t
1133 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1134 b2b3fce1 2022-10-29 op {
1135 b2b3fce1 2022-10-29 op mode_t um;
1136 b2b3fce1 2022-10-29 op
1137 b2b3fce1 2022-10-29 op um = umask(000);
1138 b2b3fce1 2022-10-29 op umask(um);
1139 b2b3fce1 2022-10-29 op return mode & ~um;
1140 13d9040b 2019-03-27 stsp }
1141 13d9040b 2019-03-27 stsp
1142 8ba819a3 2020-07-23 stsp /* forward declaration */
1143 13d9040b 2019-03-27 stsp static const struct got_error *
1144 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1145 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1146 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1147 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1148 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1149 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1150 8ba819a3 2020-07-23 stsp
1151 5a1fbc73 2020-07-23 stsp /*
1152 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1153 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1154 5a1fbc73 2020-07-23 stsp */
1155 8ba819a3 2020-07-23 stsp static const struct got_error *
1156 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1157 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1158 5a1fbc73 2020-07-23 stsp {
1159 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1160 5a1fbc73 2020-07-23 stsp ssize_t elen;
1161 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1162 5a1fbc73 2020-07-23 stsp int fd;
1163 5a1fbc73 2020-07-23 stsp
1164 c6e8a826 2021-04-05 stsp *did_something = 0;
1165 c6e8a826 2021-04-05 stsp
1166 5a1fbc73 2020-07-23 stsp /*
1167 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1168 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1169 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1170 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1171 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1172 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1173 5a1fbc73 2020-07-23 stsp */
1174 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1175 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1176 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1177 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1178 5a1fbc73 2020-07-23 stsp
1179 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1180 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1181 5a1fbc73 2020-07-23 stsp if (elen == -1)
1182 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp
1184 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1185 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1186 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1187 5a1fbc73 2020-07-23 stsp }
1188 5a1fbc73 2020-07-23 stsp
1189 c6e8a826 2021-04-05 stsp *did_something = 1;
1190 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1191 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1192 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1193 5a1fbc73 2020-07-23 stsp return err;
1194 3c1ec782 2020-07-23 stsp }
1195 3c1ec782 2020-07-23 stsp
1196 3c1ec782 2020-07-23 stsp static const struct got_error *
1197 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1198 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 3c1ec782 2020-07-23 stsp {
1200 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1201 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1202 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1203 3c1ec782 2020-07-23 stsp
1204 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1205 3c1ec782 2020-07-23 stsp
1206 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1207 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1208 3c1ec782 2020-07-23 stsp return NULL;
1209 3c1ec782 2020-07-23 stsp }
1210 3c1ec782 2020-07-23 stsp
1211 3c1ec782 2020-07-23 stsp /*
1212 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1213 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1214 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1215 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1216 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1217 3c1ec782 2020-07-23 stsp */
1218 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1219 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1220 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1221 ce031e9e 2020-10-20 stsp if (err)
1222 ce031e9e 2020-10-20 stsp return err;
1223 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 ce031e9e 2020-10-20 stsp free(parent);
1225 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1226 ce031e9e 2020-10-20 stsp }
1227 ce031e9e 2020-10-20 stsp free(parent);
1228 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1229 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1230 3c1ec782 2020-07-23 stsp free(abspath);
1231 3c1ec782 2020-07-23 stsp return err;
1232 3c1ec782 2020-07-23 stsp }
1233 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1234 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp if (err)
1236 3c1ec782 2020-07-23 stsp return err;
1237 3c1ec782 2020-07-23 stsp } else {
1238 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1239 3c1ec782 2020-07-23 stsp if (err)
1240 3c1ec782 2020-07-23 stsp return err;
1241 3c1ec782 2020-07-23 stsp }
1242 3c1ec782 2020-07-23 stsp
1243 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1244 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1245 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1246 3c1ec782 2020-07-23 stsp return NULL;
1247 3c1ec782 2020-07-23 stsp }
1248 3c1ec782 2020-07-23 stsp
1249 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1250 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1251 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1252 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1253 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1254 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1255 3c1ec782 2020-07-23 stsp
1256 3c1ec782 2020-07-23 stsp free(path_got);
1257 3c1ec782 2020-07-23 stsp return NULL;
1258 5a1fbc73 2020-07-23 stsp }
1259 5a1fbc73 2020-07-23 stsp
1260 5a1fbc73 2020-07-23 stsp static const struct got_error *
1261 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1262 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1263 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1264 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1265 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1266 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 9d31a1d8 2018-03-11 stsp {
1268 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1269 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1270 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1271 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1272 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 8ba819a3 2020-07-23 stsp
1274 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1275 2e1fa222 2020-07-23 stsp
1276 3c29341b 2022-07-21 florian /*
1277 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1278 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1279 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1280 8ba819a3 2020-07-23 stsp * in the blob object.
1281 8ba819a3 2020-07-23 stsp */
1282 8ba819a3 2020-07-23 stsp do {
1283 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1284 538b6881 2022-07-21 florian if (err)
1285 538b6881 2022-07-21 florian return err;
1286 538b6881 2022-07-21 florian
1287 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1288 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1289 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1290 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1291 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1292 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1293 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1294 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1295 3b9f0f87 2020-07-23 stsp progress_arg);
1296 8ba819a3 2020-07-23 stsp }
1297 8ba819a3 2020-07-23 stsp if (len > 0) {
1298 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1299 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1300 8ba819a3 2020-07-23 stsp len - hdrlen);
1301 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1302 8ba819a3 2020-07-23 stsp hdrlen = 0;
1303 8ba819a3 2020-07-23 stsp }
1304 8ba819a3 2020-07-23 stsp } while (len != 0);
1305 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1306 8ba819a3 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1308 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1309 3c1ec782 2020-07-23 stsp if (err)
1310 3c1ec782 2020-07-23 stsp return err;
1311 8ba819a3 2020-07-23 stsp
1312 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1313 906c123b 2020-07-23 stsp /* install as a regular file */
1314 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1315 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1316 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1317 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1318 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1319 3c29341b 2022-07-21 florian return err;
1320 906c123b 2020-07-23 stsp }
1321 906c123b 2020-07-23 stsp
1322 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1323 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1324 c6e8a826 2021-04-05 stsp int symlink_replaced;
1325 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1326 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1327 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1328 3c29341b 2022-07-21 florian return err;
1329 c90c8ce3 2020-07-23 stsp }
1330 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1331 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1332 5a1fbc73 2020-07-23 stsp if (err)
1333 3c29341b 2022-07-21 florian return err;
1334 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1335 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1336 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1337 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1338 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1340 c6e8a826 2021-04-05 stsp } else {
1341 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1343 c6e8a826 2021-04-05 stsp }
1344 f35fa46a 2020-07-23 stsp }
1345 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1346 f35fa46a 2020-07-23 stsp }
1347 f35fa46a 2020-07-23 stsp
1348 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1349 f4994adc 2020-10-20 stsp char *parent;
1350 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1351 f4994adc 2020-10-20 stsp if (err)
1352 3c29341b 2022-07-21 florian return err;
1353 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1354 f4994adc 2020-10-20 stsp free(parent);
1355 f35fa46a 2020-07-23 stsp if (err)
1356 3c29341b 2022-07-21 florian return err;
1357 f35fa46a 2020-07-23 stsp /*
1358 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1359 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1360 f35fa46a 2020-07-23 stsp */
1361 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1362 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1363 3c29341b 2022-07-21 florian return err;
1364 f35fa46a 2020-07-23 stsp }
1365 f35fa46a 2020-07-23 stsp }
1366 f35fa46a 2020-07-23 stsp
1367 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1368 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1369 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1370 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1371 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1372 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1373 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1374 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1375 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1376 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1377 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1378 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1379 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1380 8ba819a3 2020-07-23 stsp } else {
1381 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1382 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1383 8ba819a3 2020-07-23 stsp }
1384 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1385 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1386 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1387 8ba819a3 2020-07-23 stsp return err;
1388 8ba819a3 2020-07-23 stsp }
1389 8ba819a3 2020-07-23 stsp
1390 8ba819a3 2020-07-23 stsp static const struct got_error *
1391 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1392 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1393 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1394 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1395 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1396 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 8ba819a3 2020-07-23 stsp {
1398 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1399 507dc3bb 2018-12-29 stsp int fd = -1;
1400 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1401 507dc3bb 2018-12-29 stsp int update = 0;
1402 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1403 b2b3fce1 2022-10-29 op mode_t mode;
1404 8ba819a3 2020-07-23 stsp
1405 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1406 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1407 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1408 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1409 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1410 f5375317 2020-10-20 stsp char *parent;
1411 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1412 f5375317 2020-10-20 stsp if (err)
1413 f5375317 2020-10-20 stsp return err;
1414 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1415 f5375317 2020-10-20 stsp free(parent);
1416 21908da4 2019-01-13 stsp if (err)
1417 21908da4 2019-01-13 stsp return err;
1418 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1419 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1420 b2b3fce1 2022-10-29 op mode);
1421 21908da4 2019-01-13 stsp if (fd == -1)
1422 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1423 230a42bd 2019-05-11 jcs ondisk_path);
1424 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1425 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1426 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1427 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1428 3b9f0f87 2020-07-23 stsp goto done;
1429 3b9f0f87 2020-07-23 stsp }
1430 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1431 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1432 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1433 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1434 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1435 507dc3bb 2018-12-29 stsp goto done;
1436 d70b8e30 2018-12-27 stsp } else {
1437 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1438 507dc3bb 2018-12-29 stsp ondisk_path);
1439 507dc3bb 2018-12-29 stsp if (err)
1440 507dc3bb 2018-12-29 stsp goto done;
1441 507dc3bb 2018-12-29 stsp update = 1;
1442 b2b3fce1 2022-10-29 op
1443 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1444 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1445 b2b3fce1 2022-10-29 op tmppath);
1446 b2b3fce1 2022-10-29 op goto done;
1447 b2b3fce1 2022-10-29 op }
1448 9d31a1d8 2018-03-11 stsp }
1449 507dc3bb 2018-12-29 stsp } else
1450 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1451 3818e3c4 2020-11-01 naddy }
1452 3818e3c4 2020-11-01 naddy
1453 bd6aa359 2020-07-23 stsp if (progress_cb) {
1454 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1455 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1456 bd6aa359 2020-07-23 stsp path);
1457 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1458 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1459 bd6aa359 2020-07-23 stsp path);
1460 bd6aa359 2020-07-23 stsp else
1461 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1462 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1463 bd6aa359 2020-07-23 stsp if (err)
1464 bd6aa359 2020-07-23 stsp goto done;
1465 bd6aa359 2020-07-23 stsp }
1466 d7b62c98 2018-12-27 stsp
1467 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1468 9d31a1d8 2018-03-11 stsp do {
1469 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1470 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1471 9d31a1d8 2018-03-11 stsp if (err)
1472 9d31a1d8 2018-03-11 stsp break;
1473 9d31a1d8 2018-03-11 stsp if (len > 0) {
1474 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1475 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1476 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1477 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1478 b87c6f83 2018-12-24 stsp goto done;
1479 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1480 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1481 b87c6f83 2018-12-24 stsp goto done;
1482 9d31a1d8 2018-03-11 stsp }
1483 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1484 9d31a1d8 2018-03-11 stsp }
1485 9d31a1d8 2018-03-11 stsp } while (len != 0);
1486 9d31a1d8 2018-03-11 stsp
1487 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1488 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1489 816dc654 2019-02-16 stsp goto done;
1490 816dc654 2019-02-16 stsp }
1491 9d31a1d8 2018-03-11 stsp
1492 507dc3bb 2018-12-29 stsp if (update) {
1493 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1494 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1495 f6d8c0ac 2020-11-09 stsp goto done;
1496 f6d8c0ac 2020-11-09 stsp }
1497 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1499 230a42bd 2019-05-11 jcs ondisk_path);
1500 68ed9ba5 2019-02-10 stsp goto done;
1501 68ed9ba5 2019-02-10 stsp }
1502 63df146d 2020-11-09 stsp free(tmppath);
1503 63df146d 2020-11-09 stsp tmppath = NULL;
1504 507dc3bb 2018-12-29 stsp }
1505 507dc3bb 2018-12-29 stsp
1506 9d31a1d8 2018-03-11 stsp done:
1507 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1508 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1509 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1510 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1511 507dc3bb 2018-12-29 stsp free(tmppath);
1512 6353ad76 2019-02-08 stsp return err;
1513 6353ad76 2019-02-08 stsp }
1514 6353ad76 2019-02-08 stsp
1515 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1516 6353ad76 2019-02-08 stsp static const struct got_error *
1517 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1518 7154f6ce 2019-03-27 stsp {
1519 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1520 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1521 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1522 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1523 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1524 7154f6ce 2019-03-27 stsp };
1525 7154f6ce 2019-03-27 stsp int i = 0;
1526 9bdd68dd 2021-01-02 naddy char *line = NULL;
1527 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1528 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1529 7154f6ce 2019-03-27 stsp
1530 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1531 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1532 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1533 7154f6ce 2019-03-27 stsp if (feof(f))
1534 7154f6ce 2019-03-27 stsp break;
1535 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1536 7154f6ce 2019-03-27 stsp break;
1537 7154f6ce 2019-03-27 stsp }
1538 7154f6ce 2019-03-27 stsp
1539 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1540 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1541 19332e6d 2019-05-13 stsp == 0)
1542 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1543 7154f6ce 2019-03-27 stsp else
1544 7154f6ce 2019-03-27 stsp i++;
1545 7154f6ce 2019-03-27 stsp }
1546 7154f6ce 2019-03-27 stsp }
1547 9bdd68dd 2021-01-02 naddy free(line);
1548 7154f6ce 2019-03-27 stsp
1549 7154f6ce 2019-03-27 stsp return err;
1550 e2b1e152 2019-07-13 stsp }
1551 e2b1e152 2019-07-13 stsp
1552 e2b1e152 2019-07-13 stsp static int
1553 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1554 1ebedb77 2019-10-19 stsp {
1555 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1556 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1557 1ebedb77 2019-10-19 stsp }
1558 1ebedb77 2019-10-19 stsp
1559 1ebedb77 2019-10-19 stsp static int
1560 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1561 e2b1e152 2019-07-13 stsp {
1562 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1563 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1564 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1565 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1566 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1567 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1568 c363b2c1 2019-08-03 stsp }
1569 c363b2c1 2019-08-03 stsp
1570 c363b2c1 2019-08-03 stsp static unsigned char
1571 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1572 c363b2c1 2019-08-03 stsp {
1573 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1574 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1575 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1576 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1577 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1578 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1579 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1580 c363b2c1 2019-08-03 stsp default:
1581 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1582 a919d5c4 2020-07-23 stsp }
1583 a919d5c4 2020-07-23 stsp }
1584 a919d5c4 2020-07-23 stsp
1585 a919d5c4 2020-07-23 stsp static const struct got_error *
1586 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1587 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1588 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1589 a919d5c4 2020-07-23 stsp {
1590 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1591 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1592 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1593 a919d5c4 2020-07-23 stsp ssize_t elen;
1594 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1595 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1596 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1597 a919d5c4 2020-07-23 stsp
1598 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1599 a919d5c4 2020-07-23 stsp
1600 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1601 a919d5c4 2020-07-23 stsp do {
1602 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1603 a919d5c4 2020-07-23 stsp if (err)
1604 a919d5c4 2020-07-23 stsp return err;
1605 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1606 a919d5c4 2020-07-23 stsp /*
1607 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1608 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1609 a919d5c4 2020-07-23 stsp */
1610 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1611 a919d5c4 2020-07-23 stsp }
1612 a919d5c4 2020-07-23 stsp if (len > 0) {
1613 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1614 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1615 a919d5c4 2020-07-23 stsp len - hdrlen);
1616 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1617 a919d5c4 2020-07-23 stsp hdrlen = 0;
1618 a919d5c4 2020-07-23 stsp }
1619 a919d5c4 2020-07-23 stsp } while (len != 0);
1620 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1621 a919d5c4 2020-07-23 stsp
1622 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1623 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1624 a919d5c4 2020-07-23 stsp if (elen == -1)
1625 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1626 a919d5c4 2020-07-23 stsp } else {
1627 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1628 a919d5c4 2020-07-23 stsp if (elen == -1)
1629 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1630 c363b2c1 2019-08-03 stsp }
1631 a919d5c4 2020-07-23 stsp
1632 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1633 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1634 a919d5c4 2020-07-23 stsp
1635 a919d5c4 2020-07-23 stsp return NULL;
1636 7154f6ce 2019-03-27 stsp }
1637 7154f6ce 2019-03-27 stsp
1638 7154f6ce 2019-03-27 stsp static const struct got_error *
1639 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1640 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1641 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1642 6353ad76 2019-02-08 stsp {
1643 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1644 6353ad76 2019-02-08 stsp struct got_object_id id;
1645 6353ad76 2019-02-08 stsp size_t hdrlen;
1646 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1647 6353ad76 2019-02-08 stsp FILE *f = NULL;
1648 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1649 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1650 6353ad76 2019-02-08 stsp size_t flen, blen;
1651 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1652 6353ad76 2019-02-08 stsp
1653 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1654 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1655 6353ad76 2019-02-08 stsp
1656 7f91a133 2019-12-13 stsp /*
1657 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1658 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1659 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1660 7f91a133 2019-12-13 stsp */
1661 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1662 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1663 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1664 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1665 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1666 882ef1b9 2019-12-13 stsp else
1667 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1668 882ef1b9 2019-12-13 stsp goto done;
1669 882ef1b9 2019-12-13 stsp }
1670 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1671 3d35a492 2019-12-13 stsp goto done;
1672 3d35a492 2019-12-13 stsp }
1673 7f91a133 2019-12-13 stsp } else {
1674 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1675 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1676 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1677 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1678 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1679 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1680 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1681 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1682 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1683 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1684 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1685 3d35a492 2019-12-13 stsp else
1686 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1687 3d35a492 2019-12-13 stsp goto done;
1688 3d35a492 2019-12-13 stsp }
1689 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1690 1338848f 2019-12-13 stsp goto done;
1691 a378724f 2019-02-10 stsp }
1692 a378724f 2019-02-10 stsp }
1693 6353ad76 2019-02-08 stsp
1694 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1695 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1696 1338848f 2019-12-13 stsp goto done;
1697 3f148bc6 2019-07-27 stsp }
1698 efdd40df 2019-07-27 stsp
1699 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1700 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1701 1338848f 2019-12-13 stsp goto done;
1702 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1703 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1704 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1705 1338848f 2019-12-13 stsp goto done;
1706 d00136be 2019-03-26 stsp }
1707 b8f41171 2019-02-10 stsp
1708 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1709 f179e66d 2020-07-23 stsp goto done;
1710 f179e66d 2020-07-23 stsp
1711 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1712 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1713 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1714 1338848f 2019-12-13 stsp goto done;
1715 f179e66d 2020-07-23 stsp }
1716 6353ad76 2019-02-08 stsp
1717 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1718 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1719 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1720 c363b2c1 2019-08-03 stsp else
1721 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1722 c363b2c1 2019-08-03 stsp
1723 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1724 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1725 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1726 eb81bc23 2022-06-28 tracey goto done;
1727 eb81bc23 2022-06-28 tracey }
1728 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1729 6353ad76 2019-02-08 stsp if (err)
1730 1338848f 2019-12-13 stsp goto done;
1731 6353ad76 2019-02-08 stsp
1732 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1733 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1734 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1735 a919d5c4 2020-07-23 stsp goto done;
1736 a919d5c4 2020-07-23 stsp }
1737 a919d5c4 2020-07-23 stsp
1738 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1739 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 ab0d4361 2019-12-13 stsp if (fd == -1) {
1741 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1742 ab0d4361 2019-12-13 stsp goto done;
1743 ab0d4361 2019-12-13 stsp }
1744 3d35a492 2019-12-13 stsp }
1745 3d35a492 2019-12-13 stsp
1746 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1747 6353ad76 2019-02-08 stsp if (f == NULL) {
1748 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1749 6353ad76 2019-02-08 stsp goto done;
1750 6353ad76 2019-02-08 stsp }
1751 1338848f 2019-12-13 stsp fd = -1;
1752 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1753 656b1f76 2019-05-11 jcs for (;;) {
1754 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1755 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1756 6353ad76 2019-02-08 stsp if (err)
1757 7154f6ce 2019-03-27 stsp goto done;
1758 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1759 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1760 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1761 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1762 7154f6ce 2019-03-27 stsp goto done;
1763 80c5c120 2019-02-19 stsp }
1764 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1765 6353ad76 2019-02-08 stsp if (flen != 0)
1766 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1767 6353ad76 2019-02-08 stsp break;
1768 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1769 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1770 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1771 6353ad76 2019-02-08 stsp break;
1772 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1773 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1774 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1775 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1776 6353ad76 2019-02-08 stsp break;
1777 6353ad76 2019-02-08 stsp }
1778 6353ad76 2019-02-08 stsp } else {
1779 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1780 6353ad76 2019-02-08 stsp break;
1781 6353ad76 2019-02-08 stsp }
1782 6353ad76 2019-02-08 stsp hdrlen = 0;
1783 6353ad76 2019-02-08 stsp }
1784 7154f6ce 2019-03-27 stsp
1785 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1786 7154f6ce 2019-03-27 stsp rewind(f);
1787 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1788 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1789 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1790 6353ad76 2019-02-08 stsp done:
1791 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1792 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1793 6353ad76 2019-02-08 stsp if (blob)
1794 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1795 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1796 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1797 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1798 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1799 9d31a1d8 2018-03-11 stsp return err;
1800 e2b1e152 2019-07-13 stsp }
1801 e2b1e152 2019-07-13 stsp
1802 e2b1e152 2019-07-13 stsp /*
1803 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1804 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1805 e2b1e152 2019-07-13 stsp */
1806 e2b1e152 2019-07-13 stsp static const struct got_error *
1807 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1808 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1809 e2b1e152 2019-07-13 stsp {
1810 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1811 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1812 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1813 e2b1e152 2019-07-13 stsp
1814 e2b1e152 2019-07-13 stsp return NULL;
1815 9d31a1d8 2018-03-11 stsp }
1816 9d31a1d8 2018-03-11 stsp
1817 9d31a1d8 2018-03-11 stsp static const struct got_error *
1818 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1819 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1820 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1821 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1822 0584f854 2019-04-06 stsp void *progress_arg)
1823 9d31a1d8 2018-03-11 stsp {
1824 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1825 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1826 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1827 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1828 b8f41171 2019-02-10 stsp struct stat sb;
1829 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1830 9d31a1d8 2018-03-11 stsp
1831 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1833 6353ad76 2019-02-08 stsp
1834 abb4604f 2019-07-27 stsp if (ie) {
1835 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 a76c42e6 2019-08-03 stsp goto done;
1838 a76c42e6 2019-08-03 stsp }
1839 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1840 7f91a133 2019-12-13 stsp repo);
1841 abb4604f 2019-07-27 stsp if (err)
1842 abb4604f 2019-07-27 stsp goto done;
1843 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1845 c90c8ce3 2020-07-23 stsp } else {
1846 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1847 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1848 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1849 f6764181 2021-09-24 stsp ondisk_path);
1850 f6764181 2021-09-24 stsp goto done;
1851 f6764181 2021-09-24 stsp }
1852 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1853 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1854 f6764181 2021-09-24 stsp } else {
1855 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1856 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1857 f6764181 2021-09-24 stsp else
1858 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1859 f6764181 2021-09-24 stsp }
1860 c90c8ce3 2020-07-23 stsp }
1861 6353ad76 2019-02-08 stsp
1862 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1863 2c41dce7 2021-06-27 stsp if (ie)
1864 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1865 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1866 b8f41171 2019-02-10 stsp goto done;
1867 b8f41171 2019-02-10 stsp }
1868 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1869 a769b60b 2021-06-27 stsp if (ie)
1870 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1871 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1872 5036ab18 2020-04-18 stsp path);
1873 5036ab18 2020-04-18 stsp goto done;
1874 5036ab18 2020-04-18 stsp }
1875 b8f41171 2019-02-10 stsp
1876 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1877 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1878 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1879 c6e8a826 2021-04-05 stsp /*
1880 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1881 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1882 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1883 c6e8a826 2021-04-05 stsp * updating contents of this file.
1884 c6e8a826 2021-04-05 stsp */
1885 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1886 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1887 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1888 c6e8a826 2021-04-05 stsp /* Same commit. */
1889 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1890 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1891 e2b1e152 2019-07-13 stsp if (err)
1892 e2b1e152 2019-07-13 stsp goto done;
1893 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1894 b8f41171 2019-02-10 stsp path);
1895 6353ad76 2019-02-08 stsp goto done;
1896 a378724f 2019-02-10 stsp }
1897 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1898 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1899 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1900 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1901 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1902 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1903 0f58026f 2021-04-05 stsp if (err)
1904 0f58026f 2021-04-05 stsp goto done;
1905 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1906 0f58026f 2021-04-05 stsp path);
1907 b8f41171 2019-02-10 stsp goto done;
1908 e2b1e152 2019-07-13 stsp }
1909 9d31a1d8 2018-03-11 stsp }
1910 9d31a1d8 2018-03-11 stsp
1911 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1912 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1913 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1914 eb81bc23 2022-06-28 tracey goto done;
1915 eb81bc23 2022-06-28 tracey }
1916 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1917 8da9e5f4 2019-01-12 stsp if (err)
1918 6353ad76 2019-02-08 stsp goto done;
1919 512f0d0e 2019-01-02 stsp
1920 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1921 234035bc 2019-06-01 stsp int update_timestamps;
1922 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1923 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1924 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1925 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1926 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1927 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1928 eb81bc23 2022-06-28 tracey goto done;
1929 eb81bc23 2022-06-28 tracey }
1930 234035bc 2019-06-01 stsp struct got_object_id id2;
1931 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1933 eb81bc23 2022-06-28 tracey fd2);
1934 234035bc 2019-06-01 stsp if (err)
1935 f69721c3 2019-10-21 stsp goto done;
1936 f69721c3 2019-10-21 stsp }
1937 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1938 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1939 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1940 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1941 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1942 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1943 f69721c3 2019-10-21 stsp goto done;
1944 f69721c3 2019-10-21 stsp }
1945 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1946 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1947 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1948 234035bc 2019-06-01 stsp goto done;
1949 f69721c3 2019-10-21 stsp }
1950 234035bc 2019-06-01 stsp }
1951 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1952 36bf999c 2020-07-23 stsp char *link_target;
1953 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1954 36bf999c 2020-07-23 stsp if (err)
1955 36bf999c 2020-07-23 stsp goto done;
1956 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1957 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1958 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1959 36bf999c 2020-07-23 stsp free(link_target);
1960 993e2a1b 2020-07-23 stsp } else {
1961 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1962 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1963 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1964 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1965 993e2a1b 2020-07-23 stsp }
1966 f69721c3 2019-10-21 stsp free(label_orig);
1967 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1968 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1969 eb81bc23 2022-06-28 tracey goto done;
1970 eb81bc23 2022-06-28 tracey }
1971 234035bc 2019-06-01 stsp if (blob2)
1972 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1973 909d120e 2019-09-22 stsp if (err)
1974 909d120e 2019-09-22 stsp goto done;
1975 234035bc 2019-06-01 stsp /*
1976 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1977 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1978 234035bc 2019-06-01 stsp * unmodified files again.
1979 234035bc 2019-06-01 stsp */
1980 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1981 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1982 234035bc 2019-06-01 stsp update_timestamps);
1983 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1984 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1985 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1986 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1987 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1988 1ee397ad 2019-07-12 stsp if (err)
1989 1ee397ad 2019-07-12 stsp goto done;
1990 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1992 13d9040b 2019-03-27 stsp if (err)
1993 13d9040b 2019-03-27 stsp goto done;
1994 13d9040b 2019-03-27 stsp } else {
1995 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1996 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1997 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1998 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1999 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2000 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2001 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2002 2e1fa222 2020-07-23 stsp } else {
2003 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2004 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2005 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2006 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2007 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2008 2e1fa222 2020-07-23 stsp }
2009 13d9040b 2019-03-27 stsp if (err)
2010 13d9040b 2019-03-27 stsp goto done;
2011 65b05cec 2020-07-23 stsp
2012 054041d0 2020-06-24 stsp if (ie) {
2013 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2014 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2015 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2016 054041d0 2020-06-24 stsp } else {
2017 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2018 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2019 054041d0 2020-06-24 stsp &blob->id);
2020 054041d0 2020-06-24 stsp }
2021 13d9040b 2019-03-27 stsp if (err)
2022 13d9040b 2019-03-27 stsp goto done;
2023 65b05cec 2020-07-23 stsp
2024 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2025 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2026 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2027 2e1fa222 2020-07-23 stsp }
2028 13d9040b 2019-03-27 stsp }
2029 eb81bc23 2022-06-28 tracey
2030 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2031 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2032 eb81bc23 2022-06-28 tracey goto done;
2033 eb81bc23 2022-06-28 tracey }
2034 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2035 6353ad76 2019-02-08 stsp done:
2036 6353ad76 2019-02-08 stsp free(ondisk_path);
2037 8da9e5f4 2019-01-12 stsp return err;
2038 90285c3b 2019-01-08 stsp }
2039 90285c3b 2019-01-08 stsp
2040 90285c3b 2019-01-08 stsp static const struct got_error *
2041 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2042 90285c3b 2019-01-08 stsp {
2043 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2044 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2045 90285c3b 2019-01-08 stsp
2046 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2047 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2048 90285c3b 2019-01-08 stsp
2049 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2050 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2051 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2052 c97665ae 2019-01-13 stsp } else {
2053 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2054 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2055 1c4cdd89 2021-06-20 stsp if (err)
2056 1c4cdd89 2021-06-20 stsp goto done;
2057 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2058 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2059 fddefe3b 2020-10-20 stsp free(ondisk_path);
2060 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2061 1c4cdd89 2021-06-20 stsp parent = NULL;
2062 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2063 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2064 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2065 fddefe3b 2020-10-20 stsp ondisk_path);
2066 90285c3b 2019-01-08 stsp break;
2067 90285c3b 2019-01-08 stsp }
2068 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2069 1c4cdd89 2021-06-20 stsp if (err)
2070 1c4cdd89 2021-06-20 stsp break;
2071 1c4cdd89 2021-06-20 stsp }
2072 90285c3b 2019-01-08 stsp }
2073 1c4cdd89 2021-06-20 stsp done:
2074 90285c3b 2019-01-08 stsp free(ondisk_path);
2075 1c4cdd89 2021-06-20 stsp free(parent);
2076 90285c3b 2019-01-08 stsp return err;
2077 512f0d0e 2019-01-02 stsp }
2078 512f0d0e 2019-01-02 stsp
2079 708d8e67 2019-03-27 stsp static const struct got_error *
2080 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2081 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2082 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2083 708d8e67 2019-03-27 stsp {
2084 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2085 708d8e67 2019-03-27 stsp unsigned char status;
2086 708d8e67 2019-03-27 stsp struct stat sb;
2087 708d8e67 2019-03-27 stsp char *ondisk_path;
2088 708d8e67 2019-03-27 stsp
2089 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2090 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2091 a76c42e6 2019-08-03 stsp
2092 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2093 708d8e67 2019-03-27 stsp == -1)
2094 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2095 708d8e67 2019-03-27 stsp
2096 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2097 708d8e67 2019-03-27 stsp if (err)
2098 5a58a424 2020-06-23 stsp goto done;
2099 708d8e67 2019-03-27 stsp
2100 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2101 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2102 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2103 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2104 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2105 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2106 993e2a1b 2020-07-23 stsp goto done;
2107 993e2a1b 2020-07-23 stsp }
2108 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2109 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2110 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2111 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2112 993e2a1b 2020-07-23 stsp if (err)
2113 993e2a1b 2020-07-23 stsp goto done;
2114 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2115 993e2a1b 2020-07-23 stsp ie->path);
2116 993e2a1b 2020-07-23 stsp goto done;
2117 993e2a1b 2020-07-23 stsp }
2118 993e2a1b 2020-07-23 stsp
2119 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2120 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2121 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2122 1ee397ad 2019-07-12 stsp if (err)
2123 5a58a424 2020-06-23 stsp goto done;
2124 fc6346c4 2019-03-27 stsp /*
2125 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2126 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2127 fc6346c4 2019-03-27 stsp */
2128 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2129 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2130 fc6346c4 2019-03-27 stsp } else {
2131 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2132 1ee397ad 2019-07-12 stsp if (err)
2133 5a58a424 2020-06-23 stsp goto done;
2134 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2135 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2136 fc6346c4 2019-03-27 stsp if (err)
2137 5a58a424 2020-06-23 stsp goto done;
2138 fc6346c4 2019-03-27 stsp }
2139 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2140 708d8e67 2019-03-27 stsp }
2141 5a58a424 2020-06-23 stsp done:
2142 5a58a424 2020-06-23 stsp free(ondisk_path);
2143 fc6346c4 2019-03-27 stsp return err;
2144 708d8e67 2019-03-27 stsp }
2145 708d8e67 2019-03-27 stsp
2146 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2147 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2148 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2149 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2150 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2151 8da9e5f4 2019-01-12 stsp void *progress_arg;
2152 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2153 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2154 8da9e5f4 2019-01-12 stsp };
2155 8da9e5f4 2019-01-12 stsp
2156 512f0d0e 2019-01-02 stsp static const struct got_error *
2157 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2158 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2159 512f0d0e 2019-01-02 stsp {
2160 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2161 0584f854 2019-04-06 stsp
2162 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2163 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2164 512f0d0e 2019-01-02 stsp
2165 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2166 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2167 8da9e5f4 2019-01-12 stsp }
2168 512f0d0e 2019-01-02 stsp
2169 8da9e5f4 2019-01-12 stsp static const struct got_error *
2170 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2171 8da9e5f4 2019-01-12 stsp {
2172 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2173 7a9df742 2019-01-08 stsp
2174 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2176 0584f854 2019-04-06 stsp
2177 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2178 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2179 9d31a1d8 2018-03-11 stsp }
2180 9d31a1d8 2018-03-11 stsp
2181 9d31a1d8 2018-03-11 stsp static const struct got_error *
2182 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2183 9d31a1d8 2018-03-11 stsp {
2184 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2185 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2186 8da9e5f4 2019-01-12 stsp char *path;
2187 9d31a1d8 2018-03-11 stsp
2188 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2189 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2190 0584f854 2019-04-06 stsp
2191 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2192 63c5ca5d 2019-08-24 stsp return NULL;
2193 63c5ca5d 2019-08-24 stsp
2194 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2195 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2196 8da9e5f4 2019-01-12 stsp == -1)
2197 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2198 9d31a1d8 2018-03-11 stsp
2199 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2200 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2201 8da9e5f4 2019-01-12 stsp else
2202 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2203 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2204 9d31a1d8 2018-03-11 stsp
2205 8da9e5f4 2019-01-12 stsp free(path);
2206 0cd1c46a 2019-03-11 stsp return err;
2207 0cd1c46a 2019-03-11 stsp }
2208 0cd1c46a 2019-03-11 stsp
2209 b2118c49 2020-07-28 stsp const struct got_error *
2210 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2211 b2118c49 2020-07-28 stsp {
2212 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2213 b2118c49 2020-07-28 stsp
2214 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2215 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2216 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2217 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2218 b2118c49 2020-07-28 stsp }
2219 b2118c49 2020-07-28 stsp
2220 b2118c49 2020-07-28 stsp return NULL;
2221 b2118c49 2020-07-28 stsp }
2222 b2118c49 2020-07-28 stsp
2223 818c7501 2019-07-11 stsp static const struct got_error *
2224 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2225 0cd1c46a 2019-03-11 stsp {
2226 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2227 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2228 0cd1c46a 2019-03-11 stsp
2229 517bab73 2019-03-11 stsp *refname = NULL;
2230 517bab73 2019-03-11 stsp
2231 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2232 b2118c49 2020-07-28 stsp if (err)
2233 b2118c49 2020-07-28 stsp return err;
2234 0cd1c46a 2019-03-11 stsp
2235 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2236 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2237 0647c563 2019-03-11 stsp *refname = NULL;
2238 0cd1c46a 2019-03-11 stsp }
2239 517bab73 2019-03-11 stsp free(uuidstr);
2240 517bab73 2019-03-11 stsp return err;
2241 517bab73 2019-03-11 stsp }
2242 517bab73 2019-03-11 stsp
2243 818c7501 2019-07-11 stsp const struct got_error *
2244 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2245 818c7501 2019-07-11 stsp {
2246 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2247 818c7501 2019-07-11 stsp }
2248 818c7501 2019-07-11 stsp
2249 818c7501 2019-07-11 stsp static const struct got_error *
2250 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2251 818c7501 2019-07-11 stsp {
2252 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2253 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2254 818c7501 2019-07-11 stsp }
2255 818c7501 2019-07-11 stsp
2256 818c7501 2019-07-11 stsp static const struct got_error *
2257 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2258 818c7501 2019-07-11 stsp {
2259 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2260 818c7501 2019-07-11 stsp }
2261 818c7501 2019-07-11 stsp
2262 818c7501 2019-07-11 stsp static const struct got_error *
2263 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2264 818c7501 2019-07-11 stsp {
2265 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2266 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2267 818c7501 2019-07-11 stsp }
2268 818c7501 2019-07-11 stsp
2269 818c7501 2019-07-11 stsp static const struct got_error *
2270 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2271 818c7501 2019-07-11 stsp {
2272 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2273 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2274 0ebf8283 2019-07-24 stsp }
2275 0ebf8283 2019-07-24 stsp
2276 0ebf8283 2019-07-24 stsp static const struct got_error *
2277 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2278 0ebf8283 2019-07-24 stsp {
2279 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2280 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2281 0ebf8283 2019-07-24 stsp }
2282 0ebf8283 2019-07-24 stsp
2283 0ebf8283 2019-07-24 stsp static const struct got_error *
2284 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2285 0ebf8283 2019-07-24 stsp {
2286 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2287 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2288 0ebf8283 2019-07-24 stsp }
2289 0ebf8283 2019-07-24 stsp
2290 0ebf8283 2019-07-24 stsp static const struct got_error *
2291 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2292 0ebf8283 2019-07-24 stsp {
2293 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2294 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2295 818c7501 2019-07-11 stsp }
2296 818c7501 2019-07-11 stsp
2297 0ebf8283 2019-07-24 stsp static const struct got_error *
2298 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2299 0ebf8283 2019-07-24 stsp {
2300 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2301 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2302 0ebf8283 2019-07-24 stsp }
2303 818c7501 2019-07-11 stsp
2304 0ebf8283 2019-07-24 stsp const struct got_error *
2305 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2306 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2307 0ebf8283 2019-07-24 stsp {
2308 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2309 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2310 0ebf8283 2019-07-24 stsp *path = NULL;
2311 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2312 0ebf8283 2019-07-24 stsp }
2313 0ebf8283 2019-07-24 stsp return NULL;
2314 f259c4c1 2021-09-24 stsp }
2315 f259c4c1 2021-09-24 stsp
2316 f259c4c1 2021-09-24 stsp static const struct got_error *
2317 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2318 f259c4c1 2021-09-24 stsp {
2319 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2320 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2321 0ebf8283 2019-07-24 stsp }
2322 0ebf8283 2019-07-24 stsp
2323 f259c4c1 2021-09-24 stsp static const struct got_error *
2324 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2325 f259c4c1 2021-09-24 stsp {
2326 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2327 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2328 f259c4c1 2021-09-24 stsp }
2329 f259c4c1 2021-09-24 stsp
2330 517bab73 2019-03-11 stsp /*
2331 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2332 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2333 517bab73 2019-03-11 stsp */
2334 517bab73 2019-03-11 stsp static const struct got_error *
2335 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2336 517bab73 2019-03-11 stsp {
2337 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2338 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2339 517bab73 2019-03-11 stsp char *refname;
2340 517bab73 2019-03-11 stsp
2341 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2342 517bab73 2019-03-11 stsp if (err)
2343 517bab73 2019-03-11 stsp return err;
2344 0cd1c46a 2019-03-11 stsp
2345 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2346 0cd1c46a 2019-03-11 stsp if (err)
2347 0cd1c46a 2019-03-11 stsp goto done;
2348 0cd1c46a 2019-03-11 stsp
2349 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2350 0cd1c46a 2019-03-11 stsp done:
2351 0cd1c46a 2019-03-11 stsp free(refname);
2352 0cd1c46a 2019-03-11 stsp if (ref)
2353 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2354 3e3a69f1 2019-07-25 stsp return err;
2355 3e3a69f1 2019-07-25 stsp }
2356 3e3a69f1 2019-07-25 stsp
2357 3e3a69f1 2019-07-25 stsp static const struct got_error *
2358 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2359 3e3a69f1 2019-07-25 stsp {
2360 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2361 3e3a69f1 2019-07-25 stsp
2362 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2363 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2364 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2365 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2366 3e3a69f1 2019-07-25 stsp }
2367 9d31a1d8 2018-03-11 stsp return err;
2368 9d31a1d8 2018-03-11 stsp }
2369 9d31a1d8 2018-03-11 stsp
2370 3e3a69f1 2019-07-25 stsp
2371 ebf99748 2019-05-09 stsp static const struct got_error *
2372 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2373 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2374 ebf99748 2019-05-09 stsp {
2375 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2376 ebf99748 2019-05-09 stsp FILE *index = NULL;
2377 0cd1c46a 2019-03-11 stsp
2378 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2379 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2380 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2381 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2382 ebf99748 2019-05-09 stsp
2383 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2384 3e3a69f1 2019-07-25 stsp if (err)
2385 ebf99748 2019-05-09 stsp goto done;
2386 ebf99748 2019-05-09 stsp
2387 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2388 ebf99748 2019-05-09 stsp if (index == NULL) {
2389 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2390 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2391 ebf99748 2019-05-09 stsp } else {
2392 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2393 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2394 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2395 ebf99748 2019-05-09 stsp }
2396 ebf99748 2019-05-09 stsp done:
2397 ebf99748 2019-05-09 stsp if (err) {
2398 ebf99748 2019-05-09 stsp free(*fileindex_path);
2399 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2400 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2401 ebf99748 2019-05-09 stsp *fileindex = NULL;
2402 ebf99748 2019-05-09 stsp }
2403 ebf99748 2019-05-09 stsp return err;
2404 ebf99748 2019-05-09 stsp }
2405 c932eeeb 2019-05-22 stsp
2406 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2407 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2408 c932eeeb 2019-05-22 stsp const char *path;
2409 c932eeeb 2019-05-22 stsp size_t path_len;
2410 c932eeeb 2019-05-22 stsp const char *entry_name;
2411 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2412 a484d721 2019-06-10 stsp void *progress_arg;
2413 c932eeeb 2019-05-22 stsp };
2414 ebf99748 2019-05-09 stsp
2415 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2416 c932eeeb 2019-05-22 stsp static const struct got_error *
2417 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2418 c932eeeb 2019-05-22 stsp {
2419 1ee397ad 2019-07-12 stsp const struct got_error *err;
2420 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2421 c932eeeb 2019-05-22 stsp
2422 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2423 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2424 c932eeeb 2019-05-22 stsp return NULL;
2425 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2426 102ff934 2019-06-11 stsp return NULL;
2427 102ff934 2019-06-11 stsp
2428 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2429 a769b60b 2021-06-27 stsp return NULL;
2430 a769b60b 2021-06-27 stsp
2431 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2433 c932eeeb 2019-05-22 stsp return NULL;
2434 c932eeeb 2019-05-22 stsp
2435 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2436 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2437 edd02c5e 2019-07-11 stsp ie->path);
2438 1ee397ad 2019-07-12 stsp if (err)
2439 1ee397ad 2019-07-12 stsp return err;
2440 1ee397ad 2019-07-12 stsp }
2441 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2442 c932eeeb 2019-05-22 stsp return NULL;
2443 a615e0e7 2020-12-16 stsp }
2444 a615e0e7 2020-12-16 stsp
2445 a615e0e7 2020-12-16 stsp static const struct got_error *
2446 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2448 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2449 a615e0e7 2020-12-16 stsp {
2450 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2451 a615e0e7 2020-12-16 stsp
2452 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2453 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2454 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2455 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2456 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2457 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2458 a615e0e7 2020-12-16 stsp
2459 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2460 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2461 9c6338c4 2019-06-02 stsp }
2462 9c6338c4 2019-06-02 stsp
2463 9c6338c4 2019-06-02 stsp static const struct got_error *
2464 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2465 9c6338c4 2019-06-02 stsp {
2466 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2467 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2468 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2469 867630bb 2020-01-17 stsp struct timespec timeout;
2470 9c6338c4 2019-06-02 stsp
2471 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2472 9c6338c4 2019-06-02 stsp fileindex_path);
2473 9c6338c4 2019-06-02 stsp if (err)
2474 9c6338c4 2019-06-02 stsp goto done;
2475 9c6338c4 2019-06-02 stsp
2476 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2477 9c6338c4 2019-06-02 stsp if (err)
2478 9c6338c4 2019-06-02 stsp goto done;
2479 9c6338c4 2019-06-02 stsp
2480 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2482 9c6338c4 2019-06-02 stsp fileindex_path);
2483 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2484 9c6338c4 2019-06-02 stsp }
2485 867630bb 2020-01-17 stsp
2486 867630bb 2020-01-17 stsp /*
2487 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2488 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2489 867630bb 2020-01-17 stsp * was recorded in the file index.
2490 867630bb 2020-01-17 stsp */
2491 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2492 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2493 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2494 9c6338c4 2019-06-02 stsp done:
2495 9c6338c4 2019-06-02 stsp if (new_index)
2496 9c6338c4 2019-06-02 stsp fclose(new_index);
2497 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2498 9c6338c4 2019-06-02 stsp return err;
2499 c932eeeb 2019-05-22 stsp }
2500 6ced4176 2019-07-12 stsp
2501 6ced4176 2019-07-12 stsp static const struct got_error *
2502 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2504 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2505 a44927cc 2022-04-07 stsp struct got_repository *repo)
2506 6ced4176 2019-07-12 stsp {
2507 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2508 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2509 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2510 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2511 6ced4176 2019-07-12 stsp
2512 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2513 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2514 6ced4176 2019-07-12 stsp *tree_id = NULL;
2515 6ced4176 2019-07-12 stsp
2516 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2517 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2518 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2519 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2520 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2521 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2522 6ced4176 2019-07-12 stsp goto done;
2523 6ced4176 2019-07-12 stsp }
2524 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2525 a44927cc 2022-04-07 stsp worktree->path_prefix);
2526 6ced4176 2019-07-12 stsp if (err)
2527 6ced4176 2019-07-12 stsp goto done;
2528 6ced4176 2019-07-12 stsp return NULL;
2529 6ced4176 2019-07-12 stsp }
2530 6ced4176 2019-07-12 stsp
2531 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2532 6ced4176 2019-07-12 stsp
2533 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2534 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2535 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2536 6ced4176 2019-07-12 stsp goto done;
2537 6ced4176 2019-07-12 stsp }
2538 6ced4176 2019-07-12 stsp
2539 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2540 6ced4176 2019-07-12 stsp if (err)
2541 6ced4176 2019-07-12 stsp goto done;
2542 6ced4176 2019-07-12 stsp
2543 6ced4176 2019-07-12 stsp free(in_repo_path);
2544 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2545 6ced4176 2019-07-12 stsp
2546 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2547 6ced4176 2019-07-12 stsp if (err)
2548 6ced4176 2019-07-12 stsp goto done;
2549 c932eeeb 2019-05-22 stsp
2550 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 6ced4176 2019-07-12 stsp /* Check out a single file. */
2552 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2553 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2554 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2555 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2556 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2557 6ced4176 2019-07-12 stsp goto done;
2558 6ced4176 2019-07-12 stsp }
2559 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2560 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2561 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2562 6ced4176 2019-07-12 stsp goto done;
2563 6ced4176 2019-07-12 stsp }
2564 6ced4176 2019-07-12 stsp } else {
2565 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2566 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2567 6ced4176 2019-07-12 stsp if (err)
2568 6ced4176 2019-07-12 stsp return err;
2569 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2570 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2571 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2572 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2573 6ced4176 2019-07-12 stsp goto done;
2574 6ced4176 2019-07-12 stsp }
2575 6ced4176 2019-07-12 stsp }
2576 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2577 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2578 6ced4176 2019-07-12 stsp } else {
2579 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2580 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2581 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2582 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2583 6ced4176 2019-07-12 stsp goto done;
2584 6ced4176 2019-07-12 stsp }
2585 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2586 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2587 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2588 6ced4176 2019-07-12 stsp goto done;
2589 6ced4176 2019-07-12 stsp }
2590 6ced4176 2019-07-12 stsp }
2591 6ced4176 2019-07-12 stsp done:
2592 6ced4176 2019-07-12 stsp free(id);
2593 6ced4176 2019-07-12 stsp free(in_repo_path);
2594 6ced4176 2019-07-12 stsp if (err) {
2595 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2596 6ced4176 2019-07-12 stsp free(*tree_relpath);
2597 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2598 6ced4176 2019-07-12 stsp free(*tree_id);
2599 6ced4176 2019-07-12 stsp *tree_id = NULL;
2600 6ced4176 2019-07-12 stsp }
2601 07ed472d 2019-07-12 stsp return err;
2602 07ed472d 2019-07-12 stsp }
2603 07ed472d 2019-07-12 stsp
2604 07ed472d 2019-07-12 stsp static const struct got_error *
2605 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2609 07ed472d 2019-07-12 stsp {
2610 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2611 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2612 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2613 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2614 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2615 07ed472d 2019-07-12 stsp
2616 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2617 7f47418f 2019-12-20 stsp if (err) {
2618 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2619 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2620 7f47418f 2019-12-20 stsp goto done;
2621 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2622 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2623 7f47418f 2019-12-20 stsp if (err)
2624 7f47418f 2019-12-20 stsp return err;
2625 7f47418f 2019-12-20 stsp }
2626 07ed472d 2019-07-12 stsp
2627 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2628 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2629 07ed472d 2019-07-12 stsp if (err)
2630 07ed472d 2019-07-12 stsp goto done;
2631 07ed472d 2019-07-12 stsp
2632 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2633 07ed472d 2019-07-12 stsp if (err)
2634 07ed472d 2019-07-12 stsp goto done;
2635 07ed472d 2019-07-12 stsp
2636 07ed472d 2019-07-12 stsp if (entry_name &&
2637 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2639 07ed472d 2019-07-12 stsp goto done;
2640 07ed472d 2019-07-12 stsp }
2641 07ed472d 2019-07-12 stsp
2642 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2643 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2644 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2645 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2646 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2647 07ed472d 2019-07-12 stsp arg.repo = repo;
2648 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2649 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2650 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2651 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2652 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2654 07ed472d 2019-07-12 stsp done:
2655 07ed472d 2019-07-12 stsp if (tree)
2656 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2657 07ed472d 2019-07-12 stsp if (commit)
2658 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2659 6ced4176 2019-07-12 stsp return err;
2660 6ced4176 2019-07-12 stsp }
2661 6ced4176 2019-07-12 stsp
2662 9d31a1d8 2018-03-11 stsp const struct got_error *
2663 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2664 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2665 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2667 9d31a1d8 2018-03-11 stsp {
2668 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2670 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2671 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2672 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2673 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2674 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2675 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2676 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2677 f2ea84fa 2019-07-27 stsp int entry_type;
2678 f2ea84fa 2019-07-27 stsp char *relpath;
2679 f2ea84fa 2019-07-27 stsp char *entry_name;
2680 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2681 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2682 9d31a1d8 2018-03-11 stsp
2683 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2684 f2ea84fa 2019-07-27 stsp
2685 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2686 9d31a1d8 2018-03-11 stsp if (err)
2687 9d31a1d8 2018-03-11 stsp return err;
2688 a44927cc 2022-04-07 stsp
2689 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2690 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2691 a44927cc 2022-04-07 stsp if (err)
2692 a44927cc 2022-04-07 stsp goto done;
2693 93a30277 2018-12-24 stsp
2694 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2695 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2696 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2697 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2698 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2699 f2ea84fa 2019-07-27 stsp goto done;
2700 f2ea84fa 2019-07-27 stsp }
2701 6ced4176 2019-07-12 stsp
2702 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2703 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2704 a44927cc 2022-04-07 stsp worktree, repo);
2705 f2ea84fa 2019-07-27 stsp if (err) {
2706 f2ea84fa 2019-07-27 stsp free(tpd);
2707 6ced4176 2019-07-12 stsp goto done;
2708 6ced4176 2019-07-12 stsp }
2709 f2ea84fa 2019-07-27 stsp
2710 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2711 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2712 f2ea84fa 2019-07-27 stsp if (err) {
2713 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2714 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2715 f2ea84fa 2019-07-27 stsp free(tpd);
2716 f2ea84fa 2019-07-27 stsp goto done;
2717 f2ea84fa 2019-07-27 stsp }
2718 f2ea84fa 2019-07-27 stsp } else
2719 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2720 f2ea84fa 2019-07-27 stsp
2721 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2722 6ced4176 2019-07-12 stsp }
2723 6ced4176 2019-07-12 stsp
2724 51514078 2018-12-25 stsp /*
2725 51514078 2018-12-25 stsp * Read the file index.
2726 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2727 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2728 51514078 2018-12-25 stsp */
2729 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2730 8da9e5f4 2019-01-12 stsp if (err)
2731 c4cdcb68 2019-04-03 stsp goto done;
2732 c4cdcb68 2019-04-03 stsp
2733 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2734 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2735 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2736 f2ea84fa 2019-07-27 stsp
2737 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2738 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2739 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2740 f2ea84fa 2019-07-27 stsp if (err)
2741 f2ea84fa 2019-07-27 stsp break;
2742 f2ea84fa 2019-07-27 stsp
2743 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2744 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2745 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2746 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2747 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2748 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2749 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2750 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2751 f2ea84fa 2019-07-27 stsp if (err)
2752 f2ea84fa 2019-07-27 stsp break;
2753 f2ea84fa 2019-07-27 stsp
2754 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2755 711d9cd9 2019-07-12 stsp }
2756 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2757 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2758 af12c6b9 2019-06-04 stsp err = sync_err;
2759 9d31a1d8 2018-03-11 stsp done:
2760 9c6338c4 2019-06-02 stsp free(fileindex_path);
2761 edfa7d7f 2018-09-11 stsp if (tree)
2762 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2763 9d31a1d8 2018-03-11 stsp if (commit)
2764 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2765 5ade8233 2019-07-12 stsp if (fileindex)
2766 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2767 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2768 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2769 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2770 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2771 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2772 f2ea84fa 2019-07-27 stsp free(tpd);
2773 f2ea84fa 2019-07-27 stsp }
2774 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2775 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2776 9d31a1d8 2018-03-11 stsp err = unlockerr;
2777 f8d1f275 2019-02-04 stsp return err;
2778 f8d1f275 2019-02-04 stsp }
2779 f8d1f275 2019-02-04 stsp
2780 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2781 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2782 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2783 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2784 234035bc 2019-06-01 stsp void *progress_arg;
2785 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2786 234035bc 2019-06-01 stsp void *cancel_arg;
2787 f69721c3 2019-10-21 stsp const char *label_orig;
2788 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2789 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2790 234035bc 2019-06-01 stsp };
2791 234035bc 2019-06-01 stsp
2792 234035bc 2019-06-01 stsp static const struct got_error *
2793 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2794 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2795 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2796 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2797 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2798 234035bc 2019-06-01 stsp {
2799 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2800 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2801 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2802 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2803 234035bc 2019-06-01 stsp struct stat sb;
2804 234035bc 2019-06-01 stsp unsigned char status;
2805 234035bc 2019-06-01 stsp int local_changes_subsumed;
2806 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2807 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2808 234035bc 2019-06-01 stsp
2809 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2810 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2811 d6c87207 2019-08-02 stsp strlen(path2));
2812 1ee397ad 2019-07-12 stsp if (ie == NULL)
2813 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2814 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2815 234035bc 2019-06-01 stsp
2816 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2817 234035bc 2019-06-01 stsp path2) == -1)
2818 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2819 234035bc 2019-06-01 stsp
2820 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2821 7f91a133 2019-12-13 stsp repo);
2822 234035bc 2019-06-01 stsp if (err)
2823 234035bc 2019-06-01 stsp goto done;
2824 234035bc 2019-06-01 stsp
2825 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2826 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2827 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2828 234035bc 2019-06-01 stsp goto done;
2829 234035bc 2019-06-01 stsp }
2830 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2831 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2832 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2833 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2834 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2835 234035bc 2019-06-01 stsp goto done;
2836 234035bc 2019-06-01 stsp }
2837 234035bc 2019-06-01 stsp
2838 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2839 36bf999c 2020-07-23 stsp char *link_target2;
2840 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2841 36bf999c 2020-07-23 stsp if (err)
2842 36bf999c 2020-07-23 stsp goto done;
2843 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2844 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2845 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2846 36bf999c 2020-07-23 stsp free(link_target2);
2847 af57b12a 2020-07-23 stsp } else {
2848 1af628f4 2021-06-03 stsp int fd;
2849 1af628f4 2021-06-03 stsp
2850 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2851 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2852 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2853 1af628f4 2021-06-03 stsp goto done;
2854 1af628f4 2021-06-03 stsp }
2855 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2856 1af628f4 2021-06-03 stsp f_orig, blob1);
2857 1af628f4 2021-06-03 stsp if (err)
2858 1af628f4 2021-06-03 stsp goto done;
2859 1af628f4 2021-06-03 stsp
2860 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2861 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2862 1af628f4 2021-06-03 stsp goto done;
2863 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2864 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2865 1af628f4 2021-06-03 stsp if (err)
2866 1af628f4 2021-06-03 stsp goto done;
2867 1af628f4 2021-06-03 stsp
2868 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2869 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2870 1af628f4 2021-06-03 stsp if (fd == -1) {
2871 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2872 1af628f4 2021-06-03 stsp ondisk_path);
2873 1af628f4 2021-06-03 stsp goto done;
2874 1af628f4 2021-06-03 stsp }
2875 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2876 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2877 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2878 1af628f4 2021-06-03 stsp ondisk_path);
2879 1af628f4 2021-06-03 stsp close(fd);
2880 1af628f4 2021-06-03 stsp goto done;
2881 1af628f4 2021-06-03 stsp }
2882 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2883 1af628f4 2021-06-03 stsp if (err)
2884 1af628f4 2021-06-03 stsp goto done;
2885 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2886 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2887 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2888 1af628f4 2021-06-03 stsp goto done;
2889 1af628f4 2021-06-03 stsp }
2890 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2891 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2892 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2893 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2894 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2895 af57b12a 2020-07-23 stsp }
2896 234035bc 2019-06-01 stsp } else if (blob1) {
2897 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2898 d6c87207 2019-08-02 stsp strlen(path1));
2899 1ee397ad 2019-07-12 stsp if (ie == NULL)
2900 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2901 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2902 2b92fad7 2019-06-02 stsp
2903 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2904 2b92fad7 2019-06-02 stsp path1) == -1)
2905 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2906 2b92fad7 2019-06-02 stsp
2907 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2908 7f91a133 2019-12-13 stsp repo);
2909 2b92fad7 2019-06-02 stsp if (err)
2910 2b92fad7 2019-06-02 stsp goto done;
2911 2b92fad7 2019-06-02 stsp
2912 2b92fad7 2019-06-02 stsp switch (status) {
2913 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2914 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2915 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2916 1ee397ad 2019-07-12 stsp if (err)
2917 1ee397ad 2019-07-12 stsp goto done;
2918 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2919 2b92fad7 2019-06-02 stsp if (err)
2920 2b92fad7 2019-06-02 stsp goto done;
2921 2b92fad7 2019-06-02 stsp if (ie)
2922 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2923 2b92fad7 2019-06-02 stsp break;
2924 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2925 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2926 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2927 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2928 1ee397ad 2019-07-12 stsp if (err)
2929 1ee397ad 2019-07-12 stsp goto done;
2930 2b92fad7 2019-06-02 stsp if (ie)
2931 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2932 2b92fad7 2019-06-02 stsp break;
2933 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2934 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2935 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2936 72840534 2022-01-19 stsp off_t blob1_size;
2937 0a22ca1a 2020-09-23 stsp /*
2938 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2939 0a22ca1a 2020-09-23 stsp * exists in the repository.
2940 0a22ca1a 2020-09-23 stsp */
2941 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2942 72840534 2022-01-19 stsp &blob1_size, path1);
2943 0a22ca1a 2020-09-23 stsp if (err)
2944 0a22ca1a 2020-09-23 stsp goto done;
2945 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2946 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2947 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2948 0a22ca1a 2020-09-23 stsp if (err)
2949 0a22ca1a 2020-09-23 stsp goto done;
2950 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2951 0a22ca1a 2020-09-23 stsp path1);
2952 0a22ca1a 2020-09-23 stsp if (err)
2953 0a22ca1a 2020-09-23 stsp goto done;
2954 0a22ca1a 2020-09-23 stsp if (ie)
2955 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2956 0a22ca1a 2020-09-23 stsp ie);
2957 0a22ca1a 2020-09-23 stsp } else {
2958 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2959 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2960 0a22ca1a 2020-09-23 stsp }
2961 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2962 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2963 0a22ca1a 2020-09-23 stsp free(id);
2964 0a22ca1a 2020-09-23 stsp if (err)
2965 0a22ca1a 2020-09-23 stsp goto done;
2966 0a22ca1a 2020-09-23 stsp break;
2967 0a22ca1a 2020-09-23 stsp }
2968 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2969 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2970 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2971 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2972 1ee397ad 2019-07-12 stsp if (err)
2973 1ee397ad 2019-07-12 stsp goto done;
2974 2b92fad7 2019-06-02 stsp break;
2975 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2976 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2977 1ee397ad 2019-07-12 stsp if (err)
2978 1ee397ad 2019-07-12 stsp goto done;
2979 2b92fad7 2019-06-02 stsp break;
2980 2b92fad7 2019-06-02 stsp default:
2981 2b92fad7 2019-06-02 stsp break;
2982 2b92fad7 2019-06-02 stsp }
2983 234035bc 2019-06-01 stsp } else if (blob2) {
2984 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2985 234035bc 2019-06-01 stsp path2) == -1)
2986 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2987 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2988 d6c87207 2019-08-02 stsp strlen(path2));
2989 234035bc 2019-06-01 stsp if (ie) {
2990 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2991 7f91a133 2019-12-13 stsp -1, NULL, repo);
2992 234035bc 2019-06-01 stsp if (err)
2993 234035bc 2019-06-01 stsp goto done;
2994 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2995 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2996 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2997 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2998 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2999 1ee397ad 2019-07-12 stsp status, path2);
3000 234035bc 2019-06-01 stsp goto done;
3001 234035bc 2019-06-01 stsp }
3002 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3003 36bf999c 2020-07-23 stsp char *link_target2;
3004 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3005 36bf999c 2020-07-23 stsp blob2);
3006 36bf999c 2020-07-23 stsp if (err)
3007 36bf999c 2020-07-23 stsp goto done;
3008 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3009 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3010 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3011 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3012 36bf999c 2020-07-23 stsp free(link_target2);
3013 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3014 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3015 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3016 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3017 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3018 526a746f 2020-07-23 stsp a->progress_arg);
3019 dfe9fba0 2020-07-23 stsp } else {
3020 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3021 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3022 526a746f 2020-07-23 stsp }
3023 dfe9fba0 2020-07-23 stsp if (err)
3024 dfe9fba0 2020-07-23 stsp goto done;
3025 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3026 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3027 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3028 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3029 234035bc 2019-06-01 stsp if (err)
3030 234035bc 2019-06-01 stsp goto done;
3031 234035bc 2019-06-01 stsp }
3032 234035bc 2019-06-01 stsp } else {
3033 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3034 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3035 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3036 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3037 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3038 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3039 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3040 2e1fa222 2020-07-23 stsp } else {
3041 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3042 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3043 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3044 2e1fa222 2020-07-23 stsp }
3045 234035bc 2019-06-01 stsp if (err)
3046 234035bc 2019-06-01 stsp goto done;
3047 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3048 234035bc 2019-06-01 stsp if (err)
3049 234035bc 2019-06-01 stsp goto done;
3050 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3051 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3052 3969253a 2020-03-07 stsp if (err) {
3053 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3054 3969253a 2020-03-07 stsp goto done;
3055 3969253a 2020-03-07 stsp }
3056 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3057 2b92fad7 2019-06-02 stsp if (err) {
3058 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3059 2b92fad7 2019-06-02 stsp goto done;
3060 2b92fad7 2019-06-02 stsp }
3061 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3062 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3063 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3064 2e1fa222 2020-07-23 stsp }
3065 234035bc 2019-06-01 stsp }
3066 234035bc 2019-06-01 stsp }
3067 234035bc 2019-06-01 stsp done:
3068 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3069 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3070 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3071 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3072 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3073 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3074 1af628f4 2021-06-03 stsp free(id_str);
3075 54d5be07 2021-06-03 stsp free(label_deriv2);
3076 234035bc 2019-06-01 stsp free(ondisk_path);
3077 234035bc 2019-06-01 stsp return err;
3078 234035bc 2019-06-01 stsp }
3079 234035bc 2019-06-01 stsp
3080 69de9dd4 2021-09-03 stsp static const struct got_error *
3081 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3082 69de9dd4 2021-09-03 stsp {
3083 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3084 69de9dd4 2021-09-03 stsp
3085 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3086 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3087 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3088 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3089 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3090 69de9dd4 2021-09-03 stsp
3091 69de9dd4 2021-09-03 stsp return NULL;
3092 69de9dd4 2021-09-03 stsp }
3093 69de9dd4 2021-09-03 stsp
3094 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3095 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3096 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3097 234035bc 2019-06-01 stsp struct got_repository *repo;
3098 234035bc 2019-06-01 stsp };
3099 234035bc 2019-06-01 stsp
3100 234035bc 2019-06-01 stsp static const struct got_error *
3101 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3102 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3103 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3104 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3105 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3106 234035bc 2019-06-01 stsp {
3107 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3108 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3109 234035bc 2019-06-01 stsp unsigned char status;
3110 234035bc 2019-06-01 stsp struct stat sb;
3111 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3112 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3113 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3114 234035bc 2019-06-01 stsp char *ondisk_path;
3115 234035bc 2019-06-01 stsp
3116 69de9dd4 2021-09-03 stsp if (id == NULL)
3117 69de9dd4 2021-09-03 stsp return NULL;
3118 234035bc 2019-06-01 stsp
3119 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3120 69de9dd4 2021-09-03 stsp if (ie == NULL)
3121 69de9dd4 2021-09-03 stsp return NULL;
3122 69de9dd4 2021-09-03 stsp
3123 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3124 234035bc 2019-06-01 stsp == -1)
3125 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3126 234035bc 2019-06-01 stsp
3127 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3128 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3129 5546d466 2021-09-02 stsp free(ondisk_path);
3130 234035bc 2019-06-01 stsp if (err)
3131 234035bc 2019-06-01 stsp return err;
3132 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3133 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3134 234035bc 2019-06-01 stsp
3135 234035bc 2019-06-01 stsp return NULL;
3136 234035bc 2019-06-01 stsp }
3137 234035bc 2019-06-01 stsp
3138 818c7501 2019-07-11 stsp static const struct got_error *
3139 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3140 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3141 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3142 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3143 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3144 234035bc 2019-06-01 stsp {
3145 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3146 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3147 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3148 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3149 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3150 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3151 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3152 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3153 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3154 234035bc 2019-06-01 stsp
3155 03415a1a 2019-06-02 stsp if (commit_id1) {
3156 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3157 a44927cc 2022-04-07 stsp if (err)
3158 a44927cc 2022-04-07 stsp goto done;
3159 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3160 03415a1a 2019-06-02 stsp worktree->path_prefix);
3161 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3162 03415a1a 2019-06-02 stsp goto done;
3163 69d57f3d 2020-07-31 stsp }
3164 69d57f3d 2020-07-31 stsp if (tree_id1) {
3165 69d57f3d 2020-07-31 stsp char *id_str;
3166 03415a1a 2019-06-02 stsp
3167 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3168 03415a1a 2019-06-02 stsp if (err)
3169 03415a1a 2019-06-02 stsp goto done;
3170 f69721c3 2019-10-21 stsp
3171 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3172 f69721c3 2019-10-21 stsp if (err)
3173 f69721c3 2019-10-21 stsp goto done;
3174 f69721c3 2019-10-21 stsp
3175 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3176 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3177 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3178 f69721c3 2019-10-21 stsp free(id_str);
3179 f69721c3 2019-10-21 stsp goto done;
3180 f69721c3 2019-10-21 stsp }
3181 f69721c3 2019-10-21 stsp free(id_str);
3182 b72706c3 2022-06-01 stsp
3183 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3184 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3185 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3186 b72706c3 2022-06-01 stsp goto done;
3187 b72706c3 2022-06-01 stsp }
3188 03415a1a 2019-06-02 stsp }
3189 234035bc 2019-06-01 stsp
3190 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3191 a44927cc 2022-04-07 stsp if (err)
3192 a44927cc 2022-04-07 stsp goto done;
3193 a44927cc 2022-04-07 stsp
3194 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3195 234035bc 2019-06-01 stsp worktree->path_prefix);
3196 234035bc 2019-06-01 stsp if (err)
3197 234035bc 2019-06-01 stsp goto done;
3198 234035bc 2019-06-01 stsp
3199 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3200 234035bc 2019-06-01 stsp if (err)
3201 234035bc 2019-06-01 stsp goto done;
3202 234035bc 2019-06-01 stsp
3203 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3204 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3205 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3206 f9d37699 2022-06-28 stsp goto done;
3207 f9d37699 2022-06-28 stsp }
3208 f9d37699 2022-06-28 stsp
3209 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3210 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3211 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3212 b72706c3 2022-06-01 stsp goto done;
3213 b72706c3 2022-06-01 stsp }
3214 b72706c3 2022-06-01 stsp
3215 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3216 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3217 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3218 f9d37699 2022-06-28 stsp goto done;
3219 f9d37699 2022-06-28 stsp }
3220 f9d37699 2022-06-28 stsp
3221 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3222 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3223 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3224 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3226 69de9dd4 2021-09-03 stsp if (err)
3227 69de9dd4 2021-09-03 stsp goto done;
3228 69de9dd4 2021-09-03 stsp
3229 234035bc 2019-06-01 stsp arg.worktree = worktree;
3230 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3231 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3232 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3233 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3234 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3235 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3236 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3237 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3238 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3239 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3240 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3241 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3242 af12c6b9 2019-06-04 stsp err = sync_err;
3243 234035bc 2019-06-01 stsp done:
3244 a44927cc 2022-04-07 stsp if (commit1)
3245 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3246 a44927cc 2022-04-07 stsp if (commit2)
3247 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3248 234035bc 2019-06-01 stsp if (tree1)
3249 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3250 234035bc 2019-06-01 stsp if (tree2)
3251 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3252 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3253 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3254 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3255 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3256 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3257 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3258 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3259 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3260 f69721c3 2019-10-21 stsp free(label_orig);
3261 818c7501 2019-07-11 stsp return err;
3262 818c7501 2019-07-11 stsp }
3263 234035bc 2019-06-01 stsp
3264 818c7501 2019-07-11 stsp const struct got_error *
3265 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3266 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3267 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3268 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3269 818c7501 2019-07-11 stsp {
3270 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3271 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3272 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3273 818c7501 2019-07-11 stsp
3274 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3275 818c7501 2019-07-11 stsp if (err)
3276 818c7501 2019-07-11 stsp return err;
3277 818c7501 2019-07-11 stsp
3278 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3279 818c7501 2019-07-11 stsp if (err)
3280 818c7501 2019-07-11 stsp goto done;
3281 818c7501 2019-07-11 stsp
3282 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3283 69de9dd4 2021-09-03 stsp worktree);
3284 818c7501 2019-07-11 stsp if (err)
3285 818c7501 2019-07-11 stsp goto done;
3286 818c7501 2019-07-11 stsp
3287 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3288 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3289 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3290 818c7501 2019-07-11 stsp done:
3291 818c7501 2019-07-11 stsp if (fileindex)
3292 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3293 818c7501 2019-07-11 stsp free(fileindex_path);
3294 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3295 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3296 234035bc 2019-06-01 stsp err = unlockerr;
3297 234035bc 2019-06-01 stsp return err;
3298 234035bc 2019-06-01 stsp }
3299 234035bc 2019-06-01 stsp
3300 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3301 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3302 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3303 927df6b7 2019-02-10 stsp const char *status_path;
3304 927df6b7 2019-02-10 stsp size_t status_path_len;
3305 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3306 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3307 f8d1f275 2019-02-04 stsp void *status_arg;
3308 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3309 f8d1f275 2019-02-04 stsp void *cancel_arg;
3310 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3311 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3312 f2a9dc41 2019-12-13 tracey int report_unchanged;
3313 3143d852 2020-06-25 stsp int no_ignores;
3314 f8d1f275 2019-02-04 stsp };
3315 88d0e355 2019-08-03 stsp
3316 f8d1f275 2019-02-04 stsp static const struct got_error *
3317 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3318 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3319 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3320 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3321 927df6b7 2019-02-10 stsp {
3322 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3323 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3324 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3325 927df6b7 2019-02-10 stsp struct stat sb;
3326 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3327 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3328 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3329 927df6b7 2019-02-10 stsp
3330 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3331 98eaaa12 2019-08-03 stsp if (err)
3332 98eaaa12 2019-08-03 stsp return err;
3333 98eaaa12 2019-08-03 stsp
3334 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3335 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3336 98eaaa12 2019-08-03 stsp return NULL;
3337 98eaaa12 2019-08-03 stsp
3338 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3339 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3340 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3341 98eaaa12 2019-08-03 stsp }
3342 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3343 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3344 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3345 927df6b7 2019-02-10 stsp }
3346 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3347 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3348 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3349 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3350 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3351 98eaaa12 2019-08-03 stsp }
3352 98eaaa12 2019-08-03 stsp
3353 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3354 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3355 927df6b7 2019-02-10 stsp }
3356 927df6b7 2019-02-10 stsp
3357 927df6b7 2019-02-10 stsp static const struct got_error *
3358 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3359 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3360 f8d1f275 2019-02-04 stsp {
3361 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3362 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3363 f8d1f275 2019-02-04 stsp char *abspath;
3364 f8d1f275 2019-02-04 stsp
3365 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3366 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3367 0584f854 2019-04-06 stsp
3368 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3369 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3370 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3371 927df6b7 2019-02-10 stsp return NULL;
3372 927df6b7 2019-02-10 stsp
3373 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3374 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3375 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3376 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3377 f8d1f275 2019-02-04 stsp } else {
3378 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3379 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3380 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3381 f8d1f275 2019-02-04 stsp }
3382 f8d1f275 2019-02-04 stsp
3383 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3384 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3385 f8d1f275 2019-02-04 stsp free(abspath);
3386 9d31a1d8 2018-03-11 stsp return err;
3387 9d31a1d8 2018-03-11 stsp }
3388 f8d1f275 2019-02-04 stsp
3389 f8d1f275 2019-02-04 stsp static const struct got_error *
3390 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3391 f8d1f275 2019-02-04 stsp {
3392 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3393 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3394 2ec1f75b 2019-03-26 stsp unsigned char status;
3395 927df6b7 2019-02-10 stsp
3396 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3397 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3398 0584f854 2019-04-06 stsp
3399 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3400 927df6b7 2019-02-10 stsp return NULL;
3401 927df6b7 2019-02-10 stsp
3402 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3403 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3404 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3405 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3406 2ec1f75b 2019-03-26 stsp else
3407 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3408 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3409 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3410 6841da00 2019-08-08 stsp }
3411 6841da00 2019-08-08 stsp
3412 336075a4 2022-06-25 op static void
3413 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3414 6841da00 2019-08-08 stsp {
3415 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3416 6841da00 2019-08-08 stsp
3417 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3418 6841da00 2019-08-08 stsp free((char *)pe->path);
3419 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3420 6841da00 2019-08-08 stsp }
3421 6841da00 2019-08-08 stsp
3422 336075a4 2022-06-25 op static void
3423 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3424 6841da00 2019-08-08 stsp {
3425 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3426 6841da00 2019-08-08 stsp
3427 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3428 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3429 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3430 6841da00 2019-08-08 stsp free((char *)pe->path);
3431 6841da00 2019-08-08 stsp }
3432 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3433 6841da00 2019-08-08 stsp }
3434 6841da00 2019-08-08 stsp
3435 6841da00 2019-08-08 stsp static const struct got_error *
3436 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3437 6841da00 2019-08-08 stsp {
3438 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3439 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3440 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3441 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3442 6841da00 2019-08-08 stsp size_t linesize = 0;
3443 6841da00 2019-08-08 stsp ssize_t linelen;
3444 6841da00 2019-08-08 stsp
3445 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3446 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3447 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3448 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3449 6841da00 2019-08-08 stsp
3450 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3452 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3453 bd8de430 2019-10-04 stsp
3454 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3455 bd8de430 2019-10-04 stsp if (line[0] == '#')
3456 bd8de430 2019-10-04 stsp continue;
3457 bd8de430 2019-10-04 stsp
3458 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3459 bd8de430 2019-10-04 stsp if (line[0] == '!')
3460 bd8de430 2019-10-04 stsp continue;
3461 bd8de430 2019-10-04 stsp
3462 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3463 6841da00 2019-08-08 stsp line) == -1) {
3464 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3465 6841da00 2019-08-08 stsp goto done;
3466 6841da00 2019-08-08 stsp }
3467 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3468 6841da00 2019-08-08 stsp if (err)
3469 6841da00 2019-08-08 stsp goto done;
3470 6841da00 2019-08-08 stsp }
3471 6841da00 2019-08-08 stsp if (ferror(f)) {
3472 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3473 6841da00 2019-08-08 stsp goto done;
3474 6841da00 2019-08-08 stsp }
3475 6841da00 2019-08-08 stsp
3476 6841da00 2019-08-08 stsp dirpath = strdup(path);
3477 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3478 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3479 6841da00 2019-08-08 stsp goto done;
3480 6841da00 2019-08-08 stsp }
3481 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3482 6841da00 2019-08-08 stsp done:
3483 6841da00 2019-08-08 stsp free(line);
3484 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3485 6841da00 2019-08-08 stsp free(dirpath);
3486 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3487 6841da00 2019-08-08 stsp }
3488 6841da00 2019-08-08 stsp return err;
3489 6841da00 2019-08-08 stsp }
3490 6841da00 2019-08-08 stsp
3491 336075a4 2022-06-25 op static int
3492 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3493 6841da00 2019-08-08 stsp {
3494 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3495 bd8de430 2019-10-04 stsp
3496 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3497 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3498 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3499 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3500 bd8de430 2019-10-04 stsp
3501 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3502 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3503 6841da00 2019-08-08 stsp
3504 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3505 bd8de430 2019-10-04 stsp continue;
3506 bd8de430 2019-10-04 stsp pattern += 3;
3507 bd8de430 2019-10-04 stsp p = path;
3508 bd8de430 2019-10-04 stsp while (*p) {
3509 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3510 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3512 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3513 bd8de430 2019-10-04 stsp p++;
3514 bd8de430 2019-10-04 stsp while (*p == '/')
3515 bd8de430 2019-10-04 stsp p++;
3516 bd8de430 2019-10-04 stsp continue;
3517 bd8de430 2019-10-04 stsp }
3518 bd8de430 2019-10-04 stsp return 1;
3519 bd8de430 2019-10-04 stsp }
3520 bd8de430 2019-10-04 stsp }
3521 bd8de430 2019-10-04 stsp }
3522 bd8de430 2019-10-04 stsp
3523 6841da00 2019-08-08 stsp /*
3524 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3525 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3526 6841da00 2019-08-08 stsp * ignores backwards.
3527 6841da00 2019-08-08 stsp */
3528 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3529 6841da00 2019-08-08 stsp while (pe) {
3530 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3532 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3533 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3534 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3535 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3536 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3537 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3538 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3539 6841da00 2019-08-08 stsp continue;
3540 6841da00 2019-08-08 stsp return 1;
3541 6841da00 2019-08-08 stsp }
3542 6841da00 2019-08-08 stsp }
3543 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3544 6841da00 2019-08-08 stsp }
3545 6841da00 2019-08-08 stsp
3546 6841da00 2019-08-08 stsp return 0;
3547 6841da00 2019-08-08 stsp }
3548 6841da00 2019-08-08 stsp
3549 6841da00 2019-08-08 stsp static const struct got_error *
3550 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3552 6841da00 2019-08-08 stsp {
3553 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3554 6841da00 2019-08-08 stsp char *ignorespath;
3555 886cec17 2019-12-15 stsp int fd = -1;
3556 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3557 6841da00 2019-08-08 stsp
3558 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3560 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3561 6841da00 2019-08-08 stsp
3562 886cec17 2019-12-15 stsp if (dirfd != -1) {
3563 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3564 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3565 886cec17 2019-12-15 stsp if (fd == -1) {
3566 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3567 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3568 886cec17 2019-12-15 stsp ignorespath);
3569 886cec17 2019-12-15 stsp } else {
3570 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3571 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3572 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3573 886cec17 2019-12-15 stsp ignorespath);
3574 886cec17 2019-12-15 stsp else {
3575 886cec17 2019-12-15 stsp fd = -1;
3576 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3577 886cec17 2019-12-15 stsp }
3578 886cec17 2019-12-15 stsp }
3579 886cec17 2019-12-15 stsp } else {
3580 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3581 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3582 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3583 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3584 886cec17 2019-12-15 stsp ignorespath);
3585 886cec17 2019-12-15 stsp } else
3586 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3587 886cec17 2019-12-15 stsp }
3588 6841da00 2019-08-08 stsp
3589 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3591 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3592 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3593 6841da00 2019-08-08 stsp free(ignorespath);
3594 6841da00 2019-08-08 stsp return err;
3595 f8d1f275 2019-02-04 stsp }
3596 f8d1f275 2019-02-04 stsp
3597 f8d1f275 2019-02-04 stsp static const struct got_error *
3598 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3599 62da3196 2021-10-01 stsp int dirfd)
3600 f8d1f275 2019-02-04 stsp {
3601 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3602 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3603 f8d1f275 2019-02-04 stsp char *path = NULL;
3604 62da3196 2021-10-01 stsp
3605 62da3196 2021-10-01 stsp if (ignore != NULL)
3606 62da3196 2021-10-01 stsp *ignore = 0;
3607 f8d1f275 2019-02-04 stsp
3608 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3610 0584f854 2019-04-06 stsp
3611 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3612 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3614 f8d1f275 2019-02-04 stsp } else {
3615 f8d1f275 2019-02-04 stsp path = de->d_name;
3616 f8d1f275 2019-02-04 stsp }
3617 f8d1f275 2019-02-04 stsp
3618 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3619 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3620 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3621 62da3196 2021-10-01 stsp *ignore = 1;
3622 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3623 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3624 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 f8d1f275 2019-02-04 stsp if (parent_path[0])
3627 f8d1f275 2019-02-04 stsp free(path);
3628 b72f483a 2019-02-05 stsp return err;
3629 f8d1f275 2019-02-04 stsp }
3630 f8d1f275 2019-02-04 stsp
3631 347d1d3e 2019-07-12 stsp static const struct got_error *
3632 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3633 3143d852 2020-06-25 stsp {
3634 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3635 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3636 3143d852 2020-06-25 stsp
3637 3143d852 2020-06-25 stsp if (a->no_ignores)
3638 3143d852 2020-06-25 stsp return NULL;
3639 3143d852 2020-06-25 stsp
3640 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3641 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3642 3143d852 2020-06-25 stsp if (err)
3643 3143d852 2020-06-25 stsp return err;
3644 3143d852 2020-06-25 stsp
3645 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3647 3143d852 2020-06-25 stsp
3648 3143d852 2020-06-25 stsp return err;
3649 3143d852 2020-06-25 stsp }
3650 3143d852 2020-06-25 stsp
3651 3143d852 2020-06-25 stsp static const struct got_error *
3652 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3653 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3655 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3656 abb4604f 2019-07-27 stsp {
3657 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3658 abb4604f 2019-07-27 stsp struct stat sb;
3659 ff56836b 2021-07-08 stsp
3660 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3661 abb4604f 2019-07-27 stsp if (ie)
3662 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3663 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3664 abb4604f 2019-07-27 stsp
3665 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3666 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3667 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3668 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3670 abb4604f 2019-07-27 stsp }
3671 abb4604f 2019-07-27 stsp
3672 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3673 916237f3 2022-02-11 stsp return NULL;
3674 916237f3 2022-02-11 stsp
3675 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3678 abb4604f 2019-07-27 stsp
3679 abb4604f 2019-07-27 stsp return NULL;
3680 3143d852 2020-06-25 stsp }
3681 3143d852 2020-06-25 stsp
3682 3143d852 2020-06-25 stsp static const struct got_error *
3683 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3685 3143d852 2020-06-25 stsp {
3686 3143d852 2020-06-25 stsp const struct got_error *err;
3687 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3688 3143d852 2020-06-25 stsp
3689 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3690 3143d852 2020-06-25 stsp ".cvsignore");
3691 3143d852 2020-06-25 stsp if (err)
3692 3143d852 2020-06-25 stsp return err;
3693 3143d852 2020-06-25 stsp
3694 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3695 3143d852 2020-06-25 stsp ".gitignore");
3696 3143d852 2020-06-25 stsp if (err)
3697 3143d852 2020-06-25 stsp return err;
3698 3143d852 2020-06-25 stsp
3699 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3700 3143d852 2020-06-25 stsp if (err) {
3701 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3702 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3703 3143d852 2020-06-25 stsp return err;
3704 3143d852 2020-06-25 stsp }
3705 3143d852 2020-06-25 stsp for (;;) {
3706 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3707 3143d852 2020-06-25 stsp ".cvsignore");
3708 3143d852 2020-06-25 stsp if (err)
3709 3143d852 2020-06-25 stsp break;
3710 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3711 3143d852 2020-06-25 stsp ".gitignore");
3712 3143d852 2020-06-25 stsp if (err)
3713 3143d852 2020-06-25 stsp break;
3714 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3715 3143d852 2020-06-25 stsp if (err) {
3716 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3717 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3718 3143d852 2020-06-25 stsp break;
3719 3143d852 2020-06-25 stsp }
3720 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3721 ff56836b 2021-07-08 stsp break;
3722 b737c85a 2020-06-26 stsp free(parent_path);
3723 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3724 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3725 3143d852 2020-06-25 stsp }
3726 3143d852 2020-06-25 stsp
3727 b737c85a 2020-06-26 stsp free(parent_path);
3728 b737c85a 2020-06-26 stsp free(next_parent_path);
3729 3143d852 2020-06-25 stsp return err;
3730 abb4604f 2019-07-27 stsp }
3731 abb4604f 2019-07-27 stsp
3732 abb4604f 2019-07-27 stsp static const struct got_error *
3733 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3734 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3735 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3736 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 f2a9dc41 2019-12-13 tracey int report_unchanged)
3738 f8d1f275 2019-02-04 stsp {
3739 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3740 6fc93f37 2019-12-13 stsp int fd = -1;
3741 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3742 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3743 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3744 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3745 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3746 3143d852 2020-06-25 stsp
3747 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3748 f8d1f275 2019-02-04 stsp
3749 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3750 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3752 8dc303cc 2019-07-27 stsp
3753 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3754 a84c0d30 2022-03-12 stsp if (ie) {
3755 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3756 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3757 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3758 a84c0d30 2022-03-12 stsp goto done;
3759 a84c0d30 2022-03-12 stsp }
3760 a84c0d30 2022-03-12 stsp
3761 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3762 6fc93f37 2019-12-13 stsp if (fd == -1) {
3763 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3765 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3766 ff56836b 2021-07-08 stsp else {
3767 ff56836b 2021-07-08 stsp if (!no_ignores) {
3768 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3769 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3770 ff56836b 2021-07-08 stsp if (err)
3771 ff56836b 2021-07-08 stsp goto done;
3772 ff56836b 2021-07-08 stsp }
3773 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3774 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3775 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3776 ff56836b 2021-07-08 stsp }
3777 abb4604f 2019-07-27 stsp } else {
3778 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3779 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3780 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3781 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3782 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3783 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3784 abb4604f 2019-07-27 stsp arg.status_path = path;
3785 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3786 abb4604f 2019-07-27 stsp arg.repo = repo;
3787 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3788 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3789 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3790 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3791 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3792 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3793 022fae89 2019-12-06 tracey if (!no_ignores) {
3794 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3795 3143d852 2020-06-25 stsp worktree->root_path, path);
3796 3143d852 2020-06-25 stsp if (err)
3797 3143d852 2020-06-25 stsp goto done;
3798 022fae89 2019-12-06 tracey }
3799 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3800 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3801 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3802 927df6b7 2019-02-10 stsp }
3803 3143d852 2020-06-25 stsp done:
3804 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3805 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3806 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3807 927df6b7 2019-02-10 stsp free(ondisk_path);
3808 347d1d3e 2019-07-12 stsp return err;
3809 347d1d3e 2019-07-12 stsp }
3810 347d1d3e 2019-07-12 stsp
3811 347d1d3e 2019-07-12 stsp const struct got_error *
3812 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3813 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3814 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3816 347d1d3e 2019-07-12 stsp {
3817 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3818 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3819 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3820 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3821 347d1d3e 2019-07-12 stsp
3822 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3823 347d1d3e 2019-07-12 stsp if (err)
3824 347d1d3e 2019-07-12 stsp return err;
3825 347d1d3e 2019-07-12 stsp
3826 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3827 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3828 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3829 f6343036 2021-06-22 stsp no_ignores, 0);
3830 72ea6654 2019-07-27 stsp if (err)
3831 72ea6654 2019-07-27 stsp break;
3832 72ea6654 2019-07-27 stsp }
3833 f8d1f275 2019-02-04 stsp free(fileindex_path);
3834 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3835 f8d1f275 2019-02-04 stsp return err;
3836 f8d1f275 2019-02-04 stsp }
3837 6c7ab921 2019-03-18 stsp
3838 6c7ab921 2019-03-18 stsp const struct got_error *
3839 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 6c7ab921 2019-03-18 stsp const char *arg)
3841 6c7ab921 2019-03-18 stsp {
3842 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3843 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 6c7ab921 2019-03-18 stsp size_t len;
3845 00bb5ea0 2020-07-23 stsp struct stat sb;
3846 01740607 2020-11-04 stsp char *abspath = NULL;
3847 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3848 6c7ab921 2019-03-18 stsp
3849 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3850 6c7ab921 2019-03-18 stsp
3851 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3852 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3853 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3854 00bb5ea0 2020-07-23 stsp
3855 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3856 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3857 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3858 00bb5ea0 2020-07-23 stsp goto done;
3859 00bb5ea0 2020-07-23 stsp }
3860 727173c3 2020-11-06 stsp sb.st_mode = 0;
3861 00bb5ea0 2020-07-23 stsp }
3862 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3863 00bb5ea0 2020-07-23 stsp /*
3864 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3865 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3866 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3867 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3868 00bb5ea0 2020-07-23 stsp */
3869 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3870 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3872 00bb5ea0 2020-07-23 stsp goto done;
3873 00bb5ea0 2020-07-23 stsp }
3874 00bb5ea0 2020-07-23 stsp
3875 00bb5ea0 2020-07-23 stsp }
3876 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3877 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3878 00bb5ea0 2020-07-23 stsp if (err)
3879 d0710d08 2019-07-22 stsp goto done;
3880 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3881 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3882 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3883 00bb5ea0 2020-07-23 stsp goto done;
3884 00bb5ea0 2020-07-23 stsp }
3885 00bb5ea0 2020-07-23 stsp } else {
3886 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3887 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3888 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3889 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3890 00bb5ea0 2020-07-23 stsp goto done;
3891 00bb5ea0 2020-07-23 stsp }
3892 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3894 01740607 2020-11-04 stsp goto done;
3895 01740607 2020-11-04 stsp }
3896 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3897 01740607 2020-11-04 stsp sizeof(canonpath));
3898 01740607 2020-11-04 stsp if (err)
3899 00bb5ea0 2020-07-23 stsp goto done;
3900 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3901 01740607 2020-11-04 stsp if (resolved == NULL) {
3902 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3903 01740607 2020-11-04 stsp goto done;
3904 00bb5ea0 2020-07-23 stsp }
3905 d0710d08 2019-07-22 stsp }
3906 d0710d08 2019-07-22 stsp }
3907 6c7ab921 2019-03-18 stsp
3908 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3910 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3911 6c7ab921 2019-03-18 stsp goto done;
3912 6c7ab921 2019-03-18 stsp }
3913 6c7ab921 2019-03-18 stsp
3914 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3916 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3917 f6d88e1a 2019-05-29 stsp if (err)
3918 f6d88e1a 2019-05-29 stsp goto done;
3919 f6d88e1a 2019-05-29 stsp } else {
3920 f6d88e1a 2019-05-29 stsp path = strdup("");
3921 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3922 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3923 f6d88e1a 2019-05-29 stsp goto done;
3924 f6d88e1a 2019-05-29 stsp }
3925 6c7ab921 2019-03-18 stsp }
3926 6c7ab921 2019-03-18 stsp
3927 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3928 6c7ab921 2019-03-18 stsp len = strlen(path);
3929 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3930 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3931 6c7ab921 2019-03-18 stsp len--;
3932 6c7ab921 2019-03-18 stsp }
3933 6c7ab921 2019-03-18 stsp done:
3934 01740607 2020-11-04 stsp free(abspath);
3935 6c7ab921 2019-03-18 stsp free(resolved);
3936 d0710d08 2019-07-22 stsp free(cwd);
3937 6c7ab921 2019-03-18 stsp if (err == NULL)
3938 6c7ab921 2019-03-18 stsp *wt_path = path;
3939 6c7ab921 2019-03-18 stsp else
3940 6c7ab921 2019-03-18 stsp free(path);
3941 d00136be 2019-03-26 stsp return err;
3942 d00136be 2019-03-26 stsp }
3943 d00136be 2019-03-26 stsp
3944 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3945 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3946 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3947 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3948 4e68cba3 2019-11-23 stsp void *progress_arg;
3949 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3950 4e68cba3 2019-11-23 stsp };
3951 4e68cba3 2019-11-23 stsp
3952 4e68cba3 2019-11-23 stsp static const struct got_error *
3953 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3955 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3957 07f5b47a 2019-06-02 stsp {
3958 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3959 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3960 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3961 6d022e97 2019-08-04 stsp struct stat sb;
3962 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3963 07f5b47a 2019-06-02 stsp
3964 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 dbb83fbd 2019-12-12 stsp relpath) == -1)
3966 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3967 dbb83fbd 2019-12-12 stsp
3968 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 6d022e97 2019-08-04 stsp if (ie) {
3970 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3971 12463d8b 2019-12-13 stsp de_name, a->repo);
3972 6d022e97 2019-08-04 stsp if (err)
3973 dbb83fbd 2019-12-12 stsp goto done;
3974 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3975 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3976 dbb83fbd 2019-12-12 stsp goto done;
3977 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3978 dbb83fbd 2019-12-12 stsp if (err)
3979 dbb83fbd 2019-12-12 stsp goto done;
3980 6d022e97 2019-08-04 stsp }
3981 07f5b47a 2019-06-02 stsp
3982 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3983 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3984 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3985 9b4603c0 2022-01-31 stsp else
3986 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3987 dbb83fbd 2019-12-12 stsp goto done;
3988 4e68cba3 2019-11-23 stsp }
3989 07f5b47a 2019-06-02 stsp
3990 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3991 4e68cba3 2019-11-23 stsp if (err)
3992 4e68cba3 2019-11-23 stsp goto done;
3993 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3995 3969253a 2020-03-07 stsp if (err) {
3996 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3997 3969253a 2020-03-07 stsp goto done;
3998 3969253a 2020-03-07 stsp }
3999 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4000 07f5b47a 2019-06-02 stsp if (err) {
4001 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4002 4e68cba3 2019-11-23 stsp goto done;
4003 07f5b47a 2019-06-02 stsp }
4004 4e68cba3 2019-11-23 stsp done:
4005 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4006 dbb83fbd 2019-12-12 stsp if (err)
4007 dbb83fbd 2019-12-12 stsp return err;
4008 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4009 dbb83fbd 2019-12-12 stsp return NULL;
4010 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4011 07f5b47a 2019-06-02 stsp }
4012 07f5b47a 2019-06-02 stsp
4013 d00136be 2019-03-26 stsp const struct got_error *
4014 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4015 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4016 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4018 d00136be 2019-03-26 stsp {
4019 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4020 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4021 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4023 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4024 d00136be 2019-03-26 stsp
4025 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4026 d00136be 2019-03-26 stsp if (err)
4027 d00136be 2019-03-26 stsp return err;
4028 d00136be 2019-03-26 stsp
4029 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4030 d00136be 2019-03-26 stsp if (err)
4031 d00136be 2019-03-26 stsp goto done;
4032 d00136be 2019-03-26 stsp
4033 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4034 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4035 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4036 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4037 4e68cba3 2019-11-23 stsp saa.repo = repo;
4038 4e68cba3 2019-11-23 stsp
4039 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4040 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4041 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4042 1dd54920 2019-05-11 stsp if (err)
4043 af12c6b9 2019-06-04 stsp break;
4044 1dd54920 2019-05-11 stsp }
4045 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4046 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4047 af12c6b9 2019-06-04 stsp err = sync_err;
4048 d00136be 2019-03-26 stsp done:
4049 fb399478 2019-07-12 stsp free(fileindex_path);
4050 d00136be 2019-03-26 stsp if (fileindex)
4051 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4052 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4053 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4054 d00136be 2019-03-26 stsp err = unlockerr;
4055 6c7ab921 2019-03-18 stsp return err;
4056 6c7ab921 2019-03-18 stsp }
4057 17ed4618 2019-06-02 stsp
4058 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4059 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4060 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4061 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4062 f2a9dc41 2019-12-13 tracey void *progress_arg;
4063 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4064 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4065 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4066 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4067 766841c2 2020-08-13 stsp const char *status_codes;
4068 f2a9dc41 2019-12-13 tracey };
4069 f2a9dc41 2019-12-13 tracey
4070 f2a9dc41 2019-12-13 tracey static const struct got_error *
4071 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4072 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4073 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4075 17ed4618 2019-06-02 stsp {
4076 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4077 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4078 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4079 17ed4618 2019-06-02 stsp struct stat sb;
4080 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4081 4e12cd97 2022-01-25 stsp
4082 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4083 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4084 4e12cd97 2022-01-25 stsp return NULL;
4085 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4086 4e12cd97 2022-01-25 stsp }
4087 17ed4618 2019-06-02 stsp
4088 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 17ed4618 2019-06-02 stsp if (ie == NULL)
4090 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4091 2ec1f75b 2019-03-26 stsp
4092 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4093 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4095 9acbc4fa 2019-08-03 stsp return NULL;
4096 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4097 9acbc4fa 2019-08-03 stsp }
4098 9acbc4fa 2019-08-03 stsp
4099 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4100 f2a9dc41 2019-12-13 tracey relpath) == -1)
4101 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4102 f2a9dc41 2019-12-13 tracey
4103 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4104 12463d8b 2019-12-13 stsp a->repo);
4105 17ed4618 2019-06-02 stsp if (err)
4106 f2a9dc41 2019-12-13 tracey goto done;
4107 17ed4618 2019-06-02 stsp
4108 766841c2 2020-08-13 stsp if (a->status_codes) {
4109 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4110 766841c2 2020-08-13 stsp int i;
4111 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4112 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4113 766841c2 2020-08-13 stsp break;
4114 766841c2 2020-08-13 stsp }
4115 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4116 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4117 766841c2 2020-08-13 stsp free(ondisk_path);
4118 766841c2 2020-08-13 stsp return NULL;
4119 766841c2 2020-08-13 stsp }
4120 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4122 766841c2 2020-08-13 stsp static char msg[64];
4123 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4124 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4125 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4126 766841c2 2020-08-13 stsp goto done;
4127 766841c2 2020-08-13 stsp }
4128 766841c2 2020-08-13 stsp }
4129 766841c2 2020-08-13 stsp
4130 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4131 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4132 f2a9dc41 2019-12-13 tracey goto done;
4133 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4135 f2a9dc41 2019-12-13 tracey goto done;
4136 f2a9dc41 2019-12-13 tracey }
4137 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4139 4e12cd97 2022-01-25 stsp goto done;
4140 4e12cd97 2022-01-25 stsp }
4141 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4142 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4143 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 f2a9dc41 2019-12-13 tracey goto done;
4145 f2a9dc41 2019-12-13 tracey }
4146 17ed4618 2019-06-02 stsp }
4147 17ed4618 2019-06-02 stsp
4148 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 20a2ad1c 2020-10-20 stsp size_t root_len;
4150 20a2ad1c 2020-10-20 stsp
4151 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4152 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4153 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4154 12463d8b 2019-12-13 stsp ondisk_path);
4155 12463d8b 2019-12-13 stsp goto done;
4156 12463d8b 2019-12-13 stsp }
4157 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4158 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4159 12463d8b 2019-12-13 stsp goto done;
4160 15341bfd 2020-03-05 tracey }
4161 15341bfd 2020-03-05 tracey
4162 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4163 20a2ad1c 2020-10-20 stsp do {
4164 20a2ad1c 2020-10-20 stsp char *parent;
4165 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4166 20a2ad1c 2020-10-20 stsp if (err)
4167 2513f20a 2020-10-20 stsp goto done;
4168 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4169 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4170 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4171 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4172 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4173 20a2ad1c 2020-10-20 stsp ondisk_path);
4174 15341bfd 2020-03-05 tracey break;
4175 15341bfd 2020-03-05 tracey }
4176 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4178 f2a9dc41 2019-12-13 tracey }
4179 17ed4618 2019-06-02 stsp
4180 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4181 f2a9dc41 2019-12-13 tracey done:
4182 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4183 f2a9dc41 2019-12-13 tracey if (err)
4184 f2a9dc41 2019-12-13 tracey return err;
4185 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4186 f2a9dc41 2019-12-13 tracey return NULL;
4187 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4189 17ed4618 2019-06-02 stsp }
4190 17ed4618 2019-06-02 stsp
4191 2ec1f75b 2019-03-26 stsp const struct got_error *
4192 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4193 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4194 766841c2 2020-08-13 stsp const char *status_codes,
4195 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4196 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4197 2ec1f75b 2019-03-26 stsp {
4198 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4199 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4200 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4202 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4203 2ec1f75b 2019-03-26 stsp
4204 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4205 2ec1f75b 2019-03-26 stsp if (err)
4206 2ec1f75b 2019-03-26 stsp return err;
4207 2ec1f75b 2019-03-26 stsp
4208 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 2ec1f75b 2019-03-26 stsp if (err)
4210 2ec1f75b 2019-03-26 stsp goto done;
4211 f2a9dc41 2019-12-13 tracey
4212 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4213 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4214 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4215 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4216 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4217 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4218 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4219 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4220 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4221 2ec1f75b 2019-03-26 stsp
4222 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4223 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4224 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4225 17ed4618 2019-06-02 stsp if (err)
4226 af12c6b9 2019-06-04 stsp break;
4227 2ec1f75b 2019-03-26 stsp }
4228 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4229 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4230 af12c6b9 2019-06-04 stsp err = sync_err;
4231 2ec1f75b 2019-03-26 stsp done:
4232 fb399478 2019-07-12 stsp free(fileindex_path);
4233 2ec1f75b 2019-03-26 stsp if (fileindex)
4234 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4235 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4236 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4237 2ec1f75b 2019-03-26 stsp err = unlockerr;
4238 33aa809d 2019-08-08 stsp return err;
4239 33aa809d 2019-08-08 stsp }
4240 33aa809d 2019-08-08 stsp
4241 33aa809d 2019-08-08 stsp static const struct got_error *
4242 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4243 33aa809d 2019-08-08 stsp {
4244 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4245 33aa809d 2019-08-08 stsp char *line = NULL;
4246 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4247 33aa809d 2019-08-08 stsp ssize_t linelen;
4248 33aa809d 2019-08-08 stsp
4249 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4250 33aa809d 2019-08-08 stsp if (linelen == -1) {
4251 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4252 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4253 33aa809d 2019-08-08 stsp goto done;
4254 33aa809d 2019-08-08 stsp }
4255 33aa809d 2019-08-08 stsp return NULL;
4256 33aa809d 2019-08-08 stsp }
4257 33aa809d 2019-08-08 stsp if (outfile) {
4258 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4259 33aa809d 2019-08-08 stsp if (n != linelen) {
4260 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4261 33aa809d 2019-08-08 stsp goto done;
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp }
4264 33aa809d 2019-08-08 stsp if (rejectfile) {
4265 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4266 33aa809d 2019-08-08 stsp if (n != linelen)
4267 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4268 33aa809d 2019-08-08 stsp }
4269 33aa809d 2019-08-08 stsp done:
4270 33aa809d 2019-08-08 stsp free(line);
4271 2ec1f75b 2019-03-26 stsp return err;
4272 2ec1f75b 2019-03-26 stsp }
4273 1f1abb7e 2019-08-08 stsp
4274 33aa809d 2019-08-08 stsp static const struct got_error *
4275 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4276 33aa809d 2019-08-08 stsp {
4277 33aa809d 2019-08-08 stsp char *line = NULL;
4278 33aa809d 2019-08-08 stsp size_t linesize = 0;
4279 33aa809d 2019-08-08 stsp ssize_t linelen;
4280 33aa809d 2019-08-08 stsp
4281 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4282 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4283 500467ff 2019-09-25 hiltjo if (ferror(f))
4284 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4285 500467ff 2019-09-25 hiltjo return NULL;
4286 500467ff 2019-09-25 hiltjo }
4287 33aa809d 2019-08-08 stsp free(line);
4288 33aa809d 2019-08-08 stsp return NULL;
4289 33aa809d 2019-08-08 stsp }
4290 33aa809d 2019-08-08 stsp
4291 33aa809d 2019-08-08 stsp static const struct got_error *
4292 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4294 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4295 abc59930 2021-09-05 naddy {
4296 33aa809d 2019-08-08 stsp const struct got_error *err;
4297 33aa809d 2019-08-08 stsp
4298 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4299 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4300 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4301 33aa809d 2019-08-08 stsp if (err)
4302 33aa809d 2019-08-08 stsp return err;
4303 33aa809d 2019-08-08 stsp (*line_cur1)++;
4304 33aa809d 2019-08-08 stsp }
4305 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4306 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4307 33aa809d 2019-08-08 stsp if (rejectfile)
4308 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4309 33aa809d 2019-08-08 stsp else
4310 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4311 33aa809d 2019-08-08 stsp if (err)
4312 33aa809d 2019-08-08 stsp return err;
4313 33aa809d 2019-08-08 stsp (*line_cur2)++;
4314 33aa809d 2019-08-08 stsp }
4315 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4316 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4317 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4318 33aa809d 2019-08-08 stsp if (err)
4319 33aa809d 2019-08-08 stsp return err;
4320 33aa809d 2019-08-08 stsp (*line_cur2)++;
4321 33aa809d 2019-08-08 stsp }
4322 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4323 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4324 33aa809d 2019-08-08 stsp if (rejectfile)
4325 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4326 33aa809d 2019-08-08 stsp else
4327 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4328 33aa809d 2019-08-08 stsp if (err)
4329 33aa809d 2019-08-08 stsp return err;
4330 33aa809d 2019-08-08 stsp (*line_cur1)++;
4331 33aa809d 2019-08-08 stsp }
4332 f1e81a05 2019-08-10 stsp
4333 f1e81a05 2019-08-10 stsp return NULL;
4334 f1e81a05 2019-08-10 stsp }
4335 f1e81a05 2019-08-10 stsp
4336 f1e81a05 2019-08-10 stsp static const struct got_error *
4337 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4339 f1e81a05 2019-08-10 stsp {
4340 f1e81a05 2019-08-10 stsp const struct got_error *err;
4341 f1e81a05 2019-08-10 stsp
4342 f1e81a05 2019-08-10 stsp if (outfile) {
4343 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4344 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4345 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4346 f1e81a05 2019-08-10 stsp if (err)
4347 f1e81a05 2019-08-10 stsp return err;
4348 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4349 f1e81a05 2019-08-10 stsp }
4350 f1e81a05 2019-08-10 stsp }
4351 f1e81a05 2019-08-10 stsp if (rejectfile) {
4352 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4353 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4354 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4355 f1e81a05 2019-08-10 stsp if (err)
4356 f1e81a05 2019-08-10 stsp return err;
4357 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4358 f1e81a05 2019-08-10 stsp }
4359 33aa809d 2019-08-08 stsp }
4360 33aa809d 2019-08-08 stsp
4361 33aa809d 2019-08-08 stsp return NULL;
4362 33aa809d 2019-08-08 stsp }
4363 33aa809d 2019-08-08 stsp
4364 33aa809d 2019-08-08 stsp static const struct got_error *
4365 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4366 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4367 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4370 33aa809d 2019-08-08 stsp {
4371 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4372 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4373 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4374 33aa809d 2019-08-08 stsp FILE *hunkfile;
4375 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4376 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4377 fe621944 2020-11-10 stsp int rc;
4378 33aa809d 2019-08-08 stsp
4379 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4380 33aa809d 2019-08-08 stsp
4381 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4382 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 fe621944 2020-11-10 stsp start_old = cc.left.start;
4384 fe621944 2020-11-10 stsp end_old = cc.left.end;
4385 fe621944 2020-11-10 stsp start_new = cc.right.start;
4386 fe621944 2020-11-10 stsp end_new = cc.right.end;
4387 33aa809d 2019-08-08 stsp
4388 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4389 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4390 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4391 33aa809d 2019-08-08 stsp
4392 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4393 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4394 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4395 33aa809d 2019-08-08 stsp
4396 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4397 fe621944 2020-11-10 stsp if (diff_state == NULL)
4398 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4399 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4400 fe621944 2020-11-10 stsp
4401 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4402 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4403 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4404 33aa809d 2019-08-08 stsp goto done;
4405 33aa809d 2019-08-08 stsp }
4406 fe621944 2020-11-10 stsp
4407 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 fe621944 2020-11-10 stsp diff_result, &cc);
4409 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4410 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4411 33aa809d 2019-08-08 stsp goto done;
4412 33aa809d 2019-08-08 stsp }
4413 fe621944 2020-11-10 stsp
4414 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4416 33aa809d 2019-08-08 stsp goto done;
4417 33aa809d 2019-08-08 stsp }
4418 33aa809d 2019-08-08 stsp
4419 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4421 33aa809d 2019-08-08 stsp if (err)
4422 33aa809d 2019-08-08 stsp goto done;
4423 33aa809d 2019-08-08 stsp
4424 33aa809d 2019-08-08 stsp switch (*choice) {
4425 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4426 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4428 33aa809d 2019-08-08 stsp break;
4429 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4430 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4432 33aa809d 2019-08-08 stsp break;
4433 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4434 33aa809d 2019-08-08 stsp break;
4435 33aa809d 2019-08-08 stsp default:
4436 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4437 33aa809d 2019-08-08 stsp break;
4438 33aa809d 2019-08-08 stsp }
4439 33aa809d 2019-08-08 stsp done:
4440 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4441 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4443 33aa809d 2019-08-08 stsp return err;
4444 33aa809d 2019-08-08 stsp }
4445 33aa809d 2019-08-08 stsp
4446 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4447 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4448 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4449 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4450 1f1abb7e 2019-08-08 stsp void *progress_arg;
4451 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4452 33aa809d 2019-08-08 stsp void *patch_arg;
4453 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4454 f259c4c1 2021-09-24 stsp int unlink_added_files;
4455 1f1abb7e 2019-08-08 stsp };
4456 a129376b 2019-03-28 stsp
4457 e20a8b6f 2019-06-04 stsp static const struct got_error *
4458 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4459 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4460 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4461 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4462 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4463 33aa809d 2019-08-08 stsp {
4464 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4465 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4466 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4468 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4469 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4470 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4471 fe621944 2020-11-10 stsp struct stat sb2;
4472 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4473 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4475 33aa809d 2019-08-08 stsp
4476 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4477 33aa809d 2019-08-08 stsp
4478 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4479 33aa809d 2019-08-08 stsp if (err)
4480 33aa809d 2019-08-08 stsp return err;
4481 33aa809d 2019-08-08 stsp
4482 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4483 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4484 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4486 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4487 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4488 fa3cef63 2020-07-23 stsp goto done;
4489 fa3cef63 2020-07-23 stsp }
4490 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4491 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4492 c0df5966 2021-12-31 stsp if (link_len == -1) {
4493 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4494 c0df5966 2021-12-31 stsp path2);
4495 c0df5966 2021-12-31 stsp }
4496 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4497 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4498 12463d8b 2019-12-13 stsp }
4499 12463d8b 2019-12-13 stsp } else {
4500 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4501 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4502 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4503 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4504 fa3cef63 2020-07-23 stsp goto done;
4505 fa3cef63 2020-07-23 stsp }
4506 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4507 fa3cef63 2020-07-23 stsp sizeof(link_target));
4508 fa3cef63 2020-07-23 stsp if (link_len == -1)
4509 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4510 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4511 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4512 12463d8b 2019-12-13 stsp }
4513 1ebedb77 2019-10-19 stsp }
4514 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4515 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4516 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4517 fa3cef63 2020-07-23 stsp goto done;
4518 fa3cef63 2020-07-23 stsp }
4519 1ebedb77 2019-10-19 stsp
4520 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4521 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4522 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4523 fa3cef63 2020-07-23 stsp goto done;
4524 fa3cef63 2020-07-23 stsp }
4525 fa3cef63 2020-07-23 stsp fd2 = -1;
4526 fa3cef63 2020-07-23 stsp } else {
4527 fa3cef63 2020-07-23 stsp size_t n;
4528 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4529 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4530 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4531 fa3cef63 2020-07-23 stsp goto done;
4532 fa3cef63 2020-07-23 stsp }
4533 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4534 fa3cef63 2020-07-23 stsp if (n != link_len) {
4535 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4536 fa3cef63 2020-07-23 stsp goto done;
4537 fa3cef63 2020-07-23 stsp }
4538 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4539 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4540 fa3cef63 2020-07-23 stsp goto done;
4541 fa3cef63 2020-07-23 stsp }
4542 fa3cef63 2020-07-23 stsp rewind(f2);
4543 33aa809d 2019-08-08 stsp }
4544 33aa809d 2019-08-08 stsp
4545 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4546 eb81bc23 2022-06-28 tracey if (fd == -1) {
4547 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4548 eb81bc23 2022-06-28 tracey goto done;
4549 eb81bc23 2022-06-28 tracey }
4550 eb81bc23 2022-06-28 tracey
4551 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4552 33aa809d 2019-08-08 stsp if (err)
4553 33aa809d 2019-08-08 stsp goto done;
4554 33aa809d 2019-08-08 stsp
4555 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4556 33aa809d 2019-08-08 stsp if (err)
4557 33aa809d 2019-08-08 stsp goto done;
4558 33aa809d 2019-08-08 stsp
4559 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4560 33aa809d 2019-08-08 stsp if (err)
4561 33aa809d 2019-08-08 stsp goto done;
4562 33aa809d 2019-08-08 stsp
4563 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4565 33aa809d 2019-08-08 stsp if (err)
4566 33aa809d 2019-08-08 stsp goto done;
4567 33aa809d 2019-08-08 stsp
4568 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4569 33aa809d 2019-08-08 stsp if (err)
4570 33aa809d 2019-08-08 stsp goto done;
4571 33aa809d 2019-08-08 stsp
4572 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4573 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4574 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4575 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4576 fe621944 2020-11-10 stsp
4577 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4578 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4580 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4581 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4582 fe621944 2020-11-10 stsp nchanges++;
4583 fe621944 2020-11-10 stsp }
4584 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4585 33aa809d 2019-08-08 stsp int choice;
4586 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4587 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4588 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4589 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4590 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4591 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4592 33aa809d 2019-08-08 stsp if (err)
4593 33aa809d 2019-08-08 stsp goto done;
4594 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4595 33aa809d 2019-08-08 stsp have_content = 1;
4596 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4597 33aa809d 2019-08-08 stsp break;
4598 33aa809d 2019-08-08 stsp }
4599 1ebedb77 2019-10-19 stsp if (have_content) {
4600 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4602 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4603 1ebedb77 2019-10-19 stsp if (err)
4604 1ebedb77 2019-10-19 stsp goto done;
4605 1ebedb77 2019-10-19 stsp
4606 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4607 b2b3fce1 2022-10-29 op mode_t mode;
4608 b2b3fce1 2022-10-29 op
4609 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4610 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4611 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4612 fa3cef63 2020-07-23 stsp goto done;
4613 fa3cef63 2020-07-23 stsp }
4614 1ebedb77 2019-10-19 stsp }
4615 1ebedb77 2019-10-19 stsp }
4616 33aa809d 2019-08-08 stsp done:
4617 33aa809d 2019-08-08 stsp free(id_str);
4618 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4619 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4620 33aa809d 2019-08-08 stsp if (blob)
4621 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4622 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4623 fe621944 2020-11-10 stsp if (err == NULL)
4624 fe621944 2020-11-10 stsp err = free_err;
4625 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4626 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4627 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4628 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4629 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4631 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4632 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4633 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4634 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4635 33aa809d 2019-08-08 stsp if (err || !have_content) {
4636 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4638 33aa809d 2019-08-08 stsp free(*path_outfile);
4639 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4640 33aa809d 2019-08-08 stsp }
4641 33aa809d 2019-08-08 stsp free(path1);
4642 33aa809d 2019-08-08 stsp return err;
4643 33aa809d 2019-08-08 stsp }
4644 33aa809d 2019-08-08 stsp
4645 33aa809d 2019-08-08 stsp static const struct got_error *
4646 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4648 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4650 a129376b 2019-03-28 stsp {
4651 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4652 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4653 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4654 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4655 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4656 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4657 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4658 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4659 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4660 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4661 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4662 eb81bc23 2022-06-28 tracey int fd = -1;
4663 a129376b 2019-03-28 stsp
4664 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4665 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4666 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4667 d3bcc3d1 2019-08-08 stsp return NULL;
4668 3d69ad8d 2019-08-17 semarie
4669 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4670 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4671 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4672 d3bcc3d1 2019-08-08 stsp
4673 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4674 65084dad 2019-08-08 stsp if (ie == NULL)
4675 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4676 a129376b 2019-03-28 stsp
4677 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4678 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4679 a129376b 2019-03-28 stsp if (err) {
4680 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4681 a129376b 2019-03-28 stsp goto done;
4682 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4683 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4684 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4685 e20a8b6f 2019-06-04 stsp goto done;
4686 e20a8b6f 2019-06-04 stsp }
4687 a129376b 2019-03-28 stsp }
4688 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4690 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4691 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4692 a129376b 2019-03-28 stsp goto done;
4693 a129376b 2019-03-28 stsp }
4694 a129376b 2019-03-28 stsp } else {
4695 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4696 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4697 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4698 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4699 a129376b 2019-03-28 stsp goto done;
4700 a129376b 2019-03-28 stsp }
4701 a129376b 2019-03-28 stsp } else {
4702 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4703 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4704 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4705 a129376b 2019-03-28 stsp goto done;
4706 a129376b 2019-03-28 stsp }
4707 a129376b 2019-03-28 stsp }
4708 a129376b 2019-03-28 stsp }
4709 a129376b 2019-03-28 stsp
4710 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4711 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4712 a44927cc 2022-04-07 stsp if (err)
4713 a44927cc 2022-04-07 stsp goto done;
4714 a44927cc 2022-04-07 stsp
4715 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4716 a9fa2909 2019-07-27 stsp if (err) {
4717 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4719 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4720 a9fa2909 2019-07-27 stsp goto done;
4721 a9fa2909 2019-07-27 stsp } else {
4722 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4723 a9fa2909 2019-07-27 stsp if (err)
4724 a9fa2909 2019-07-27 stsp goto done;
4725 a9fa2909 2019-07-27 stsp
4726 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4727 1233e6b6 2020-10-19 stsp if (err)
4728 a9fa2909 2019-07-27 stsp goto done;
4729 a9fa2909 2019-07-27 stsp
4730 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4731 1233e6b6 2020-10-19 stsp free(te_name);
4732 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4733 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4734 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4735 a9fa2909 2019-07-27 stsp goto done;
4736 a9fa2909 2019-07-27 stsp }
4737 a129376b 2019-03-28 stsp }
4738 a129376b 2019-03-28 stsp
4739 a129376b 2019-03-28 stsp switch (status) {
4740 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4741 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4742 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4743 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4744 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4745 33aa809d 2019-08-08 stsp if (err)
4746 33aa809d 2019-08-08 stsp goto done;
4747 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4748 33aa809d 2019-08-08 stsp break;
4749 33aa809d 2019-08-08 stsp }
4750 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4751 1f1abb7e 2019-08-08 stsp ie->path);
4752 1ee397ad 2019-07-12 stsp if (err)
4753 1ee397ad 2019-07-12 stsp goto done;
4754 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4755 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4756 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4757 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4758 f259c4c1 2021-09-24 stsp relpath) == -1) {
4759 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4760 f259c4c1 2021-09-24 stsp goto done;
4761 f259c4c1 2021-09-24 stsp }
4762 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4763 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4764 f259c4c1 2021-09-24 stsp ondisk_path);
4765 f259c4c1 2021-09-24 stsp break;
4766 f259c4c1 2021-09-24 stsp }
4767 f259c4c1 2021-09-24 stsp }
4768 a129376b 2019-03-28 stsp break;
4769 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4770 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4771 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4772 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4773 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4774 33aa809d 2019-08-08 stsp if (err)
4775 33aa809d 2019-08-08 stsp goto done;
4776 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4777 33aa809d 2019-08-08 stsp break;
4778 33aa809d 2019-08-08 stsp }
4779 33aa809d 2019-08-08 stsp /* fall through */
4780 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4781 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4782 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4783 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4784 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4785 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4786 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4787 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4788 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4789 24278f30 2019-08-03 stsp } else
4790 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4791 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4792 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4793 eb81bc23 2022-06-28 tracey if (fd == -1) {
4794 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4795 eb81bc23 2022-06-28 tracey goto done;
4796 eb81bc23 2022-06-28 tracey }
4797 eb81bc23 2022-06-28 tracey
4798 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4799 a129376b 2019-03-28 stsp if (err)
4800 65084dad 2019-08-08 stsp goto done;
4801 65084dad 2019-08-08 stsp
4802 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4803 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4805 a129376b 2019-03-28 stsp goto done;
4806 65084dad 2019-08-08 stsp }
4807 65084dad 2019-08-08 stsp
4808 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4810 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4811 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4812 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4814 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4815 33aa809d 2019-08-08 stsp break;
4816 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4817 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4818 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4819 369fd7e5 2020-07-23 stsp path_content);
4820 369fd7e5 2020-07-23 stsp break;
4821 369fd7e5 2020-07-23 stsp }
4822 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4823 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4824 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4825 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4826 369fd7e5 2020-07-23 stsp } else {
4827 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4828 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4829 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4830 369fd7e5 2020-07-23 stsp goto done;
4831 369fd7e5 2020-07-23 stsp }
4832 33aa809d 2019-08-08 stsp }
4833 33aa809d 2019-08-08 stsp } else {
4834 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4835 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4836 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4837 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4838 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4839 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4840 2e1fa222 2020-07-23 stsp } else {
4841 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4842 2e1fa222 2020-07-23 stsp ie->path,
4843 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4845 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4846 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4847 2e1fa222 2020-07-23 stsp }
4848 a129376b 2019-03-28 stsp if (err)
4849 a129376b 2019-03-28 stsp goto done;
4850 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4851 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4852 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4853 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4854 437adc9d 2020-12-10 yzhong blob->id.sha1,
4855 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4856 2e1fa222 2020-07-23 stsp if (err)
4857 2e1fa222 2020-07-23 stsp goto done;
4858 2e1fa222 2020-07-23 stsp }
4859 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4860 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4861 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4862 33aa809d 2019-08-08 stsp }
4863 a129376b 2019-03-28 stsp }
4864 a129376b 2019-03-28 stsp break;
4865 e20a8b6f 2019-06-04 stsp }
4866 a129376b 2019-03-28 stsp default:
4867 1f1abb7e 2019-08-08 stsp break;
4868 a129376b 2019-03-28 stsp }
4869 a129376b 2019-03-28 stsp done:
4870 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4871 33aa809d 2019-08-08 stsp free(path_content);
4872 e20a8b6f 2019-06-04 stsp free(parent_path);
4873 a129376b 2019-03-28 stsp free(tree_path);
4874 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4875 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4876 a129376b 2019-03-28 stsp if (blob)
4877 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4878 a129376b 2019-03-28 stsp if (tree)
4879 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4880 a129376b 2019-03-28 stsp free(tree_id);
4881 a44927cc 2022-04-07 stsp if (base_commit)
4882 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4883 e20a8b6f 2019-06-04 stsp return err;
4884 e20a8b6f 2019-06-04 stsp }
4885 e20a8b6f 2019-06-04 stsp
4886 e20a8b6f 2019-06-04 stsp const struct got_error *
4887 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4888 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4889 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4891 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4892 e20a8b6f 2019-06-04 stsp {
4893 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4894 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4895 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4896 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4897 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4898 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4899 e20a8b6f 2019-06-04 stsp
4900 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4901 e20a8b6f 2019-06-04 stsp if (err)
4902 e20a8b6f 2019-06-04 stsp return err;
4903 e20a8b6f 2019-06-04 stsp
4904 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4905 e20a8b6f 2019-06-04 stsp if (err)
4906 e20a8b6f 2019-06-04 stsp goto done;
4907 e20a8b6f 2019-06-04 stsp
4908 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4909 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4910 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4911 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4912 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4913 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4914 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4915 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4916 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4917 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4918 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4919 e20a8b6f 2019-06-04 stsp if (err)
4920 af12c6b9 2019-06-04 stsp break;
4921 e20a8b6f 2019-06-04 stsp }
4922 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4923 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4924 e20a8b6f 2019-06-04 stsp err = sync_err;
4925 af12c6b9 2019-06-04 stsp done:
4926 fb399478 2019-07-12 stsp free(fileindex_path);
4927 a129376b 2019-03-28 stsp if (fileindex)
4928 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4929 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4930 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4931 a129376b 2019-03-28 stsp err = unlockerr;
4932 c4296144 2019-05-09 stsp return err;
4933 c4296144 2019-05-09 stsp }
4934 c4296144 2019-05-09 stsp
4935 cf066bf8 2019-05-09 stsp static void
4936 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4937 cf066bf8 2019-05-09 stsp {
4938 24519714 2019-05-09 stsp free(ct->path);
4939 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4940 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4941 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4942 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4943 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4944 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4945 cf066bf8 2019-05-09 stsp free(ct);
4946 cf066bf8 2019-05-09 stsp }
4947 24519714 2019-05-09 stsp
4948 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4949 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4950 24519714 2019-05-09 stsp struct got_repository *repo;
4951 24519714 2019-05-09 stsp struct got_worktree *worktree;
4952 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4953 5f8a88c6 2019-08-03 stsp int have_staged_files;
4954 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4955 24519714 2019-05-09 stsp };
4956 cf066bf8 2019-05-09 stsp
4957 c4296144 2019-05-09 stsp static const struct got_error *
4958 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4959 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4960 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4961 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4962 c4296144 2019-05-09 stsp {
4963 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4964 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4965 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4966 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4967 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4968 768aea60 2019-05-09 stsp struct stat sb;
4969 c4296144 2019-05-09 stsp
4970 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4971 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4972 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4973 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4974 5f8a88c6 2019-08-03 stsp return NULL;
4975 5f8a88c6 2019-08-03 stsp } else {
4976 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4977 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4978 c4296144 2019-05-09 stsp
4979 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4980 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4981 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4982 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4983 5f8a88c6 2019-08-03 stsp return NULL;
4984 5f8a88c6 2019-08-03 stsp }
4985 0b5cc0d6 2019-05-09 stsp
4986 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4987 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4988 036813ee 2019-05-09 stsp goto done;
4989 036813ee 2019-05-09 stsp }
4990 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4991 036813ee 2019-05-09 stsp parent_path = strdup("");
4992 69960a46 2019-05-09 stsp if (parent_path == NULL)
4993 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4994 69960a46 2019-05-09 stsp } else {
4995 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4996 69960a46 2019-05-09 stsp if (err)
4997 69960a46 2019-05-09 stsp return err;
4998 69960a46 2019-05-09 stsp }
4999 c4296144 2019-05-09 stsp
5000 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5001 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5002 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5003 c4296144 2019-05-09 stsp goto done;
5004 768aea60 2019-05-09 stsp }
5005 768aea60 2019-05-09 stsp
5006 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5007 768aea60 2019-05-09 stsp relpath) == -1) {
5008 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5009 768aea60 2019-05-09 stsp goto done;
5010 768aea60 2019-05-09 stsp }
5011 0aeb8099 2020-07-23 stsp
5012 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5013 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5014 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5015 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5016 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5017 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5018 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5019 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5020 0aeb8099 2020-07-23 stsp break;
5021 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5022 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5023 0aeb8099 2020-07-23 stsp break;
5024 0aeb8099 2020-07-23 stsp default:
5025 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5026 0aeb8099 2020-07-23 stsp goto done;
5027 0aeb8099 2020-07-23 stsp }
5028 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5029 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5030 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5031 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5032 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5033 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5034 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5035 12463d8b 2019-12-13 stsp ct->ondisk_path);
5036 12463d8b 2019-12-13 stsp goto done;
5037 12463d8b 2019-12-13 stsp }
5038 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5039 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5040 768aea60 2019-05-09 stsp goto done;
5041 768aea60 2019-05-09 stsp }
5042 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5043 c4296144 2019-05-09 stsp }
5044 c4296144 2019-05-09 stsp
5045 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5046 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5047 44d03001 2019-05-09 stsp relpath) == -1) {
5048 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5049 44d03001 2019-05-09 stsp goto done;
5050 44d03001 2019-05-09 stsp }
5051 44d03001 2019-05-09 stsp
5052 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5053 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5054 35213c7c 2020-07-23 stsp int is_bad_symlink;
5055 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5056 35213c7c 2020-07-23 stsp ssize_t target_len;
5057 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5058 35213c7c 2020-07-23 stsp sizeof(target_path));
5059 35213c7c 2020-07-23 stsp if (target_len == -1) {
5060 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5061 35213c7c 2020-07-23 stsp ct->ondisk_path);
5062 35213c7c 2020-07-23 stsp goto done;
5063 35213c7c 2020-07-23 stsp }
5064 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5065 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5066 35213c7c 2020-07-23 stsp if (err)
5067 35213c7c 2020-07-23 stsp goto done;
5068 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5069 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5070 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5071 35213c7c 2020-07-23 stsp goto done;
5072 35213c7c 2020-07-23 stsp }
5073 35213c7c 2020-07-23 stsp }
5074 35213c7c 2020-07-23 stsp
5075 35213c7c 2020-07-23 stsp
5076 cf066bf8 2019-05-09 stsp ct->status = status;
5077 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5078 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5079 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5080 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5081 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5082 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5083 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5084 b416585c 2019-05-13 stsp goto done;
5085 b416585c 2019-05-13 stsp }
5086 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5087 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5088 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5089 f0b75401 2019-08-03 stsp goto done;
5090 f0b75401 2019-08-03 stsp }
5091 f0b75401 2019-08-03 stsp }
5092 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5093 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5094 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5095 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5096 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5097 036813ee 2019-05-09 stsp goto done;
5098 036813ee 2019-05-09 stsp }
5099 ca2503ea 2019-05-09 stsp }
5100 24519714 2019-05-09 stsp ct->path = strdup(path);
5101 24519714 2019-05-09 stsp if (ct->path == NULL) {
5102 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5103 24519714 2019-05-09 stsp goto done;
5104 24519714 2019-05-09 stsp }
5105 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5106 c4296144 2019-05-09 stsp done:
5107 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5108 ed175427 2019-05-09 stsp free_commitable(ct);
5109 24519714 2019-05-09 stsp free(parent_path);
5110 036813ee 2019-05-09 stsp free(path);
5111 a129376b 2019-03-28 stsp return err;
5112 a129376b 2019-03-28 stsp }
5113 c4296144 2019-05-09 stsp
5114 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5115 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5116 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5117 036813ee 2019-05-09 stsp struct got_repository *);
5118 ed175427 2019-05-09 stsp
5119 ed175427 2019-05-09 stsp static const struct got_error *
5120 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5121 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5122 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5123 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5124 afa376bf 2019-05-09 stsp struct got_repository *repo)
5125 ed175427 2019-05-09 stsp {
5126 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5127 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5128 036813ee 2019-05-09 stsp char *subpath;
5129 ed175427 2019-05-09 stsp
5130 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5131 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5132 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5133 ed175427 2019-05-09 stsp
5134 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5135 ed175427 2019-05-09 stsp if (err)
5136 ed175427 2019-05-09 stsp return err;
5137 ed175427 2019-05-09 stsp
5138 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5139 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5140 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5141 036813ee 2019-05-09 stsp free(subpath);
5142 036813ee 2019-05-09 stsp return err;
5143 036813ee 2019-05-09 stsp }
5144 ed175427 2019-05-09 stsp
5145 036813ee 2019-05-09 stsp static const struct got_error *
5146 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5147 036813ee 2019-05-09 stsp {
5148 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5149 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5150 ed175427 2019-05-09 stsp
5151 036813ee 2019-05-09 stsp *match = 0;
5152 ed175427 2019-05-09 stsp
5153 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5154 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5155 0f63689d 2019-05-10 stsp return NULL;
5156 036813ee 2019-05-09 stsp }
5157 0b5cc0d6 2019-05-09 stsp
5158 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5159 0f63689d 2019-05-10 stsp if (err)
5160 0f63689d 2019-05-10 stsp return err;
5161 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5162 036813ee 2019-05-09 stsp free(ct_parent_path);
5163 036813ee 2019-05-09 stsp return err;
5164 036813ee 2019-05-09 stsp }
5165 0b5cc0d6 2019-05-09 stsp
5166 768aea60 2019-05-09 stsp static mode_t
5167 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5168 768aea60 2019-05-09 stsp {
5169 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5170 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5171 3d9a4ec4 2020-07-23 stsp
5172 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5173 768aea60 2019-05-09 stsp }
5174 768aea60 2019-05-09 stsp
5175 036813ee 2019-05-09 stsp static const struct got_error *
5176 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5177 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5178 036813ee 2019-05-09 stsp {
5179 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5180 ca2503ea 2019-05-09 stsp
5181 036813ee 2019-05-09 stsp *new_te = NULL;
5182 0b5cc0d6 2019-05-09 stsp
5183 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5184 036813ee 2019-05-09 stsp if (err)
5185 036813ee 2019-05-09 stsp goto done;
5186 0b5cc0d6 2019-05-09 stsp
5187 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5188 036813ee 2019-05-09 stsp
5189 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5190 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5191 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5192 5f8a88c6 2019-08-03 stsp else
5193 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5194 036813ee 2019-05-09 stsp done:
5195 036813ee 2019-05-09 stsp if (err && *new_te) {
5196 56e0773d 2019-11-28 stsp free(*new_te);
5197 036813ee 2019-05-09 stsp *new_te = NULL;
5198 036813ee 2019-05-09 stsp }
5199 036813ee 2019-05-09 stsp return err;
5200 036813ee 2019-05-09 stsp }
5201 036813ee 2019-05-09 stsp
5202 036813ee 2019-05-09 stsp static const struct got_error *
5203 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5204 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5205 036813ee 2019-05-09 stsp {
5206 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5207 102b254e 2020-10-19 stsp char *ct_name = NULL;
5208 036813ee 2019-05-09 stsp
5209 036813ee 2019-05-09 stsp *new_te = NULL;
5210 036813ee 2019-05-09 stsp
5211 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5212 036813ee 2019-05-09 stsp if (*new_te == NULL)
5213 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5214 036813ee 2019-05-09 stsp
5215 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5216 102b254e 2020-10-19 stsp if (err)
5217 036813ee 2019-05-09 stsp goto done;
5218 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5219 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5220 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5221 036813ee 2019-05-09 stsp goto done;
5222 036813ee 2019-05-09 stsp }
5223 036813ee 2019-05-09 stsp
5224 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5225 036813ee 2019-05-09 stsp
5226 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5227 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5228 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5229 5f8a88c6 2019-08-03 stsp else
5230 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5231 036813ee 2019-05-09 stsp done:
5232 102b254e 2020-10-19 stsp free(ct_name);
5233 036813ee 2019-05-09 stsp if (err && *new_te) {
5234 56e0773d 2019-11-28 stsp free(*new_te);
5235 036813ee 2019-05-09 stsp *new_te = NULL;
5236 036813ee 2019-05-09 stsp }
5237 036813ee 2019-05-09 stsp return err;
5238 036813ee 2019-05-09 stsp }
5239 036813ee 2019-05-09 stsp
5240 036813ee 2019-05-09 stsp static const struct got_error *
5241 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5242 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5243 036813ee 2019-05-09 stsp {
5244 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5245 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5246 036813ee 2019-05-09 stsp
5247 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5248 036813ee 2019-05-09 stsp if (err)
5249 036813ee 2019-05-09 stsp return err;
5250 036813ee 2019-05-09 stsp if (new_pe == NULL)
5251 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5252 036813ee 2019-05-09 stsp return NULL;
5253 afa376bf 2019-05-09 stsp }
5254 afa376bf 2019-05-09 stsp
5255 afa376bf 2019-05-09 stsp static const struct got_error *
5256 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5257 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5258 afa376bf 2019-05-09 stsp {
5259 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5260 5f8a88c6 2019-08-03 stsp unsigned char status;
5261 0ff8d236 2021-09-28 stsp
5262 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5263 0ff8d236 2021-09-28 stsp return NULL;
5264 5f8a88c6 2019-08-03 stsp
5265 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5266 afa376bf 2019-05-09 stsp ct_path++;
5267 5f8a88c6 2019-08-03 stsp
5268 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5269 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5270 5f8a88c6 2019-08-03 stsp else
5271 5f8a88c6 2019-08-03 stsp status = ct->status;
5272 5f8a88c6 2019-08-03 stsp
5273 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5274 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5275 036813ee 2019-05-09 stsp }
5276 036813ee 2019-05-09 stsp
5277 036813ee 2019-05-09 stsp static const struct got_error *
5278 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5279 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5280 44d03001 2019-05-09 stsp {
5281 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5282 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5283 44d03001 2019-05-09 stsp char *te_path;
5284 44d03001 2019-05-09 stsp
5285 44d03001 2019-05-09 stsp *modified = 0;
5286 44d03001 2019-05-09 stsp
5287 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5288 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5289 44d03001 2019-05-09 stsp te->name) == -1)
5290 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5291 44d03001 2019-05-09 stsp
5292 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5293 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5294 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5295 44d03001 2019-05-09 stsp strlen(te_path));
5296 62d463ca 2020-10-20 naddy if (*modified)
5297 44d03001 2019-05-09 stsp break;
5298 44d03001 2019-05-09 stsp }
5299 44d03001 2019-05-09 stsp
5300 44d03001 2019-05-09 stsp free(te_path);
5301 44d03001 2019-05-09 stsp return err;
5302 44d03001 2019-05-09 stsp }
5303 44d03001 2019-05-09 stsp
5304 44d03001 2019-05-09 stsp static const struct got_error *
5305 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5306 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5307 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5308 036813ee 2019-05-09 stsp {
5309 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5310 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5311 036813ee 2019-05-09 stsp
5312 036813ee 2019-05-09 stsp *ctp = NULL;
5313 036813ee 2019-05-09 stsp
5314 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5315 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5316 036813ee 2019-05-09 stsp char *ct_name = NULL;
5317 036813ee 2019-05-09 stsp int path_matches;
5318 036813ee 2019-05-09 stsp
5319 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5320 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5321 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5322 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5323 5f8a88c6 2019-08-03 stsp continue;
5324 5f8a88c6 2019-08-03 stsp } else {
5325 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5326 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5327 5f8a88c6 2019-08-03 stsp continue;
5328 5f8a88c6 2019-08-03 stsp }
5329 036813ee 2019-05-09 stsp
5330 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5331 036813ee 2019-05-09 stsp continue;
5332 036813ee 2019-05-09 stsp
5333 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5334 62d463ca 2020-10-20 naddy if (err)
5335 036813ee 2019-05-09 stsp return err;
5336 036813ee 2019-05-09 stsp if (!path_matches)
5337 036813ee 2019-05-09 stsp continue;
5338 036813ee 2019-05-09 stsp
5339 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5340 d34b633e 2020-10-19 stsp if (err)
5341 d34b633e 2020-10-19 stsp return err;
5342 d34b633e 2020-10-19 stsp
5343 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5344 d34b633e 2020-10-19 stsp free(ct_name);
5345 036813ee 2019-05-09 stsp continue;
5346 d34b633e 2020-10-19 stsp }
5347 d34b633e 2020-10-19 stsp free(ct_name);
5348 036813ee 2019-05-09 stsp
5349 036813ee 2019-05-09 stsp *ctp = ct;
5350 036813ee 2019-05-09 stsp break;
5351 036813ee 2019-05-09 stsp }
5352 2b496619 2019-07-10 stsp
5353 2b496619 2019-07-10 stsp return err;
5354 2b496619 2019-07-10 stsp }
5355 2b496619 2019-07-10 stsp
5356 2b496619 2019-07-10 stsp static const struct got_error *
5357 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5358 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5359 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5360 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5361 2b496619 2019-07-10 stsp struct got_repository *repo)
5362 2b496619 2019-07-10 stsp {
5363 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5364 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5365 2b496619 2019-07-10 stsp char *subtree_path;
5366 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5367 ba580f68 2020-03-22 stsp int nentries;
5368 2b496619 2019-07-10 stsp
5369 2b496619 2019-07-10 stsp *new_tep = NULL;
5370 2b496619 2019-07-10 stsp
5371 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5372 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5373 2b496619 2019-07-10 stsp child_path) == -1)
5374 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5375 036813ee 2019-05-09 stsp
5376 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5377 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5378 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5379 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5380 56e0773d 2019-11-28 stsp
5381 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5382 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5383 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5384 2b496619 2019-07-10 stsp goto done;
5385 2b496619 2019-07-10 stsp }
5386 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5387 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5388 2b496619 2019-07-10 stsp if (err) {
5389 56e0773d 2019-11-28 stsp free(new_te);
5390 2b496619 2019-07-10 stsp goto done;
5391 2b496619 2019-07-10 stsp }
5392 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5393 2b496619 2019-07-10 stsp done:
5394 56e0773d 2019-11-28 stsp free(id);
5395 2b496619 2019-07-10 stsp free(subtree_path);
5396 2b496619 2019-07-10 stsp if (err == NULL)
5397 2b496619 2019-07-10 stsp *new_tep = new_te;
5398 036813ee 2019-05-09 stsp return err;
5399 036813ee 2019-05-09 stsp }
5400 036813ee 2019-05-09 stsp
5401 036813ee 2019-05-09 stsp static const struct got_error *
5402 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5403 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5404 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5405 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5406 036813ee 2019-05-09 stsp struct got_repository *repo)
5407 036813ee 2019-05-09 stsp {
5408 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5409 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5410 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5411 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5412 036813ee 2019-05-09 stsp
5413 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5414 ba580f68 2020-03-22 stsp *nentries = 0;
5415 036813ee 2019-05-09 stsp
5416 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5417 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5418 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5419 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5420 036813ee 2019-05-09 stsp
5421 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5422 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5423 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5424 036813ee 2019-05-09 stsp continue;
5425 036813ee 2019-05-09 stsp
5426 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5427 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5428 036813ee 2019-05-09 stsp continue;
5429 036813ee 2019-05-09 stsp
5430 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5431 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5432 036813ee 2019-05-09 stsp if (err)
5433 036813ee 2019-05-09 stsp goto done;
5434 036813ee 2019-05-09 stsp
5435 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5436 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5437 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5438 036813ee 2019-05-09 stsp if (err)
5439 036813ee 2019-05-09 stsp goto done;
5440 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5441 afa376bf 2019-05-09 stsp if (err)
5442 afa376bf 2019-05-09 stsp goto done;
5443 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5444 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5445 2b496619 2019-07-10 stsp if (err)
5446 2f51b5b3 2019-05-09 stsp goto done;
5447 ba580f68 2020-03-22 stsp (*nentries)++;
5448 2b496619 2019-07-10 stsp } else {
5449 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5450 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5451 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5452 2b496619 2019-07-10 stsp == NULL) {
5453 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5454 2b496619 2019-07-10 stsp child_path, path_base_tree,
5455 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5456 2b496619 2019-07-10 stsp repo);
5457 2b496619 2019-07-10 stsp if (err)
5458 2b496619 2019-07-10 stsp goto done;
5459 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5460 2b496619 2019-07-10 stsp if (err)
5461 2b496619 2019-07-10 stsp goto done;
5462 ba580f68 2020-03-22 stsp (*nentries)++;
5463 9ba0479c 2019-05-10 stsp }
5464 ed175427 2019-05-09 stsp }
5465 2f51b5b3 2019-05-09 stsp }
5466 2f51b5b3 2019-05-09 stsp
5467 2f51b5b3 2019-05-09 stsp if (base_tree) {
5468 56e0773d 2019-11-28 stsp int i, nbase_entries;
5469 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5470 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5471 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5472 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5473 63c5ca5d 2019-08-24 stsp
5474 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5475 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5476 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5477 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5478 63c5ca5d 2019-08-24 stsp if (err)
5479 63c5ca5d 2019-08-24 stsp goto done;
5480 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5481 63c5ca5d 2019-08-24 stsp if (err)
5482 63c5ca5d 2019-08-24 stsp goto done;
5483 ba580f68 2020-03-22 stsp (*nentries)++;
5484 63c5ca5d 2019-08-24 stsp continue;
5485 63c5ca5d 2019-08-24 stsp }
5486 2f51b5b3 2019-05-09 stsp
5487 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5488 44d03001 2019-05-09 stsp int modified;
5489 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5490 036813ee 2019-05-09 stsp if (err)
5491 036813ee 2019-05-09 stsp goto done;
5492 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5493 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5494 2f51b5b3 2019-05-09 stsp if (err)
5495 2f51b5b3 2019-05-09 stsp goto done;
5496 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5497 44d03001 2019-05-09 stsp if (modified) {
5498 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5499 ba580f68 2020-03-22 stsp int nsubentries;
5500 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5501 ba580f68 2020-03-22 stsp &nsubentries, te,
5502 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5503 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5504 44d03001 2019-05-09 stsp if (err)
5505 44d03001 2019-05-09 stsp goto done;
5506 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5507 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5508 ba580f68 2020-03-22 stsp free(new_id);
5509 ba580f68 2020-03-22 stsp continue;
5510 ba580f68 2020-03-22 stsp }
5511 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5512 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5513 56e0773d 2019-11-28 stsp free(new_id);
5514 44d03001 2019-05-09 stsp }
5515 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5516 036813ee 2019-05-09 stsp if (err)
5517 036813ee 2019-05-09 stsp goto done;
5518 ba580f68 2020-03-22 stsp (*nentries)++;
5519 2f51b5b3 2019-05-09 stsp continue;
5520 036813ee 2019-05-09 stsp }
5521 2f51b5b3 2019-05-09 stsp
5522 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5523 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5524 f66c734c 2019-09-22 stsp if (err)
5525 f66c734c 2019-09-22 stsp goto done;
5526 2f51b5b3 2019-05-09 stsp if (ct) {
5527 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5528 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5529 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5530 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5531 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5532 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5533 2f51b5b3 2019-05-09 stsp if (err)
5534 2f51b5b3 2019-05-09 stsp goto done;
5535 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5536 2f51b5b3 2019-05-09 stsp if (err)
5537 2f51b5b3 2019-05-09 stsp goto done;
5538 ba580f68 2020-03-22 stsp (*nentries)++;
5539 2f51b5b3 2019-05-09 stsp }
5540 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5541 b416585c 2019-05-13 stsp status_arg);
5542 afa376bf 2019-05-09 stsp if (err)
5543 afa376bf 2019-05-09 stsp goto done;
5544 2f51b5b3 2019-05-09 stsp } else {
5545 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5546 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5547 2f51b5b3 2019-05-09 stsp if (err)
5548 2f51b5b3 2019-05-09 stsp goto done;
5549 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5550 2f51b5b3 2019-05-09 stsp if (err)
5551 2f51b5b3 2019-05-09 stsp goto done;
5552 ba580f68 2020-03-22 stsp (*nentries)++;
5553 2f51b5b3 2019-05-09 stsp }
5554 ca2503ea 2019-05-09 stsp }
5555 ed175427 2019-05-09 stsp }
5556 0b5cc0d6 2019-05-09 stsp
5557 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5558 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5559 ed175427 2019-05-09 stsp done:
5560 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5561 2e1fa222 2020-07-23 stsp return err;
5562 2e1fa222 2020-07-23 stsp }
5563 2e1fa222 2020-07-23 stsp
5564 2e1fa222 2020-07-23 stsp static const struct got_error *
5565 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5566 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5567 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5568 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5569 ebf99748 2019-05-09 stsp {
5570 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5571 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5572 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5573 ebf99748 2019-05-09 stsp
5574 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5575 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5576 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5577 ebf99748 2019-05-09 stsp
5578 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5579 437adc9d 2020-12-10 yzhong
5580 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5581 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5582 437adc9d 2020-12-10 yzhong if (err)
5583 437adc9d 2020-12-10 yzhong goto done;
5584 437adc9d 2020-12-10 yzhong
5585 ebf99748 2019-05-09 stsp if (ie) {
5586 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5587 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5588 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5589 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5590 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5591 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5592 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5593 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5594 437adc9d 2020-12-10 yzhong
5595 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5596 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5597 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5598 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5599 72fd46fa 2019-09-06 stsp !have_staged_files);
5600 ebf99748 2019-05-09 stsp } else
5601 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5602 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5603 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5604 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5605 72fd46fa 2019-09-06 stsp !have_staged_files);
5606 ebf99748 2019-05-09 stsp } else {
5607 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5608 ebf99748 2019-05-09 stsp if (err)
5609 437adc9d 2020-12-10 yzhong goto done;
5610 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5611 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5612 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5613 3969253a 2020-03-07 stsp if (err) {
5614 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5615 437adc9d 2020-12-10 yzhong goto done;
5616 3969253a 2020-03-07 stsp }
5617 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5618 3969253a 2020-03-07 stsp if (err) {
5619 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5620 437adc9d 2020-12-10 yzhong goto done;
5621 3969253a 2020-03-07 stsp }
5622 ebf99748 2019-05-09 stsp }
5623 437adc9d 2020-12-10 yzhong free(relpath);
5624 437adc9d 2020-12-10 yzhong relpath = NULL;
5625 ebf99748 2019-05-09 stsp }
5626 437adc9d 2020-12-10 yzhong done:
5627 437adc9d 2020-12-10 yzhong free(relpath);
5628 d56d26ce 2019-05-10 stsp return err;
5629 d56d26ce 2019-05-10 stsp }
5630 735ef5ac 2019-08-03 stsp
5631 d56d26ce 2019-05-10 stsp
5632 d56d26ce 2019-05-10 stsp static const struct got_error *
5633 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5634 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5635 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5636 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5637 735ef5ac 2019-08-03 stsp int ood_errcode)
5638 d56d26ce 2019-05-10 stsp {
5639 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5640 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5641 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5642 1a36436d 2019-06-10 stsp
5643 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5644 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5645 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5646 1a36436d 2019-06-10 stsp return NULL;
5647 9bead371 2019-07-28 stsp /*
5648 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5649 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5650 9bead371 2019-07-28 stsp */
5651 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5652 a44927cc 2022-04-07 stsp if (err)
5653 a44927cc 2022-04-07 stsp goto done;
5654 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5655 9bead371 2019-07-28 stsp if (err) {
5656 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5657 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5658 1a36436d 2019-06-10 stsp goto done;
5659 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5660 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5661 1a36436d 2019-06-10 stsp } else {
5662 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5663 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5664 a44927cc 2022-04-07 stsp if (err)
5665 a44927cc 2022-04-07 stsp goto done;
5666 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5667 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5668 1a36436d 2019-06-10 stsp goto done;
5669 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5670 1a36436d 2019-06-10 stsp }
5671 1a36436d 2019-06-10 stsp done:
5672 1a36436d 2019-06-10 stsp free(id);
5673 a44927cc 2022-04-07 stsp if (commit)
5674 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5675 1a36436d 2019-06-10 stsp return err;
5676 ebf99748 2019-05-09 stsp }
5677 ebf99748 2019-05-09 stsp
5678 336075a4 2022-06-25 op static const struct got_error *
5679 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5680 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5681 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5682 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5683 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5684 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5685 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5686 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5687 afa376bf 2019-05-09 stsp struct got_repository *repo)
5688 c4296144 2019-05-09 stsp {
5689 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5690 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5691 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5692 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5693 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5694 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5695 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5696 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5697 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5698 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5699 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5700 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5701 f8399b8f 2022-07-22 stsp time_t timestamp;
5702 c4296144 2019-05-09 stsp
5703 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5704 c4296144 2019-05-09 stsp
5705 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5706 675c7539 2019-05-09 stsp
5707 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5708 588edf97 2019-05-10 stsp if (err)
5709 588edf97 2019-05-10 stsp goto done;
5710 de18fc63 2019-05-09 stsp
5711 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5712 588edf97 2019-05-10 stsp if (err)
5713 588edf97 2019-05-10 stsp goto done;
5714 588edf97 2019-05-10 stsp
5715 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5716 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5717 33ad4cbe 2019-05-12 jcs if (err)
5718 33ad4cbe 2019-05-12 jcs goto done;
5719 33ad4cbe 2019-05-12 jcs }
5720 c4296144 2019-05-09 stsp
5721 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5722 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5723 33ad4cbe 2019-05-12 jcs goto done;
5724 33ad4cbe 2019-05-12 jcs }
5725 33ad4cbe 2019-05-12 jcs
5726 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5727 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5728 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5729 cf066bf8 2019-05-09 stsp char *ondisk_path;
5730 cf066bf8 2019-05-09 stsp
5731 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5732 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5733 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5734 5f8a88c6 2019-08-03 stsp continue;
5735 5f8a88c6 2019-08-03 stsp
5736 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5737 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5738 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5739 cf066bf8 2019-05-09 stsp continue;
5740 cf066bf8 2019-05-09 stsp
5741 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5742 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5743 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5744 cf066bf8 2019-05-09 stsp goto done;
5745 cf066bf8 2019-05-09 stsp }
5746 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5747 2e1fa222 2020-07-23 stsp free(ondisk_path);
5748 2e1fa222 2020-07-23 stsp if (err)
5749 cf066bf8 2019-05-09 stsp goto done;
5750 cf066bf8 2019-05-09 stsp }
5751 036813ee 2019-05-09 stsp
5752 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5753 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5754 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5755 036813ee 2019-05-09 stsp if (err)
5756 036813ee 2019-05-09 stsp goto done;
5757 036813ee 2019-05-09 stsp
5758 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5759 de18fc63 2019-05-09 stsp if (err)
5760 de18fc63 2019-05-09 stsp goto done;
5761 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5762 f259c4c1 2021-09-24 stsp nparents++;
5763 f259c4c1 2021-09-24 stsp if (parent_id2) {
5764 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5765 f259c4c1 2021-09-24 stsp if (err)
5766 f259c4c1 2021-09-24 stsp goto done;
5767 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5768 f259c4c1 2021-09-24 stsp nparents++;
5769 f259c4c1 2021-09-24 stsp }
5770 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
5771 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5772 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
5773 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5774 33ad4cbe 2019-05-12 jcs free(logmsg);
5775 9d40349a 2019-05-09 stsp if (err)
5776 9d40349a 2019-05-09 stsp goto done;
5777 9d40349a 2019-05-09 stsp
5778 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5779 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5780 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5781 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5782 09f5bd90 2019-05-09 stsp goto done;
5783 09f5bd90 2019-05-09 stsp }
5784 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5785 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5786 09f5bd90 2019-05-09 stsp if (err)
5787 09f5bd90 2019-05-09 stsp goto done;
5788 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5789 ebf99748 2019-05-09 stsp if (err)
5790 ebf99748 2019-05-09 stsp goto done;
5791 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5792 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5793 09f5bd90 2019-05-09 stsp goto done;
5794 09f5bd90 2019-05-09 stsp }
5795 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5796 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5797 f2c16586 2019-05-09 stsp if (err)
5798 f2c16586 2019-05-09 stsp goto done;
5799 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5800 f2c16586 2019-05-09 stsp if (err)
5801 f2c16586 2019-05-09 stsp goto done;
5802 f2c16586 2019-05-09 stsp
5803 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5804 09f5bd90 2019-05-09 stsp if (err)
5805 09f5bd90 2019-05-09 stsp goto done;
5806 09f5bd90 2019-05-09 stsp
5807 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5808 ebf99748 2019-05-09 stsp if (err)
5809 ebf99748 2019-05-09 stsp goto done;
5810 c4296144 2019-05-09 stsp done:
5811 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5812 588edf97 2019-05-10 stsp if (head_tree)
5813 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5814 588edf97 2019-05-10 stsp if (head_commit)
5815 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5816 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5817 2f17228e 2019-05-12 stsp if (head_ref2) {
5818 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5819 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5820 2f17228e 2019-05-12 stsp err = unlockerr;
5821 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5822 39cd0ff6 2019-07-12 stsp }
5823 39cd0ff6 2019-07-12 stsp return err;
5824 39cd0ff6 2019-07-12 stsp }
5825 5c1e53bc 2019-07-28 stsp
5826 5c1e53bc 2019-07-28 stsp static const struct got_error *
5827 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5828 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5829 5c1e53bc 2019-07-28 stsp {
5830 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5831 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5832 39cd0ff6 2019-07-12 stsp
5833 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5834 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5835 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5836 5c1e53bc 2019-07-28 stsp
5837 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5838 5c1e53bc 2019-07-28 stsp ct_path++;
5839 5c1e53bc 2019-07-28 stsp
5840 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5841 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5842 5c1e53bc 2019-07-28 stsp break;
5843 5c1e53bc 2019-07-28 stsp }
5844 5c1e53bc 2019-07-28 stsp
5845 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5846 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5847 5c1e53bc 2019-07-28 stsp
5848 5c1e53bc 2019-07-28 stsp return NULL;
5849 5c1e53bc 2019-07-28 stsp }
5850 5c1e53bc 2019-07-28 stsp
5851 f0b75401 2019-08-03 stsp static const struct got_error *
5852 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5853 f0b75401 2019-08-03 stsp {
5854 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5855 f0b75401 2019-08-03 stsp
5856 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5857 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5858 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5859 f0b75401 2019-08-03 stsp }
5860 f0b75401 2019-08-03 stsp
5861 f0b75401 2019-08-03 stsp return NULL;
5862 f0b75401 2019-08-03 stsp }
5863 f0b75401 2019-08-03 stsp
5864 5f8a88c6 2019-08-03 stsp static const struct got_error *
5865 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5866 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5867 5f8a88c6 2019-08-03 stsp {
5868 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5869 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5870 5f8a88c6 2019-08-03 stsp
5871 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5872 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5873 5f8a88c6 2019-08-03 stsp continue;
5874 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5875 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5876 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5877 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5878 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5879 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5880 5f8a88c6 2019-08-03 stsp }
5881 5f8a88c6 2019-08-03 stsp
5882 5f8a88c6 2019-08-03 stsp return NULL;
5883 5f8a88c6 2019-08-03 stsp }
5884 5f8a88c6 2019-08-03 stsp
5885 39cd0ff6 2019-07-12 stsp const struct got_error *
5886 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5887 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5888 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5889 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5890 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5891 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5892 39cd0ff6 2019-07-12 stsp {
5893 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5894 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5895 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5896 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5897 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5898 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5899 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5900 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5901 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5902 39cd0ff6 2019-07-12 stsp
5903 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5904 39cd0ff6 2019-07-12 stsp
5905 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5906 39cd0ff6 2019-07-12 stsp
5907 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5908 39cd0ff6 2019-07-12 stsp if (err)
5909 39cd0ff6 2019-07-12 stsp goto done;
5910 39cd0ff6 2019-07-12 stsp
5911 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5912 39cd0ff6 2019-07-12 stsp if (err)
5913 39cd0ff6 2019-07-12 stsp goto done;
5914 39cd0ff6 2019-07-12 stsp
5915 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5916 39cd0ff6 2019-07-12 stsp if (err)
5917 39cd0ff6 2019-07-12 stsp goto done;
5918 39cd0ff6 2019-07-12 stsp
5919 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5920 39cd0ff6 2019-07-12 stsp if (err)
5921 39cd0ff6 2019-07-12 stsp goto done;
5922 39cd0ff6 2019-07-12 stsp
5923 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5924 5f8a88c6 2019-08-03 stsp &have_staged_files);
5925 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5926 5f8a88c6 2019-08-03 stsp goto done;
5927 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5928 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5929 5f8a88c6 2019-08-03 stsp if (err)
5930 5f8a88c6 2019-08-03 stsp goto done;
5931 5f8a88c6 2019-08-03 stsp }
5932 5f8a88c6 2019-08-03 stsp
5933 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5934 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5935 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5936 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5937 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5938 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5939 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5940 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5941 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5942 5c1e53bc 2019-07-28 stsp if (err)
5943 5c1e53bc 2019-07-28 stsp goto done;
5944 5c1e53bc 2019-07-28 stsp }
5945 39cd0ff6 2019-07-12 stsp
5946 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5947 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5948 39cd0ff6 2019-07-12 stsp goto done;
5949 5c1e53bc 2019-07-28 stsp }
5950 5c1e53bc 2019-07-28 stsp
5951 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5952 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5953 5c1e53bc 2019-07-28 stsp if (err)
5954 5c1e53bc 2019-07-28 stsp goto done;
5955 39cd0ff6 2019-07-12 stsp }
5956 f0b75401 2019-08-03 stsp
5957 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5958 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5959 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5960 f0b75401 2019-08-03 stsp
5961 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5962 f0b75401 2019-08-03 stsp ct_path++;
5963 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5964 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5965 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5966 b50cabdf 2019-07-12 stsp if (err)
5967 b50cabdf 2019-07-12 stsp goto done;
5968 f0b75401 2019-08-03 stsp
5969 b50cabdf 2019-07-12 stsp }
5970 b50cabdf 2019-07-12 stsp
5971 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5972 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5973 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5974 39cd0ff6 2019-07-12 stsp if (err)
5975 39cd0ff6 2019-07-12 stsp goto done;
5976 39cd0ff6 2019-07-12 stsp
5977 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5978 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5979 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5980 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5981 39cd0ff6 2019-07-12 stsp err = sync_err;
5982 39cd0ff6 2019-07-12 stsp done:
5983 39cd0ff6 2019-07-12 stsp if (fileindex)
5984 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5985 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5986 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5987 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5988 39cd0ff6 2019-07-12 stsp err = unlockerr;
5989 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5990 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5991 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5992 39cd0ff6 2019-07-12 stsp }
5993 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5994 c4296144 2019-05-09 stsp return err;
5995 8656d6c4 2019-05-20 stsp }
5996 8656d6c4 2019-05-20 stsp
5997 8656d6c4 2019-05-20 stsp const char *
5998 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5999 8656d6c4 2019-05-20 stsp {
6000 8656d6c4 2019-05-20 stsp return ct->path;
6001 8656d6c4 2019-05-20 stsp }
6002 8656d6c4 2019-05-20 stsp
6003 8656d6c4 2019-05-20 stsp unsigned int
6004 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6005 8656d6c4 2019-05-20 stsp {
6006 8656d6c4 2019-05-20 stsp return ct->status;
6007 818c7501 2019-07-11 stsp }
6008 818c7501 2019-07-11 stsp
6009 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6010 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6011 818c7501 2019-07-11 stsp struct got_repository *repo;
6012 818c7501 2019-07-11 stsp };
6013 818c7501 2019-07-11 stsp
6014 818c7501 2019-07-11 stsp static const struct got_error *
6015 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6016 818c7501 2019-07-11 stsp {
6017 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6018 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6019 818c7501 2019-07-11 stsp unsigned char status;
6020 818c7501 2019-07-11 stsp struct stat sb;
6021 818c7501 2019-07-11 stsp char *ondisk_path;
6022 818c7501 2019-07-11 stsp
6023 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6024 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6025 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6026 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6027 818c7501 2019-07-11 stsp
6028 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6029 818c7501 2019-07-11 stsp == -1)
6030 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6031 818c7501 2019-07-11 stsp
6032 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6033 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6034 818c7501 2019-07-11 stsp free(ondisk_path);
6035 818c7501 2019-07-11 stsp if (err)
6036 818c7501 2019-07-11 stsp return err;
6037 818c7501 2019-07-11 stsp
6038 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6039 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6040 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6041 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6042 818c7501 2019-07-11 stsp
6043 818c7501 2019-07-11 stsp return NULL;
6044 818c7501 2019-07-11 stsp }
6045 818c7501 2019-07-11 stsp
6046 818c7501 2019-07-11 stsp const struct got_error *
6047 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6048 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6049 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6050 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6051 818c7501 2019-07-11 stsp {
6052 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6053 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6054 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6055 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6056 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6057 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6058 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6059 818c7501 2019-07-11 stsp
6060 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6061 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6062 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6063 818c7501 2019-07-11 stsp
6064 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6065 818c7501 2019-07-11 stsp if (err)
6066 818c7501 2019-07-11 stsp return err;
6067 818c7501 2019-07-11 stsp
6068 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6069 818c7501 2019-07-11 stsp if (err)
6070 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6073 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6074 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6075 818c7501 2019-07-11 stsp &ok_arg);
6076 818c7501 2019-07-11 stsp if (err)
6077 818c7501 2019-07-11 stsp goto done;
6078 818c7501 2019-07-11 stsp
6079 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6080 818c7501 2019-07-11 stsp if (err)
6081 818c7501 2019-07-11 stsp goto done;
6082 818c7501 2019-07-11 stsp
6083 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6084 818c7501 2019-07-11 stsp if (err)
6085 818c7501 2019-07-11 stsp goto done;
6086 818c7501 2019-07-11 stsp
6087 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6088 818c7501 2019-07-11 stsp if (err)
6089 818c7501 2019-07-11 stsp goto done;
6090 818c7501 2019-07-11 stsp
6091 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6092 818c7501 2019-07-11 stsp 0);
6093 e51d7b55 2020-01-04 stsp if (err)
6094 e51d7b55 2020-01-04 stsp goto done;
6095 e51d7b55 2020-01-04 stsp
6096 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6097 818c7501 2019-07-11 stsp if (err)
6098 818c7501 2019-07-11 stsp goto done;
6099 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6100 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6101 e51d7b55 2020-01-04 stsp goto done;
6102 e51d7b55 2020-01-04 stsp }
6103 818c7501 2019-07-11 stsp
6104 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6105 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6106 818c7501 2019-07-11 stsp if (err)
6107 818c7501 2019-07-11 stsp goto done;
6108 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6109 818c7501 2019-07-11 stsp if (err)
6110 818c7501 2019-07-11 stsp goto done;
6111 818c7501 2019-07-11 stsp
6112 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6113 818c7501 2019-07-11 stsp
6114 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6115 818c7501 2019-07-11 stsp if (err)
6116 818c7501 2019-07-11 stsp goto done;
6117 818c7501 2019-07-11 stsp
6118 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6119 818c7501 2019-07-11 stsp if (err)
6120 818c7501 2019-07-11 stsp goto done;
6121 818c7501 2019-07-11 stsp
6122 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6123 818c7501 2019-07-11 stsp worktree->base_commit_id);
6124 818c7501 2019-07-11 stsp if (err)
6125 818c7501 2019-07-11 stsp goto done;
6126 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6127 818c7501 2019-07-11 stsp if (err)
6128 818c7501 2019-07-11 stsp goto done;
6129 818c7501 2019-07-11 stsp
6130 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6131 818c7501 2019-07-11 stsp if (err)
6132 818c7501 2019-07-11 stsp goto done;
6133 818c7501 2019-07-11 stsp done:
6134 818c7501 2019-07-11 stsp free(fileindex_path);
6135 818c7501 2019-07-11 stsp free(tmp_branch_name);
6136 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6137 818c7501 2019-07-11 stsp free(branch_ref_name);
6138 818c7501 2019-07-11 stsp if (branch_ref)
6139 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6140 818c7501 2019-07-11 stsp if (wt_branch)
6141 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6142 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6143 818c7501 2019-07-11 stsp if (err) {
6144 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6145 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6146 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6147 818c7501 2019-07-11 stsp }
6148 818c7501 2019-07-11 stsp if (*tmp_branch) {
6149 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6150 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6151 818c7501 2019-07-11 stsp }
6152 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6153 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6154 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6155 3e3a69f1 2019-07-25 stsp }
6156 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6157 818c7501 2019-07-11 stsp }
6158 818c7501 2019-07-11 stsp return err;
6159 818c7501 2019-07-11 stsp }
6160 818c7501 2019-07-11 stsp
6161 818c7501 2019-07-11 stsp const struct got_error *
6162 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6163 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6164 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6165 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6166 818c7501 2019-07-11 stsp {
6167 818c7501 2019-07-11 stsp const struct got_error *err;
6168 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6169 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6170 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6171 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6172 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6173 818c7501 2019-07-11 stsp
6174 818c7501 2019-07-11 stsp *commit_id = NULL;
6175 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6176 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6177 3e3a69f1 2019-07-25 stsp *branch = NULL;
6178 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6179 3e3a69f1 2019-07-25 stsp
6180 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6181 3e3a69f1 2019-07-25 stsp if (err)
6182 3e3a69f1 2019-07-25 stsp return err;
6183 818c7501 2019-07-11 stsp
6184 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6185 3e3a69f1 2019-07-25 stsp if (err)
6186 f032f1f7 2019-08-04 stsp goto done;
6187 f032f1f7 2019-08-04 stsp
6188 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6189 f032f1f7 2019-08-04 stsp &have_staged_files);
6190 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6191 f032f1f7 2019-08-04 stsp goto done;
6192 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6193 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6194 3e3a69f1 2019-07-25 stsp goto done;
6195 f032f1f7 2019-08-04 stsp }
6196 3e3a69f1 2019-07-25 stsp
6197 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6198 818c7501 2019-07-11 stsp if (err)
6199 f032f1f7 2019-08-04 stsp goto done;
6200 818c7501 2019-07-11 stsp
6201 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6202 818c7501 2019-07-11 stsp if (err)
6203 818c7501 2019-07-11 stsp goto done;
6204 818c7501 2019-07-11 stsp
6205 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6206 818c7501 2019-07-11 stsp if (err)
6207 818c7501 2019-07-11 stsp goto done;
6208 818c7501 2019-07-11 stsp
6209 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6210 818c7501 2019-07-11 stsp if (err)
6211 818c7501 2019-07-11 stsp goto done;
6212 818c7501 2019-07-11 stsp
6213 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6214 818c7501 2019-07-11 stsp if (err)
6215 818c7501 2019-07-11 stsp goto done;
6216 818c7501 2019-07-11 stsp
6217 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6218 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6219 818c7501 2019-07-11 stsp if (err)
6220 818c7501 2019-07-11 stsp goto done;
6221 818c7501 2019-07-11 stsp
6222 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6223 818c7501 2019-07-11 stsp if (err)
6224 818c7501 2019-07-11 stsp goto done;
6225 818c7501 2019-07-11 stsp
6226 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6227 818c7501 2019-07-11 stsp if (err)
6228 818c7501 2019-07-11 stsp goto done;
6229 818c7501 2019-07-11 stsp
6230 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6231 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6232 818c7501 2019-07-11 stsp if (err)
6233 818c7501 2019-07-11 stsp goto done;
6234 818c7501 2019-07-11 stsp
6235 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6236 818c7501 2019-07-11 stsp if (err)
6237 818c7501 2019-07-11 stsp goto done;
6238 818c7501 2019-07-11 stsp done:
6239 818c7501 2019-07-11 stsp free(commit_ref_name);
6240 818c7501 2019-07-11 stsp free(branch_ref_name);
6241 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6242 818c7501 2019-07-11 stsp if (commit_ref)
6243 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6244 818c7501 2019-07-11 stsp if (branch_ref)
6245 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6246 818c7501 2019-07-11 stsp if (err) {
6247 818c7501 2019-07-11 stsp free(*commit_id);
6248 818c7501 2019-07-11 stsp *commit_id = NULL;
6249 818c7501 2019-07-11 stsp if (*tmp_branch) {
6250 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6251 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6252 818c7501 2019-07-11 stsp }
6253 818c7501 2019-07-11 stsp if (*new_base_branch) {
6254 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6255 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6256 818c7501 2019-07-11 stsp }
6257 818c7501 2019-07-11 stsp if (*branch) {
6258 818c7501 2019-07-11 stsp got_ref_close(*branch);
6259 818c7501 2019-07-11 stsp *branch = NULL;
6260 818c7501 2019-07-11 stsp }
6261 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6262 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6263 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6264 3e3a69f1 2019-07-25 stsp }
6265 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6266 818c7501 2019-07-11 stsp }
6267 818c7501 2019-07-11 stsp return err;
6268 c4296144 2019-05-09 stsp }
6269 818c7501 2019-07-11 stsp
6270 818c7501 2019-07-11 stsp const struct got_error *
6271 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6272 818c7501 2019-07-11 stsp {
6273 818c7501 2019-07-11 stsp const struct got_error *err;
6274 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6275 818c7501 2019-07-11 stsp
6276 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6277 818c7501 2019-07-11 stsp if (err)
6278 818c7501 2019-07-11 stsp return err;
6279 818c7501 2019-07-11 stsp
6280 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6281 818c7501 2019-07-11 stsp free(tmp_branch_name);
6282 818c7501 2019-07-11 stsp return NULL;
6283 818c7501 2019-07-11 stsp }
6284 818c7501 2019-07-11 stsp
6285 818c7501 2019-07-11 stsp static const struct got_error *
6286 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6287 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6288 818c7501 2019-07-11 stsp {
6289 0ebf8283 2019-07-24 stsp *logmsg = arg;
6290 818c7501 2019-07-11 stsp return NULL;
6291 818c7501 2019-07-11 stsp }
6292 818c7501 2019-07-11 stsp
6293 818c7501 2019-07-11 stsp static const struct got_error *
6294 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6295 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6296 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6297 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6298 818c7501 2019-07-11 stsp {
6299 818c7501 2019-07-11 stsp return NULL;
6300 01757395 2019-07-12 stsp }
6301 01757395 2019-07-12 stsp
6302 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6303 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6304 01757395 2019-07-12 stsp void *progress_arg;
6305 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6306 01757395 2019-07-12 stsp };
6307 01757395 2019-07-12 stsp
6308 01757395 2019-07-12 stsp static const struct got_error *
6309 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6310 01757395 2019-07-12 stsp {
6311 01757395 2019-07-12 stsp const struct got_error *err;
6312 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6313 01757395 2019-07-12 stsp char *p;
6314 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6315 01757395 2019-07-12 stsp
6316 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6317 01757395 2019-07-12 stsp if (err)
6318 01757395 2019-07-12 stsp return err;
6319 01757395 2019-07-12 stsp
6320 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6321 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6322 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6323 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6324 01757395 2019-07-12 stsp return NULL;
6325 01757395 2019-07-12 stsp
6326 01757395 2019-07-12 stsp p = strdup(path);
6327 01757395 2019-07-12 stsp if (p == NULL)
6328 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6329 01757395 2019-07-12 stsp
6330 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6331 01757395 2019-07-12 stsp if (err || new == NULL)
6332 01757395 2019-07-12 stsp free(p);
6333 01757395 2019-07-12 stsp return err;
6334 01757395 2019-07-12 stsp }
6335 01757395 2019-07-12 stsp
6336 01757395 2019-07-12 stsp void
6337 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6338 01757395 2019-07-12 stsp {
6339 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6340 01757395 2019-07-12 stsp
6341 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6342 01757395 2019-07-12 stsp free((char *)pe->path);
6343 01757395 2019-07-12 stsp
6344 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6345 818c7501 2019-07-11 stsp }
6346 818c7501 2019-07-11 stsp
6347 0ebf8283 2019-07-24 stsp static const struct got_error *
6348 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6349 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6350 818c7501 2019-07-11 stsp {
6351 818c7501 2019-07-11 stsp const struct got_error *err;
6352 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6353 818c7501 2019-07-11 stsp
6354 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6355 818c7501 2019-07-11 stsp if (err) {
6356 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6357 818c7501 2019-07-11 stsp goto done;
6358 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6359 818c7501 2019-07-11 stsp if (err)
6360 818c7501 2019-07-11 stsp goto done;
6361 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6362 818c7501 2019-07-11 stsp if (err)
6363 818c7501 2019-07-11 stsp goto done;
6364 de05890f 2020-03-05 stsp } else if (is_rebase) {
6365 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6366 818c7501 2019-07-11 stsp int cmp;
6367 818c7501 2019-07-11 stsp
6368 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6369 818c7501 2019-07-11 stsp if (err)
6370 818c7501 2019-07-11 stsp goto done;
6371 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6372 818c7501 2019-07-11 stsp free(stored_id);
6373 818c7501 2019-07-11 stsp if (cmp != 0) {
6374 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6375 818c7501 2019-07-11 stsp goto done;
6376 818c7501 2019-07-11 stsp }
6377 818c7501 2019-07-11 stsp }
6378 0ebf8283 2019-07-24 stsp done:
6379 0ebf8283 2019-07-24 stsp if (commit_ref)
6380 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6381 0ebf8283 2019-07-24 stsp return err;
6382 0ebf8283 2019-07-24 stsp }
6383 0ebf8283 2019-07-24 stsp
6384 0ebf8283 2019-07-24 stsp static const struct got_error *
6385 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6386 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6387 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6388 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6389 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6390 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6391 0ebf8283 2019-07-24 stsp {
6392 0ebf8283 2019-07-24 stsp const struct got_error *err;
6393 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6394 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6395 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6396 818c7501 2019-07-11 stsp
6397 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6398 0ebf8283 2019-07-24 stsp
6399 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6400 0ebf8283 2019-07-24 stsp if (err)
6401 0ebf8283 2019-07-24 stsp return err;
6402 0ebf8283 2019-07-24 stsp
6403 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6404 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6405 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6406 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6407 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6408 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6409 818c7501 2019-07-11 stsp if (commit_ref)
6410 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6411 818c7501 2019-07-11 stsp return err;
6412 818c7501 2019-07-11 stsp }
6413 818c7501 2019-07-11 stsp
6414 818c7501 2019-07-11 stsp const struct got_error *
6415 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6416 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6417 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6418 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6419 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6420 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6421 818c7501 2019-07-11 stsp {
6422 0ebf8283 2019-07-24 stsp const struct got_error *err;
6423 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6424 0ebf8283 2019-07-24 stsp
6425 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6426 0ebf8283 2019-07-24 stsp if (err)
6427 0ebf8283 2019-07-24 stsp return err;
6428 0ebf8283 2019-07-24 stsp
6429 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6430 0ebf8283 2019-07-24 stsp if (err)
6431 0ebf8283 2019-07-24 stsp goto done;
6432 0ebf8283 2019-07-24 stsp
6433 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6434 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6435 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6436 0ebf8283 2019-07-24 stsp done:
6437 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6438 0ebf8283 2019-07-24 stsp return err;
6439 0ebf8283 2019-07-24 stsp }
6440 0ebf8283 2019-07-24 stsp
6441 0ebf8283 2019-07-24 stsp const struct got_error *
6442 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6443 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6444 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6445 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6446 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6447 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6448 0ebf8283 2019-07-24 stsp {
6449 0ebf8283 2019-07-24 stsp const struct got_error *err;
6450 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6451 0ebf8283 2019-07-24 stsp
6452 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6453 0ebf8283 2019-07-24 stsp if (err)
6454 0ebf8283 2019-07-24 stsp return err;
6455 0ebf8283 2019-07-24 stsp
6456 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6457 0ebf8283 2019-07-24 stsp if (err)
6458 0ebf8283 2019-07-24 stsp goto done;
6459 0ebf8283 2019-07-24 stsp
6460 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6461 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6462 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6463 0ebf8283 2019-07-24 stsp done:
6464 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6465 0ebf8283 2019-07-24 stsp return err;
6466 0ebf8283 2019-07-24 stsp }
6467 0ebf8283 2019-07-24 stsp
6468 0ebf8283 2019-07-24 stsp static const struct got_error *
6469 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6470 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6471 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6472 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6473 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6474 598eac43 2022-07-22 stsp struct got_repository *repo)
6475 0ebf8283 2019-07-24 stsp {
6476 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6477 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6478 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6479 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6480 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6481 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6482 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6483 818c7501 2019-07-11 stsp
6484 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6485 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6486 a0e95631 2019-07-12 stsp
6487 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6488 818c7501 2019-07-11 stsp
6489 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6490 a0e95631 2019-07-12 stsp if (err)
6491 3e3a69f1 2019-07-25 stsp return err;
6492 a0e95631 2019-07-12 stsp
6493 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6494 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6495 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6496 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6497 01757395 2019-07-12 stsp /*
6498 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6499 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6500 6c13b005 2021-09-02 stsp *
6501 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6502 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6503 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6504 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6505 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6506 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6507 01757395 2019-07-12 stsp */
6508 01757395 2019-07-12 stsp if (merged_paths) {
6509 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6510 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6511 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6512 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6513 f2a9dc41 2019-12-13 tracey 0);
6514 01757395 2019-07-12 stsp if (err)
6515 01757395 2019-07-12 stsp goto done;
6516 01757395 2019-07-12 stsp }
6517 01757395 2019-07-12 stsp } else {
6518 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6519 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6520 01757395 2019-07-12 stsp if (err)
6521 01757395 2019-07-12 stsp goto done;
6522 01757395 2019-07-12 stsp }
6523 a0e95631 2019-07-12 stsp
6524 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6525 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6526 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6527 a0e95631 2019-07-12 stsp if (err)
6528 a0e95631 2019-07-12 stsp goto done;
6529 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6530 a0e95631 2019-07-12 stsp goto done;
6531 ff0d2220 2019-07-11 stsp }
6532 818c7501 2019-07-11 stsp
6533 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6534 edd02c5e 2019-07-11 stsp if (err)
6535 edd02c5e 2019-07-11 stsp goto done;
6536 edd02c5e 2019-07-11 stsp
6537 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6538 818c7501 2019-07-11 stsp if (err)
6539 818c7501 2019-07-11 stsp goto done;
6540 818c7501 2019-07-11 stsp
6541 5943eee2 2019-08-13 stsp if (new_logmsg) {
6542 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6543 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6544 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6545 5943eee2 2019-08-13 stsp goto done;
6546 5943eee2 2019-08-13 stsp }
6547 5943eee2 2019-08-13 stsp } else {
6548 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6549 5943eee2 2019-08-13 stsp if (err)
6550 5943eee2 2019-08-13 stsp goto done;
6551 5943eee2 2019-08-13 stsp }
6552 0ebf8283 2019-07-24 stsp
6553 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6554 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6555 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6556 50e7a649 2022-07-22 stsp committer ? committer :
6557 50e7a649 2022-07-22 stsp got_object_commit_get_committer(orig_commit),
6558 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6559 a0e95631 2019-07-12 stsp if (err)
6560 a0e95631 2019-07-12 stsp goto done;
6561 a0e95631 2019-07-12 stsp
6562 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6563 a0e95631 2019-07-12 stsp if (err)
6564 a0e95631 2019-07-12 stsp goto done;
6565 a0e95631 2019-07-12 stsp
6566 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6567 a0e95631 2019-07-12 stsp if (err)
6568 a0e95631 2019-07-12 stsp goto done;
6569 a0e95631 2019-07-12 stsp
6570 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6571 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6572 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6573 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6574 a0e95631 2019-07-12 stsp err = sync_err;
6575 818c7501 2019-07-11 stsp done:
6576 a0e95631 2019-07-12 stsp free(fileindex_path);
6577 a0e95631 2019-07-12 stsp free(head_commit_id);
6578 a0e95631 2019-07-12 stsp if (head_ref)
6579 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6580 818c7501 2019-07-11 stsp if (err) {
6581 818c7501 2019-07-11 stsp free(*new_commit_id);
6582 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6583 818c7501 2019-07-11 stsp }
6584 818c7501 2019-07-11 stsp return err;
6585 818c7501 2019-07-11 stsp }
6586 818c7501 2019-07-11 stsp
6587 818c7501 2019-07-11 stsp const struct got_error *
6588 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6589 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6590 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6591 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6592 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6593 0ebf8283 2019-07-24 stsp {
6594 0ebf8283 2019-07-24 stsp const struct got_error *err;
6595 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6596 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6597 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6598 0ebf8283 2019-07-24 stsp
6599 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6600 0ebf8283 2019-07-24 stsp if (err)
6601 0ebf8283 2019-07-24 stsp return err;
6602 0ebf8283 2019-07-24 stsp
6603 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6604 0ebf8283 2019-07-24 stsp if (err)
6605 0ebf8283 2019-07-24 stsp goto done;
6606 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6607 0ebf8283 2019-07-24 stsp if (err)
6608 0ebf8283 2019-07-24 stsp goto done;
6609 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6610 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6611 0ebf8283 2019-07-24 stsp goto done;
6612 0ebf8283 2019-07-24 stsp }
6613 0ebf8283 2019-07-24 stsp
6614 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6615 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6616 598eac43 2022-07-22 stsp NULL, repo);
6617 0ebf8283 2019-07-24 stsp done:
6618 0ebf8283 2019-07-24 stsp if (commit_ref)
6619 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6620 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6621 0ebf8283 2019-07-24 stsp free(commit_id);
6622 0ebf8283 2019-07-24 stsp return err;
6623 0ebf8283 2019-07-24 stsp }
6624 0ebf8283 2019-07-24 stsp
6625 0ebf8283 2019-07-24 stsp const struct got_error *
6626 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6627 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6628 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6629 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6630 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6631 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6632 0ebf8283 2019-07-24 stsp {
6633 0ebf8283 2019-07-24 stsp const struct got_error *err;
6634 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6635 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6636 0ebf8283 2019-07-24 stsp
6637 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6638 0ebf8283 2019-07-24 stsp if (err)
6639 0ebf8283 2019-07-24 stsp return err;
6640 0ebf8283 2019-07-24 stsp
6641 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6642 0ebf8283 2019-07-24 stsp if (err)
6643 0ebf8283 2019-07-24 stsp goto done;
6644 0ebf8283 2019-07-24 stsp
6645 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6646 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6647 598eac43 2022-07-22 stsp new_logmsg, repo);
6648 0ebf8283 2019-07-24 stsp done:
6649 0ebf8283 2019-07-24 stsp if (commit_ref)
6650 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6651 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6652 0ebf8283 2019-07-24 stsp return err;
6653 0ebf8283 2019-07-24 stsp }
6654 0ebf8283 2019-07-24 stsp
6655 0ebf8283 2019-07-24 stsp const struct got_error *
6656 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6657 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6658 818c7501 2019-07-11 stsp {
6659 3e3a69f1 2019-07-25 stsp if (fileindex)
6660 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6661 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6662 69844fba 2019-07-11 stsp }
6663 69844fba 2019-07-11 stsp
6664 69844fba 2019-07-11 stsp static const struct got_error *
6665 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6666 69844fba 2019-07-11 stsp {
6667 69844fba 2019-07-11 stsp const struct got_error *err;
6668 69844fba 2019-07-11 stsp struct got_reference *ref;
6669 69844fba 2019-07-11 stsp
6670 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6671 69844fba 2019-07-11 stsp if (err) {
6672 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6673 69844fba 2019-07-11 stsp return NULL;
6674 69844fba 2019-07-11 stsp return err;
6675 69844fba 2019-07-11 stsp }
6676 69844fba 2019-07-11 stsp
6677 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6678 69844fba 2019-07-11 stsp got_ref_close(ref);
6679 69844fba 2019-07-11 stsp return err;
6680 818c7501 2019-07-11 stsp }
6681 818c7501 2019-07-11 stsp
6682 69844fba 2019-07-11 stsp static const struct got_error *
6683 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6684 69844fba 2019-07-11 stsp {
6685 69844fba 2019-07-11 stsp const struct got_error *err;
6686 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6687 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6688 69844fba 2019-07-11 stsp
6689 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6690 69844fba 2019-07-11 stsp if (err)
6691 69844fba 2019-07-11 stsp goto done;
6692 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6693 69844fba 2019-07-11 stsp if (err)
6694 69844fba 2019-07-11 stsp goto done;
6695 69844fba 2019-07-11 stsp
6696 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6697 69844fba 2019-07-11 stsp if (err)
6698 69844fba 2019-07-11 stsp goto done;
6699 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6700 69844fba 2019-07-11 stsp if (err)
6701 69844fba 2019-07-11 stsp goto done;
6702 69844fba 2019-07-11 stsp
6703 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6704 69844fba 2019-07-11 stsp if (err)
6705 69844fba 2019-07-11 stsp goto done;
6706 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6707 69844fba 2019-07-11 stsp if (err)
6708 69844fba 2019-07-11 stsp goto done;
6709 69844fba 2019-07-11 stsp
6710 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6711 69844fba 2019-07-11 stsp if (err)
6712 69844fba 2019-07-11 stsp goto done;
6713 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6714 69844fba 2019-07-11 stsp if (err)
6715 69844fba 2019-07-11 stsp goto done;
6716 69844fba 2019-07-11 stsp
6717 69844fba 2019-07-11 stsp done:
6718 69844fba 2019-07-11 stsp free(tmp_branch_name);
6719 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6720 69844fba 2019-07-11 stsp free(branch_ref_name);
6721 69844fba 2019-07-11 stsp free(commit_ref_name);
6722 e600f124 2021-03-21 stsp return err;
6723 e600f124 2021-03-21 stsp }
6724 e600f124 2021-03-21 stsp
6725 336075a4 2022-06-25 op static const struct got_error *
6726 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6727 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6728 e600f124 2021-03-21 stsp {
6729 e600f124 2021-03-21 stsp const struct got_error *err;
6730 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6731 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6732 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6733 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6734 e600f124 2021-03-21 stsp char *refname = NULL;
6735 e600f124 2021-03-21 stsp
6736 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6737 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6738 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6739 e600f124 2021-03-21 stsp branch_name += 11;
6740 e600f124 2021-03-21 stsp
6741 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6742 e600f124 2021-03-21 stsp if (err)
6743 e600f124 2021-03-21 stsp return err;
6744 e600f124 2021-03-21 stsp
6745 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6746 e600f124 2021-03-21 stsp new_id_str) == -1) {
6747 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6748 e600f124 2021-03-21 stsp goto done;
6749 e600f124 2021-03-21 stsp }
6750 e600f124 2021-03-21 stsp
6751 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6752 e600f124 2021-03-21 stsp if (err)
6753 e600f124 2021-03-21 stsp goto done;
6754 e600f124 2021-03-21 stsp
6755 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6756 e600f124 2021-03-21 stsp if (err)
6757 e600f124 2021-03-21 stsp goto done;
6758 e600f124 2021-03-21 stsp
6759 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6760 e600f124 2021-03-21 stsp done:
6761 e600f124 2021-03-21 stsp free(new_id_str);
6762 e600f124 2021-03-21 stsp free(refname);
6763 e600f124 2021-03-21 stsp free(old_commit_id);
6764 e600f124 2021-03-21 stsp if (ref)
6765 e600f124 2021-03-21 stsp got_ref_close(ref);
6766 69844fba 2019-07-11 stsp return err;
6767 69844fba 2019-07-11 stsp }
6768 69844fba 2019-07-11 stsp
6769 818c7501 2019-07-11 stsp const struct got_error *
6770 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6771 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6772 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6773 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6774 818c7501 2019-07-11 stsp {
6775 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6776 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6777 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6778 818c7501 2019-07-11 stsp
6779 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6780 818c7501 2019-07-11 stsp if (err)
6781 818c7501 2019-07-11 stsp return err;
6782 e600f124 2021-03-21 stsp
6783 e600f124 2021-03-21 stsp if (create_backup) {
6784 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6785 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6786 e600f124 2021-03-21 stsp if (err)
6787 e600f124 2021-03-21 stsp goto done;
6788 e600f124 2021-03-21 stsp }
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6791 818c7501 2019-07-11 stsp if (err)
6792 818c7501 2019-07-11 stsp goto done;
6793 818c7501 2019-07-11 stsp
6794 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6795 818c7501 2019-07-11 stsp if (err)
6796 818c7501 2019-07-11 stsp goto done;
6797 818c7501 2019-07-11 stsp
6798 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6799 818c7501 2019-07-11 stsp if (err)
6800 818c7501 2019-07-11 stsp goto done;
6801 818c7501 2019-07-11 stsp
6802 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6803 a615e0e7 2020-12-16 stsp if (err)
6804 a615e0e7 2020-12-16 stsp goto done;
6805 a615e0e7 2020-12-16 stsp
6806 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6807 a615e0e7 2020-12-16 stsp if (err)
6808 a615e0e7 2020-12-16 stsp goto done;
6809 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6810 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6811 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6812 a615e0e7 2020-12-16 stsp err = sync_err;
6813 818c7501 2019-07-11 stsp done:
6814 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6815 a615e0e7 2020-12-16 stsp free(fileindex_path);
6816 818c7501 2019-07-11 stsp free(new_head_commit_id);
6817 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6818 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6819 818c7501 2019-07-11 stsp err = unlockerr;
6820 818c7501 2019-07-11 stsp return err;
6821 818c7501 2019-07-11 stsp }
6822 818c7501 2019-07-11 stsp
6823 818c7501 2019-07-11 stsp const struct got_error *
6824 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6825 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6826 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6827 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6828 818c7501 2019-07-11 stsp {
6829 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6830 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6831 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6832 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6833 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6834 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6835 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6836 818c7501 2019-07-11 stsp
6837 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6838 818c7501 2019-07-11 stsp if (err)
6839 818c7501 2019-07-11 stsp return err;
6840 818c7501 2019-07-11 stsp
6841 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
6842 a44927cc 2022-04-07 stsp worktree->base_commit_id);
6843 a44927cc 2022-04-07 stsp if (err)
6844 a44927cc 2022-04-07 stsp goto done;
6845 a44927cc 2022-04-07 stsp
6846 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6847 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6848 818c7501 2019-07-11 stsp if (err)
6849 818c7501 2019-07-11 stsp goto done;
6850 818c7501 2019-07-11 stsp
6851 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6852 818c7501 2019-07-11 stsp if (err)
6853 818c7501 2019-07-11 stsp goto done;
6854 818c7501 2019-07-11 stsp
6855 818c7501 2019-07-11 stsp /*
6856 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6857 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6858 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6859 818c7501 2019-07-11 stsp */
6860 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6861 818c7501 2019-07-11 stsp if (err)
6862 818c7501 2019-07-11 stsp goto done;
6863 818c7501 2019-07-11 stsp
6864 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6865 818c7501 2019-07-11 stsp if (err)
6866 818c7501 2019-07-11 stsp goto done;
6867 818c7501 2019-07-11 stsp
6868 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
6869 a44927cc 2022-04-07 stsp worktree->path_prefix);
6870 ca355955 2019-07-12 stsp if (err)
6871 ca355955 2019-07-12 stsp goto done;
6872 ca355955 2019-07-12 stsp
6873 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6874 ca355955 2019-07-12 stsp if (err)
6875 ca355955 2019-07-12 stsp goto done;
6876 ca355955 2019-07-12 stsp
6877 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6878 818c7501 2019-07-11 stsp if (err)
6879 818c7501 2019-07-11 stsp goto done;
6880 818c7501 2019-07-11 stsp
6881 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6882 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6883 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6884 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6885 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6886 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6887 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6888 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6889 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6890 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6891 55bd499d 2019-07-12 stsp if (err)
6892 1f1abb7e 2019-08-08 stsp goto sync;
6893 55bd499d 2019-07-12 stsp
6894 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6895 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6896 ca355955 2019-07-12 stsp sync:
6897 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6898 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6899 ca355955 2019-07-12 stsp err = sync_err;
6900 818c7501 2019-07-11 stsp done:
6901 818c7501 2019-07-11 stsp got_ref_close(resolved);
6902 a3a2faf2 2019-07-12 stsp free(tree_id);
6903 818c7501 2019-07-11 stsp free(commit_id);
6904 a44927cc 2022-04-07 stsp if (commit)
6905 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6906 0ebf8283 2019-07-24 stsp if (fileindex)
6907 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6908 0ebf8283 2019-07-24 stsp free(fileindex_path);
6909 0ebf8283 2019-07-24 stsp
6910 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6911 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6912 0ebf8283 2019-07-24 stsp err = unlockerr;
6913 0ebf8283 2019-07-24 stsp return err;
6914 0ebf8283 2019-07-24 stsp }
6915 0ebf8283 2019-07-24 stsp
6916 0ebf8283 2019-07-24 stsp const struct got_error *
6917 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6918 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6919 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6920 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6921 0ebf8283 2019-07-24 stsp {
6922 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6923 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6924 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6925 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6926 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6927 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6928 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6929 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6930 0ebf8283 2019-07-24 stsp
6931 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6932 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6933 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6934 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6935 0ebf8283 2019-07-24 stsp
6936 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6937 0ebf8283 2019-07-24 stsp if (err)
6938 0ebf8283 2019-07-24 stsp return err;
6939 0ebf8283 2019-07-24 stsp
6940 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6941 0ebf8283 2019-07-24 stsp if (err)
6942 0ebf8283 2019-07-24 stsp goto done;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6945 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6946 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6947 0ebf8283 2019-07-24 stsp &ok_arg);
6948 0ebf8283 2019-07-24 stsp if (err)
6949 0ebf8283 2019-07-24 stsp goto done;
6950 0ebf8283 2019-07-24 stsp
6951 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6952 0ebf8283 2019-07-24 stsp if (err)
6953 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp
6955 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6956 0ebf8283 2019-07-24 stsp if (err)
6957 0ebf8283 2019-07-24 stsp goto done;
6958 0ebf8283 2019-07-24 stsp
6959 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6960 0ebf8283 2019-07-24 stsp worktree);
6961 0ebf8283 2019-07-24 stsp if (err)
6962 0ebf8283 2019-07-24 stsp goto done;
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6965 0ebf8283 2019-07-24 stsp 0);
6966 0ebf8283 2019-07-24 stsp if (err)
6967 0ebf8283 2019-07-24 stsp goto done;
6968 0ebf8283 2019-07-24 stsp
6969 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6970 0ebf8283 2019-07-24 stsp if (err)
6971 0ebf8283 2019-07-24 stsp goto done;
6972 0ebf8283 2019-07-24 stsp
6973 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6974 0ebf8283 2019-07-24 stsp if (err)
6975 0ebf8283 2019-07-24 stsp goto done;
6976 0ebf8283 2019-07-24 stsp
6977 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6978 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6979 0ebf8283 2019-07-24 stsp if (err)
6980 0ebf8283 2019-07-24 stsp goto done;
6981 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6982 0ebf8283 2019-07-24 stsp if (err)
6983 0ebf8283 2019-07-24 stsp goto done;
6984 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6985 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6986 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6987 0ebf8283 2019-07-24 stsp goto done;
6988 0ebf8283 2019-07-24 stsp }
6989 0ebf8283 2019-07-24 stsp
6990 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6991 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6992 0ebf8283 2019-07-24 stsp if (err)
6993 0ebf8283 2019-07-24 stsp goto done;
6994 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6995 0ebf8283 2019-07-24 stsp if (err)
6996 0ebf8283 2019-07-24 stsp goto done;
6997 0ebf8283 2019-07-24 stsp
6998 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6999 0ebf8283 2019-07-24 stsp if (err)
7000 0ebf8283 2019-07-24 stsp goto done;
7001 0ebf8283 2019-07-24 stsp done:
7002 0ebf8283 2019-07-24 stsp free(fileindex_path);
7003 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7004 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7005 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7006 0ebf8283 2019-07-24 stsp if (wt_branch)
7007 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7008 0ebf8283 2019-07-24 stsp if (err) {
7009 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7010 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7011 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7012 0ebf8283 2019-07-24 stsp }
7013 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7014 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7015 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7016 0ebf8283 2019-07-24 stsp }
7017 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7018 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7019 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7020 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7021 3e3a69f1 2019-07-25 stsp }
7022 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7023 0ebf8283 2019-07-24 stsp }
7024 0ebf8283 2019-07-24 stsp return err;
7025 0ebf8283 2019-07-24 stsp }
7026 0ebf8283 2019-07-24 stsp
7027 0ebf8283 2019-07-24 stsp const struct got_error *
7028 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7029 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7030 0ebf8283 2019-07-24 stsp {
7031 3e3a69f1 2019-07-25 stsp if (fileindex)
7032 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7033 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7034 0ebf8283 2019-07-24 stsp }
7035 0ebf8283 2019-07-24 stsp
7036 0ebf8283 2019-07-24 stsp const struct got_error *
7037 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7038 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7039 0ebf8283 2019-07-24 stsp {
7040 0ebf8283 2019-07-24 stsp const struct got_error *err;
7041 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7042 0ebf8283 2019-07-24 stsp
7043 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7044 0ebf8283 2019-07-24 stsp if (err)
7045 0ebf8283 2019-07-24 stsp return err;
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7048 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7049 0ebf8283 2019-07-24 stsp return NULL;
7050 0ebf8283 2019-07-24 stsp }
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp const struct got_error *
7053 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7054 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7055 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7056 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7057 0ebf8283 2019-07-24 stsp {
7058 0ebf8283 2019-07-24 stsp const struct got_error *err;
7059 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7060 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7061 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7062 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7063 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7064 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7065 0ebf8283 2019-07-24 stsp
7066 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7067 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7068 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7069 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7070 0ebf8283 2019-07-24 stsp
7071 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7072 3e3a69f1 2019-07-25 stsp if (err)
7073 3e3a69f1 2019-07-25 stsp return err;
7074 3e3a69f1 2019-07-25 stsp
7075 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7076 3e3a69f1 2019-07-25 stsp if (err)
7077 f032f1f7 2019-08-04 stsp goto done;
7078 f032f1f7 2019-08-04 stsp
7079 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7080 f032f1f7 2019-08-04 stsp &have_staged_files);
7081 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7082 f032f1f7 2019-08-04 stsp goto done;
7083 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7084 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7085 3e3a69f1 2019-07-25 stsp goto done;
7086 f032f1f7 2019-08-04 stsp }
7087 3e3a69f1 2019-07-25 stsp
7088 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7089 0ebf8283 2019-07-24 stsp if (err)
7090 f032f1f7 2019-08-04 stsp goto done;
7091 0ebf8283 2019-07-24 stsp
7092 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7093 0ebf8283 2019-07-24 stsp if (err)
7094 0ebf8283 2019-07-24 stsp goto done;
7095 0ebf8283 2019-07-24 stsp
7096 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7097 0ebf8283 2019-07-24 stsp if (err)
7098 0ebf8283 2019-07-24 stsp goto done;
7099 0ebf8283 2019-07-24 stsp
7100 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7101 0ebf8283 2019-07-24 stsp worktree);
7102 0ebf8283 2019-07-24 stsp if (err)
7103 0ebf8283 2019-07-24 stsp goto done;
7104 0ebf8283 2019-07-24 stsp
7105 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7106 0ebf8283 2019-07-24 stsp if (err)
7107 0ebf8283 2019-07-24 stsp goto done;
7108 0ebf8283 2019-07-24 stsp
7109 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7110 0ebf8283 2019-07-24 stsp if (err)
7111 0ebf8283 2019-07-24 stsp goto done;
7112 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7113 0ebf8283 2019-07-24 stsp if (err)
7114 0ebf8283 2019-07-24 stsp goto done;
7115 0ebf8283 2019-07-24 stsp
7116 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7117 0ebf8283 2019-07-24 stsp if (err)
7118 0ebf8283 2019-07-24 stsp goto done;
7119 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7120 0ebf8283 2019-07-24 stsp if (err)
7121 0ebf8283 2019-07-24 stsp goto done;
7122 0ebf8283 2019-07-24 stsp
7123 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7124 0ebf8283 2019-07-24 stsp if (err)
7125 0ebf8283 2019-07-24 stsp goto done;
7126 0ebf8283 2019-07-24 stsp done:
7127 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7128 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7129 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7130 0ebf8283 2019-07-24 stsp if (commit_ref)
7131 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7132 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7133 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7134 0ebf8283 2019-07-24 stsp if (err) {
7135 0ebf8283 2019-07-24 stsp free(*commit_id);
7136 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7137 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7138 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7139 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7140 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7141 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7142 0ebf8283 2019-07-24 stsp }
7143 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7144 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7145 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7146 3e3a69f1 2019-07-25 stsp }
7147 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7148 0ebf8283 2019-07-24 stsp }
7149 0ebf8283 2019-07-24 stsp return err;
7150 0ebf8283 2019-07-24 stsp }
7151 0ebf8283 2019-07-24 stsp
7152 0ebf8283 2019-07-24 stsp static const struct got_error *
7153 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7154 0ebf8283 2019-07-24 stsp {
7155 0ebf8283 2019-07-24 stsp const struct got_error *err;
7156 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7157 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7158 0ebf8283 2019-07-24 stsp
7159 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7160 0ebf8283 2019-07-24 stsp if (err)
7161 0ebf8283 2019-07-24 stsp goto done;
7162 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7163 0ebf8283 2019-07-24 stsp if (err)
7164 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7167 0ebf8283 2019-07-24 stsp worktree);
7168 0ebf8283 2019-07-24 stsp if (err)
7169 0ebf8283 2019-07-24 stsp goto done;
7170 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7171 0ebf8283 2019-07-24 stsp if (err)
7172 0ebf8283 2019-07-24 stsp goto done;
7173 0ebf8283 2019-07-24 stsp
7174 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7175 0ebf8283 2019-07-24 stsp if (err)
7176 0ebf8283 2019-07-24 stsp goto done;
7177 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7178 0ebf8283 2019-07-24 stsp if (err)
7179 0ebf8283 2019-07-24 stsp goto done;
7180 0ebf8283 2019-07-24 stsp
7181 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7182 0ebf8283 2019-07-24 stsp if (err)
7183 0ebf8283 2019-07-24 stsp goto done;
7184 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7185 0ebf8283 2019-07-24 stsp if (err)
7186 0ebf8283 2019-07-24 stsp goto done;
7187 0ebf8283 2019-07-24 stsp done:
7188 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7189 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7190 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7191 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7192 0ebf8283 2019-07-24 stsp return err;
7193 0ebf8283 2019-07-24 stsp }
7194 0ebf8283 2019-07-24 stsp
7195 0ebf8283 2019-07-24 stsp const struct got_error *
7196 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7197 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7198 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7199 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7200 0ebf8283 2019-07-24 stsp {
7201 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7202 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7203 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7204 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7205 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7206 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7207 0ebf8283 2019-07-24 stsp
7208 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp return err;
7211 0ebf8283 2019-07-24 stsp
7212 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7213 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7214 a44927cc 2022-04-07 stsp if (err)
7215 a44927cc 2022-04-07 stsp goto done;
7216 a44927cc 2022-04-07 stsp
7217 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7218 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
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_worktree_set_head_ref(worktree, resolved);
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_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7227 0ebf8283 2019-07-24 stsp if (err)
7228 0ebf8283 2019-07-24 stsp goto done;
7229 0ebf8283 2019-07-24 stsp
7230 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7231 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7232 0ebf8283 2019-07-24 stsp if (err)
7233 0ebf8283 2019-07-24 stsp goto done;
7234 0ebf8283 2019-07-24 stsp
7235 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7236 0ebf8283 2019-07-24 stsp if (err)
7237 0ebf8283 2019-07-24 stsp goto done;
7238 0ebf8283 2019-07-24 stsp
7239 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7240 0ebf8283 2019-07-24 stsp if (err)
7241 0ebf8283 2019-07-24 stsp goto done;
7242 0ebf8283 2019-07-24 stsp
7243 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7244 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7245 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7246 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7247 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7248 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7249 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7250 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7251 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7252 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7253 0ebf8283 2019-07-24 stsp if (err)
7254 1f1abb7e 2019-08-08 stsp goto sync;
7255 0ebf8283 2019-07-24 stsp
7256 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7257 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7258 0ebf8283 2019-07-24 stsp sync:
7259 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7260 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7261 0ebf8283 2019-07-24 stsp err = sync_err;
7262 0ebf8283 2019-07-24 stsp done:
7263 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7264 0ebf8283 2019-07-24 stsp free(tree_id);
7265 818c7501 2019-07-11 stsp free(fileindex_path);
7266 818c7501 2019-07-11 stsp
7267 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7268 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7269 818c7501 2019-07-11 stsp err = unlockerr;
7270 818c7501 2019-07-11 stsp return err;
7271 818c7501 2019-07-11 stsp }
7272 0ebf8283 2019-07-24 stsp
7273 0ebf8283 2019-07-24 stsp const struct got_error *
7274 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7275 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7276 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7277 0ebf8283 2019-07-24 stsp {
7278 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7279 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7280 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7281 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7282 0ebf8283 2019-07-24 stsp
7283 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7284 0ebf8283 2019-07-24 stsp if (err)
7285 0ebf8283 2019-07-24 stsp return err;
7286 0ebf8283 2019-07-24 stsp
7287 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7288 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7289 e600f124 2021-03-21 stsp if (err)
7290 e600f124 2021-03-21 stsp goto done;
7291 e600f124 2021-03-21 stsp
7292 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7293 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7294 0ebf8283 2019-07-24 stsp if (err)
7295 0ebf8283 2019-07-24 stsp goto done;
7296 0ebf8283 2019-07-24 stsp
7297 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7298 0ebf8283 2019-07-24 stsp if (err)
7299 0ebf8283 2019-07-24 stsp goto done;
7300 0ebf8283 2019-07-24 stsp
7301 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7302 0ebf8283 2019-07-24 stsp if (err)
7303 0ebf8283 2019-07-24 stsp goto done;
7304 0ebf8283 2019-07-24 stsp
7305 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7306 0ebf8283 2019-07-24 stsp if (err)
7307 0ebf8283 2019-07-24 stsp goto done;
7308 0ebf8283 2019-07-24 stsp
7309 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7310 a615e0e7 2020-12-16 stsp if (err)
7311 a615e0e7 2020-12-16 stsp goto done;
7312 a615e0e7 2020-12-16 stsp
7313 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7314 a615e0e7 2020-12-16 stsp if (err)
7315 a615e0e7 2020-12-16 stsp goto done;
7316 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7317 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7318 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7319 a615e0e7 2020-12-16 stsp err = sync_err;
7320 0ebf8283 2019-07-24 stsp done:
7321 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7322 a615e0e7 2020-12-16 stsp free(fileindex_path);
7323 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7324 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7325 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7326 0ebf8283 2019-07-24 stsp err = unlockerr;
7327 0ebf8283 2019-07-24 stsp return err;
7328 0ebf8283 2019-07-24 stsp }
7329 0ebf8283 2019-07-24 stsp
7330 0ebf8283 2019-07-24 stsp const struct got_error *
7331 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7332 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7333 0ebf8283 2019-07-24 stsp {
7334 0ebf8283 2019-07-24 stsp const struct got_error *err;
7335 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7336 0ebf8283 2019-07-24 stsp
7337 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7338 0ebf8283 2019-07-24 stsp if (err)
7339 0ebf8283 2019-07-24 stsp return err;
7340 0ebf8283 2019-07-24 stsp
7341 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
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 = delete_ref(commit_ref_name, repo);
7346 0ebf8283 2019-07-24 stsp done:
7347 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7348 2822a352 2019-10-15 stsp return err;
7349 2822a352 2019-10-15 stsp }
7350 2822a352 2019-10-15 stsp
7351 2822a352 2019-10-15 stsp const struct got_error *
7352 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7353 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7354 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7355 2822a352 2019-10-15 stsp struct got_repository *repo)
7356 2822a352 2019-10-15 stsp {
7357 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7358 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7359 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7360 2822a352 2019-10-15 stsp
7361 2822a352 2019-10-15 stsp *fileindex = NULL;
7362 2822a352 2019-10-15 stsp *branch_ref = NULL;
7363 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7364 2822a352 2019-10-15 stsp
7365 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7366 2822a352 2019-10-15 stsp if (err)
7367 2822a352 2019-10-15 stsp return err;
7368 2822a352 2019-10-15 stsp
7369 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7370 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7371 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7372 2822a352 2019-10-15 stsp "update -b or different branch name required");
7373 2822a352 2019-10-15 stsp goto done;
7374 2822a352 2019-10-15 stsp }
7375 2822a352 2019-10-15 stsp
7376 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7377 2822a352 2019-10-15 stsp if (err)
7378 2822a352 2019-10-15 stsp goto done;
7379 2822a352 2019-10-15 stsp
7380 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7381 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7382 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7383 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7384 2822a352 2019-10-15 stsp &ok_arg);
7385 2822a352 2019-10-15 stsp if (err)
7386 2822a352 2019-10-15 stsp goto done;
7387 2822a352 2019-10-15 stsp
7388 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7389 2822a352 2019-10-15 stsp if (err)
7390 2822a352 2019-10-15 stsp goto done;
7391 2822a352 2019-10-15 stsp
7392 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7393 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7394 2822a352 2019-10-15 stsp done:
7395 2822a352 2019-10-15 stsp if (err) {
7396 2822a352 2019-10-15 stsp if (*branch_ref) {
7397 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7398 2822a352 2019-10-15 stsp *branch_ref = NULL;
7399 2822a352 2019-10-15 stsp }
7400 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7401 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7402 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7403 2822a352 2019-10-15 stsp }
7404 2822a352 2019-10-15 stsp if (*fileindex) {
7405 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7406 2822a352 2019-10-15 stsp *fileindex = NULL;
7407 2822a352 2019-10-15 stsp }
7408 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7409 2822a352 2019-10-15 stsp }
7410 0cb83759 2019-08-03 stsp return err;
7411 0cb83759 2019-08-03 stsp }
7412 0cb83759 2019-08-03 stsp
7413 2822a352 2019-10-15 stsp const struct got_error *
7414 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7415 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7416 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7417 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7418 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7419 2822a352 2019-10-15 stsp {
7420 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7421 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7422 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7423 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7424 2822a352 2019-10-15 stsp
7425 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7426 2822a352 2019-10-15 stsp if (err)
7427 2822a352 2019-10-15 stsp goto done;
7428 2822a352 2019-10-15 stsp
7429 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7430 2822a352 2019-10-15 stsp if (err)
7431 2822a352 2019-10-15 stsp goto done;
7432 2822a352 2019-10-15 stsp
7433 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7434 a44927cc 2022-04-07 stsp if (err)
7435 a44927cc 2022-04-07 stsp goto done;
7436 a44927cc 2022-04-07 stsp
7437 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7438 2822a352 2019-10-15 stsp worktree->path_prefix);
7439 2822a352 2019-10-15 stsp if (err)
7440 2822a352 2019-10-15 stsp goto done;
7441 2822a352 2019-10-15 stsp
7442 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7443 2822a352 2019-10-15 stsp if (err)
7444 2822a352 2019-10-15 stsp goto done;
7445 2822a352 2019-10-15 stsp
7446 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7447 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7448 2822a352 2019-10-15 stsp if (err)
7449 2822a352 2019-10-15 stsp goto sync;
7450 2822a352 2019-10-15 stsp
7451 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7452 2822a352 2019-10-15 stsp if (err)
7453 2822a352 2019-10-15 stsp goto sync;
7454 2822a352 2019-10-15 stsp
7455 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7456 6e210706 2021-01-22 stsp if (err)
7457 6e210706 2021-01-22 stsp goto sync;
7458 6e210706 2021-01-22 stsp
7459 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7460 2822a352 2019-10-15 stsp sync:
7461 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7462 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7463 2822a352 2019-10-15 stsp err = sync_err;
7464 2822a352 2019-10-15 stsp
7465 2822a352 2019-10-15 stsp done:
7466 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7467 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7468 2822a352 2019-10-15 stsp err = unlockerr;
7469 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7470 2822a352 2019-10-15 stsp
7471 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7472 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7473 2822a352 2019-10-15 stsp err = unlockerr;
7474 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7475 2822a352 2019-10-15 stsp
7476 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7477 2822a352 2019-10-15 stsp free(fileindex_path);
7478 2822a352 2019-10-15 stsp free(tree_id);
7479 a44927cc 2022-04-07 stsp if (commit)
7480 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7481 2822a352 2019-10-15 stsp
7482 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7483 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7484 2822a352 2019-10-15 stsp err = unlockerr;
7485 2822a352 2019-10-15 stsp return err;
7486 2822a352 2019-10-15 stsp }
7487 2822a352 2019-10-15 stsp
7488 2822a352 2019-10-15 stsp const struct got_error *
7489 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7490 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7491 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7492 2822a352 2019-10-15 stsp {
7493 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7494 8b692cd0 2019-10-21 stsp
7495 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7496 8b692cd0 2019-10-21 stsp
7497 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7498 8b692cd0 2019-10-21 stsp
7499 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7500 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7501 8b692cd0 2019-10-21 stsp err = unlockerr;
7502 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7503 8b692cd0 2019-10-21 stsp
7504 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7505 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7506 8b692cd0 2019-10-21 stsp err = unlockerr;
7507 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7508 f259c4c1 2021-09-24 stsp
7509 f259c4c1 2021-09-24 stsp return err;
7510 f259c4c1 2021-09-24 stsp }
7511 f259c4c1 2021-09-24 stsp
7512 f259c4c1 2021-09-24 stsp const struct got_error *
7513 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7514 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7515 f259c4c1 2021-09-24 stsp {
7516 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7517 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7518 f259c4c1 2021-09-24 stsp
7519 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7520 f259c4c1 2021-09-24 stsp if (err)
7521 f259c4c1 2021-09-24 stsp goto done;
7522 8b692cd0 2019-10-21 stsp
7523 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7524 f259c4c1 2021-09-24 stsp
7525 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7526 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7527 f259c4c1 2021-09-24 stsp err = sync_err;
7528 f259c4c1 2021-09-24 stsp done:
7529 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7530 f259c4c1 2021-09-24 stsp free(fileindex_path);
7531 8b692cd0 2019-10-21 stsp return err;
7532 2822a352 2019-10-15 stsp }
7533 2822a352 2019-10-15 stsp
7534 f259c4c1 2021-09-24 stsp static const struct got_error *
7535 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7536 f259c4c1 2021-09-24 stsp {
7537 f259c4c1 2021-09-24 stsp const struct got_error *err;
7538 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7539 f259c4c1 2021-09-24 stsp
7540 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7541 f259c4c1 2021-09-24 stsp if (err)
7542 f259c4c1 2021-09-24 stsp goto done;
7543 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7544 f259c4c1 2021-09-24 stsp if (err)
7545 f259c4c1 2021-09-24 stsp goto done;
7546 f259c4c1 2021-09-24 stsp
7547 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7548 f259c4c1 2021-09-24 stsp if (err)
7549 f259c4c1 2021-09-24 stsp goto done;
7550 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7551 f259c4c1 2021-09-24 stsp if (err)
7552 f259c4c1 2021-09-24 stsp goto done;
7553 f259c4c1 2021-09-24 stsp
7554 f259c4c1 2021-09-24 stsp done:
7555 f259c4c1 2021-09-24 stsp free(branch_refname);
7556 f259c4c1 2021-09-24 stsp free(commit_refname);
7557 f259c4c1 2021-09-24 stsp return err;
7558 f259c4c1 2021-09-24 stsp }
7559 f259c4c1 2021-09-24 stsp
7560 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7561 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7562 f259c4c1 2021-09-24 stsp const char *branch_name;
7563 f259c4c1 2021-09-24 stsp };
7564 f259c4c1 2021-09-24 stsp
7565 f259c4c1 2021-09-24 stsp static const struct got_error *
7566 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7567 f259c4c1 2021-09-24 stsp void *arg)
7568 f259c4c1 2021-09-24 stsp {
7569 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7570 f259c4c1 2021-09-24 stsp
7571 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7572 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7573 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7574 f259c4c1 2021-09-24 stsp
7575 f259c4c1 2021-09-24 stsp return NULL;
7576 f259c4c1 2021-09-24 stsp }
7577 f259c4c1 2021-09-24 stsp
7578 f259c4c1 2021-09-24 stsp
7579 f259c4c1 2021-09-24 stsp const struct got_error *
7580 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7581 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7582 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7583 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7584 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7585 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7586 f259c4c1 2021-09-24 stsp {
7587 f259c4c1 2021-09-24 stsp const struct got_error *err;
7588 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7589 f259c4c1 2021-09-24 stsp
7590 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7591 f259c4c1 2021-09-24 stsp if (err)
7592 f259c4c1 2021-09-24 stsp goto done;
7593 f259c4c1 2021-09-24 stsp
7594 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7595 f259c4c1 2021-09-24 stsp worktree);
7596 f259c4c1 2021-09-24 stsp if (err)
7597 f259c4c1 2021-09-24 stsp goto done;
7598 f259c4c1 2021-09-24 stsp
7599 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7600 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7601 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7602 f259c4c1 2021-09-24 stsp done:
7603 f259c4c1 2021-09-24 stsp free(fileindex_path);
7604 f259c4c1 2021-09-24 stsp return err;
7605 f259c4c1 2021-09-24 stsp }
7606 f259c4c1 2021-09-24 stsp
7607 f259c4c1 2021-09-24 stsp const struct got_error *
7608 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7609 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7610 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7611 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7612 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7613 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7614 0ff8d236 2021-09-28 stsp
7615 f259c4c1 2021-09-24 stsp {
7616 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7617 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7618 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7619 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7620 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7621 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7622 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7623 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7624 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7625 f259c4c1 2021-09-24 stsp
7626 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7627 f259c4c1 2021-09-24 stsp
7628 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7629 f259c4c1 2021-09-24 stsp
7630 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7631 f259c4c1 2021-09-24 stsp if (err)
7632 f259c4c1 2021-09-24 stsp goto done;
7633 f259c4c1 2021-09-24 stsp
7634 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7635 f259c4c1 2021-09-24 stsp if (err)
7636 f259c4c1 2021-09-24 stsp goto done;
7637 f259c4c1 2021-09-24 stsp
7638 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7639 f259c4c1 2021-09-24 stsp if (err)
7640 f259c4c1 2021-09-24 stsp goto done;
7641 f259c4c1 2021-09-24 stsp
7642 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7643 f259c4c1 2021-09-24 stsp &have_staged_files);
7644 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7645 f259c4c1 2021-09-24 stsp goto done;
7646 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7647 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7648 f259c4c1 2021-09-24 stsp goto done;
7649 f259c4c1 2021-09-24 stsp }
7650 f259c4c1 2021-09-24 stsp
7651 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7652 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7653 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7654 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7655 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7656 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7657 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7658 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7659 f259c4c1 2021-09-24 stsp if (err)
7660 f259c4c1 2021-09-24 stsp goto done;
7661 f259c4c1 2021-09-24 stsp
7662 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7663 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7664 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7665 f259c4c1 2021-09-24 stsp goto done;
7666 f259c4c1 2021-09-24 stsp }
7667 f259c4c1 2021-09-24 stsp
7668 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7669 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7670 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7671 f259c4c1 2021-09-24 stsp
7672 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7673 f259c4c1 2021-09-24 stsp ct_path++;
7674 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7675 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7676 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7677 f259c4c1 2021-09-24 stsp if (err)
7678 f259c4c1 2021-09-24 stsp goto done;
7679 f259c4c1 2021-09-24 stsp
7680 f259c4c1 2021-09-24 stsp }
7681 f259c4c1 2021-09-24 stsp
7682 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7683 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7684 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7685 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7686 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7687 f259c4c1 2021-09-24 stsp if (err)
7688 f259c4c1 2021-09-24 stsp goto done;
7689 f259c4c1 2021-09-24 stsp
7690 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7691 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7692 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7693 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7694 f259c4c1 2021-09-24 stsp err = sync_err;
7695 f259c4c1 2021-09-24 stsp done:
7696 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7697 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7698 f259c4c1 2021-09-24 stsp free_commitable(ct);
7699 f259c4c1 2021-09-24 stsp }
7700 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7701 f259c4c1 2021-09-24 stsp free(fileindex_path);
7702 f259c4c1 2021-09-24 stsp return err;
7703 f259c4c1 2021-09-24 stsp }
7704 f259c4c1 2021-09-24 stsp
7705 f259c4c1 2021-09-24 stsp const struct got_error *
7706 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7707 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7708 f259c4c1 2021-09-24 stsp {
7709 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7710 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7711 f259c4c1 2021-09-24 stsp
7712 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7713 f259c4c1 2021-09-24 stsp if (err)
7714 f259c4c1 2021-09-24 stsp goto done;
7715 f259c4c1 2021-09-24 stsp
7716 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7717 f259c4c1 2021-09-24 stsp if (err)
7718 f259c4c1 2021-09-24 stsp goto done;
7719 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7720 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7721 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7722 f259c4c1 2021-09-24 stsp err = sync_err;
7723 f259c4c1 2021-09-24 stsp done:
7724 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7725 f259c4c1 2021-09-24 stsp free(fileindex_path);
7726 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7727 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7728 f259c4c1 2021-09-24 stsp err = unlockerr;
7729 f259c4c1 2021-09-24 stsp return err;
7730 f259c4c1 2021-09-24 stsp }
7731 f259c4c1 2021-09-24 stsp
7732 f259c4c1 2021-09-24 stsp const struct got_error *
7733 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7734 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7735 f259c4c1 2021-09-24 stsp {
7736 f259c4c1 2021-09-24 stsp const struct got_error *err;
7737 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7738 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7739 f259c4c1 2021-09-24 stsp
7740 f259c4c1 2021-09-24 stsp *in_progress = 0;
7741 f259c4c1 2021-09-24 stsp
7742 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7743 f259c4c1 2021-09-24 stsp if (err)
7744 f259c4c1 2021-09-24 stsp return err;
7745 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7746 793fcac3 2021-09-24 stsp free(branch_refname);
7747 f259c4c1 2021-09-24 stsp if (err) {
7748 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7749 f259c4c1 2021-09-24 stsp return err;
7750 f259c4c1 2021-09-24 stsp } else
7751 f259c4c1 2021-09-24 stsp *in_progress = 1;
7752 f259c4c1 2021-09-24 stsp
7753 f259c4c1 2021-09-24 stsp return NULL;
7754 f259c4c1 2021-09-24 stsp }
7755 f259c4c1 2021-09-24 stsp
7756 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7757 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7758 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7759 f259c4c1 2021-09-24 stsp {
7760 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7761 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7762 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7763 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7764 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7765 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7766 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7767 f259c4c1 2021-09-24 stsp
7768 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7769 f259c4c1 2021-09-24 stsp
7770 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7771 f259c4c1 2021-09-24 stsp if (err)
7772 f259c4c1 2021-09-24 stsp return err;
7773 f259c4c1 2021-09-24 stsp
7774 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7775 f259c4c1 2021-09-24 stsp if (err)
7776 f259c4c1 2021-09-24 stsp goto done;
7777 f259c4c1 2021-09-24 stsp
7778 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7779 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7780 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7781 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7782 f259c4c1 2021-09-24 stsp &ok_arg);
7783 f259c4c1 2021-09-24 stsp if (err)
7784 f259c4c1 2021-09-24 stsp goto done;
7785 f259c4c1 2021-09-24 stsp
7786 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7787 f259c4c1 2021-09-24 stsp if (err)
7788 f259c4c1 2021-09-24 stsp return err;
7789 f259c4c1 2021-09-24 stsp
7790 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7791 f259c4c1 2021-09-24 stsp if (err)
7792 f259c4c1 2021-09-24 stsp return err;
7793 f259c4c1 2021-09-24 stsp
7794 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7795 f259c4c1 2021-09-24 stsp 0);
7796 f259c4c1 2021-09-24 stsp if (err)
7797 f259c4c1 2021-09-24 stsp goto done;
7798 f259c4c1 2021-09-24 stsp
7799 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
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 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7804 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7805 f259c4c1 2021-09-24 stsp goto done;
7806 f259c4c1 2021-09-24 stsp }
7807 f259c4c1 2021-09-24 stsp
7808 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7809 f259c4c1 2021-09-24 stsp if (err)
7810 f259c4c1 2021-09-24 stsp goto done;
7811 f259c4c1 2021-09-24 stsp
7812 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7813 f259c4c1 2021-09-24 stsp if (err)
7814 f259c4c1 2021-09-24 stsp goto done;
7815 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7816 f259c4c1 2021-09-24 stsp if (err)
7817 f259c4c1 2021-09-24 stsp goto done;
7818 f259c4c1 2021-09-24 stsp
7819 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7820 f259c4c1 2021-09-24 stsp if (err)
7821 f259c4c1 2021-09-24 stsp goto done;
7822 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7823 f259c4c1 2021-09-24 stsp if (err)
7824 f259c4c1 2021-09-24 stsp goto done;
7825 f259c4c1 2021-09-24 stsp
7826 f259c4c1 2021-09-24 stsp done:
7827 f259c4c1 2021-09-24 stsp free(branch_refname);
7828 f259c4c1 2021-09-24 stsp free(commit_refname);
7829 f259c4c1 2021-09-24 stsp free(fileindex_path);
7830 f259c4c1 2021-09-24 stsp if (branch_ref)
7831 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7832 f259c4c1 2021-09-24 stsp if (commit_ref)
7833 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7834 f259c4c1 2021-09-24 stsp if (wt_branch)
7835 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7836 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7837 f259c4c1 2021-09-24 stsp if (err) {
7838 f259c4c1 2021-09-24 stsp if (*fileindex) {
7839 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7840 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7841 f259c4c1 2021-09-24 stsp }
7842 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7843 f259c4c1 2021-09-24 stsp }
7844 f259c4c1 2021-09-24 stsp return err;
7845 f259c4c1 2021-09-24 stsp }
7846 f259c4c1 2021-09-24 stsp
7847 f259c4c1 2021-09-24 stsp const struct got_error *
7848 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7849 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7850 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7851 f259c4c1 2021-09-24 stsp {
7852 f259c4c1 2021-09-24 stsp const struct got_error *err;
7853 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7854 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7855 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7856 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7857 f259c4c1 2021-09-24 stsp
7858 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7859 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7860 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7861 f259c4c1 2021-09-24 stsp
7862 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7863 f259c4c1 2021-09-24 stsp if (err)
7864 f259c4c1 2021-09-24 stsp return err;
7865 f259c4c1 2021-09-24 stsp
7866 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7867 f259c4c1 2021-09-24 stsp if (err)
7868 f259c4c1 2021-09-24 stsp goto done;
7869 f259c4c1 2021-09-24 stsp
7870 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7871 f259c4c1 2021-09-24 stsp &have_staged_files);
7872 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7873 f259c4c1 2021-09-24 stsp goto done;
7874 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7875 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7876 f259c4c1 2021-09-24 stsp goto done;
7877 f259c4c1 2021-09-24 stsp }
7878 f259c4c1 2021-09-24 stsp
7879 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7880 f259c4c1 2021-09-24 stsp if (err)
7881 f259c4c1 2021-09-24 stsp goto done;
7882 f259c4c1 2021-09-24 stsp
7883 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7884 f259c4c1 2021-09-24 stsp if (err)
7885 f259c4c1 2021-09-24 stsp goto done;
7886 f259c4c1 2021-09-24 stsp
7887 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7888 f259c4c1 2021-09-24 stsp if (err)
7889 f259c4c1 2021-09-24 stsp goto done;
7890 f259c4c1 2021-09-24 stsp
7891 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7892 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7893 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7894 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7895 f259c4c1 2021-09-24 stsp goto done;
7896 f259c4c1 2021-09-24 stsp }
7897 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7898 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7899 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7900 f259c4c1 2021-09-24 stsp goto done;
7901 f259c4c1 2021-09-24 stsp }
7902 f259c4c1 2021-09-24 stsp
7903 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7904 f259c4c1 2021-09-24 stsp if (err)
7905 f259c4c1 2021-09-24 stsp goto done;
7906 f259c4c1 2021-09-24 stsp
7907 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7908 f259c4c1 2021-09-24 stsp if (err)
7909 f259c4c1 2021-09-24 stsp goto done;
7910 f259c4c1 2021-09-24 stsp done:
7911 f259c4c1 2021-09-24 stsp free(commit_refname);
7912 f259c4c1 2021-09-24 stsp free(branch_refname);
7913 f259c4c1 2021-09-24 stsp free(fileindex_path);
7914 f259c4c1 2021-09-24 stsp if (commit_ref)
7915 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7916 f259c4c1 2021-09-24 stsp if (branch_ref)
7917 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7918 f259c4c1 2021-09-24 stsp if (err) {
7919 f259c4c1 2021-09-24 stsp if (*branch_name) {
7920 f259c4c1 2021-09-24 stsp free(*branch_name);
7921 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7922 f259c4c1 2021-09-24 stsp }
7923 f259c4c1 2021-09-24 stsp free(*branch_tip);
7924 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7925 f259c4c1 2021-09-24 stsp if (*fileindex) {
7926 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7927 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7928 f259c4c1 2021-09-24 stsp }
7929 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7930 f259c4c1 2021-09-24 stsp }
7931 f259c4c1 2021-09-24 stsp return err;
7932 f259c4c1 2021-09-24 stsp }
7933 f259c4c1 2021-09-24 stsp
7934 f259c4c1 2021-09-24 stsp const struct got_error *
7935 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7936 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7937 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7938 f259c4c1 2021-09-24 stsp {
7939 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7940 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7941 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7942 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7943 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7944 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7945 f259c4c1 2021-09-24 stsp
7946 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7947 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7948 a44927cc 2022-04-07 stsp if (err)
7949 a44927cc 2022-04-07 stsp goto done;
7950 a44927cc 2022-04-07 stsp
7951 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7952 a44927cc 2022-04-07 stsp worktree->path_prefix);
7953 f259c4c1 2021-09-24 stsp if (err)
7954 f259c4c1 2021-09-24 stsp goto done;
7955 f259c4c1 2021-09-24 stsp
7956 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7957 f259c4c1 2021-09-24 stsp if (err)
7958 f259c4c1 2021-09-24 stsp goto done;
7959 f259c4c1 2021-09-24 stsp
7960 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7961 f259c4c1 2021-09-24 stsp if (err)
7962 f259c4c1 2021-09-24 stsp goto done;
7963 f259c4c1 2021-09-24 stsp
7964 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7965 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7966 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7967 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7968 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7969 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7970 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7971 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7972 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7973 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7974 f259c4c1 2021-09-24 stsp if (err)
7975 f259c4c1 2021-09-24 stsp goto sync;
7976 f259c4c1 2021-09-24 stsp
7977 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7978 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7979 f259c4c1 2021-09-24 stsp sync:
7980 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7981 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7982 f259c4c1 2021-09-24 stsp err = sync_err;
7983 f259c4c1 2021-09-24 stsp done:
7984 f259c4c1 2021-09-24 stsp free(tree_id);
7985 f259c4c1 2021-09-24 stsp free(commit_id);
7986 a44927cc 2022-04-07 stsp if (commit)
7987 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7988 f259c4c1 2021-09-24 stsp if (fileindex)
7989 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7990 f259c4c1 2021-09-24 stsp free(fileindex_path);
7991 f259c4c1 2021-09-24 stsp
7992 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7993 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7994 f259c4c1 2021-09-24 stsp err = unlockerr;
7995 f259c4c1 2021-09-24 stsp return err;
7996 f259c4c1 2021-09-24 stsp }
7997 f259c4c1 2021-09-24 stsp
7998 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7999 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8000 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8001 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8002 2db2652d 2019-08-07 stsp struct got_repository *repo;
8003 2db2652d 2019-08-07 stsp int have_changes;
8004 2db2652d 2019-08-07 stsp };
8005 2db2652d 2019-08-07 stsp
8006 336075a4 2022-06-25 op static const struct got_error *
8007 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8008 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8009 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8010 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8011 735ef5ac 2019-08-03 stsp {
8012 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8013 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8014 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8015 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8016 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8017 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8018 8b13ce36 2019-08-08 stsp
8019 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8020 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8021 8b13ce36 2019-08-08 stsp return NULL;
8022 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8023 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8024 735ef5ac 2019-08-03 stsp
8025 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8026 735ef5ac 2019-08-03 stsp if (ie == NULL)
8027 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8028 735ef5ac 2019-08-03 stsp
8029 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8030 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8031 735ef5ac 2019-08-03 stsp relpath) == -1)
8032 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8033 735ef5ac 2019-08-03 stsp
8034 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8035 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
8036 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8037 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8038 735ef5ac 2019-08-03 stsp }
8039 735ef5ac 2019-08-03 stsp
8040 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8041 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8042 3aa5969e 2019-08-06 stsp goto done;
8043 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8044 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8045 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8046 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8047 735ef5ac 2019-08-03 stsp goto done;
8048 3aa5969e 2019-08-06 stsp }
8049 735ef5ac 2019-08-03 stsp
8050 2db2652d 2019-08-07 stsp a->have_changes = 1;
8051 2db2652d 2019-08-07 stsp
8052 735ef5ac 2019-08-03 stsp p = in_repo_path;
8053 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8054 735ef5ac 2019-08-03 stsp p++;
8055 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8056 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8057 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8058 735ef5ac 2019-08-03 stsp done:
8059 735ef5ac 2019-08-03 stsp free(in_repo_path);
8060 dc424a06 2019-08-07 stsp return err;
8061 dc424a06 2019-08-07 stsp }
8062 dc424a06 2019-08-07 stsp
8063 2db2652d 2019-08-07 stsp struct stage_path_arg {
8064 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8065 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8066 2db2652d 2019-08-07 stsp struct got_repository *repo;
8067 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8068 2db2652d 2019-08-07 stsp void *status_arg;
8069 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8070 2db2652d 2019-08-07 stsp void *patch_arg;
8071 7b5dc508 2019-10-28 stsp int staged_something;
8072 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8073 2db2652d 2019-08-07 stsp };
8074 2db2652d 2019-08-07 stsp
8075 2db2652d 2019-08-07 stsp static const struct got_error *
8076 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8077 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8078 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8079 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8080 0cb83759 2019-08-03 stsp {
8081 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8082 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8083 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8084 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8085 0cb83759 2019-08-03 stsp uint32_t stage;
8086 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8087 0aeb8099 2020-07-23 stsp struct stat sb;
8088 8b13ce36 2019-08-08 stsp
8089 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8090 8b13ce36 2019-08-08 stsp return NULL;
8091 0cb83759 2019-08-03 stsp
8092 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8093 d3e7c587 2019-08-03 stsp if (ie == NULL)
8094 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8095 0cb83759 2019-08-03 stsp
8096 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8097 2db2652d 2019-08-07 stsp relpath)== -1)
8098 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8099 0cb83759 2019-08-03 stsp
8100 0cb83759 2019-08-03 stsp switch (status) {
8101 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8102 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8103 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8104 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8105 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8106 0aeb8099 2020-07-23 stsp break;
8107 0aeb8099 2020-07-23 stsp }
8108 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8109 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8110 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8111 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8112 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8113 dc424a06 2019-08-07 stsp if (err)
8114 dc424a06 2019-08-07 stsp break;
8115 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8116 dc424a06 2019-08-07 stsp break;
8117 dc424a06 2019-08-07 stsp } else {
8118 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8119 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8120 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8121 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8122 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8123 dc424a06 2019-08-07 stsp break;
8124 dc424a06 2019-08-07 stsp }
8125 dc424a06 2019-08-07 stsp }
8126 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8127 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8128 0cb83759 2019-08-03 stsp if (err)
8129 dc424a06 2019-08-07 stsp break;
8130 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8131 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8132 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8133 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8134 0cb83759 2019-08-03 stsp else
8135 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8136 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8137 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8138 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8139 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8140 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8141 35213c7c 2020-07-23 stsp ssize_t target_len;
8142 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8143 35213c7c 2020-07-23 stsp sizeof(target_path));
8144 35213c7c 2020-07-23 stsp if (target_len == -1) {
8145 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8146 35213c7c 2020-07-23 stsp ondisk_path);
8147 35213c7c 2020-07-23 stsp break;
8148 35213c7c 2020-07-23 stsp }
8149 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8150 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8151 35213c7c 2020-07-23 stsp a->worktree->root_path);
8152 35213c7c 2020-07-23 stsp if (err)
8153 35213c7c 2020-07-23 stsp break;
8154 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8155 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8156 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8157 35213c7c 2020-07-23 stsp break;
8158 35213c7c 2020-07-23 stsp }
8159 35213c7c 2020-07-23 stsp }
8160 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8161 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8162 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8163 35213c7c 2020-07-23 stsp else
8164 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8165 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8166 0aeb8099 2020-07-23 stsp } else {
8167 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8168 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8169 0aeb8099 2020-07-23 stsp }
8170 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8171 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8172 dc424a06 2019-08-07 stsp break;
8173 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8174 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8175 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8176 9fdde394 2022-06-04 op if (err)
8177 9fdde394 2022-06-04 op break;
8178 9fdde394 2022-06-04 op /*
8179 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8180 9fdde394 2022-06-04 op * implicitly unstage the file.
8181 9fdde394 2022-06-04 op */
8182 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8183 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8184 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8185 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8186 9fdde394 2022-06-04 op }
8187 0cb83759 2019-08-03 stsp break;
8188 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8189 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8190 d3e7c587 2019-08-03 stsp break;
8191 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8192 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8193 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8194 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8195 dc424a06 2019-08-07 stsp if (err)
8196 dc424a06 2019-08-07 stsp break;
8197 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8198 88f33a19 2019-08-08 stsp break;
8199 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8200 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8201 dc424a06 2019-08-07 stsp break;
8202 88f33a19 2019-08-08 stsp }
8203 dc424a06 2019-08-07 stsp }
8204 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8205 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8206 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8207 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8208 dc424a06 2019-08-07 stsp break;
8209 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8210 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8211 12463d8b 2019-12-13 stsp de_name);
8212 0cb83759 2019-08-03 stsp break;
8213 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8214 d3e7c587 2019-08-03 stsp break;
8215 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8216 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8217 ebf48fd5 2019-08-03 stsp break;
8218 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8219 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8220 2a06fe5f 2019-08-24 stsp break;
8221 0cb83759 2019-08-03 stsp default:
8222 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8223 537ac44b 2019-08-03 stsp break;
8224 0cb83759 2019-08-03 stsp }
8225 dc424a06 2019-08-07 stsp
8226 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8227 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8228 dc424a06 2019-08-07 stsp free(path_content);
8229 2db2652d 2019-08-07 stsp free(ondisk_path);
8230 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8231 0ebf8283 2019-07-24 stsp return err;
8232 0ebf8283 2019-07-24 stsp }
8233 0cb83759 2019-08-03 stsp
8234 0cb83759 2019-08-03 stsp const struct got_error *
8235 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8236 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8237 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8238 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8239 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8240 0cb83759 2019-08-03 stsp {
8241 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8242 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8243 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8244 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8245 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8246 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8247 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8248 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8249 0cb83759 2019-08-03 stsp
8250 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8251 0cb83759 2019-08-03 stsp if (err)
8252 0cb83759 2019-08-03 stsp return err;
8253 0cb83759 2019-08-03 stsp
8254 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8255 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8256 735ef5ac 2019-08-03 stsp if (err)
8257 735ef5ac 2019-08-03 stsp goto done;
8258 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8259 735ef5ac 2019-08-03 stsp if (err)
8260 735ef5ac 2019-08-03 stsp goto done;
8261 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8262 0cb83759 2019-08-03 stsp if (err)
8263 0cb83759 2019-08-03 stsp goto done;
8264 0cb83759 2019-08-03 stsp
8265 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8266 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8267 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8268 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8269 2db2652d 2019-08-07 stsp oka.repo = repo;
8270 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8271 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8272 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8273 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8274 735ef5ac 2019-08-03 stsp if (err)
8275 735ef5ac 2019-08-03 stsp goto done;
8276 735ef5ac 2019-08-03 stsp }
8277 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8278 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8279 2db2652d 2019-08-07 stsp goto done;
8280 2db2652d 2019-08-07 stsp }
8281 735ef5ac 2019-08-03 stsp
8282 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8283 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8284 2db2652d 2019-08-07 stsp spa.repo = repo;
8285 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8286 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8287 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8288 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8289 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8290 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8291 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8292 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8293 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8294 42005733 2019-08-03 stsp if (err)
8295 2db2652d 2019-08-07 stsp goto done;
8296 7b5dc508 2019-10-28 stsp }
8297 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8298 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8299 7b5dc508 2019-10-28 stsp goto done;
8300 0cb83759 2019-08-03 stsp }
8301 0cb83759 2019-08-03 stsp
8302 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8303 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8304 0cb83759 2019-08-03 stsp err = sync_err;
8305 0cb83759 2019-08-03 stsp done:
8306 735ef5ac 2019-08-03 stsp if (head_ref)
8307 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8308 735ef5ac 2019-08-03 stsp free(head_commit_id);
8309 0cb83759 2019-08-03 stsp free(fileindex_path);
8310 0cb83759 2019-08-03 stsp if (fileindex)
8311 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8312 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8313 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8314 0cb83759 2019-08-03 stsp err = unlockerr;
8315 0cb83759 2019-08-03 stsp return err;
8316 0cb83759 2019-08-03 stsp }
8317 ad493afc 2019-08-03 stsp
8318 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8319 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8320 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8321 ad493afc 2019-08-03 stsp struct got_repository *repo;
8322 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8323 ad493afc 2019-08-03 stsp void *progress_arg;
8324 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8325 2e1f37b0 2019-08-08 stsp void *patch_arg;
8326 ad493afc 2019-08-03 stsp };
8327 2e1f37b0 2019-08-08 stsp
8328 2e1f37b0 2019-08-08 stsp static const struct got_error *
8329 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8330 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8331 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8332 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8333 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8334 2e1f37b0 2019-08-08 stsp {
8335 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8336 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8337 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8338 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8339 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8340 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8341 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8342 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8343 2e1f37b0 2019-08-08 stsp
8344 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8345 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8346 2e1f37b0 2019-08-08 stsp
8347 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8348 2e1f37b0 2019-08-08 stsp if (err)
8349 2e1f37b0 2019-08-08 stsp return err;
8350 eb81bc23 2022-06-28 tracey
8351 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8352 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8353 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8354 eb81bc23 2022-06-28 tracey goto done;
8355 eb81bc23 2022-06-28 tracey }
8356 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8357 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8358 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8359 eb81bc23 2022-06-28 tracey goto done;
8360 eb81bc23 2022-06-28 tracey }
8361 eb81bc23 2022-06-28 tracey
8362 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8363 2e1f37b0 2019-08-08 stsp if (err)
8364 2e1f37b0 2019-08-08 stsp goto done;
8365 2e1f37b0 2019-08-08 stsp
8366 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8367 2e1f37b0 2019-08-08 stsp if (err)
8368 2e1f37b0 2019-08-08 stsp goto done;
8369 2e1f37b0 2019-08-08 stsp
8370 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8371 2e1f37b0 2019-08-08 stsp if (err)
8372 2e1f37b0 2019-08-08 stsp goto done;
8373 2e1f37b0 2019-08-08 stsp
8374 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8375 eb81bc23 2022-06-28 tracey fd2);
8376 2e1f37b0 2019-08-08 stsp if (err)
8377 2e1f37b0 2019-08-08 stsp goto done;
8378 2e1f37b0 2019-08-08 stsp
8379 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8380 2e1f37b0 2019-08-08 stsp if (err)
8381 2e1f37b0 2019-08-08 stsp goto done;
8382 ad493afc 2019-08-03 stsp
8383 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8384 2e1f37b0 2019-08-08 stsp if (err)
8385 2e1f37b0 2019-08-08 stsp goto done;
8386 2e1f37b0 2019-08-08 stsp
8387 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8388 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8389 2e1f37b0 2019-08-08 stsp if (err)
8390 2e1f37b0 2019-08-08 stsp goto done;
8391 2e1f37b0 2019-08-08 stsp
8392 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8393 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8394 2e1f37b0 2019-08-08 stsp if (err)
8395 2e1f37b0 2019-08-08 stsp goto done;
8396 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8397 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8398 2e1f37b0 2019-08-08 stsp if (err)
8399 2e1f37b0 2019-08-08 stsp goto done;
8400 2e1f37b0 2019-08-08 stsp
8401 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8402 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8403 2e1f37b0 2019-08-08 stsp goto done;
8404 2e1f37b0 2019-08-08 stsp }
8405 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8406 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8407 2e1f37b0 2019-08-08 stsp goto done;
8408 2e1f37b0 2019-08-08 stsp }
8409 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8410 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8411 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8412 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8413 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8414 fe621944 2020-11-10 stsp nchanges++;
8415 fe621944 2020-11-10 stsp }
8416 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8417 2e1f37b0 2019-08-08 stsp int choice;
8418 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8419 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8420 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8421 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8422 2e1f37b0 2019-08-08 stsp if (err)
8423 2e1f37b0 2019-08-08 stsp goto done;
8424 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8425 2e1f37b0 2019-08-08 stsp have_content = 1;
8426 19e4b907 2019-08-08 stsp else
8427 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8428 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8429 2e1f37b0 2019-08-08 stsp break;
8430 2e1f37b0 2019-08-08 stsp }
8431 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8432 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8433 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8434 2e1f37b0 2019-08-08 stsp done:
8435 2e1f37b0 2019-08-08 stsp free(label1);
8436 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8437 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8438 2e1f37b0 2019-08-08 stsp if (blob)
8439 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8440 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8441 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8442 2e1f37b0 2019-08-08 stsp if (staged_blob)
8443 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8444 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8445 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8446 fe621944 2020-11-10 stsp err = free_err;
8447 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8448 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8449 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8450 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8451 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8452 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8453 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8454 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8455 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8456 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8457 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8458 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8459 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8460 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8461 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8462 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8463 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8464 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8465 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8466 2e1f37b0 2019-08-08 stsp }
8467 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8468 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8469 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8470 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8471 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8472 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8473 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8474 2e1f37b0 2019-08-08 stsp }
8475 2e1f37b0 2019-08-08 stsp free(path1);
8476 2e1f37b0 2019-08-08 stsp free(path2);
8477 fda8017d 2020-07-23 stsp return err;
8478 fda8017d 2020-07-23 stsp }
8479 fda8017d 2020-07-23 stsp
8480 fda8017d 2020-07-23 stsp static const struct got_error *
8481 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8482 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8483 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8484 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8485 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8486 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8487 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8488 fda8017d 2020-07-23 stsp {
8489 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8490 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8491 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8492 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8493 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8494 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8495 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8496 fda8017d 2020-07-23 stsp struct stat sb;
8497 fda8017d 2020-07-23 stsp
8498 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8499 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8500 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8501 fda8017d 2020-07-23 stsp if (err)
8502 fda8017d 2020-07-23 stsp return err;
8503 fda8017d 2020-07-23 stsp
8504 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8505 fda8017d 2020-07-23 stsp return NULL;
8506 fda8017d 2020-07-23 stsp
8507 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8508 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8509 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8510 fda8017d 2020-07-23 stsp if (err)
8511 fda8017d 2020-07-23 stsp goto done;
8512 fda8017d 2020-07-23 stsp }
8513 fda8017d 2020-07-23 stsp
8514 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8515 fda8017d 2020-07-23 stsp if (f == NULL) {
8516 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8517 fda8017d 2020-07-23 stsp path_unstaged_content);
8518 fda8017d 2020-07-23 stsp goto done;
8519 fda8017d 2020-07-23 stsp }
8520 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8521 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8522 fda8017d 2020-07-23 stsp goto done;
8523 fda8017d 2020-07-23 stsp }
8524 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8525 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8526 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8527 fda8017d 2020-07-23 stsp size_t r;
8528 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8529 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8530 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8531 fda8017d 2020-07-23 stsp goto done;
8532 fda8017d 2020-07-23 stsp }
8533 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8534 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8535 fda8017d 2020-07-23 stsp goto done;
8536 fda8017d 2020-07-23 stsp }
8537 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8538 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8539 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8540 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8541 fda8017d 2020-07-23 stsp progress_arg);
8542 fda8017d 2020-07-23 stsp } else {
8543 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8544 67a66647 2021-05-31 stsp
8545 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8546 67a66647 2021-05-31 stsp if (err)
8547 67a66647 2021-05-31 stsp return err;
8548 67a66647 2021-05-31 stsp
8549 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8550 67a66647 2021-05-31 stsp parent) == -1) {
8551 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8552 67a66647 2021-05-31 stsp base_path = NULL;
8553 67a66647 2021-05-31 stsp goto done;
8554 67a66647 2021-05-31 stsp }
8555 67a66647 2021-05-31 stsp
8556 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8557 67a66647 2021-05-31 stsp if (err)
8558 67a66647 2021-05-31 stsp goto done;
8559 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8560 67a66647 2021-05-31 stsp blob_base);
8561 67a66647 2021-05-31 stsp if (err)
8562 67a66647 2021-05-31 stsp goto done;
8563 67a66647 2021-05-31 stsp
8564 5e91dae4 2022-08-30 stsp /*
8565 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8566 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8567 eec2f5a9 2021-06-03 stsp */
8568 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8569 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8570 eec2f5a9 2021-06-03 stsp ondisk_path);
8571 eec2f5a9 2021-06-03 stsp if (err)
8572 eec2f5a9 2021-06-03 stsp goto done;
8573 eec2f5a9 2021-06-03 stsp } else {
8574 eec2f5a9 2021-06-03 stsp int fd;
8575 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8576 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8577 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8578 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8579 eec2f5a9 2021-06-03 stsp goto done;
8580 eec2f5a9 2021-06-03 stsp }
8581 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8582 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8583 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8584 eec2f5a9 2021-06-03 stsp close(fd);
8585 eec2f5a9 2021-06-03 stsp goto done;
8586 eec2f5a9 2021-06-03 stsp }
8587 eec2f5a9 2021-06-03 stsp }
8588 eec2f5a9 2021-06-03 stsp
8589 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8590 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8591 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8592 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8593 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8594 fda8017d 2020-07-23 stsp }
8595 fda8017d 2020-07-23 stsp if (err)
8596 fda8017d 2020-07-23 stsp goto done;
8597 fda8017d 2020-07-23 stsp
8598 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8599 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8600 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8601 2ac8aa02 2020-11-09 stsp } else {
8602 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8603 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8604 2ac8aa02 2020-11-09 stsp }
8605 fda8017d 2020-07-23 stsp done:
8606 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8607 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8608 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8609 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8610 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8611 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8612 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8613 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8614 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8615 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8616 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8617 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8618 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8619 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8620 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8621 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8622 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8623 67a66647 2021-05-31 stsp free(blob_base_path);
8624 67a66647 2021-05-31 stsp free(parent);
8625 67a66647 2021-05-31 stsp free(base_path);
8626 2e1f37b0 2019-08-08 stsp return err;
8627 2e1f37b0 2019-08-08 stsp }
8628 2e1f37b0 2019-08-08 stsp
8629 ad493afc 2019-08-03 stsp static const struct got_error *
8630 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8631 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8632 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8633 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8634 ad493afc 2019-08-03 stsp {
8635 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8636 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8637 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8638 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8639 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8640 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8641 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8642 9bc94a15 2019-08-03 stsp struct stat sb;
8643 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8644 ad493afc 2019-08-03 stsp
8645 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8646 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8647 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8648 2e1f37b0 2019-08-08 stsp return NULL;
8649 2e1f37b0 2019-08-08 stsp
8650 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8651 ad493afc 2019-08-03 stsp if (ie == NULL)
8652 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8653 9bc94a15 2019-08-03 stsp
8654 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8655 9bc94a15 2019-08-03 stsp == -1)
8656 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8657 ad493afc 2019-08-03 stsp
8658 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8659 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8660 f69721c3 2019-10-21 stsp if (err)
8661 f69721c3 2019-10-21 stsp goto done;
8662 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8663 f69721c3 2019-10-21 stsp id_str) == -1) {
8664 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8665 eb81bc23 2022-06-28 tracey goto done;
8666 eb81bc23 2022-06-28 tracey }
8667 eb81bc23 2022-06-28 tracey
8668 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8669 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8670 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8671 eb81bc23 2022-06-28 tracey goto done;
8672 eb81bc23 2022-06-28 tracey }
8673 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8674 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8675 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8676 f69721c3 2019-10-21 stsp goto done;
8677 f69721c3 2019-10-21 stsp }
8678 f69721c3 2019-10-21 stsp
8679 ad493afc 2019-08-03 stsp switch (staged_status) {
8680 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8681 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8682 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8683 ad493afc 2019-08-03 stsp if (err)
8684 ad493afc 2019-08-03 stsp break;
8685 ad493afc 2019-08-03 stsp /* fall through */
8686 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8687 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8688 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8689 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8690 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8691 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8692 2e1f37b0 2019-08-08 stsp if (err)
8693 2e1f37b0 2019-08-08 stsp break;
8694 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8695 2e1f37b0 2019-08-08 stsp break;
8696 2e1f37b0 2019-08-08 stsp } else {
8697 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8698 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8699 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8700 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8701 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8702 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8703 2e1f37b0 2019-08-08 stsp }
8704 2e1f37b0 2019-08-08 stsp }
8705 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8706 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8707 ad493afc 2019-08-03 stsp if (err)
8708 ad493afc 2019-08-03 stsp break;
8709 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8710 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8711 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8712 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8713 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8714 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8715 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8716 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8717 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8718 ea7786be 2020-07-23 stsp break;
8719 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8720 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8721 36bf999c 2020-07-23 stsp char *staged_target;
8722 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8723 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8724 36bf999c 2020-07-23 stsp if (err)
8725 36bf999c 2020-07-23 stsp goto done;
8726 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8727 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8728 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8729 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8730 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8731 36bf999c 2020-07-23 stsp free(staged_target);
8732 dfe9fba0 2020-07-23 stsp } else {
8733 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8734 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8735 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8736 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8737 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8738 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8739 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8740 dfe9fba0 2020-07-23 stsp }
8741 ea7786be 2020-07-23 stsp break;
8742 ea7786be 2020-07-23 stsp default:
8743 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8744 ea7786be 2020-07-23 stsp break;
8745 ea7786be 2020-07-23 stsp }
8746 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8747 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8748 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8749 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8750 2ac8aa02 2020-11-09 stsp }
8751 ad493afc 2019-08-03 stsp break;
8752 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8753 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8754 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8755 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8756 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8757 2e1f37b0 2019-08-08 stsp if (err)
8758 2e1f37b0 2019-08-08 stsp break;
8759 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8760 2e1f37b0 2019-08-08 stsp break;
8761 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8762 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8763 2e1f37b0 2019-08-08 stsp break;
8764 2e1f37b0 2019-08-08 stsp }
8765 2e1f37b0 2019-08-08 stsp }
8766 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8767 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8768 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8769 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8770 9bc94a15 2019-08-03 stsp if (err)
8771 9bc94a15 2019-08-03 stsp break;
8772 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8773 ad493afc 2019-08-03 stsp break;
8774 ad493afc 2019-08-03 stsp }
8775 f69721c3 2019-10-21 stsp done:
8776 ad493afc 2019-08-03 stsp free(ondisk_path);
8777 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8778 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8779 ad493afc 2019-08-03 stsp if (blob_base)
8780 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8781 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8782 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8783 ad493afc 2019-08-03 stsp if (blob_staged)
8784 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8785 f69721c3 2019-10-21 stsp free(id_str);
8786 f69721c3 2019-10-21 stsp free(label_orig);
8787 ad493afc 2019-08-03 stsp return err;
8788 ad493afc 2019-08-03 stsp }
8789 ad493afc 2019-08-03 stsp
8790 ad493afc 2019-08-03 stsp const struct got_error *
8791 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8792 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8793 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8794 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8795 ad493afc 2019-08-03 stsp struct got_repository *repo)
8796 ad493afc 2019-08-03 stsp {
8797 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8798 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8799 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8800 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8801 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8802 ad493afc 2019-08-03 stsp
8803 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8804 ad493afc 2019-08-03 stsp if (err)
8805 ad493afc 2019-08-03 stsp return err;
8806 ad493afc 2019-08-03 stsp
8807 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8808 ad493afc 2019-08-03 stsp if (err)
8809 ad493afc 2019-08-03 stsp goto done;
8810 ad493afc 2019-08-03 stsp
8811 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8812 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8813 ad493afc 2019-08-03 stsp upa.repo = repo;
8814 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8815 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8816 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8817 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8818 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8819 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8820 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8821 ad493afc 2019-08-03 stsp if (err)
8822 ad493afc 2019-08-03 stsp goto done;
8823 ad493afc 2019-08-03 stsp }
8824 ad493afc 2019-08-03 stsp
8825 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8826 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8827 ad493afc 2019-08-03 stsp err = sync_err;
8828 ad493afc 2019-08-03 stsp done:
8829 ad493afc 2019-08-03 stsp free(fileindex_path);
8830 ad493afc 2019-08-03 stsp if (fileindex)
8831 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8832 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8833 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8834 ad493afc 2019-08-03 stsp err = unlockerr;
8835 ad493afc 2019-08-03 stsp return err;
8836 ad493afc 2019-08-03 stsp }
8837 b2118c49 2020-07-28 stsp
8838 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8839 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8840 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8841 b2118c49 2020-07-28 stsp void *info_arg;
8842 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8843 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8844 b2118c49 2020-07-28 stsp void *cancel_arg;
8845 b2118c49 2020-07-28 stsp };
8846 b2118c49 2020-07-28 stsp
8847 b2118c49 2020-07-28 stsp static const struct got_error *
8848 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8849 b2118c49 2020-07-28 stsp {
8850 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8851 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8852 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8853 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8854 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8855 b2118c49 2020-07-28 stsp int stage;
8856 b2118c49 2020-07-28 stsp
8857 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8858 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8859 b2118c49 2020-07-28 stsp
8860 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8861 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8862 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8863 b2118c49 2020-07-28 stsp break;
8864 b2118c49 2020-07-28 stsp }
8865 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8866 b2118c49 2020-07-28 stsp return NULL;
8867 b2118c49 2020-07-28 stsp
8868 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8869 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8870 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8871 b2118c49 2020-07-28 stsp }
8872 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8873 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8874 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8875 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8876 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8877 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8878 b2118c49 2020-07-28 stsp }
8879 b2118c49 2020-07-28 stsp
8880 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8881 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8882 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8883 b2118c49 2020-07-28 stsp }
8884 b2118c49 2020-07-28 stsp
8885 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8886 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8887 b2118c49 2020-07-28 stsp }
8888 b2118c49 2020-07-28 stsp
8889 b2118c49 2020-07-28 stsp const struct got_error *
8890 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8891 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8892 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8893 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8894 b2118c49 2020-07-28 stsp
8895 b2118c49 2020-07-28 stsp {
8896 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8897 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8898 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8899 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8900 b2118c49 2020-07-28 stsp
8901 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8902 b2118c49 2020-07-28 stsp if (err)
8903 b2118c49 2020-07-28 stsp return err;
8904 b2118c49 2020-07-28 stsp
8905 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8906 b2118c49 2020-07-28 stsp if (err)
8907 b2118c49 2020-07-28 stsp goto done;
8908 b2118c49 2020-07-28 stsp
8909 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8910 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8911 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8912 b2118c49 2020-07-28 stsp arg.paths = paths;
8913 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8914 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8915 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8916 b2118c49 2020-07-28 stsp &arg);
8917 b2118c49 2020-07-28 stsp done:
8918 b2118c49 2020-07-28 stsp free(fileindex_path);
8919 b2118c49 2020-07-28 stsp if (fileindex)
8920 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8921 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8922 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8923 b2118c49 2020-07-28 stsp err = unlockerr;
8924 b2118c49 2020-07-28 stsp return err;
8925 b2118c49 2020-07-28 stsp }
8926 78f5ac24 2022-03-19 op
8927 78f5ac24 2022-03-19 op static const struct got_error *
8928 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
8929 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
8930 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
8931 78f5ac24 2022-03-19 op {
8932 78f5ac24 2022-03-19 op const struct got_error *err;
8933 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
8934 78f5ac24 2022-03-19 op struct stat sb;
8935 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
8936 78f5ac24 2022-03-19 op
8937 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
8938 78f5ac24 2022-03-19 op if (err)
8939 78f5ac24 2022-03-19 op return err;
8940 78f5ac24 2022-03-19 op
8941 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8942 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
8943 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
8944 78f5ac24 2022-03-19 op
8945 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8946 78f5ac24 2022-03-19 op if (ie) {
8947 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
8948 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8949 a05fb460 2022-04-23 op repo);
8950 78f5ac24 2022-03-19 op if (err)
8951 78f5ac24 2022-03-19 op goto done;
8952 78f5ac24 2022-03-19 op } else {
8953 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
8954 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
8955 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
8956 78f5ac24 2022-03-19 op if (errno != ENOENT) {
8957 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
8958 78f5ac24 2022-03-19 op ondisk_path);
8959 78f5ac24 2022-03-19 op goto done;
8960 78f5ac24 2022-03-19 op }
8961 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
8962 78f5ac24 2022-03-19 op }
8963 78f5ac24 2022-03-19 op }
8964 78f5ac24 2022-03-19 op
8965 78f5ac24 2022-03-19 op done:
8966 78f5ac24 2022-03-19 op free(ondisk_path);
8967 78f5ac24 2022-03-19 op return err;
8968 78f5ac24 2022-03-19 op }
8969 78f5ac24 2022-03-19 op
8970 78f5ac24 2022-03-19 op static const struct got_error *
8971 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
8972 78f5ac24 2022-03-19 op unsigned char staged_status)
8973 78f5ac24 2022-03-19 op {
8974 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8975 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8976 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8977 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8978 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
8979 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
8980 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
8982 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8983 78f5ac24 2022-03-19 op return NULL;
8984 78f5ac24 2022-03-19 op }
8985 78f5ac24 2022-03-19 op
8986 78f5ac24 2022-03-19 op static const struct got_error *
8987 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
8988 78f5ac24 2022-03-19 op {
8989 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
8990 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8991 78f5ac24 2022-03-19 op return NULL;
8992 78f5ac24 2022-03-19 op }
8993 78f5ac24 2022-03-19 op
8994 78f5ac24 2022-03-19 op static const struct got_error *
8995 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
8996 78f5ac24 2022-03-19 op unsigned char staged_status)
8997 78f5ac24 2022-03-19 op {
8998 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8999 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9000 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9001 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9002 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9003 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9004 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9005 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9006 78f5ac24 2022-03-19 op return NULL;
9007 78f5ac24 2022-03-19 op }
9008 78f5ac24 2022-03-19 op
9009 78f5ac24 2022-03-19 op const struct got_error *
9010 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9011 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9012 78f5ac24 2022-03-19 op {
9013 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9014 78f5ac24 2022-03-19 op }
9015 78f5ac24 2022-03-19 op
9016 78f5ac24 2022-03-19 op const struct got_error *
9017 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9018 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9019 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9020 78f5ac24 2022-03-19 op {
9021 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9022 78f5ac24 2022-03-19 op int file_renamed = 0;
9023 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9024 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9025 78f5ac24 2022-03-19 op
9026 78f5ac24 2022-03-19 op *oldpath = NULL;
9027 78f5ac24 2022-03-19 op *newpath = NULL;
9028 78f5ac24 2022-03-19 op
9029 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9030 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9031 78f5ac24 2022-03-19 op if (err)
9032 78f5ac24 2022-03-19 op goto done;
9033 78f5ac24 2022-03-19 op
9034 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9035 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9036 78f5ac24 2022-03-19 op if (err)
9037 78f5ac24 2022-03-19 op goto done;
9038 78f5ac24 2022-03-19 op
9039 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9040 78f5ac24 2022-03-19 op file_renamed = 1;
9041 78f5ac24 2022-03-19 op
9042 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9043 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9044 78f5ac24 2022-03-19 op else if (file_renamed) {
9045 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9046 78f5ac24 2022-03-19 op if (err == NULL)
9047 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9048 78f5ac24 2022-03-19 op } else if (old == NULL)
9049 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9050 78f5ac24 2022-03-19 op else
9051 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9052 78f5ac24 2022-03-19 op
9053 78f5ac24 2022-03-19 op done:
9054 78f5ac24 2022-03-19 op if (err) {
9055 78f5ac24 2022-03-19 op free(*oldpath);
9056 78f5ac24 2022-03-19 op *oldpath = NULL;
9057 78f5ac24 2022-03-19 op free(*newpath);
9058 78f5ac24 2022-03-19 op *newpath = NULL;
9059 78f5ac24 2022-03-19 op }
9060 78f5ac24 2022-03-19 op return err;
9061 78f5ac24 2022-03-19 op }
9062 78f5ac24 2022-03-19 op
9063 78f5ac24 2022-03-19 op const struct got_error *
9064 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9065 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9066 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9067 78f5ac24 2022-03-19 op {
9068 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9069 f2dd7807 2022-05-17 op
9070 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9071 f2dd7807 2022-05-17 op saa.worktree = worktree;
9072 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9073 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9074 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9075 f2dd7807 2022-05-17 op saa.repo = repo;
9076 f2dd7807 2022-05-17 op
9077 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9078 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9079 78f5ac24 2022-03-19 op }
9080 f2dd7807 2022-05-17 op
9081 f2dd7807 2022-05-17 op const struct got_error *
9082 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9083 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9084 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9085 f2dd7807 2022-05-17 op {
9086 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9087 f2dd7807 2022-05-17 op
9088 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9089 f2dd7807 2022-05-17 op sda.worktree = worktree;
9090 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9091 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9092 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9093 f2dd7807 2022-05-17 op sda.repo = repo;
9094 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9095 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9096 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9097 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9098 f2dd7807 2022-05-17 op
9099 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9100 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9101 f2dd7807 2022-05-17 op }
9102 f2dd7807 2022-05-17 op
9103 f2dd7807 2022-05-17 op const struct got_error *
9104 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9105 4bcdc895 2022-05-17 op const char *fileindex_path)
9106 f2dd7807 2022-05-17 op {
9107 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9108 f2dd7807 2022-05-17 op
9109 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9110 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9111 f2dd7807 2022-05-17 op
9112 f2dd7807 2022-05-17 op return err;
9113 f2dd7807 2022-05-17 op }