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 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 0e039681 2021-11-15 stsp }
478 0e039681 2021-11-15 stsp
479 0e039681 2021-11-15 stsp static const struct got_error *
480 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 0e039681 2021-11-15 stsp {
482 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
483 0e039681 2021-11-15 stsp size_t flen;
484 0e039681 2021-11-15 stsp ssize_t outlen;
485 0e039681 2021-11-15 stsp
486 0e039681 2021-11-15 stsp *outsize = 0;
487 0e039681 2021-11-15 stsp
488 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
489 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
490 0e039681 2021-11-15 stsp
491 0e039681 2021-11-15 stsp for (;;) {
492 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
493 0e039681 2021-11-15 stsp if (flen == 0) {
494 0e039681 2021-11-15 stsp if (ferror(f))
495 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
496 0e039681 2021-11-15 stsp if (feof(f))
497 0e039681 2021-11-15 stsp break;
498 0e039681 2021-11-15 stsp }
499 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
500 0e039681 2021-11-15 stsp if (outlen == -1)
501 0e039681 2021-11-15 stsp return got_error_from_errno("write");
502 0e039681 2021-11-15 stsp if (outlen != flen)
503 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
504 0e039681 2021-11-15 stsp *outsize += outlen;
505 0e039681 2021-11-15 stsp }
506 0e039681 2021-11-15 stsp
507 0e039681 2021-11-15 stsp return NULL;
508 0e039681 2021-11-15 stsp }
509 0e039681 2021-11-15 stsp
510 0e039681 2021-11-15 stsp static const struct got_error *
511 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
512 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 0e039681 2021-11-15 stsp const char *ondisk_path)
515 0e039681 2021-11-15 stsp {
516 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
517 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
518 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
522 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp
524 0e039681 2021-11-15 stsp *overlapcnt = 0;
525 0e039681 2021-11-15 stsp
526 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 0e039681 2021-11-15 stsp if (err)
528 0e039681 2021-11-15 stsp return err;
529 0e039681 2021-11-15 stsp
530 0e039681 2021-11-15 stsp if (same_content)
531 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 0e039681 2021-11-15 stsp
533 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
534 0e039681 2021-11-15 stsp if (err)
535 0e039681 2021-11-15 stsp return err;
536 0e039681 2021-11-15 stsp changed_deriv = !same_content;
537 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
538 0e039681 2021-11-15 stsp if (err)
539 0e039681 2021-11-15 stsp return err;
540 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
541 0e039681 2021-11-15 stsp
542 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
543 0e039681 2021-11-15 stsp *overlapcnt = 1;
544 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
546 0e039681 2021-11-15 stsp goto done;
547 0e039681 2021-11-15 stsp }
548 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
550 0e039681 2021-11-15 stsp goto done;
551 0e039681 2021-11-15 stsp }
552 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
554 0e039681 2021-11-15 stsp goto done;
555 0e039681 2021-11-15 stsp }
556 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 0e039681 2021-11-15 stsp base_path_orig);
558 0e039681 2021-11-15 stsp if (err)
559 0e039681 2021-11-15 stsp goto done;
560 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 0e039681 2021-11-15 stsp base_path_deriv);
562 0e039681 2021-11-15 stsp if (err)
563 0e039681 2021-11-15 stsp goto done;
564 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 0e039681 2021-11-15 stsp base_path_deriv2);
566 0e039681 2021-11-15 stsp if (err)
567 0e039681 2021-11-15 stsp goto done;
568 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 0e039681 2021-11-15 stsp if (err)
570 0e039681 2021-11-15 stsp goto done;
571 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 0e039681 2021-11-15 stsp if (err)
573 0e039681 2021-11-15 stsp goto done;
574 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 0e039681 2021-11-15 stsp if (err)
576 0e039681 2021-11-15 stsp goto done;
577 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
578 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
579 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
580 0e039681 2021-11-15 stsp goto done;
581 0e039681 2021-11-15 stsp }
582 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 0e039681 2021-11-15 stsp label_deriv ? " " : "",
585 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
586 0e039681 2021-11-15 stsp path_deriv) < 0) {
587 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
588 0e039681 2021-11-15 stsp goto done;
589 0e039681 2021-11-15 stsp }
590 0e039681 2021-11-15 stsp if (size_orig > 0) {
591 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
593 0e039681 2021-11-15 stsp label_orig ? " " : "",
594 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
595 0e039681 2021-11-15 stsp path_orig) < 0) {
596 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
597 0e039681 2021-11-15 stsp goto done;
598 0e039681 2021-11-15 stsp }
599 0e039681 2021-11-15 stsp }
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
602 0e039681 2021-11-15 stsp path_deriv2,
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
604 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
605 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
606 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
607 0e039681 2021-11-15 stsp goto done;
608 0e039681 2021-11-15 stsp }
609 0e039681 2021-11-15 stsp } else if (changed_deriv)
610 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 0e039681 2021-11-15 stsp else if (changed_deriv2)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 0e039681 2021-11-15 stsp done:
614 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 0e039681 2021-11-15 stsp err == NULL)
616 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
617 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
619 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
621 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
623 0e039681 2021-11-15 stsp free(path_orig);
624 0e039681 2021-11-15 stsp free(path_deriv);
625 0e039681 2021-11-15 stsp free(path_deriv2);
626 0e039681 2021-11-15 stsp free(base_path_orig);
627 0e039681 2021-11-15 stsp free(base_path_deriv);
628 0e039681 2021-11-15 stsp free(base_path_deriv2);
629 0e039681 2021-11-15 stsp return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 0e039681 2021-11-15 stsp if (err) {
672 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
673 0e039681 2021-11-15 stsp goto done;
674 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 0e039681 2021-11-15 stsp ondisk_path);
677 0e039681 2021-11-15 stsp if (err)
678 0e039681 2021-11-15 stsp goto done;
679 0e039681 2021-11-15 stsp }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 67a66647 2021-05-31 stsp if (err)
995 67a66647 2021-05-31 stsp goto done;
996 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 67a66647 2021-05-31 stsp blob_orig);
998 67a66647 2021-05-31 stsp if (err)
999 67a66647 2021-05-31 stsp goto done;
1000 67a66647 2021-05-31 stsp free(base_path);
1001 db590691 2021-06-02 stsp } else {
1002 db590691 2021-06-02 stsp /*
1003 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1004 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1005 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1006 db590691 2021-06-02 stsp */
1007 db590691 2021-06-02 stsp f_orig = got_opentemp();
1008 db590691 2021-06-02 stsp if (f_orig == NULL) {
1009 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1010 db590691 2021-06-02 stsp goto done;
1011 db590691 2021-06-02 stsp }
1012 67a66647 2021-05-31 stsp }
1013 67a66647 2021-05-31 stsp
1014 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1016 14c901f1 2019-08-08 stsp base_path = NULL;
1017 14c901f1 2019-08-08 stsp goto done;
1018 14c901f1 2019-08-08 stsp }
1019 14c901f1 2019-08-08 stsp
1020 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 14c901f1 2019-08-08 stsp if (err)
1022 14c901f1 2019-08-08 stsp goto done;
1023 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 14c901f1 2019-08-08 stsp blob_deriv);
1025 14c901f1 2019-08-08 stsp if (err)
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp
1028 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 14c901f1 2019-08-08 stsp if (err)
1030 14c901f1 2019-08-08 stsp goto done;
1031 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1032 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1034 14c901f1 2019-08-08 stsp goto done;
1035 eec2f5a9 2021-06-03 stsp }
1036 eec2f5a9 2021-06-03 stsp
1037 eec2f5a9 2021-06-03 stsp /*
1038 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1039 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1040 eec2f5a9 2021-06-03 stsp */
1041 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1042 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 eec2f5a9 2021-06-03 stsp if (err)
1044 eec2f5a9 2021-06-03 stsp goto done;
1045 eec2f5a9 2021-06-03 stsp } else {
1046 eec2f5a9 2021-06-03 stsp int fd;
1047 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1049 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1050 eec2f5a9 2021-06-03 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp }
1052 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1053 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1054 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1055 eec2f5a9 2021-06-03 stsp close(fd);
1056 eec2f5a9 2021-06-03 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp }
1058 14c901f1 2019-08-08 stsp }
1059 14c901f1 2019-08-08 stsp
1060 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 14c901f1 2019-08-08 stsp done:
1064 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1066 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1068 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1070 14c901f1 2019-08-08 stsp free(base_path);
1071 67a66647 2021-05-31 stsp if (blob_orig_path) {
1072 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1073 67a66647 2021-05-31 stsp free(blob_orig_path);
1074 67a66647 2021-05-31 stsp }
1075 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1076 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1077 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp }
1079 6353ad76 2019-02-08 stsp free(id_str);
1080 818c7501 2019-07-11 stsp free(label_deriv);
1081 ed6b5030 2020-10-20 stsp free(parent);
1082 4a1ddfc2 2019-01-12 stsp return err;
1083 4a1ddfc2 2019-01-12 stsp }
1084 4a1ddfc2 2019-01-12 stsp
1085 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1086 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1089 13d9040b 2019-03-27 stsp {
1090 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1091 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1092 13d9040b 2019-03-27 stsp
1093 65b05cec 2020-07-23 stsp *new_iep = NULL;
1094 65b05cec 2020-07-23 stsp
1095 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1096 054041d0 2020-06-24 stsp if (err)
1097 054041d0 2020-06-24 stsp return err;
1098 054041d0 2020-06-24 stsp
1099 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1101 054041d0 2020-06-24 stsp if (err)
1102 054041d0 2020-06-24 stsp goto done;
1103 054041d0 2020-06-24 stsp
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1105 054041d0 2020-06-24 stsp done:
1106 054041d0 2020-06-24 stsp if (err)
1107 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1108 65b05cec 2020-07-23 stsp else
1109 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1110 13d9040b 2019-03-27 stsp return err;
1111 1ebedb77 2019-10-19 stsp }
1112 1ebedb77 2019-10-19 stsp
1113 1ebedb77 2019-10-19 stsp static mode_t
1114 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1115 1ebedb77 2019-10-19 stsp {
1116 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1117 1ebedb77 2019-10-19 stsp
1118 1ebedb77 2019-10-19 stsp if (executable) {
1119 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1124 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1125 1ebedb77 2019-10-19 stsp }
1126 1ebedb77 2019-10-19 stsp
1127 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 13d9040b 2019-03-27 stsp }
1129 13d9040b 2019-03-27 stsp
1130 8ba819a3 2020-07-23 stsp /* forward declaration */
1131 13d9040b 2019-03-27 stsp static const struct got_error *
1132 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1134 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1135 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1136 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1137 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 8ba819a3 2020-07-23 stsp
1139 5a1fbc73 2020-07-23 stsp /*
1140 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1141 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1142 5a1fbc73 2020-07-23 stsp */
1143 8ba819a3 2020-07-23 stsp static const struct got_error *
1144 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1146 5a1fbc73 2020-07-23 stsp {
1147 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1148 5a1fbc73 2020-07-23 stsp ssize_t elen;
1149 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1150 5a1fbc73 2020-07-23 stsp int fd;
1151 5a1fbc73 2020-07-23 stsp
1152 c6e8a826 2021-04-05 stsp *did_something = 0;
1153 c6e8a826 2021-04-05 stsp
1154 5a1fbc73 2020-07-23 stsp /*
1155 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1156 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1157 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1158 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1159 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1160 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1161 5a1fbc73 2020-07-23 stsp */
1162 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1164 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1165 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1166 5a1fbc73 2020-07-23 stsp
1167 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1168 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 5a1fbc73 2020-07-23 stsp if (elen == -1)
1170 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1171 5a1fbc73 2020-07-23 stsp
1172 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1173 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1174 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1175 5a1fbc73 2020-07-23 stsp }
1176 5a1fbc73 2020-07-23 stsp
1177 c6e8a826 2021-04-05 stsp *did_something = 1;
1178 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1179 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1180 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp return err;
1182 3c1ec782 2020-07-23 stsp }
1183 3c1ec782 2020-07-23 stsp
1184 3c1ec782 2020-07-23 stsp static const struct got_error *
1185 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1187 3c1ec782 2020-07-23 stsp {
1188 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1189 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1190 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1191 3c1ec782 2020-07-23 stsp
1192 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1193 3c1ec782 2020-07-23 stsp
1194 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1195 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1196 3c1ec782 2020-07-23 stsp return NULL;
1197 3c1ec782 2020-07-23 stsp }
1198 3c1ec782 2020-07-23 stsp
1199 3c1ec782 2020-07-23 stsp /*
1200 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1201 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1202 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1203 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1204 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1205 3c1ec782 2020-07-23 stsp */
1206 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1207 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1208 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1209 ce031e9e 2020-10-20 stsp if (err)
1210 ce031e9e 2020-10-20 stsp return err;
1211 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 ce031e9e 2020-10-20 stsp free(parent);
1213 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1214 ce031e9e 2020-10-20 stsp }
1215 ce031e9e 2020-10-20 stsp free(parent);
1216 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 3c1ec782 2020-07-23 stsp free(abspath);
1219 3c1ec782 2020-07-23 stsp return err;
1220 3c1ec782 2020-07-23 stsp }
1221 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 3c1ec782 2020-07-23 stsp free(abspath);
1223 3c1ec782 2020-07-23 stsp if (err)
1224 3c1ec782 2020-07-23 stsp return err;
1225 3c1ec782 2020-07-23 stsp } else {
1226 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 3c1ec782 2020-07-23 stsp if (err)
1228 3c1ec782 2020-07-23 stsp return err;
1229 3c1ec782 2020-07-23 stsp }
1230 3c1ec782 2020-07-23 stsp
1231 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1232 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1234 3c1ec782 2020-07-23 stsp return NULL;
1235 3c1ec782 2020-07-23 stsp }
1236 3c1ec782 2020-07-23 stsp
1237 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1238 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1240 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1241 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1243 3c1ec782 2020-07-23 stsp
1244 3c1ec782 2020-07-23 stsp free(path_got);
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 5a1fbc73 2020-07-23 stsp }
1247 5a1fbc73 2020-07-23 stsp
1248 5a1fbc73 2020-07-23 stsp static const struct got_error *
1249 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1252 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1253 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1254 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1255 9d31a1d8 2018-03-11 stsp {
1256 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1257 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1258 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1259 906c123b 2020-07-23 stsp char *path_got = NULL;
1260 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1262 8ba819a3 2020-07-23 stsp
1263 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1264 2e1fa222 2020-07-23 stsp
1265 8ba819a3 2020-07-23 stsp /*
1266 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1267 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1268 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1269 8ba819a3 2020-07-23 stsp * in the blob object.
1270 8ba819a3 2020-07-23 stsp */
1271 8ba819a3 2020-07-23 stsp do {
1272 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1273 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1274 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1276 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1277 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1278 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1280 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1281 3b9f0f87 2020-07-23 stsp progress_arg);
1282 8ba819a3 2020-07-23 stsp }
1283 8ba819a3 2020-07-23 stsp if (len > 0) {
1284 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1285 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1286 8ba819a3 2020-07-23 stsp len - hdrlen);
1287 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1288 8ba819a3 2020-07-23 stsp hdrlen = 0;
1289 8ba819a3 2020-07-23 stsp }
1290 8ba819a3 2020-07-23 stsp } while (len != 0);
1291 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1292 8ba819a3 2020-07-23 stsp
1293 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1295 3c1ec782 2020-07-23 stsp if (err)
1296 3c1ec782 2020-07-23 stsp return err;
1297 8ba819a3 2020-07-23 stsp
1298 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1299 906c123b 2020-07-23 stsp /* install as a regular file */
1300 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1301 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1302 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1304 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1305 906c123b 2020-07-23 stsp goto done;
1306 906c123b 2020-07-23 stsp }
1307 906c123b 2020-07-23 stsp
1308 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1309 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1310 c6e8a826 2021-04-05 stsp int symlink_replaced;
1311 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1312 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1313 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1314 c90c8ce3 2020-07-23 stsp goto done;
1315 c90c8ce3 2020-07-23 stsp }
1316 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1317 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1318 5a1fbc73 2020-07-23 stsp if (err)
1319 f35fa46a 2020-07-23 stsp goto done;
1320 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1321 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1322 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1323 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1324 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1326 c6e8a826 2021-04-05 stsp } else {
1327 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1328 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1329 c6e8a826 2021-04-05 stsp }
1330 f35fa46a 2020-07-23 stsp }
1331 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1332 f35fa46a 2020-07-23 stsp }
1333 f35fa46a 2020-07-23 stsp
1334 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1335 f4994adc 2020-10-20 stsp char *parent;
1336 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1337 f4994adc 2020-10-20 stsp if (err)
1338 f4994adc 2020-10-20 stsp goto done;
1339 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1340 f4994adc 2020-10-20 stsp free(parent);
1341 f35fa46a 2020-07-23 stsp if (err)
1342 f35fa46a 2020-07-23 stsp goto done;
1343 f35fa46a 2020-07-23 stsp /*
1344 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1345 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1346 f35fa46a 2020-07-23 stsp */
1347 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1348 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1349 f35fa46a 2020-07-23 stsp goto done;
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp }
1352 f35fa46a 2020-07-23 stsp
1353 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1354 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1355 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1356 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1357 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1358 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1359 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1361 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1362 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1363 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1364 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1365 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1366 8ba819a3 2020-07-23 stsp } else {
1367 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1368 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1369 8ba819a3 2020-07-23 stsp }
1370 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1371 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 8ba819a3 2020-07-23 stsp done:
1374 906c123b 2020-07-23 stsp free(path_got);
1375 8ba819a3 2020-07-23 stsp return err;
1376 8ba819a3 2020-07-23 stsp }
1377 8ba819a3 2020-07-23 stsp
1378 8ba819a3 2020-07-23 stsp static const struct got_error *
1379 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1381 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1382 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1383 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1384 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1385 8ba819a3 2020-07-23 stsp {
1386 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1387 507dc3bb 2018-12-29 stsp int fd = -1;
1388 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1389 507dc3bb 2018-12-29 stsp int update = 0;
1390 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1391 8ba819a3 2020-07-23 stsp
1392 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 8bd0cdad 2021-12-31 stsp O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1395 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1396 f5375317 2020-10-20 stsp char *parent;
1397 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1398 f5375317 2020-10-20 stsp if (err)
1399 f5375317 2020-10-20 stsp return err;
1400 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1401 f5375317 2020-10-20 stsp free(parent);
1402 21908da4 2019-01-13 stsp if (err)
1403 21908da4 2019-01-13 stsp return err;
1404 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1405 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1407 21908da4 2019-01-13 stsp if (fd == -1)
1408 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1409 230a42bd 2019-05-11 jcs ondisk_path);
1410 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1411 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1412 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1413 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1414 3b9f0f87 2020-07-23 stsp goto done;
1415 3b9f0f87 2020-07-23 stsp }
1416 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1419 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1420 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1421 507dc3bb 2018-12-29 stsp goto done;
1422 d70b8e30 2018-12-27 stsp } else {
1423 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1424 507dc3bb 2018-12-29 stsp ondisk_path);
1425 507dc3bb 2018-12-29 stsp if (err)
1426 507dc3bb 2018-12-29 stsp goto done;
1427 507dc3bb 2018-12-29 stsp update = 1;
1428 9d31a1d8 2018-03-11 stsp }
1429 507dc3bb 2018-12-29 stsp } else
1430 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1431 9d31a1d8 2018-03-11 stsp }
1432 9d31a1d8 2018-03-11 stsp
1433 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1435 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1436 3818e3c4 2020-11-01 naddy goto done;
1437 3818e3c4 2020-11-01 naddy }
1438 3818e3c4 2020-11-01 naddy
1439 bd6aa359 2020-07-23 stsp if (progress_cb) {
1440 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1441 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 bd6aa359 2020-07-23 stsp path);
1443 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1444 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 bd6aa359 2020-07-23 stsp path);
1446 bd6aa359 2020-07-23 stsp else
1447 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 bd6aa359 2020-07-23 stsp if (err)
1450 bd6aa359 2020-07-23 stsp goto done;
1451 bd6aa359 2020-07-23 stsp }
1452 d7b62c98 2018-12-27 stsp
1453 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1454 9d31a1d8 2018-03-11 stsp do {
1455 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1457 9d31a1d8 2018-03-11 stsp if (err)
1458 9d31a1d8 2018-03-11 stsp break;
1459 9d31a1d8 2018-03-11 stsp if (len > 0) {
1460 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1461 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1463 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1464 b87c6f83 2018-12-24 stsp goto done;
1465 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1466 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1467 b87c6f83 2018-12-24 stsp goto done;
1468 9d31a1d8 2018-03-11 stsp }
1469 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1470 9d31a1d8 2018-03-11 stsp }
1471 9d31a1d8 2018-03-11 stsp } while (len != 0);
1472 9d31a1d8 2018-03-11 stsp
1473 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1474 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1475 816dc654 2019-02-16 stsp goto done;
1476 816dc654 2019-02-16 stsp }
1477 9d31a1d8 2018-03-11 stsp
1478 507dc3bb 2018-12-29 stsp if (update) {
1479 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1481 f6d8c0ac 2020-11-09 stsp goto done;
1482 f6d8c0ac 2020-11-09 stsp }
1483 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1484 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1485 230a42bd 2019-05-11 jcs ondisk_path);
1486 68ed9ba5 2019-02-10 stsp goto done;
1487 68ed9ba5 2019-02-10 stsp }
1488 63df146d 2020-11-09 stsp free(tmppath);
1489 63df146d 2020-11-09 stsp tmppath = NULL;
1490 507dc3bb 2018-12-29 stsp }
1491 507dc3bb 2018-12-29 stsp
1492 9d31a1d8 2018-03-11 stsp done:
1493 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1494 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1495 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1497 507dc3bb 2018-12-29 stsp free(tmppath);
1498 6353ad76 2019-02-08 stsp return err;
1499 6353ad76 2019-02-08 stsp }
1500 6353ad76 2019-02-08 stsp
1501 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 6353ad76 2019-02-08 stsp static const struct got_error *
1503 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1504 7154f6ce 2019-03-27 stsp {
1505 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1506 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1509 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1510 7154f6ce 2019-03-27 stsp };
1511 7154f6ce 2019-03-27 stsp int i = 0;
1512 9bdd68dd 2021-01-02 naddy char *line = NULL;
1513 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1514 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1515 7154f6ce 2019-03-27 stsp
1516 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1517 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1518 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1519 7154f6ce 2019-03-27 stsp if (feof(f))
1520 7154f6ce 2019-03-27 stsp break;
1521 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1522 7154f6ce 2019-03-27 stsp break;
1523 7154f6ce 2019-03-27 stsp }
1524 7154f6ce 2019-03-27 stsp
1525 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 19332e6d 2019-05-13 stsp == 0)
1528 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1529 7154f6ce 2019-03-27 stsp else
1530 7154f6ce 2019-03-27 stsp i++;
1531 7154f6ce 2019-03-27 stsp }
1532 7154f6ce 2019-03-27 stsp }
1533 9bdd68dd 2021-01-02 naddy free(line);
1534 7154f6ce 2019-03-27 stsp
1535 7154f6ce 2019-03-27 stsp return err;
1536 e2b1e152 2019-07-13 stsp }
1537 e2b1e152 2019-07-13 stsp
1538 e2b1e152 2019-07-13 stsp static int
1539 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1540 1ebedb77 2019-10-19 stsp {
1541 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 1ebedb77 2019-10-19 stsp }
1544 1ebedb77 2019-10-19 stsp
1545 1ebedb77 2019-10-19 stsp static int
1546 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1547 e2b1e152 2019-07-13 stsp {
1548 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1553 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1554 c363b2c1 2019-08-03 stsp }
1555 c363b2c1 2019-08-03 stsp
1556 c363b2c1 2019-08-03 stsp static unsigned char
1557 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1558 c363b2c1 2019-08-03 stsp {
1559 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1564 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1566 c363b2c1 2019-08-03 stsp default:
1567 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1568 a919d5c4 2020-07-23 stsp }
1569 a919d5c4 2020-07-23 stsp }
1570 a919d5c4 2020-07-23 stsp
1571 a919d5c4 2020-07-23 stsp static const struct got_error *
1572 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1573 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1574 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1575 a919d5c4 2020-07-23 stsp {
1576 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1577 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1578 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1579 a919d5c4 2020-07-23 stsp ssize_t elen;
1580 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1581 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1583 a919d5c4 2020-07-23 stsp
1584 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1585 a919d5c4 2020-07-23 stsp
1586 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1587 a919d5c4 2020-07-23 stsp do {
1588 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1589 a919d5c4 2020-07-23 stsp if (err)
1590 a919d5c4 2020-07-23 stsp return err;
1591 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1592 a919d5c4 2020-07-23 stsp /*
1593 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1594 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1595 a919d5c4 2020-07-23 stsp */
1596 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1597 a919d5c4 2020-07-23 stsp }
1598 a919d5c4 2020-07-23 stsp if (len > 0) {
1599 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1600 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1601 a919d5c4 2020-07-23 stsp len - hdrlen);
1602 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1603 a919d5c4 2020-07-23 stsp hdrlen = 0;
1604 a919d5c4 2020-07-23 stsp }
1605 a919d5c4 2020-07-23 stsp } while (len != 0);
1606 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1607 a919d5c4 2020-07-23 stsp
1608 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1609 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 a919d5c4 2020-07-23 stsp if (elen == -1)
1611 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1612 a919d5c4 2020-07-23 stsp } else {
1613 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1614 a919d5c4 2020-07-23 stsp if (elen == -1)
1615 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1616 c363b2c1 2019-08-03 stsp }
1617 a919d5c4 2020-07-23 stsp
1618 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1620 a919d5c4 2020-07-23 stsp
1621 a919d5c4 2020-07-23 stsp return NULL;
1622 7154f6ce 2019-03-27 stsp }
1623 7154f6ce 2019-03-27 stsp
1624 7154f6ce 2019-03-27 stsp static const struct got_error *
1625 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1626 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1627 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1628 6353ad76 2019-02-08 stsp {
1629 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1630 6353ad76 2019-02-08 stsp struct got_object_id id;
1631 6353ad76 2019-02-08 stsp size_t hdrlen;
1632 1338848f 2019-12-13 stsp int fd = -1;
1633 6353ad76 2019-02-08 stsp FILE *f = NULL;
1634 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1635 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1636 6353ad76 2019-02-08 stsp size_t flen, blen;
1637 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1638 6353ad76 2019-02-08 stsp
1639 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1640 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1641 6353ad76 2019-02-08 stsp
1642 7f91a133 2019-12-13 stsp /*
1643 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1644 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1645 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1646 7f91a133 2019-12-13 stsp */
1647 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1648 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1650 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1652 882ef1b9 2019-12-13 stsp else
1653 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1654 882ef1b9 2019-12-13 stsp goto done;
1655 882ef1b9 2019-12-13 stsp }
1656 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1657 3d35a492 2019-12-13 stsp goto done;
1658 3d35a492 2019-12-13 stsp }
1659 7f91a133 2019-12-13 stsp } else {
1660 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1662 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1663 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1664 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1666 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1667 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1669 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1671 3d35a492 2019-12-13 stsp else
1672 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1673 3d35a492 2019-12-13 stsp goto done;
1674 3d35a492 2019-12-13 stsp }
1675 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1676 1338848f 2019-12-13 stsp goto done;
1677 a378724f 2019-02-10 stsp }
1678 a378724f 2019-02-10 stsp }
1679 6353ad76 2019-02-08 stsp
1680 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1682 1338848f 2019-12-13 stsp goto done;
1683 3f148bc6 2019-07-27 stsp }
1684 efdd40df 2019-07-27 stsp
1685 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1687 1338848f 2019-12-13 stsp goto done;
1688 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1689 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1690 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1691 1338848f 2019-12-13 stsp goto done;
1692 d00136be 2019-03-26 stsp }
1693 b8f41171 2019-02-10 stsp
1694 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1695 f179e66d 2020-07-23 stsp goto done;
1696 f179e66d 2020-07-23 stsp
1697 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1698 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 1338848f 2019-12-13 stsp goto done;
1701 f179e66d 2020-07-23 stsp }
1702 6353ad76 2019-02-08 stsp
1703 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1704 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 c363b2c1 2019-08-03 stsp else
1707 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1708 c363b2c1 2019-08-03 stsp
1709 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 6353ad76 2019-02-08 stsp if (err)
1711 1338848f 2019-12-13 stsp goto done;
1712 6353ad76 2019-02-08 stsp
1713 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1714 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1715 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1716 a919d5c4 2020-07-23 stsp goto done;
1717 a919d5c4 2020-07-23 stsp }
1718 a919d5c4 2020-07-23 stsp
1719 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1720 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1721 ab0d4361 2019-12-13 stsp if (fd == -1) {
1722 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1723 ab0d4361 2019-12-13 stsp goto done;
1724 ab0d4361 2019-12-13 stsp }
1725 3d35a492 2019-12-13 stsp }
1726 3d35a492 2019-12-13 stsp
1727 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1728 6353ad76 2019-02-08 stsp if (f == NULL) {
1729 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1730 6353ad76 2019-02-08 stsp goto done;
1731 6353ad76 2019-02-08 stsp }
1732 1338848f 2019-12-13 stsp fd = -1;
1733 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1734 656b1f76 2019-05-11 jcs for (;;) {
1735 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1737 6353ad76 2019-02-08 stsp if (err)
1738 7154f6ce 2019-03-27 stsp goto done;
1739 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1740 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1742 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1743 7154f6ce 2019-03-27 stsp goto done;
1744 80c5c120 2019-02-19 stsp }
1745 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1746 6353ad76 2019-02-08 stsp if (flen != 0)
1747 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1748 6353ad76 2019-02-08 stsp break;
1749 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1750 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1751 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1752 6353ad76 2019-02-08 stsp break;
1753 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1754 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1755 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1757 6353ad76 2019-02-08 stsp break;
1758 6353ad76 2019-02-08 stsp }
1759 6353ad76 2019-02-08 stsp } else {
1760 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1761 6353ad76 2019-02-08 stsp break;
1762 6353ad76 2019-02-08 stsp }
1763 6353ad76 2019-02-08 stsp hdrlen = 0;
1764 6353ad76 2019-02-08 stsp }
1765 7154f6ce 2019-03-27 stsp
1766 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1767 7154f6ce 2019-03-27 stsp rewind(f);
1768 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1769 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1770 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1771 6353ad76 2019-02-08 stsp done:
1772 6353ad76 2019-02-08 stsp if (blob)
1773 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1774 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1775 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1776 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1777 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1778 9d31a1d8 2018-03-11 stsp return err;
1779 e2b1e152 2019-07-13 stsp }
1780 e2b1e152 2019-07-13 stsp
1781 e2b1e152 2019-07-13 stsp /*
1782 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1783 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1784 e2b1e152 2019-07-13 stsp */
1785 e2b1e152 2019-07-13 stsp static const struct got_error *
1786 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1788 e2b1e152 2019-07-13 stsp {
1789 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1791 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1792 e2b1e152 2019-07-13 stsp
1793 e2b1e152 2019-07-13 stsp return NULL;
1794 9d31a1d8 2018-03-11 stsp }
1795 9d31a1d8 2018-03-11 stsp
1796 9d31a1d8 2018-03-11 stsp static const struct got_error *
1797 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1798 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1800 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 0584f854 2019-04-06 stsp void *progress_arg)
1802 9d31a1d8 2018-03-11 stsp {
1803 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1804 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1805 6353ad76 2019-02-08 stsp char *ondisk_path;
1806 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1807 b8f41171 2019-02-10 stsp struct stat sb;
1808 9d31a1d8 2018-03-11 stsp
1809 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1811 6353ad76 2019-02-08 stsp
1812 abb4604f 2019-07-27 stsp if (ie) {
1813 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 a76c42e6 2019-08-03 stsp goto done;
1816 a76c42e6 2019-08-03 stsp }
1817 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 7f91a133 2019-12-13 stsp repo);
1819 abb4604f 2019-07-27 stsp if (err)
1820 abb4604f 2019-07-27 stsp goto done;
1821 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1823 c90c8ce3 2020-07-23 stsp } else {
1824 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1825 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1826 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1827 f6764181 2021-09-24 stsp ondisk_path);
1828 f6764181 2021-09-24 stsp goto done;
1829 f6764181 2021-09-24 stsp }
1830 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1832 f6764181 2021-09-24 stsp } else {
1833 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1835 f6764181 2021-09-24 stsp else
1836 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1837 f6764181 2021-09-24 stsp }
1838 c90c8ce3 2020-07-23 stsp }
1839 6353ad76 2019-02-08 stsp
1840 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1841 2c41dce7 2021-06-27 stsp if (ie)
1842 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1843 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1844 b8f41171 2019-02-10 stsp goto done;
1845 b8f41171 2019-02-10 stsp }
1846 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1847 a769b60b 2021-06-27 stsp if (ie)
1848 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1849 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1850 5036ab18 2020-04-18 stsp path);
1851 5036ab18 2020-04-18 stsp goto done;
1852 5036ab18 2020-04-18 stsp }
1853 b8f41171 2019-02-10 stsp
1854 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1856 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1857 c6e8a826 2021-04-05 stsp /*
1858 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1859 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1860 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1861 c6e8a826 2021-04-05 stsp * updating contents of this file.
1862 c6e8a826 2021-04-05 stsp */
1863 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1864 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1866 c6e8a826 2021-04-05 stsp /* Same commit. */
1867 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1868 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1869 e2b1e152 2019-07-13 stsp if (err)
1870 e2b1e152 2019-07-13 stsp goto done;
1871 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 b8f41171 2019-02-10 stsp path);
1873 6353ad76 2019-02-08 stsp goto done;
1874 a378724f 2019-02-10 stsp }
1875 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1876 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1877 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1878 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1879 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1880 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1881 0f58026f 2021-04-05 stsp if (err)
1882 0f58026f 2021-04-05 stsp goto done;
1883 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1884 0f58026f 2021-04-05 stsp path);
1885 b8f41171 2019-02-10 stsp goto done;
1886 e2b1e152 2019-07-13 stsp }
1887 9d31a1d8 2018-03-11 stsp }
1888 9d31a1d8 2018-03-11 stsp
1889 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 8da9e5f4 2019-01-12 stsp if (err)
1891 6353ad76 2019-02-08 stsp goto done;
1892 512f0d0e 2019-01-02 stsp
1893 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 234035bc 2019-06-01 stsp int update_timestamps;
1895 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1896 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1897 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1898 234035bc 2019-06-01 stsp struct got_object_id id2;
1899 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 234035bc 2019-06-01 stsp if (err)
1902 f69721c3 2019-10-21 stsp goto done;
1903 f69721c3 2019-10-21 stsp }
1904 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1905 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1908 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1909 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1910 f69721c3 2019-10-21 stsp goto done;
1911 f69721c3 2019-10-21 stsp }
1912 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1913 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1915 234035bc 2019-06-01 stsp goto done;
1916 f69721c3 2019-10-21 stsp }
1917 234035bc 2019-06-01 stsp }
1918 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 36bf999c 2020-07-23 stsp char *link_target;
1920 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1921 36bf999c 2020-07-23 stsp if (err)
1922 36bf999c 2020-07-23 stsp goto done;
1923 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1925 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1926 36bf999c 2020-07-23 stsp free(link_target);
1927 993e2a1b 2020-07-23 stsp } else {
1928 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1929 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1930 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1931 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1932 993e2a1b 2020-07-23 stsp }
1933 f69721c3 2019-10-21 stsp free(label_orig);
1934 234035bc 2019-06-01 stsp if (blob2)
1935 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1936 909d120e 2019-09-22 stsp if (err)
1937 909d120e 2019-09-22 stsp goto done;
1938 234035bc 2019-06-01 stsp /*
1939 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1940 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1941 234035bc 2019-06-01 stsp * unmodified files again.
1942 234035bc 2019-06-01 stsp */
1943 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1945 234035bc 2019-06-01 stsp update_timestamps);
1946 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1950 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 1ee397ad 2019-07-12 stsp if (err)
1952 1ee397ad 2019-07-12 stsp goto done;
1953 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 13d9040b 2019-03-27 stsp if (err)
1956 13d9040b 2019-03-27 stsp goto done;
1957 13d9040b 2019-03-27 stsp } else {
1958 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1959 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1960 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1961 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1962 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1963 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
1964 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
1965 2e1fa222 2020-07-23 stsp } else {
1966 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1967 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1968 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1969 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1970 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1971 2e1fa222 2020-07-23 stsp }
1972 13d9040b 2019-03-27 stsp if (err)
1973 13d9040b 2019-03-27 stsp goto done;
1974 65b05cec 2020-07-23 stsp
1975 054041d0 2020-06-24 stsp if (ie) {
1976 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1977 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1978 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1979 054041d0 2020-06-24 stsp } else {
1980 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1981 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
1982 054041d0 2020-06-24 stsp &blob->id);
1983 054041d0 2020-06-24 stsp }
1984 13d9040b 2019-03-27 stsp if (err)
1985 13d9040b 2019-03-27 stsp goto done;
1986 65b05cec 2020-07-23 stsp
1987 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1988 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1989 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1990 2e1fa222 2020-07-23 stsp }
1991 13d9040b 2019-03-27 stsp }
1992 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1993 6353ad76 2019-02-08 stsp done:
1994 6353ad76 2019-02-08 stsp free(ondisk_path);
1995 8da9e5f4 2019-01-12 stsp return err;
1996 90285c3b 2019-01-08 stsp }
1997 90285c3b 2019-01-08 stsp
1998 90285c3b 2019-01-08 stsp static const struct got_error *
1999 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2000 90285c3b 2019-01-08 stsp {
2001 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2002 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2003 90285c3b 2019-01-08 stsp
2004 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2006 90285c3b 2019-01-08 stsp
2007 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2008 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2009 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2010 c97665ae 2019-01-13 stsp } else {
2011 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2012 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2013 1c4cdd89 2021-06-20 stsp if (err)
2014 1c4cdd89 2021-06-20 stsp goto done;
2015 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2016 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2017 fddefe3b 2020-10-20 stsp free(ondisk_path);
2018 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2019 1c4cdd89 2021-06-20 stsp parent = NULL;
2020 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2021 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2022 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2023 fddefe3b 2020-10-20 stsp ondisk_path);
2024 90285c3b 2019-01-08 stsp break;
2025 90285c3b 2019-01-08 stsp }
2026 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2027 1c4cdd89 2021-06-20 stsp if (err)
2028 1c4cdd89 2021-06-20 stsp break;
2029 1c4cdd89 2021-06-20 stsp }
2030 90285c3b 2019-01-08 stsp }
2031 1c4cdd89 2021-06-20 stsp done:
2032 90285c3b 2019-01-08 stsp free(ondisk_path);
2033 1c4cdd89 2021-06-20 stsp free(parent);
2034 90285c3b 2019-01-08 stsp return err;
2035 512f0d0e 2019-01-02 stsp }
2036 512f0d0e 2019-01-02 stsp
2037 708d8e67 2019-03-27 stsp static const struct got_error *
2038 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2040 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2041 708d8e67 2019-03-27 stsp {
2042 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2043 708d8e67 2019-03-27 stsp unsigned char status;
2044 708d8e67 2019-03-27 stsp struct stat sb;
2045 708d8e67 2019-03-27 stsp char *ondisk_path;
2046 708d8e67 2019-03-27 stsp
2047 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2049 a76c42e6 2019-08-03 stsp
2050 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 708d8e67 2019-03-27 stsp == -1)
2052 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2053 708d8e67 2019-03-27 stsp
2054 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 708d8e67 2019-03-27 stsp if (err)
2056 5a58a424 2020-06-23 stsp goto done;
2057 708d8e67 2019-03-27 stsp
2058 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2060 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2062 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2063 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2064 993e2a1b 2020-07-23 stsp goto done;
2065 993e2a1b 2020-07-23 stsp }
2066 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2067 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2069 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2070 993e2a1b 2020-07-23 stsp if (err)
2071 993e2a1b 2020-07-23 stsp goto done;
2072 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 993e2a1b 2020-07-23 stsp ie->path);
2074 993e2a1b 2020-07-23 stsp goto done;
2075 993e2a1b 2020-07-23 stsp }
2076 993e2a1b 2020-07-23 stsp
2077 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2079 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 1ee397ad 2019-07-12 stsp if (err)
2081 5a58a424 2020-06-23 stsp goto done;
2082 fc6346c4 2019-03-27 stsp /*
2083 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2084 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2085 fc6346c4 2019-03-27 stsp */
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2088 fc6346c4 2019-03-27 stsp } else {
2089 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 1ee397ad 2019-07-12 stsp if (err)
2091 5a58a424 2020-06-23 stsp goto done;
2092 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2093 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2094 fc6346c4 2019-03-27 stsp if (err)
2095 5a58a424 2020-06-23 stsp goto done;
2096 fc6346c4 2019-03-27 stsp }
2097 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2098 708d8e67 2019-03-27 stsp }
2099 5a58a424 2020-06-23 stsp done:
2100 5a58a424 2020-06-23 stsp free(ondisk_path);
2101 fc6346c4 2019-03-27 stsp return err;
2102 708d8e67 2019-03-27 stsp }
2103 708d8e67 2019-03-27 stsp
2104 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2105 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2106 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2107 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2108 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2109 8da9e5f4 2019-01-12 stsp void *progress_arg;
2110 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2111 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2112 8da9e5f4 2019-01-12 stsp };
2113 8da9e5f4 2019-01-12 stsp
2114 512f0d0e 2019-01-02 stsp static const struct got_error *
2115 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2117 512f0d0e 2019-01-02 stsp {
2118 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2119 0584f854 2019-04-06 stsp
2120 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2122 512f0d0e 2019-01-02 stsp
2123 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2124 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2125 8da9e5f4 2019-01-12 stsp }
2126 512f0d0e 2019-01-02 stsp
2127 8da9e5f4 2019-01-12 stsp static const struct got_error *
2128 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2129 8da9e5f4 2019-01-12 stsp {
2130 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2131 7a9df742 2019-01-08 stsp
2132 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2134 0584f854 2019-04-06 stsp
2135 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2136 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2137 9d31a1d8 2018-03-11 stsp }
2138 9d31a1d8 2018-03-11 stsp
2139 9d31a1d8 2018-03-11 stsp static const struct got_error *
2140 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2141 9d31a1d8 2018-03-11 stsp {
2142 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2143 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2144 8da9e5f4 2019-01-12 stsp char *path;
2145 9d31a1d8 2018-03-11 stsp
2146 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2148 0584f854 2019-04-06 stsp
2149 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2150 63c5ca5d 2019-08-24 stsp return NULL;
2151 63c5ca5d 2019-08-24 stsp
2152 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2153 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2154 8da9e5f4 2019-01-12 stsp == -1)
2155 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2156 9d31a1d8 2018-03-11 stsp
2157 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2158 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2159 8da9e5f4 2019-01-12 stsp else
2160 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2162 9d31a1d8 2018-03-11 stsp
2163 8da9e5f4 2019-01-12 stsp free(path);
2164 0cd1c46a 2019-03-11 stsp return err;
2165 0cd1c46a 2019-03-11 stsp }
2166 0cd1c46a 2019-03-11 stsp
2167 b2118c49 2020-07-28 stsp const struct got_error *
2168 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2169 b2118c49 2020-07-28 stsp {
2170 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2171 b2118c49 2020-07-28 stsp
2172 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2174 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2175 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2176 b2118c49 2020-07-28 stsp }
2177 b2118c49 2020-07-28 stsp
2178 b2118c49 2020-07-28 stsp return NULL;
2179 b2118c49 2020-07-28 stsp }
2180 b2118c49 2020-07-28 stsp
2181 818c7501 2019-07-11 stsp static const struct got_error *
2182 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2183 0cd1c46a 2019-03-11 stsp {
2184 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2185 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2186 0cd1c46a 2019-03-11 stsp
2187 517bab73 2019-03-11 stsp *refname = NULL;
2188 517bab73 2019-03-11 stsp
2189 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2190 b2118c49 2020-07-28 stsp if (err)
2191 b2118c49 2020-07-28 stsp return err;
2192 0cd1c46a 2019-03-11 stsp
2193 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2195 0647c563 2019-03-11 stsp *refname = NULL;
2196 0cd1c46a 2019-03-11 stsp }
2197 517bab73 2019-03-11 stsp free(uuidstr);
2198 517bab73 2019-03-11 stsp return err;
2199 517bab73 2019-03-11 stsp }
2200 517bab73 2019-03-11 stsp
2201 818c7501 2019-07-11 stsp const struct got_error *
2202 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2203 818c7501 2019-07-11 stsp {
2204 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2205 818c7501 2019-07-11 stsp }
2206 818c7501 2019-07-11 stsp
2207 818c7501 2019-07-11 stsp static const struct got_error *
2208 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 818c7501 2019-07-11 stsp {
2210 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2211 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2212 818c7501 2019-07-11 stsp }
2213 818c7501 2019-07-11 stsp
2214 818c7501 2019-07-11 stsp static const struct got_error *
2215 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2216 818c7501 2019-07-11 stsp {
2217 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2218 818c7501 2019-07-11 stsp }
2219 818c7501 2019-07-11 stsp
2220 818c7501 2019-07-11 stsp static const struct got_error *
2221 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2222 818c7501 2019-07-11 stsp {
2223 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2224 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2225 818c7501 2019-07-11 stsp }
2226 818c7501 2019-07-11 stsp
2227 818c7501 2019-07-11 stsp static const struct got_error *
2228 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 818c7501 2019-07-11 stsp {
2230 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2231 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2232 0ebf8283 2019-07-24 stsp }
2233 0ebf8283 2019-07-24 stsp
2234 0ebf8283 2019-07-24 stsp static const struct got_error *
2235 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 0ebf8283 2019-07-24 stsp {
2237 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2238 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2239 0ebf8283 2019-07-24 stsp }
2240 0ebf8283 2019-07-24 stsp
2241 0ebf8283 2019-07-24 stsp static const struct got_error *
2242 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 0ebf8283 2019-07-24 stsp {
2244 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2245 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2246 0ebf8283 2019-07-24 stsp }
2247 0ebf8283 2019-07-24 stsp
2248 0ebf8283 2019-07-24 stsp static const struct got_error *
2249 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 0ebf8283 2019-07-24 stsp {
2251 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2252 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2253 818c7501 2019-07-11 stsp }
2254 818c7501 2019-07-11 stsp
2255 0ebf8283 2019-07-24 stsp static const struct got_error *
2256 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 0ebf8283 2019-07-24 stsp {
2258 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2259 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2260 0ebf8283 2019-07-24 stsp }
2261 818c7501 2019-07-11 stsp
2262 0ebf8283 2019-07-24 stsp const struct got_error *
2263 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2264 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2265 0ebf8283 2019-07-24 stsp {
2266 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 0ebf8283 2019-07-24 stsp *path = NULL;
2269 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2270 0ebf8283 2019-07-24 stsp }
2271 0ebf8283 2019-07-24 stsp return NULL;
2272 f259c4c1 2021-09-24 stsp }
2273 f259c4c1 2021-09-24 stsp
2274 f259c4c1 2021-09-24 stsp static const struct got_error *
2275 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2276 f259c4c1 2021-09-24 stsp {
2277 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2278 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2279 0ebf8283 2019-07-24 stsp }
2280 0ebf8283 2019-07-24 stsp
2281 f259c4c1 2021-09-24 stsp static const struct got_error *
2282 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 f259c4c1 2021-09-24 stsp {
2284 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2285 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 f259c4c1 2021-09-24 stsp }
2287 f259c4c1 2021-09-24 stsp
2288 517bab73 2019-03-11 stsp /*
2289 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2290 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2291 517bab73 2019-03-11 stsp */
2292 517bab73 2019-03-11 stsp static const struct got_error *
2293 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2294 517bab73 2019-03-11 stsp {
2295 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2296 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2297 517bab73 2019-03-11 stsp char *refname;
2298 517bab73 2019-03-11 stsp
2299 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2300 517bab73 2019-03-11 stsp if (err)
2301 517bab73 2019-03-11 stsp return err;
2302 0cd1c46a 2019-03-11 stsp
2303 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 0cd1c46a 2019-03-11 stsp if (err)
2305 0cd1c46a 2019-03-11 stsp goto done;
2306 0cd1c46a 2019-03-11 stsp
2307 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2308 0cd1c46a 2019-03-11 stsp done:
2309 0cd1c46a 2019-03-11 stsp free(refname);
2310 0cd1c46a 2019-03-11 stsp if (ref)
2311 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2312 3e3a69f1 2019-07-25 stsp return err;
2313 3e3a69f1 2019-07-25 stsp }
2314 3e3a69f1 2019-07-25 stsp
2315 3e3a69f1 2019-07-25 stsp static const struct got_error *
2316 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2317 3e3a69f1 2019-07-25 stsp {
2318 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2319 3e3a69f1 2019-07-25 stsp
2320 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2323 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2324 3e3a69f1 2019-07-25 stsp }
2325 9d31a1d8 2018-03-11 stsp return err;
2326 9d31a1d8 2018-03-11 stsp }
2327 9d31a1d8 2018-03-11 stsp
2328 3e3a69f1 2019-07-25 stsp
2329 ebf99748 2019-05-09 stsp static const struct got_error *
2330 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2332 ebf99748 2019-05-09 stsp {
2333 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2334 ebf99748 2019-05-09 stsp FILE *index = NULL;
2335 0cd1c46a 2019-03-11 stsp
2336 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2337 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2338 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2339 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2340 ebf99748 2019-05-09 stsp
2341 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2342 3e3a69f1 2019-07-25 stsp if (err)
2343 ebf99748 2019-05-09 stsp goto done;
2344 ebf99748 2019-05-09 stsp
2345 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2346 ebf99748 2019-05-09 stsp if (index == NULL) {
2347 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2348 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2349 ebf99748 2019-05-09 stsp } else {
2350 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2351 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2353 ebf99748 2019-05-09 stsp }
2354 ebf99748 2019-05-09 stsp done:
2355 ebf99748 2019-05-09 stsp if (err) {
2356 ebf99748 2019-05-09 stsp free(*fileindex_path);
2357 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2358 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2359 ebf99748 2019-05-09 stsp *fileindex = NULL;
2360 ebf99748 2019-05-09 stsp }
2361 ebf99748 2019-05-09 stsp return err;
2362 ebf99748 2019-05-09 stsp }
2363 c932eeeb 2019-05-22 stsp
2364 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2365 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2366 c932eeeb 2019-05-22 stsp const char *path;
2367 c932eeeb 2019-05-22 stsp size_t path_len;
2368 c932eeeb 2019-05-22 stsp const char *entry_name;
2369 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2370 a484d721 2019-06-10 stsp void *progress_arg;
2371 c932eeeb 2019-05-22 stsp };
2372 ebf99748 2019-05-09 stsp
2373 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2374 c932eeeb 2019-05-22 stsp static const struct got_error *
2375 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2376 c932eeeb 2019-05-22 stsp {
2377 1ee397ad 2019-07-12 stsp const struct got_error *err;
2378 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2379 c932eeeb 2019-05-22 stsp
2380 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2381 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2382 c932eeeb 2019-05-22 stsp return NULL;
2383 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 102ff934 2019-06-11 stsp return NULL;
2385 102ff934 2019-06-11 stsp
2386 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2387 a769b60b 2021-06-27 stsp return NULL;
2388 a769b60b 2021-06-27 stsp
2389 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2391 c932eeeb 2019-05-22 stsp return NULL;
2392 c932eeeb 2019-05-22 stsp
2393 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2394 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 edd02c5e 2019-07-11 stsp ie->path);
2396 1ee397ad 2019-07-12 stsp if (err)
2397 1ee397ad 2019-07-12 stsp return err;
2398 1ee397ad 2019-07-12 stsp }
2399 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 c932eeeb 2019-05-22 stsp return NULL;
2401 a615e0e7 2020-12-16 stsp }
2402 a615e0e7 2020-12-16 stsp
2403 a615e0e7 2020-12-16 stsp static const struct got_error *
2404 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2406 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2407 a615e0e7 2020-12-16 stsp {
2408 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2409 a615e0e7 2020-12-16 stsp
2410 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2411 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2412 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2413 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2414 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2415 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2416 a615e0e7 2020-12-16 stsp
2417 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2418 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2419 9c6338c4 2019-06-02 stsp }
2420 9c6338c4 2019-06-02 stsp
2421 9c6338c4 2019-06-02 stsp static const struct got_error *
2422 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2423 9c6338c4 2019-06-02 stsp {
2424 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2425 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2426 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2427 867630bb 2020-01-17 stsp struct timespec timeout;
2428 9c6338c4 2019-06-02 stsp
2429 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 9c6338c4 2019-06-02 stsp fileindex_path);
2431 9c6338c4 2019-06-02 stsp if (err)
2432 9c6338c4 2019-06-02 stsp goto done;
2433 9c6338c4 2019-06-02 stsp
2434 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2435 9c6338c4 2019-06-02 stsp if (err)
2436 9c6338c4 2019-06-02 stsp goto done;
2437 9c6338c4 2019-06-02 stsp
2438 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2440 9c6338c4 2019-06-02 stsp fileindex_path);
2441 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2442 9c6338c4 2019-06-02 stsp }
2443 867630bb 2020-01-17 stsp
2444 867630bb 2020-01-17 stsp /*
2445 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2446 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2447 867630bb 2020-01-17 stsp * was recorded in the file index.
2448 867630bb 2020-01-17 stsp */
2449 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2450 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2451 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2452 9c6338c4 2019-06-02 stsp done:
2453 9c6338c4 2019-06-02 stsp if (new_index)
2454 9c6338c4 2019-06-02 stsp fclose(new_index);
2455 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2456 9c6338c4 2019-06-02 stsp return err;
2457 c932eeeb 2019-05-22 stsp }
2458 6ced4176 2019-07-12 stsp
2459 6ced4176 2019-07-12 stsp static const struct got_error *
2460 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2462 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2463 a44927cc 2022-04-07 stsp struct got_repository *repo)
2464 6ced4176 2019-07-12 stsp {
2465 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2466 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2467 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2468 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 6ced4176 2019-07-12 stsp
2470 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2471 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2472 6ced4176 2019-07-12 stsp *tree_id = NULL;
2473 6ced4176 2019-07-12 stsp
2474 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2475 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2476 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2477 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2478 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2479 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2480 6ced4176 2019-07-12 stsp goto done;
2481 6ced4176 2019-07-12 stsp }
2482 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2483 a44927cc 2022-04-07 stsp worktree->path_prefix);
2484 6ced4176 2019-07-12 stsp if (err)
2485 6ced4176 2019-07-12 stsp goto done;
2486 6ced4176 2019-07-12 stsp return NULL;
2487 6ced4176 2019-07-12 stsp }
2488 6ced4176 2019-07-12 stsp
2489 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2490 6ced4176 2019-07-12 stsp
2491 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2492 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2493 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2494 6ced4176 2019-07-12 stsp goto done;
2495 6ced4176 2019-07-12 stsp }
2496 6ced4176 2019-07-12 stsp
2497 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2498 6ced4176 2019-07-12 stsp if (err)
2499 6ced4176 2019-07-12 stsp goto done;
2500 6ced4176 2019-07-12 stsp
2501 6ced4176 2019-07-12 stsp free(in_repo_path);
2502 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2503 6ced4176 2019-07-12 stsp
2504 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2505 6ced4176 2019-07-12 stsp if (err)
2506 6ced4176 2019-07-12 stsp goto done;
2507 c932eeeb 2019-05-22 stsp
2508 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 6ced4176 2019-07-12 stsp /* Check out a single file. */
2510 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2511 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2512 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2513 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2514 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2515 6ced4176 2019-07-12 stsp goto done;
2516 6ced4176 2019-07-12 stsp }
2517 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2518 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2520 6ced4176 2019-07-12 stsp goto done;
2521 6ced4176 2019-07-12 stsp }
2522 6ced4176 2019-07-12 stsp } else {
2523 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2524 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2525 6ced4176 2019-07-12 stsp if (err)
2526 6ced4176 2019-07-12 stsp return err;
2527 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2528 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2529 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2531 6ced4176 2019-07-12 stsp goto done;
2532 6ced4176 2019-07-12 stsp }
2533 6ced4176 2019-07-12 stsp }
2534 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2535 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2536 6ced4176 2019-07-12 stsp } else {
2537 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2538 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2539 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2540 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2541 6ced4176 2019-07-12 stsp goto done;
2542 6ced4176 2019-07-12 stsp }
2543 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2544 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2546 6ced4176 2019-07-12 stsp goto done;
2547 6ced4176 2019-07-12 stsp }
2548 6ced4176 2019-07-12 stsp }
2549 6ced4176 2019-07-12 stsp done:
2550 6ced4176 2019-07-12 stsp free(id);
2551 6ced4176 2019-07-12 stsp free(in_repo_path);
2552 6ced4176 2019-07-12 stsp if (err) {
2553 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2554 6ced4176 2019-07-12 stsp free(*tree_relpath);
2555 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2556 6ced4176 2019-07-12 stsp free(*tree_id);
2557 6ced4176 2019-07-12 stsp *tree_id = NULL;
2558 6ced4176 2019-07-12 stsp }
2559 07ed472d 2019-07-12 stsp return err;
2560 07ed472d 2019-07-12 stsp }
2561 07ed472d 2019-07-12 stsp
2562 07ed472d 2019-07-12 stsp static const struct got_error *
2563 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2567 07ed472d 2019-07-12 stsp {
2568 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2569 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2570 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2571 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2572 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2573 07ed472d 2019-07-12 stsp
2574 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2575 7f47418f 2019-12-20 stsp if (err) {
2576 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2577 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2578 7f47418f 2019-12-20 stsp goto done;
2579 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2580 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 7f47418f 2019-12-20 stsp if (err)
2582 7f47418f 2019-12-20 stsp return err;
2583 7f47418f 2019-12-20 stsp }
2584 07ed472d 2019-07-12 stsp
2585 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2586 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2587 07ed472d 2019-07-12 stsp if (err)
2588 07ed472d 2019-07-12 stsp goto done;
2589 07ed472d 2019-07-12 stsp
2590 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2591 07ed472d 2019-07-12 stsp if (err)
2592 07ed472d 2019-07-12 stsp goto done;
2593 07ed472d 2019-07-12 stsp
2594 07ed472d 2019-07-12 stsp if (entry_name &&
2595 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 07ed472d 2019-07-12 stsp goto done;
2598 07ed472d 2019-07-12 stsp }
2599 07ed472d 2019-07-12 stsp
2600 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2601 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2602 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2603 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2604 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2605 07ed472d 2019-07-12 stsp arg.repo = repo;
2606 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2607 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2608 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2609 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2610 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2612 07ed472d 2019-07-12 stsp done:
2613 07ed472d 2019-07-12 stsp if (tree)
2614 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2615 07ed472d 2019-07-12 stsp if (commit)
2616 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2617 6ced4176 2019-07-12 stsp return err;
2618 6ced4176 2019-07-12 stsp }
2619 6ced4176 2019-07-12 stsp
2620 9d31a1d8 2018-03-11 stsp const struct got_error *
2621 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2622 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2623 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2625 9d31a1d8 2018-03-11 stsp {
2626 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2628 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2629 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2630 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2631 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2632 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2633 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2634 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2635 f2ea84fa 2019-07-27 stsp int entry_type;
2636 f2ea84fa 2019-07-27 stsp char *relpath;
2637 f2ea84fa 2019-07-27 stsp char *entry_name;
2638 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2639 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2640 9d31a1d8 2018-03-11 stsp
2641 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2642 f2ea84fa 2019-07-27 stsp
2643 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2644 9d31a1d8 2018-03-11 stsp if (err)
2645 9d31a1d8 2018-03-11 stsp return err;
2646 a44927cc 2022-04-07 stsp
2647 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2648 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2649 a44927cc 2022-04-07 stsp if (err)
2650 a44927cc 2022-04-07 stsp goto done;
2651 93a30277 2018-12-24 stsp
2652 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2653 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2654 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2655 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2656 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2657 f2ea84fa 2019-07-27 stsp goto done;
2658 f2ea84fa 2019-07-27 stsp }
2659 6ced4176 2019-07-12 stsp
2660 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2661 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2662 a44927cc 2022-04-07 stsp worktree, repo);
2663 f2ea84fa 2019-07-27 stsp if (err) {
2664 f2ea84fa 2019-07-27 stsp free(tpd);
2665 6ced4176 2019-07-12 stsp goto done;
2666 6ced4176 2019-07-12 stsp }
2667 f2ea84fa 2019-07-27 stsp
2668 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2669 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2670 f2ea84fa 2019-07-27 stsp if (err) {
2671 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2672 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2673 f2ea84fa 2019-07-27 stsp free(tpd);
2674 f2ea84fa 2019-07-27 stsp goto done;
2675 f2ea84fa 2019-07-27 stsp }
2676 f2ea84fa 2019-07-27 stsp } else
2677 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2678 f2ea84fa 2019-07-27 stsp
2679 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2680 6ced4176 2019-07-12 stsp }
2681 6ced4176 2019-07-12 stsp
2682 51514078 2018-12-25 stsp /*
2683 51514078 2018-12-25 stsp * Read the file index.
2684 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2685 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2686 51514078 2018-12-25 stsp */
2687 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2688 8da9e5f4 2019-01-12 stsp if (err)
2689 c4cdcb68 2019-04-03 stsp goto done;
2690 c4cdcb68 2019-04-03 stsp
2691 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2692 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2693 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2694 f2ea84fa 2019-07-27 stsp
2695 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2696 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2697 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2698 f2ea84fa 2019-07-27 stsp if (err)
2699 f2ea84fa 2019-07-27 stsp break;
2700 f2ea84fa 2019-07-27 stsp
2701 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2702 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2703 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2704 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2705 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2706 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2707 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2708 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2709 f2ea84fa 2019-07-27 stsp if (err)
2710 f2ea84fa 2019-07-27 stsp break;
2711 f2ea84fa 2019-07-27 stsp
2712 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2713 711d9cd9 2019-07-12 stsp }
2714 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2715 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2716 af12c6b9 2019-06-04 stsp err = sync_err;
2717 9d31a1d8 2018-03-11 stsp done:
2718 9c6338c4 2019-06-02 stsp free(fileindex_path);
2719 edfa7d7f 2018-09-11 stsp if (tree)
2720 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2721 9d31a1d8 2018-03-11 stsp if (commit)
2722 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2723 5ade8233 2019-07-12 stsp if (fileindex)
2724 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2725 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2726 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2727 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2728 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2729 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2730 f2ea84fa 2019-07-27 stsp free(tpd);
2731 f2ea84fa 2019-07-27 stsp }
2732 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2733 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2734 9d31a1d8 2018-03-11 stsp err = unlockerr;
2735 f8d1f275 2019-02-04 stsp return err;
2736 f8d1f275 2019-02-04 stsp }
2737 f8d1f275 2019-02-04 stsp
2738 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2739 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2740 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2741 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2742 234035bc 2019-06-01 stsp void *progress_arg;
2743 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2744 234035bc 2019-06-01 stsp void *cancel_arg;
2745 f69721c3 2019-10-21 stsp const char *label_orig;
2746 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2747 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2748 234035bc 2019-06-01 stsp };
2749 234035bc 2019-06-01 stsp
2750 234035bc 2019-06-01 stsp static const struct got_error *
2751 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2752 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2753 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2754 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2755 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2756 234035bc 2019-06-01 stsp {
2757 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2758 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2759 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2760 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2761 234035bc 2019-06-01 stsp struct stat sb;
2762 234035bc 2019-06-01 stsp unsigned char status;
2763 234035bc 2019-06-01 stsp int local_changes_subsumed;
2764 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2765 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2766 234035bc 2019-06-01 stsp
2767 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2768 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2769 d6c87207 2019-08-02 stsp strlen(path2));
2770 1ee397ad 2019-07-12 stsp if (ie == NULL)
2771 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2772 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2773 234035bc 2019-06-01 stsp
2774 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2775 234035bc 2019-06-01 stsp path2) == -1)
2776 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2777 234035bc 2019-06-01 stsp
2778 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2779 7f91a133 2019-12-13 stsp repo);
2780 234035bc 2019-06-01 stsp if (err)
2781 234035bc 2019-06-01 stsp goto done;
2782 234035bc 2019-06-01 stsp
2783 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2784 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2785 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2786 234035bc 2019-06-01 stsp goto done;
2787 234035bc 2019-06-01 stsp }
2788 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2789 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2790 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2791 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2792 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2793 234035bc 2019-06-01 stsp goto done;
2794 234035bc 2019-06-01 stsp }
2795 234035bc 2019-06-01 stsp
2796 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2797 36bf999c 2020-07-23 stsp char *link_target2;
2798 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2799 36bf999c 2020-07-23 stsp if (err)
2800 36bf999c 2020-07-23 stsp goto done;
2801 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2802 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2803 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2804 36bf999c 2020-07-23 stsp free(link_target2);
2805 af57b12a 2020-07-23 stsp } else {
2806 1af628f4 2021-06-03 stsp int fd;
2807 1af628f4 2021-06-03 stsp
2808 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2809 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2810 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2811 1af628f4 2021-06-03 stsp goto done;
2812 1af628f4 2021-06-03 stsp }
2813 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2814 1af628f4 2021-06-03 stsp f_orig, blob1);
2815 1af628f4 2021-06-03 stsp if (err)
2816 1af628f4 2021-06-03 stsp goto done;
2817 1af628f4 2021-06-03 stsp
2818 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2819 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2820 1af628f4 2021-06-03 stsp goto done;
2821 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2822 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2823 1af628f4 2021-06-03 stsp if (err)
2824 1af628f4 2021-06-03 stsp goto done;
2825 1af628f4 2021-06-03 stsp
2826 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2827 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2828 1af628f4 2021-06-03 stsp if (fd == -1) {
2829 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2830 1af628f4 2021-06-03 stsp ondisk_path);
2831 1af628f4 2021-06-03 stsp goto done;
2832 1af628f4 2021-06-03 stsp }
2833 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2834 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2835 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2836 1af628f4 2021-06-03 stsp ondisk_path);
2837 1af628f4 2021-06-03 stsp close(fd);
2838 1af628f4 2021-06-03 stsp goto done;
2839 1af628f4 2021-06-03 stsp }
2840 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2841 1af628f4 2021-06-03 stsp if (err)
2842 1af628f4 2021-06-03 stsp goto done;
2843 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2844 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2845 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2846 1af628f4 2021-06-03 stsp goto done;
2847 1af628f4 2021-06-03 stsp }
2848 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2849 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2850 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2851 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2852 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2853 af57b12a 2020-07-23 stsp }
2854 234035bc 2019-06-01 stsp } else if (blob1) {
2855 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2856 d6c87207 2019-08-02 stsp strlen(path1));
2857 1ee397ad 2019-07-12 stsp if (ie == NULL)
2858 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2859 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2860 2b92fad7 2019-06-02 stsp
2861 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2862 2b92fad7 2019-06-02 stsp path1) == -1)
2863 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2864 2b92fad7 2019-06-02 stsp
2865 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2866 7f91a133 2019-12-13 stsp repo);
2867 2b92fad7 2019-06-02 stsp if (err)
2868 2b92fad7 2019-06-02 stsp goto done;
2869 2b92fad7 2019-06-02 stsp
2870 2b92fad7 2019-06-02 stsp switch (status) {
2871 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2872 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2873 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2874 1ee397ad 2019-07-12 stsp if (err)
2875 1ee397ad 2019-07-12 stsp goto done;
2876 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2877 2b92fad7 2019-06-02 stsp if (err)
2878 2b92fad7 2019-06-02 stsp goto done;
2879 2b92fad7 2019-06-02 stsp if (ie)
2880 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2881 2b92fad7 2019-06-02 stsp break;
2882 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2883 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2884 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2885 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2886 1ee397ad 2019-07-12 stsp if (err)
2887 1ee397ad 2019-07-12 stsp goto done;
2888 2b92fad7 2019-06-02 stsp if (ie)
2889 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2890 2b92fad7 2019-06-02 stsp break;
2891 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2892 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2893 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2894 72840534 2022-01-19 stsp off_t blob1_size;
2895 0a22ca1a 2020-09-23 stsp /*
2896 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2897 0a22ca1a 2020-09-23 stsp * exists in the repository.
2898 0a22ca1a 2020-09-23 stsp */
2899 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2900 72840534 2022-01-19 stsp &blob1_size, path1);
2901 0a22ca1a 2020-09-23 stsp if (err)
2902 0a22ca1a 2020-09-23 stsp goto done;
2903 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2904 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2905 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2906 0a22ca1a 2020-09-23 stsp if (err)
2907 0a22ca1a 2020-09-23 stsp goto done;
2908 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2909 0a22ca1a 2020-09-23 stsp path1);
2910 0a22ca1a 2020-09-23 stsp if (err)
2911 0a22ca1a 2020-09-23 stsp goto done;
2912 0a22ca1a 2020-09-23 stsp if (ie)
2913 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2914 0a22ca1a 2020-09-23 stsp ie);
2915 0a22ca1a 2020-09-23 stsp } else {
2916 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2917 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2918 0a22ca1a 2020-09-23 stsp }
2919 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2920 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2921 0a22ca1a 2020-09-23 stsp free(id);
2922 0a22ca1a 2020-09-23 stsp if (err)
2923 0a22ca1a 2020-09-23 stsp goto done;
2924 0a22ca1a 2020-09-23 stsp break;
2925 0a22ca1a 2020-09-23 stsp }
2926 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2927 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2928 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2929 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2930 1ee397ad 2019-07-12 stsp if (err)
2931 1ee397ad 2019-07-12 stsp goto done;
2932 2b92fad7 2019-06-02 stsp break;
2933 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2934 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2935 1ee397ad 2019-07-12 stsp if (err)
2936 1ee397ad 2019-07-12 stsp goto done;
2937 2b92fad7 2019-06-02 stsp break;
2938 2b92fad7 2019-06-02 stsp default:
2939 2b92fad7 2019-06-02 stsp break;
2940 2b92fad7 2019-06-02 stsp }
2941 234035bc 2019-06-01 stsp } else if (blob2) {
2942 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2943 234035bc 2019-06-01 stsp path2) == -1)
2944 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2945 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2946 d6c87207 2019-08-02 stsp strlen(path2));
2947 234035bc 2019-06-01 stsp if (ie) {
2948 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2949 7f91a133 2019-12-13 stsp -1, NULL, repo);
2950 234035bc 2019-06-01 stsp if (err)
2951 234035bc 2019-06-01 stsp goto done;
2952 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2953 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2954 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2955 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2956 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2957 1ee397ad 2019-07-12 stsp status, path2);
2958 234035bc 2019-06-01 stsp goto done;
2959 234035bc 2019-06-01 stsp }
2960 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2961 36bf999c 2020-07-23 stsp char *link_target2;
2962 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2963 36bf999c 2020-07-23 stsp blob2);
2964 36bf999c 2020-07-23 stsp if (err)
2965 36bf999c 2020-07-23 stsp goto done;
2966 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2967 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2968 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2969 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2970 36bf999c 2020-07-23 stsp free(link_target2);
2971 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2972 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2973 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2974 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2975 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2976 526a746f 2020-07-23 stsp a->progress_arg);
2977 dfe9fba0 2020-07-23 stsp } else {
2978 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2979 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2980 526a746f 2020-07-23 stsp }
2981 dfe9fba0 2020-07-23 stsp if (err)
2982 dfe9fba0 2020-07-23 stsp goto done;
2983 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2984 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2985 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2986 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2987 234035bc 2019-06-01 stsp if (err)
2988 234035bc 2019-06-01 stsp goto done;
2989 234035bc 2019-06-01 stsp }
2990 234035bc 2019-06-01 stsp } else {
2991 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2992 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2993 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2994 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2995 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2996 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
2997 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
2998 2e1fa222 2020-07-23 stsp } else {
2999 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3000 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3001 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3002 2e1fa222 2020-07-23 stsp }
3003 234035bc 2019-06-01 stsp if (err)
3004 234035bc 2019-06-01 stsp goto done;
3005 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3006 234035bc 2019-06-01 stsp if (err)
3007 234035bc 2019-06-01 stsp goto done;
3008 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3009 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3010 3969253a 2020-03-07 stsp if (err) {
3011 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3012 3969253a 2020-03-07 stsp goto done;
3013 3969253a 2020-03-07 stsp }
3014 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3015 2b92fad7 2019-06-02 stsp if (err) {
3016 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3017 2b92fad7 2019-06-02 stsp goto done;
3018 2b92fad7 2019-06-02 stsp }
3019 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3020 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3021 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3022 2e1fa222 2020-07-23 stsp }
3023 234035bc 2019-06-01 stsp }
3024 234035bc 2019-06-01 stsp }
3025 234035bc 2019-06-01 stsp done:
3026 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3027 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3028 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3029 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3030 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3031 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3032 1af628f4 2021-06-03 stsp free(id_str);
3033 54d5be07 2021-06-03 stsp free(label_deriv2);
3034 234035bc 2019-06-01 stsp free(ondisk_path);
3035 234035bc 2019-06-01 stsp return err;
3036 234035bc 2019-06-01 stsp }
3037 234035bc 2019-06-01 stsp
3038 69de9dd4 2021-09-03 stsp static const struct got_error *
3039 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3040 69de9dd4 2021-09-03 stsp {
3041 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3042 69de9dd4 2021-09-03 stsp
3043 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3044 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3045 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3046 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3047 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3048 69de9dd4 2021-09-03 stsp
3049 69de9dd4 2021-09-03 stsp return NULL;
3050 69de9dd4 2021-09-03 stsp }
3051 69de9dd4 2021-09-03 stsp
3052 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3053 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3054 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3055 234035bc 2019-06-01 stsp struct got_repository *repo;
3056 234035bc 2019-06-01 stsp };
3057 234035bc 2019-06-01 stsp
3058 234035bc 2019-06-01 stsp static const struct got_error *
3059 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3060 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3061 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3062 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3063 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3064 234035bc 2019-06-01 stsp {
3065 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3066 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3067 234035bc 2019-06-01 stsp unsigned char status;
3068 234035bc 2019-06-01 stsp struct stat sb;
3069 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3070 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3071 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3072 234035bc 2019-06-01 stsp char *ondisk_path;
3073 234035bc 2019-06-01 stsp
3074 69de9dd4 2021-09-03 stsp if (id == NULL)
3075 69de9dd4 2021-09-03 stsp return NULL;
3076 234035bc 2019-06-01 stsp
3077 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3078 69de9dd4 2021-09-03 stsp if (ie == NULL)
3079 69de9dd4 2021-09-03 stsp return NULL;
3080 69de9dd4 2021-09-03 stsp
3081 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3082 234035bc 2019-06-01 stsp == -1)
3083 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3084 234035bc 2019-06-01 stsp
3085 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3086 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3087 5546d466 2021-09-02 stsp free(ondisk_path);
3088 234035bc 2019-06-01 stsp if (err)
3089 234035bc 2019-06-01 stsp return err;
3090 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3091 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3092 234035bc 2019-06-01 stsp
3093 234035bc 2019-06-01 stsp return NULL;
3094 234035bc 2019-06-01 stsp }
3095 234035bc 2019-06-01 stsp
3096 818c7501 2019-07-11 stsp static const struct got_error *
3097 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3098 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3099 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3100 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3101 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3102 234035bc 2019-06-01 stsp {
3103 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3104 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3105 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3106 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3107 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3108 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3109 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3110 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3111 234035bc 2019-06-01 stsp
3112 03415a1a 2019-06-02 stsp if (commit_id1) {
3113 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3114 a44927cc 2022-04-07 stsp if (err)
3115 a44927cc 2022-04-07 stsp goto done;
3116 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3117 03415a1a 2019-06-02 stsp worktree->path_prefix);
3118 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3119 03415a1a 2019-06-02 stsp goto done;
3120 69d57f3d 2020-07-31 stsp }
3121 69d57f3d 2020-07-31 stsp if (tree_id1) {
3122 69d57f3d 2020-07-31 stsp char *id_str;
3123 03415a1a 2019-06-02 stsp
3124 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3125 03415a1a 2019-06-02 stsp if (err)
3126 03415a1a 2019-06-02 stsp goto done;
3127 f69721c3 2019-10-21 stsp
3128 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3129 f69721c3 2019-10-21 stsp if (err)
3130 f69721c3 2019-10-21 stsp goto done;
3131 f69721c3 2019-10-21 stsp
3132 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3133 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3134 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3135 f69721c3 2019-10-21 stsp free(id_str);
3136 f69721c3 2019-10-21 stsp goto done;
3137 f69721c3 2019-10-21 stsp }
3138 f69721c3 2019-10-21 stsp free(id_str);
3139 b72706c3 2022-06-01 stsp
3140 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3141 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3142 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3143 b72706c3 2022-06-01 stsp goto done;
3144 b72706c3 2022-06-01 stsp }
3145 03415a1a 2019-06-02 stsp }
3146 234035bc 2019-06-01 stsp
3147 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3148 a44927cc 2022-04-07 stsp if (err)
3149 a44927cc 2022-04-07 stsp goto done;
3150 a44927cc 2022-04-07 stsp
3151 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3152 234035bc 2019-06-01 stsp worktree->path_prefix);
3153 234035bc 2019-06-01 stsp if (err)
3154 234035bc 2019-06-01 stsp goto done;
3155 234035bc 2019-06-01 stsp
3156 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3157 234035bc 2019-06-01 stsp if (err)
3158 234035bc 2019-06-01 stsp goto done;
3159 234035bc 2019-06-01 stsp
3160 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3161 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3162 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3163 b72706c3 2022-06-01 stsp goto done;
3164 b72706c3 2022-06-01 stsp }
3165 b72706c3 2022-06-01 stsp
3166 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3167 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3168 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3169 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3170 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3171 69de9dd4 2021-09-03 stsp if (err)
3172 69de9dd4 2021-09-03 stsp goto done;
3173 69de9dd4 2021-09-03 stsp
3174 234035bc 2019-06-01 stsp arg.worktree = worktree;
3175 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3176 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3177 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3178 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3179 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3180 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3181 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3182 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3183 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3184 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3185 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3186 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3187 af12c6b9 2019-06-04 stsp err = sync_err;
3188 234035bc 2019-06-01 stsp done:
3189 a44927cc 2022-04-07 stsp if (commit1)
3190 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3191 a44927cc 2022-04-07 stsp if (commit2)
3192 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3193 234035bc 2019-06-01 stsp if (tree1)
3194 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3195 234035bc 2019-06-01 stsp if (tree2)
3196 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3197 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3198 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3199 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3200 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3201 f69721c3 2019-10-21 stsp free(label_orig);
3202 818c7501 2019-07-11 stsp return err;
3203 818c7501 2019-07-11 stsp }
3204 234035bc 2019-06-01 stsp
3205 818c7501 2019-07-11 stsp const struct got_error *
3206 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3207 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3208 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3209 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3210 818c7501 2019-07-11 stsp {
3211 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3212 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3213 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3214 818c7501 2019-07-11 stsp
3215 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3216 818c7501 2019-07-11 stsp if (err)
3217 818c7501 2019-07-11 stsp return err;
3218 818c7501 2019-07-11 stsp
3219 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3220 818c7501 2019-07-11 stsp if (err)
3221 818c7501 2019-07-11 stsp goto done;
3222 818c7501 2019-07-11 stsp
3223 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3224 69de9dd4 2021-09-03 stsp worktree);
3225 818c7501 2019-07-11 stsp if (err)
3226 818c7501 2019-07-11 stsp goto done;
3227 818c7501 2019-07-11 stsp
3228 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3229 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3230 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3231 818c7501 2019-07-11 stsp done:
3232 818c7501 2019-07-11 stsp if (fileindex)
3233 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3234 818c7501 2019-07-11 stsp free(fileindex_path);
3235 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3236 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3237 234035bc 2019-06-01 stsp err = unlockerr;
3238 234035bc 2019-06-01 stsp return err;
3239 234035bc 2019-06-01 stsp }
3240 234035bc 2019-06-01 stsp
3241 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3242 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3243 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3244 927df6b7 2019-02-10 stsp const char *status_path;
3245 927df6b7 2019-02-10 stsp size_t status_path_len;
3246 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3247 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3248 f8d1f275 2019-02-04 stsp void *status_arg;
3249 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3250 f8d1f275 2019-02-04 stsp void *cancel_arg;
3251 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3252 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3253 f2a9dc41 2019-12-13 tracey int report_unchanged;
3254 3143d852 2020-06-25 stsp int no_ignores;
3255 f8d1f275 2019-02-04 stsp };
3256 88d0e355 2019-08-03 stsp
3257 f8d1f275 2019-02-04 stsp static const struct got_error *
3258 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3259 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3260 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3261 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3262 927df6b7 2019-02-10 stsp {
3263 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3264 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3265 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3266 927df6b7 2019-02-10 stsp struct stat sb;
3267 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3268 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3269 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3270 927df6b7 2019-02-10 stsp
3271 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3272 98eaaa12 2019-08-03 stsp if (err)
3273 98eaaa12 2019-08-03 stsp return err;
3274 98eaaa12 2019-08-03 stsp
3275 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3276 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3277 98eaaa12 2019-08-03 stsp return NULL;
3278 98eaaa12 2019-08-03 stsp
3279 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3280 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3281 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3282 98eaaa12 2019-08-03 stsp }
3283 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3284 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3285 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3286 927df6b7 2019-02-10 stsp }
3287 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3288 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3289 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3290 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3291 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3292 98eaaa12 2019-08-03 stsp }
3293 98eaaa12 2019-08-03 stsp
3294 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3295 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3296 927df6b7 2019-02-10 stsp }
3297 927df6b7 2019-02-10 stsp
3298 927df6b7 2019-02-10 stsp static const struct got_error *
3299 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3300 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3301 f8d1f275 2019-02-04 stsp {
3302 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3303 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3304 f8d1f275 2019-02-04 stsp char *abspath;
3305 f8d1f275 2019-02-04 stsp
3306 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3307 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3308 0584f854 2019-04-06 stsp
3309 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3310 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3311 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3312 927df6b7 2019-02-10 stsp return NULL;
3313 927df6b7 2019-02-10 stsp
3314 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3315 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3316 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3317 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3318 f8d1f275 2019-02-04 stsp } else {
3319 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3320 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3321 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3322 f8d1f275 2019-02-04 stsp }
3323 f8d1f275 2019-02-04 stsp
3324 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3325 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3326 f8d1f275 2019-02-04 stsp free(abspath);
3327 9d31a1d8 2018-03-11 stsp return err;
3328 9d31a1d8 2018-03-11 stsp }
3329 f8d1f275 2019-02-04 stsp
3330 f8d1f275 2019-02-04 stsp static const struct got_error *
3331 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3332 f8d1f275 2019-02-04 stsp {
3333 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3334 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3335 2ec1f75b 2019-03-26 stsp unsigned char status;
3336 927df6b7 2019-02-10 stsp
3337 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3338 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3339 0584f854 2019-04-06 stsp
3340 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3341 927df6b7 2019-02-10 stsp return NULL;
3342 927df6b7 2019-02-10 stsp
3343 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3344 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3345 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3346 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3347 2ec1f75b 2019-03-26 stsp else
3348 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3349 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3350 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3351 6841da00 2019-08-08 stsp }
3352 6841da00 2019-08-08 stsp
3353 6841da00 2019-08-08 stsp void
3354 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3355 6841da00 2019-08-08 stsp {
3356 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3357 6841da00 2019-08-08 stsp
3358 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3359 6841da00 2019-08-08 stsp free((char *)pe->path);
3360 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3361 6841da00 2019-08-08 stsp }
3362 6841da00 2019-08-08 stsp
3363 6841da00 2019-08-08 stsp void
3364 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3365 6841da00 2019-08-08 stsp {
3366 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3367 6841da00 2019-08-08 stsp
3368 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3369 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3370 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3371 6841da00 2019-08-08 stsp free((char *)pe->path);
3372 6841da00 2019-08-08 stsp }
3373 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3374 6841da00 2019-08-08 stsp }
3375 6841da00 2019-08-08 stsp
3376 6841da00 2019-08-08 stsp static const struct got_error *
3377 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3378 6841da00 2019-08-08 stsp {
3379 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3380 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3381 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3382 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3383 6841da00 2019-08-08 stsp size_t linesize = 0;
3384 6841da00 2019-08-08 stsp ssize_t linelen;
3385 6841da00 2019-08-08 stsp
3386 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3387 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3388 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3389 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3390 6841da00 2019-08-08 stsp
3391 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3392 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3393 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3394 bd8de430 2019-10-04 stsp
3395 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3396 bd8de430 2019-10-04 stsp if (line[0] == '#')
3397 bd8de430 2019-10-04 stsp continue;
3398 bd8de430 2019-10-04 stsp
3399 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3400 bd8de430 2019-10-04 stsp if (line[0] == '!')
3401 bd8de430 2019-10-04 stsp continue;
3402 bd8de430 2019-10-04 stsp
3403 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3404 6841da00 2019-08-08 stsp line) == -1) {
3405 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3406 6841da00 2019-08-08 stsp goto done;
3407 6841da00 2019-08-08 stsp }
3408 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3409 6841da00 2019-08-08 stsp if (err)
3410 6841da00 2019-08-08 stsp goto done;
3411 6841da00 2019-08-08 stsp }
3412 6841da00 2019-08-08 stsp if (ferror(f)) {
3413 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3414 6841da00 2019-08-08 stsp goto done;
3415 6841da00 2019-08-08 stsp }
3416 6841da00 2019-08-08 stsp
3417 6841da00 2019-08-08 stsp dirpath = strdup(path);
3418 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3419 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3420 6841da00 2019-08-08 stsp goto done;
3421 6841da00 2019-08-08 stsp }
3422 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3423 6841da00 2019-08-08 stsp done:
3424 6841da00 2019-08-08 stsp free(line);
3425 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3426 6841da00 2019-08-08 stsp free(dirpath);
3427 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3428 6841da00 2019-08-08 stsp }
3429 6841da00 2019-08-08 stsp return err;
3430 6841da00 2019-08-08 stsp }
3431 6841da00 2019-08-08 stsp
3432 6841da00 2019-08-08 stsp int
3433 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3434 6841da00 2019-08-08 stsp {
3435 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3436 bd8de430 2019-10-04 stsp
3437 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3438 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3439 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3440 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3441 bd8de430 2019-10-04 stsp
3442 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3443 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3444 6841da00 2019-08-08 stsp
3445 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3446 bd8de430 2019-10-04 stsp continue;
3447 bd8de430 2019-10-04 stsp pattern += 3;
3448 bd8de430 2019-10-04 stsp p = path;
3449 bd8de430 2019-10-04 stsp while (*p) {
3450 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3451 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3452 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3453 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3454 bd8de430 2019-10-04 stsp p++;
3455 bd8de430 2019-10-04 stsp while (*p == '/')
3456 bd8de430 2019-10-04 stsp p++;
3457 bd8de430 2019-10-04 stsp continue;
3458 bd8de430 2019-10-04 stsp }
3459 bd8de430 2019-10-04 stsp return 1;
3460 bd8de430 2019-10-04 stsp }
3461 bd8de430 2019-10-04 stsp }
3462 bd8de430 2019-10-04 stsp }
3463 bd8de430 2019-10-04 stsp
3464 6841da00 2019-08-08 stsp /*
3465 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3466 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3467 6841da00 2019-08-08 stsp * ignores backwards.
3468 6841da00 2019-08-08 stsp */
3469 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3470 6841da00 2019-08-08 stsp while (pe) {
3471 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3472 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3473 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3474 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3475 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3476 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3477 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3478 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3479 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3480 6841da00 2019-08-08 stsp continue;
3481 6841da00 2019-08-08 stsp return 1;
3482 6841da00 2019-08-08 stsp }
3483 6841da00 2019-08-08 stsp }
3484 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3485 6841da00 2019-08-08 stsp }
3486 6841da00 2019-08-08 stsp
3487 6841da00 2019-08-08 stsp return 0;
3488 6841da00 2019-08-08 stsp }
3489 6841da00 2019-08-08 stsp
3490 6841da00 2019-08-08 stsp static const struct got_error *
3491 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3492 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3493 6841da00 2019-08-08 stsp {
3494 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3495 6841da00 2019-08-08 stsp char *ignorespath;
3496 886cec17 2019-12-15 stsp int fd = -1;
3497 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3498 6841da00 2019-08-08 stsp
3499 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3500 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3501 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3502 6841da00 2019-08-08 stsp
3503 886cec17 2019-12-15 stsp if (dirfd != -1) {
3504 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3505 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3506 886cec17 2019-12-15 stsp if (fd == -1) {
3507 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3508 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3509 886cec17 2019-12-15 stsp ignorespath);
3510 886cec17 2019-12-15 stsp } else {
3511 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3512 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3513 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3514 886cec17 2019-12-15 stsp ignorespath);
3515 886cec17 2019-12-15 stsp else {
3516 886cec17 2019-12-15 stsp fd = -1;
3517 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3518 886cec17 2019-12-15 stsp }
3519 886cec17 2019-12-15 stsp }
3520 886cec17 2019-12-15 stsp } else {
3521 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3522 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3523 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3524 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3525 886cec17 2019-12-15 stsp ignorespath);
3526 886cec17 2019-12-15 stsp } else
3527 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3528 886cec17 2019-12-15 stsp }
3529 6841da00 2019-08-08 stsp
3530 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3531 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3532 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3533 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3534 6841da00 2019-08-08 stsp free(ignorespath);
3535 6841da00 2019-08-08 stsp return err;
3536 f8d1f275 2019-02-04 stsp }
3537 f8d1f275 2019-02-04 stsp
3538 f8d1f275 2019-02-04 stsp static const struct got_error *
3539 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3540 62da3196 2021-10-01 stsp int dirfd)
3541 f8d1f275 2019-02-04 stsp {
3542 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3543 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3544 f8d1f275 2019-02-04 stsp char *path = NULL;
3545 62da3196 2021-10-01 stsp
3546 62da3196 2021-10-01 stsp if (ignore != NULL)
3547 62da3196 2021-10-01 stsp *ignore = 0;
3548 f8d1f275 2019-02-04 stsp
3549 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3550 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3551 0584f854 2019-04-06 stsp
3552 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3553 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3554 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3555 f8d1f275 2019-02-04 stsp } else {
3556 f8d1f275 2019-02-04 stsp path = de->d_name;
3557 f8d1f275 2019-02-04 stsp }
3558 f8d1f275 2019-02-04 stsp
3559 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3560 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3561 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3562 62da3196 2021-10-01 stsp *ignore = 1;
3563 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3564 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3565 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3566 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3567 f8d1f275 2019-02-04 stsp if (parent_path[0])
3568 f8d1f275 2019-02-04 stsp free(path);
3569 b72f483a 2019-02-05 stsp return err;
3570 f8d1f275 2019-02-04 stsp }
3571 f8d1f275 2019-02-04 stsp
3572 347d1d3e 2019-07-12 stsp static const struct got_error *
3573 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3574 3143d852 2020-06-25 stsp {
3575 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3576 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3577 3143d852 2020-06-25 stsp
3578 3143d852 2020-06-25 stsp if (a->no_ignores)
3579 3143d852 2020-06-25 stsp return NULL;
3580 3143d852 2020-06-25 stsp
3581 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3582 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3583 3143d852 2020-06-25 stsp if (err)
3584 3143d852 2020-06-25 stsp return err;
3585 3143d852 2020-06-25 stsp
3586 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3587 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3588 3143d852 2020-06-25 stsp
3589 3143d852 2020-06-25 stsp return err;
3590 3143d852 2020-06-25 stsp }
3591 3143d852 2020-06-25 stsp
3592 3143d852 2020-06-25 stsp static const struct got_error *
3593 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3594 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3595 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3596 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3597 abb4604f 2019-07-27 stsp {
3598 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3599 abb4604f 2019-07-27 stsp struct stat sb;
3600 ff56836b 2021-07-08 stsp
3601 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3602 abb4604f 2019-07-27 stsp if (ie)
3603 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3604 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3605 abb4604f 2019-07-27 stsp
3606 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3607 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3608 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3609 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3610 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3611 abb4604f 2019-07-27 stsp }
3612 abb4604f 2019-07-27 stsp
3613 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3614 916237f3 2022-02-11 stsp return NULL;
3615 916237f3 2022-02-11 stsp
3616 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3617 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3618 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3619 abb4604f 2019-07-27 stsp
3620 abb4604f 2019-07-27 stsp return NULL;
3621 3143d852 2020-06-25 stsp }
3622 3143d852 2020-06-25 stsp
3623 3143d852 2020-06-25 stsp static const struct got_error *
3624 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3625 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3626 3143d852 2020-06-25 stsp {
3627 3143d852 2020-06-25 stsp const struct got_error *err;
3628 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3629 3143d852 2020-06-25 stsp
3630 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3631 3143d852 2020-06-25 stsp ".cvsignore");
3632 3143d852 2020-06-25 stsp if (err)
3633 3143d852 2020-06-25 stsp return err;
3634 3143d852 2020-06-25 stsp
3635 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3636 3143d852 2020-06-25 stsp ".gitignore");
3637 3143d852 2020-06-25 stsp if (err)
3638 3143d852 2020-06-25 stsp return err;
3639 3143d852 2020-06-25 stsp
3640 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3641 3143d852 2020-06-25 stsp if (err) {
3642 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3643 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3644 3143d852 2020-06-25 stsp return err;
3645 3143d852 2020-06-25 stsp }
3646 3143d852 2020-06-25 stsp for (;;) {
3647 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3648 3143d852 2020-06-25 stsp ".cvsignore");
3649 3143d852 2020-06-25 stsp if (err)
3650 3143d852 2020-06-25 stsp break;
3651 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3652 3143d852 2020-06-25 stsp ".gitignore");
3653 3143d852 2020-06-25 stsp if (err)
3654 3143d852 2020-06-25 stsp break;
3655 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3656 3143d852 2020-06-25 stsp if (err) {
3657 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3658 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3659 3143d852 2020-06-25 stsp break;
3660 3143d852 2020-06-25 stsp }
3661 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3662 ff56836b 2021-07-08 stsp break;
3663 b737c85a 2020-06-26 stsp free(parent_path);
3664 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3665 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3666 3143d852 2020-06-25 stsp }
3667 3143d852 2020-06-25 stsp
3668 b737c85a 2020-06-26 stsp free(parent_path);
3669 b737c85a 2020-06-26 stsp free(next_parent_path);
3670 3143d852 2020-06-25 stsp return err;
3671 abb4604f 2019-07-27 stsp }
3672 abb4604f 2019-07-27 stsp
3673 abb4604f 2019-07-27 stsp static const struct got_error *
3674 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3675 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3676 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3677 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3678 f2a9dc41 2019-12-13 tracey int report_unchanged)
3679 f8d1f275 2019-02-04 stsp {
3680 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3681 6fc93f37 2019-12-13 stsp int fd = -1;
3682 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3683 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3684 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3685 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3686 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3687 3143d852 2020-06-25 stsp
3688 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3689 f8d1f275 2019-02-04 stsp
3690 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3691 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3692 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3693 8dc303cc 2019-07-27 stsp
3694 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3695 a84c0d30 2022-03-12 stsp if (ie) {
3696 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3697 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3698 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3699 a84c0d30 2022-03-12 stsp goto done;
3700 a84c0d30 2022-03-12 stsp }
3701 a84c0d30 2022-03-12 stsp
3702 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3703 6fc93f37 2019-12-13 stsp if (fd == -1) {
3704 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3705 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3706 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3707 ff56836b 2021-07-08 stsp else {
3708 ff56836b 2021-07-08 stsp if (!no_ignores) {
3709 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3710 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3711 ff56836b 2021-07-08 stsp if (err)
3712 ff56836b 2021-07-08 stsp goto done;
3713 ff56836b 2021-07-08 stsp }
3714 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3715 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3716 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3717 ff56836b 2021-07-08 stsp }
3718 abb4604f 2019-07-27 stsp } else {
3719 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3720 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3721 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3722 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3723 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3724 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3725 abb4604f 2019-07-27 stsp arg.status_path = path;
3726 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3727 abb4604f 2019-07-27 stsp arg.repo = repo;
3728 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3729 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3730 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3731 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3732 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3733 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3734 022fae89 2019-12-06 tracey if (!no_ignores) {
3735 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3736 3143d852 2020-06-25 stsp worktree->root_path, path);
3737 3143d852 2020-06-25 stsp if (err)
3738 3143d852 2020-06-25 stsp goto done;
3739 022fae89 2019-12-06 tracey }
3740 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3741 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3742 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3743 927df6b7 2019-02-10 stsp }
3744 3143d852 2020-06-25 stsp done:
3745 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3746 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3747 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3748 927df6b7 2019-02-10 stsp free(ondisk_path);
3749 347d1d3e 2019-07-12 stsp return err;
3750 347d1d3e 2019-07-12 stsp }
3751 347d1d3e 2019-07-12 stsp
3752 347d1d3e 2019-07-12 stsp const struct got_error *
3753 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3754 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3755 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3756 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3757 347d1d3e 2019-07-12 stsp {
3758 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3759 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3760 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3761 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3762 347d1d3e 2019-07-12 stsp
3763 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3764 347d1d3e 2019-07-12 stsp if (err)
3765 347d1d3e 2019-07-12 stsp return err;
3766 347d1d3e 2019-07-12 stsp
3767 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3768 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3769 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3770 f6343036 2021-06-22 stsp no_ignores, 0);
3771 72ea6654 2019-07-27 stsp if (err)
3772 72ea6654 2019-07-27 stsp break;
3773 72ea6654 2019-07-27 stsp }
3774 f8d1f275 2019-02-04 stsp free(fileindex_path);
3775 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3776 f8d1f275 2019-02-04 stsp return err;
3777 f8d1f275 2019-02-04 stsp }
3778 6c7ab921 2019-03-18 stsp
3779 6c7ab921 2019-03-18 stsp const struct got_error *
3780 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3781 6c7ab921 2019-03-18 stsp const char *arg)
3782 6c7ab921 2019-03-18 stsp {
3783 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3784 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3785 6c7ab921 2019-03-18 stsp size_t len;
3786 00bb5ea0 2020-07-23 stsp struct stat sb;
3787 01740607 2020-11-04 stsp char *abspath = NULL;
3788 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3789 6c7ab921 2019-03-18 stsp
3790 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3791 6c7ab921 2019-03-18 stsp
3792 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3793 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3794 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3795 00bb5ea0 2020-07-23 stsp
3796 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3797 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3798 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3799 00bb5ea0 2020-07-23 stsp goto done;
3800 00bb5ea0 2020-07-23 stsp }
3801 727173c3 2020-11-06 stsp sb.st_mode = 0;
3802 00bb5ea0 2020-07-23 stsp }
3803 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3804 00bb5ea0 2020-07-23 stsp /*
3805 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3806 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3807 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3808 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3809 00bb5ea0 2020-07-23 stsp */
3810 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3811 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3812 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3813 00bb5ea0 2020-07-23 stsp goto done;
3814 00bb5ea0 2020-07-23 stsp }
3815 00bb5ea0 2020-07-23 stsp
3816 00bb5ea0 2020-07-23 stsp }
3817 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3818 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3819 00bb5ea0 2020-07-23 stsp if (err)
3820 d0710d08 2019-07-22 stsp goto done;
3821 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3822 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3823 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3824 00bb5ea0 2020-07-23 stsp goto done;
3825 00bb5ea0 2020-07-23 stsp }
3826 00bb5ea0 2020-07-23 stsp } else {
3827 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3828 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3829 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3830 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3831 00bb5ea0 2020-07-23 stsp goto done;
3832 00bb5ea0 2020-07-23 stsp }
3833 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3834 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3835 01740607 2020-11-04 stsp goto done;
3836 01740607 2020-11-04 stsp }
3837 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3838 01740607 2020-11-04 stsp sizeof(canonpath));
3839 01740607 2020-11-04 stsp if (err)
3840 00bb5ea0 2020-07-23 stsp goto done;
3841 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3842 01740607 2020-11-04 stsp if (resolved == NULL) {
3843 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3844 01740607 2020-11-04 stsp goto done;
3845 00bb5ea0 2020-07-23 stsp }
3846 d0710d08 2019-07-22 stsp }
3847 d0710d08 2019-07-22 stsp }
3848 6c7ab921 2019-03-18 stsp
3849 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3850 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3851 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3852 6c7ab921 2019-03-18 stsp goto done;
3853 6c7ab921 2019-03-18 stsp }
3854 6c7ab921 2019-03-18 stsp
3855 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3856 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3857 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3858 f6d88e1a 2019-05-29 stsp if (err)
3859 f6d88e1a 2019-05-29 stsp goto done;
3860 f6d88e1a 2019-05-29 stsp } else {
3861 f6d88e1a 2019-05-29 stsp path = strdup("");
3862 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3863 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3864 f6d88e1a 2019-05-29 stsp goto done;
3865 f6d88e1a 2019-05-29 stsp }
3866 6c7ab921 2019-03-18 stsp }
3867 6c7ab921 2019-03-18 stsp
3868 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3869 6c7ab921 2019-03-18 stsp len = strlen(path);
3870 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3871 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3872 6c7ab921 2019-03-18 stsp len--;
3873 6c7ab921 2019-03-18 stsp }
3874 6c7ab921 2019-03-18 stsp done:
3875 01740607 2020-11-04 stsp free(abspath);
3876 6c7ab921 2019-03-18 stsp free(resolved);
3877 d0710d08 2019-07-22 stsp free(cwd);
3878 6c7ab921 2019-03-18 stsp if (err == NULL)
3879 6c7ab921 2019-03-18 stsp *wt_path = path;
3880 6c7ab921 2019-03-18 stsp else
3881 6c7ab921 2019-03-18 stsp free(path);
3882 d00136be 2019-03-26 stsp return err;
3883 d00136be 2019-03-26 stsp }
3884 d00136be 2019-03-26 stsp
3885 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3886 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3887 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3888 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3889 4e68cba3 2019-11-23 stsp void *progress_arg;
3890 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3891 4e68cba3 2019-11-23 stsp };
3892 4e68cba3 2019-11-23 stsp
3893 4e68cba3 2019-11-23 stsp static const struct got_error *
3894 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3895 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3896 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3897 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3898 07f5b47a 2019-06-02 stsp {
3899 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3900 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3901 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3902 6d022e97 2019-08-04 stsp struct stat sb;
3903 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3904 07f5b47a 2019-06-02 stsp
3905 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3906 dbb83fbd 2019-12-12 stsp relpath) == -1)
3907 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3908 dbb83fbd 2019-12-12 stsp
3909 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3910 6d022e97 2019-08-04 stsp if (ie) {
3911 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3912 12463d8b 2019-12-13 stsp de_name, a->repo);
3913 6d022e97 2019-08-04 stsp if (err)
3914 dbb83fbd 2019-12-12 stsp goto done;
3915 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3916 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3917 dbb83fbd 2019-12-12 stsp goto done;
3918 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3919 dbb83fbd 2019-12-12 stsp if (err)
3920 dbb83fbd 2019-12-12 stsp goto done;
3921 6d022e97 2019-08-04 stsp }
3922 07f5b47a 2019-06-02 stsp
3923 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3924 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3925 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3926 9b4603c0 2022-01-31 stsp else
3927 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3928 dbb83fbd 2019-12-12 stsp goto done;
3929 4e68cba3 2019-11-23 stsp }
3930 07f5b47a 2019-06-02 stsp
3931 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3932 4e68cba3 2019-11-23 stsp if (err)
3933 4e68cba3 2019-11-23 stsp goto done;
3934 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3935 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3936 3969253a 2020-03-07 stsp if (err) {
3937 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3938 3969253a 2020-03-07 stsp goto done;
3939 3969253a 2020-03-07 stsp }
3940 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3941 07f5b47a 2019-06-02 stsp if (err) {
3942 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3943 4e68cba3 2019-11-23 stsp goto done;
3944 07f5b47a 2019-06-02 stsp }
3945 4e68cba3 2019-11-23 stsp done:
3946 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3947 dbb83fbd 2019-12-12 stsp if (err)
3948 dbb83fbd 2019-12-12 stsp return err;
3949 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3950 dbb83fbd 2019-12-12 stsp return NULL;
3951 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3952 07f5b47a 2019-06-02 stsp }
3953 07f5b47a 2019-06-02 stsp
3954 d00136be 2019-03-26 stsp const struct got_error *
3955 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3956 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3957 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3958 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3959 d00136be 2019-03-26 stsp {
3960 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3961 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3962 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3963 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3964 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3965 d00136be 2019-03-26 stsp
3966 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3967 d00136be 2019-03-26 stsp if (err)
3968 d00136be 2019-03-26 stsp return err;
3969 d00136be 2019-03-26 stsp
3970 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3971 d00136be 2019-03-26 stsp if (err)
3972 d00136be 2019-03-26 stsp goto done;
3973 d00136be 2019-03-26 stsp
3974 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3975 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3976 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3977 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3978 4e68cba3 2019-11-23 stsp saa.repo = repo;
3979 4e68cba3 2019-11-23 stsp
3980 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3981 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3982 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3983 1dd54920 2019-05-11 stsp if (err)
3984 af12c6b9 2019-06-04 stsp break;
3985 1dd54920 2019-05-11 stsp }
3986 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3987 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3988 af12c6b9 2019-06-04 stsp err = sync_err;
3989 d00136be 2019-03-26 stsp done:
3990 fb399478 2019-07-12 stsp free(fileindex_path);
3991 d00136be 2019-03-26 stsp if (fileindex)
3992 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3993 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3994 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3995 d00136be 2019-03-26 stsp err = unlockerr;
3996 6c7ab921 2019-03-18 stsp return err;
3997 6c7ab921 2019-03-18 stsp }
3998 17ed4618 2019-06-02 stsp
3999 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4000 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4001 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4002 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4003 f2a9dc41 2019-12-13 tracey void *progress_arg;
4004 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4005 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4006 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4007 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4008 766841c2 2020-08-13 stsp const char *status_codes;
4009 f2a9dc41 2019-12-13 tracey };
4010 f2a9dc41 2019-12-13 tracey
4011 f2a9dc41 2019-12-13 tracey static const struct got_error *
4012 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4013 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4014 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4015 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4016 17ed4618 2019-06-02 stsp {
4017 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4018 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4019 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4020 17ed4618 2019-06-02 stsp struct stat sb;
4021 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4022 4e12cd97 2022-01-25 stsp
4023 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4024 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4025 4e12cd97 2022-01-25 stsp return NULL;
4026 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4027 4e12cd97 2022-01-25 stsp }
4028 17ed4618 2019-06-02 stsp
4029 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4030 17ed4618 2019-06-02 stsp if (ie == NULL)
4031 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4032 2ec1f75b 2019-03-26 stsp
4033 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4034 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4035 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4036 9acbc4fa 2019-08-03 stsp return NULL;
4037 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4038 9acbc4fa 2019-08-03 stsp }
4039 9acbc4fa 2019-08-03 stsp
4040 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4041 f2a9dc41 2019-12-13 tracey relpath) == -1)
4042 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4043 f2a9dc41 2019-12-13 tracey
4044 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4045 12463d8b 2019-12-13 stsp a->repo);
4046 17ed4618 2019-06-02 stsp if (err)
4047 f2a9dc41 2019-12-13 tracey goto done;
4048 17ed4618 2019-06-02 stsp
4049 766841c2 2020-08-13 stsp if (a->status_codes) {
4050 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4051 766841c2 2020-08-13 stsp int i;
4052 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4053 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4054 766841c2 2020-08-13 stsp break;
4055 766841c2 2020-08-13 stsp }
4056 766841c2 2020-08-13 stsp if (i == ncodes) {
4057 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4058 766841c2 2020-08-13 stsp free(ondisk_path);
4059 766841c2 2020-08-13 stsp return NULL;
4060 766841c2 2020-08-13 stsp }
4061 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4062 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4063 766841c2 2020-08-13 stsp static char msg[64];
4064 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4065 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4066 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4067 766841c2 2020-08-13 stsp goto done;
4068 766841c2 2020-08-13 stsp }
4069 766841c2 2020-08-13 stsp }
4070 766841c2 2020-08-13 stsp
4071 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4072 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4073 f2a9dc41 2019-12-13 tracey goto done;
4074 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4075 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4076 f2a9dc41 2019-12-13 tracey goto done;
4077 f2a9dc41 2019-12-13 tracey }
4078 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4079 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4080 4e12cd97 2022-01-25 stsp goto done;
4081 4e12cd97 2022-01-25 stsp }
4082 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4083 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4084 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4085 f2a9dc41 2019-12-13 tracey goto done;
4086 f2a9dc41 2019-12-13 tracey }
4087 17ed4618 2019-06-02 stsp }
4088 17ed4618 2019-06-02 stsp
4089 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4090 20a2ad1c 2020-10-20 stsp size_t root_len;
4091 20a2ad1c 2020-10-20 stsp
4092 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4093 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4094 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4095 12463d8b 2019-12-13 stsp ondisk_path);
4096 12463d8b 2019-12-13 stsp goto done;
4097 12463d8b 2019-12-13 stsp }
4098 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4099 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4100 12463d8b 2019-12-13 stsp goto done;
4101 15341bfd 2020-03-05 tracey }
4102 15341bfd 2020-03-05 tracey
4103 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4104 20a2ad1c 2020-10-20 stsp do {
4105 20a2ad1c 2020-10-20 stsp char *parent;
4106 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4107 20a2ad1c 2020-10-20 stsp if (err)
4108 2513f20a 2020-10-20 stsp goto done;
4109 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4110 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4111 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4112 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4113 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4114 20a2ad1c 2020-10-20 stsp ondisk_path);
4115 15341bfd 2020-03-05 tracey break;
4116 15341bfd 2020-03-05 tracey }
4117 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4118 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4119 f2a9dc41 2019-12-13 tracey }
4120 17ed4618 2019-06-02 stsp
4121 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4122 f2a9dc41 2019-12-13 tracey done:
4123 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4124 f2a9dc41 2019-12-13 tracey if (err)
4125 f2a9dc41 2019-12-13 tracey return err;
4126 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4127 f2a9dc41 2019-12-13 tracey return NULL;
4128 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4129 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4130 17ed4618 2019-06-02 stsp }
4131 17ed4618 2019-06-02 stsp
4132 2ec1f75b 2019-03-26 stsp const struct got_error *
4133 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4134 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4135 766841c2 2020-08-13 stsp const char *status_codes,
4136 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4137 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4138 2ec1f75b 2019-03-26 stsp {
4139 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4140 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4141 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4142 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4143 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4144 2ec1f75b 2019-03-26 stsp
4145 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4146 2ec1f75b 2019-03-26 stsp if (err)
4147 2ec1f75b 2019-03-26 stsp return err;
4148 2ec1f75b 2019-03-26 stsp
4149 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4150 2ec1f75b 2019-03-26 stsp if (err)
4151 2ec1f75b 2019-03-26 stsp goto done;
4152 f2a9dc41 2019-12-13 tracey
4153 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4154 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4155 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4156 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4157 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4158 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4159 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4160 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4161 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4162 2ec1f75b 2019-03-26 stsp
4163 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4164 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4165 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4166 17ed4618 2019-06-02 stsp if (err)
4167 af12c6b9 2019-06-04 stsp break;
4168 2ec1f75b 2019-03-26 stsp }
4169 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4170 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4171 af12c6b9 2019-06-04 stsp err = sync_err;
4172 2ec1f75b 2019-03-26 stsp done:
4173 fb399478 2019-07-12 stsp free(fileindex_path);
4174 2ec1f75b 2019-03-26 stsp if (fileindex)
4175 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4176 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4177 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4178 2ec1f75b 2019-03-26 stsp err = unlockerr;
4179 33aa809d 2019-08-08 stsp return err;
4180 33aa809d 2019-08-08 stsp }
4181 33aa809d 2019-08-08 stsp
4182 33aa809d 2019-08-08 stsp static const struct got_error *
4183 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4184 33aa809d 2019-08-08 stsp {
4185 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4186 33aa809d 2019-08-08 stsp char *line = NULL;
4187 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4188 33aa809d 2019-08-08 stsp ssize_t linelen;
4189 33aa809d 2019-08-08 stsp
4190 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4191 33aa809d 2019-08-08 stsp if (linelen == -1) {
4192 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4193 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4194 33aa809d 2019-08-08 stsp goto done;
4195 33aa809d 2019-08-08 stsp }
4196 33aa809d 2019-08-08 stsp return NULL;
4197 33aa809d 2019-08-08 stsp }
4198 33aa809d 2019-08-08 stsp if (outfile) {
4199 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4200 33aa809d 2019-08-08 stsp if (n != linelen) {
4201 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4202 33aa809d 2019-08-08 stsp goto done;
4203 33aa809d 2019-08-08 stsp }
4204 33aa809d 2019-08-08 stsp }
4205 33aa809d 2019-08-08 stsp if (rejectfile) {
4206 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4207 33aa809d 2019-08-08 stsp if (n != linelen)
4208 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4209 33aa809d 2019-08-08 stsp }
4210 33aa809d 2019-08-08 stsp done:
4211 33aa809d 2019-08-08 stsp free(line);
4212 2ec1f75b 2019-03-26 stsp return err;
4213 2ec1f75b 2019-03-26 stsp }
4214 1f1abb7e 2019-08-08 stsp
4215 33aa809d 2019-08-08 stsp static const struct got_error *
4216 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4217 33aa809d 2019-08-08 stsp {
4218 33aa809d 2019-08-08 stsp char *line = NULL;
4219 33aa809d 2019-08-08 stsp size_t linesize = 0;
4220 33aa809d 2019-08-08 stsp ssize_t linelen;
4221 33aa809d 2019-08-08 stsp
4222 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4223 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4224 500467ff 2019-09-25 hiltjo if (ferror(f))
4225 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4226 500467ff 2019-09-25 hiltjo return NULL;
4227 500467ff 2019-09-25 hiltjo }
4228 33aa809d 2019-08-08 stsp free(line);
4229 33aa809d 2019-08-08 stsp return NULL;
4230 33aa809d 2019-08-08 stsp }
4231 33aa809d 2019-08-08 stsp
4232 33aa809d 2019-08-08 stsp static const struct got_error *
4233 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4234 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4235 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4236 abc59930 2021-09-05 naddy {
4237 33aa809d 2019-08-08 stsp const struct got_error *err;
4238 33aa809d 2019-08-08 stsp
4239 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4240 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4241 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4242 33aa809d 2019-08-08 stsp if (err)
4243 33aa809d 2019-08-08 stsp return err;
4244 33aa809d 2019-08-08 stsp (*line_cur1)++;
4245 33aa809d 2019-08-08 stsp }
4246 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4247 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4248 33aa809d 2019-08-08 stsp if (rejectfile)
4249 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4250 33aa809d 2019-08-08 stsp else
4251 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4252 33aa809d 2019-08-08 stsp if (err)
4253 33aa809d 2019-08-08 stsp return err;
4254 33aa809d 2019-08-08 stsp (*line_cur2)++;
4255 33aa809d 2019-08-08 stsp }
4256 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4257 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4258 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4259 33aa809d 2019-08-08 stsp if (err)
4260 33aa809d 2019-08-08 stsp return err;
4261 33aa809d 2019-08-08 stsp (*line_cur2)++;
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4264 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4265 33aa809d 2019-08-08 stsp if (rejectfile)
4266 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4267 33aa809d 2019-08-08 stsp else
4268 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4269 33aa809d 2019-08-08 stsp if (err)
4270 33aa809d 2019-08-08 stsp return err;
4271 33aa809d 2019-08-08 stsp (*line_cur1)++;
4272 33aa809d 2019-08-08 stsp }
4273 f1e81a05 2019-08-10 stsp
4274 f1e81a05 2019-08-10 stsp return NULL;
4275 f1e81a05 2019-08-10 stsp }
4276 f1e81a05 2019-08-10 stsp
4277 f1e81a05 2019-08-10 stsp static const struct got_error *
4278 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4280 f1e81a05 2019-08-10 stsp {
4281 f1e81a05 2019-08-10 stsp const struct got_error *err;
4282 f1e81a05 2019-08-10 stsp
4283 f1e81a05 2019-08-10 stsp if (outfile) {
4284 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4285 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4286 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4287 f1e81a05 2019-08-10 stsp if (err)
4288 f1e81a05 2019-08-10 stsp return err;
4289 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4290 f1e81a05 2019-08-10 stsp }
4291 f1e81a05 2019-08-10 stsp }
4292 f1e81a05 2019-08-10 stsp if (rejectfile) {
4293 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4294 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4295 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4296 f1e81a05 2019-08-10 stsp if (err)
4297 f1e81a05 2019-08-10 stsp return err;
4298 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4299 f1e81a05 2019-08-10 stsp }
4300 33aa809d 2019-08-08 stsp }
4301 33aa809d 2019-08-08 stsp
4302 33aa809d 2019-08-08 stsp return NULL;
4303 33aa809d 2019-08-08 stsp }
4304 33aa809d 2019-08-08 stsp
4305 33aa809d 2019-08-08 stsp static const struct got_error *
4306 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4307 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4308 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4309 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4310 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4311 33aa809d 2019-08-08 stsp {
4312 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4313 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4314 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4315 33aa809d 2019-08-08 stsp FILE *hunkfile;
4316 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4317 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4318 fe621944 2020-11-10 stsp int rc;
4319 33aa809d 2019-08-08 stsp
4320 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4321 33aa809d 2019-08-08 stsp
4322 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4323 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4324 fe621944 2020-11-10 stsp start_old = cc.left.start;
4325 fe621944 2020-11-10 stsp end_old = cc.left.end;
4326 fe621944 2020-11-10 stsp start_new = cc.right.start;
4327 fe621944 2020-11-10 stsp end_new = cc.right.end;
4328 33aa809d 2019-08-08 stsp
4329 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4330 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4331 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4332 33aa809d 2019-08-08 stsp
4333 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4334 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4335 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4336 33aa809d 2019-08-08 stsp
4337 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4338 fe621944 2020-11-10 stsp if (diff_state == NULL)
4339 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4340 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4341 fe621944 2020-11-10 stsp
4342 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4343 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4344 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4345 33aa809d 2019-08-08 stsp goto done;
4346 33aa809d 2019-08-08 stsp }
4347 fe621944 2020-11-10 stsp
4348 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4349 fe621944 2020-11-10 stsp diff_result, &cc);
4350 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4351 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4352 33aa809d 2019-08-08 stsp goto done;
4353 33aa809d 2019-08-08 stsp }
4354 fe621944 2020-11-10 stsp
4355 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4356 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4357 33aa809d 2019-08-08 stsp goto done;
4358 33aa809d 2019-08-08 stsp }
4359 33aa809d 2019-08-08 stsp
4360 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4361 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4362 33aa809d 2019-08-08 stsp if (err)
4363 33aa809d 2019-08-08 stsp goto done;
4364 33aa809d 2019-08-08 stsp
4365 33aa809d 2019-08-08 stsp switch (*choice) {
4366 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4367 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4368 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4369 33aa809d 2019-08-08 stsp break;
4370 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4371 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4372 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4373 33aa809d 2019-08-08 stsp break;
4374 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4375 33aa809d 2019-08-08 stsp break;
4376 33aa809d 2019-08-08 stsp default:
4377 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4378 33aa809d 2019-08-08 stsp break;
4379 33aa809d 2019-08-08 stsp }
4380 33aa809d 2019-08-08 stsp done:
4381 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4382 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4383 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4384 33aa809d 2019-08-08 stsp return err;
4385 33aa809d 2019-08-08 stsp }
4386 33aa809d 2019-08-08 stsp
4387 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4388 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4389 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4390 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4391 1f1abb7e 2019-08-08 stsp void *progress_arg;
4392 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4393 33aa809d 2019-08-08 stsp void *patch_arg;
4394 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4395 f259c4c1 2021-09-24 stsp int unlink_added_files;
4396 1f1abb7e 2019-08-08 stsp };
4397 a129376b 2019-03-28 stsp
4398 e20a8b6f 2019-06-04 stsp static const struct got_error *
4399 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4400 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4401 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4402 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4403 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4404 33aa809d 2019-08-08 stsp {
4405 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4406 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4407 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4408 1ebedb77 2019-10-19 stsp int fd2 = -1;
4409 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4410 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4411 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4412 fe621944 2020-11-10 stsp struct stat sb2;
4413 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4414 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4415 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4416 33aa809d 2019-08-08 stsp
4417 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4418 33aa809d 2019-08-08 stsp
4419 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4420 33aa809d 2019-08-08 stsp if (err)
4421 33aa809d 2019-08-08 stsp return err;
4422 33aa809d 2019-08-08 stsp
4423 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4424 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4425 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4426 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4427 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4428 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4429 fa3cef63 2020-07-23 stsp goto done;
4430 fa3cef63 2020-07-23 stsp }
4431 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4432 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4433 c0df5966 2021-12-31 stsp if (link_len == -1) {
4434 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4435 c0df5966 2021-12-31 stsp path2);
4436 c0df5966 2021-12-31 stsp }
4437 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4438 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4439 12463d8b 2019-12-13 stsp }
4440 12463d8b 2019-12-13 stsp } else {
4441 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4442 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4443 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4444 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4445 fa3cef63 2020-07-23 stsp goto done;
4446 fa3cef63 2020-07-23 stsp }
4447 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4448 fa3cef63 2020-07-23 stsp sizeof(link_target));
4449 fa3cef63 2020-07-23 stsp if (link_len == -1)
4450 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4451 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4452 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4453 12463d8b 2019-12-13 stsp }
4454 1ebedb77 2019-10-19 stsp }
4455 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4456 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4457 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4458 fa3cef63 2020-07-23 stsp goto done;
4459 fa3cef63 2020-07-23 stsp }
4460 1ebedb77 2019-10-19 stsp
4461 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4462 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4463 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4464 fa3cef63 2020-07-23 stsp goto done;
4465 fa3cef63 2020-07-23 stsp }
4466 fa3cef63 2020-07-23 stsp fd2 = -1;
4467 fa3cef63 2020-07-23 stsp } else {
4468 fa3cef63 2020-07-23 stsp size_t n;
4469 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4470 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4471 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4472 fa3cef63 2020-07-23 stsp goto done;
4473 fa3cef63 2020-07-23 stsp }
4474 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4475 fa3cef63 2020-07-23 stsp if (n != link_len) {
4476 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4477 fa3cef63 2020-07-23 stsp goto done;
4478 fa3cef63 2020-07-23 stsp }
4479 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4480 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4481 fa3cef63 2020-07-23 stsp goto done;
4482 fa3cef63 2020-07-23 stsp }
4483 fa3cef63 2020-07-23 stsp rewind(f2);
4484 33aa809d 2019-08-08 stsp }
4485 33aa809d 2019-08-08 stsp
4486 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4487 33aa809d 2019-08-08 stsp if (err)
4488 33aa809d 2019-08-08 stsp goto done;
4489 33aa809d 2019-08-08 stsp
4490 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4491 33aa809d 2019-08-08 stsp if (err)
4492 33aa809d 2019-08-08 stsp goto done;
4493 33aa809d 2019-08-08 stsp
4494 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4495 33aa809d 2019-08-08 stsp if (err)
4496 33aa809d 2019-08-08 stsp goto done;
4497 33aa809d 2019-08-08 stsp
4498 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4499 fe621944 2020-11-10 stsp NULL);
4500 33aa809d 2019-08-08 stsp if (err)
4501 33aa809d 2019-08-08 stsp goto done;
4502 33aa809d 2019-08-08 stsp
4503 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4504 33aa809d 2019-08-08 stsp if (err)
4505 33aa809d 2019-08-08 stsp goto done;
4506 33aa809d 2019-08-08 stsp
4507 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4508 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4509 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4510 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4511 fe621944 2020-11-10 stsp
4512 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4513 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4514 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4515 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4516 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4517 fe621944 2020-11-10 stsp nchanges++;
4518 fe621944 2020-11-10 stsp }
4519 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4520 33aa809d 2019-08-08 stsp int choice;
4521 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4522 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4523 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4524 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4525 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4526 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4527 33aa809d 2019-08-08 stsp if (err)
4528 33aa809d 2019-08-08 stsp goto done;
4529 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4530 33aa809d 2019-08-08 stsp have_content = 1;
4531 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4532 33aa809d 2019-08-08 stsp break;
4533 33aa809d 2019-08-08 stsp }
4534 1ebedb77 2019-10-19 stsp if (have_content) {
4535 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4536 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4537 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4538 1ebedb77 2019-10-19 stsp if (err)
4539 1ebedb77 2019-10-19 stsp goto done;
4540 1ebedb77 2019-10-19 stsp
4541 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4542 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4543 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4544 fa3cef63 2020-07-23 stsp goto done;
4545 fa3cef63 2020-07-23 stsp }
4546 1ebedb77 2019-10-19 stsp }
4547 1ebedb77 2019-10-19 stsp }
4548 33aa809d 2019-08-08 stsp done:
4549 33aa809d 2019-08-08 stsp free(id_str);
4550 33aa809d 2019-08-08 stsp if (blob)
4551 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4552 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4553 fe621944 2020-11-10 stsp if (err == NULL)
4554 fe621944 2020-11-10 stsp err = free_err;
4555 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4556 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4557 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4558 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4559 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4560 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4561 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4562 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4563 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4564 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4565 33aa809d 2019-08-08 stsp if (err || !have_content) {
4566 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4567 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4568 33aa809d 2019-08-08 stsp free(*path_outfile);
4569 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4570 33aa809d 2019-08-08 stsp }
4571 33aa809d 2019-08-08 stsp free(path1);
4572 33aa809d 2019-08-08 stsp return err;
4573 33aa809d 2019-08-08 stsp }
4574 33aa809d 2019-08-08 stsp
4575 33aa809d 2019-08-08 stsp static const struct got_error *
4576 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4577 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4578 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4579 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4580 a129376b 2019-03-28 stsp {
4581 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4582 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4583 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4584 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4585 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4586 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4587 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4588 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4589 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4590 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4591 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4592 a129376b 2019-03-28 stsp
4593 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4594 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4595 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4596 d3bcc3d1 2019-08-08 stsp return NULL;
4597 3d69ad8d 2019-08-17 semarie
4598 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4599 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4600 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4601 d3bcc3d1 2019-08-08 stsp
4602 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4603 65084dad 2019-08-08 stsp if (ie == NULL)
4604 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4605 a129376b 2019-03-28 stsp
4606 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4607 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4608 a129376b 2019-03-28 stsp if (err) {
4609 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4610 a129376b 2019-03-28 stsp goto done;
4611 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4612 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4613 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4614 e20a8b6f 2019-06-04 stsp goto done;
4615 e20a8b6f 2019-06-04 stsp }
4616 a129376b 2019-03-28 stsp }
4617 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4618 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4619 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4620 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4621 a129376b 2019-03-28 stsp goto done;
4622 a129376b 2019-03-28 stsp }
4623 a129376b 2019-03-28 stsp } else {
4624 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4625 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4626 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4627 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4628 a129376b 2019-03-28 stsp goto done;
4629 a129376b 2019-03-28 stsp }
4630 a129376b 2019-03-28 stsp } else {
4631 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4632 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4633 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4634 a129376b 2019-03-28 stsp goto done;
4635 a129376b 2019-03-28 stsp }
4636 a129376b 2019-03-28 stsp }
4637 a129376b 2019-03-28 stsp }
4638 a129376b 2019-03-28 stsp
4639 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4640 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4641 a44927cc 2022-04-07 stsp if (err)
4642 a44927cc 2022-04-07 stsp goto done;
4643 a44927cc 2022-04-07 stsp
4644 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4645 a9fa2909 2019-07-27 stsp if (err) {
4646 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4647 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4648 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4649 a9fa2909 2019-07-27 stsp goto done;
4650 a9fa2909 2019-07-27 stsp } else {
4651 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4652 a9fa2909 2019-07-27 stsp if (err)
4653 a9fa2909 2019-07-27 stsp goto done;
4654 a9fa2909 2019-07-27 stsp
4655 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4656 1233e6b6 2020-10-19 stsp if (err)
4657 a9fa2909 2019-07-27 stsp goto done;
4658 a9fa2909 2019-07-27 stsp
4659 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4660 1233e6b6 2020-10-19 stsp free(te_name);
4661 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4662 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4663 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4664 a9fa2909 2019-07-27 stsp goto done;
4665 a9fa2909 2019-07-27 stsp }
4666 a129376b 2019-03-28 stsp }
4667 a129376b 2019-03-28 stsp
4668 a129376b 2019-03-28 stsp switch (status) {
4669 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4670 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4671 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4672 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4673 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4674 33aa809d 2019-08-08 stsp if (err)
4675 33aa809d 2019-08-08 stsp goto done;
4676 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4677 33aa809d 2019-08-08 stsp break;
4678 33aa809d 2019-08-08 stsp }
4679 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4680 1f1abb7e 2019-08-08 stsp ie->path);
4681 1ee397ad 2019-07-12 stsp if (err)
4682 1ee397ad 2019-07-12 stsp goto done;
4683 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4684 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4685 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4686 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4687 f259c4c1 2021-09-24 stsp relpath) == -1) {
4688 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4689 f259c4c1 2021-09-24 stsp goto done;
4690 f259c4c1 2021-09-24 stsp }
4691 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4692 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4693 f259c4c1 2021-09-24 stsp ondisk_path);
4694 f259c4c1 2021-09-24 stsp break;
4695 f259c4c1 2021-09-24 stsp }
4696 f259c4c1 2021-09-24 stsp }
4697 a129376b 2019-03-28 stsp break;
4698 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4699 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4700 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4701 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4702 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4703 33aa809d 2019-08-08 stsp if (err)
4704 33aa809d 2019-08-08 stsp goto done;
4705 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4706 33aa809d 2019-08-08 stsp break;
4707 33aa809d 2019-08-08 stsp }
4708 33aa809d 2019-08-08 stsp /* fall through */
4709 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4710 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4711 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4712 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4713 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4714 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4715 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4716 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4717 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4718 24278f30 2019-08-03 stsp } else
4719 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4720 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4721 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4722 a129376b 2019-03-28 stsp if (err)
4723 65084dad 2019-08-08 stsp goto done;
4724 65084dad 2019-08-08 stsp
4725 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4726 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4727 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4728 a129376b 2019-03-28 stsp goto done;
4729 65084dad 2019-08-08 stsp }
4730 65084dad 2019-08-08 stsp
4731 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4732 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4733 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4734 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4735 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4736 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4737 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4738 33aa809d 2019-08-08 stsp break;
4739 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4740 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4741 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4742 369fd7e5 2020-07-23 stsp path_content);
4743 369fd7e5 2020-07-23 stsp break;
4744 369fd7e5 2020-07-23 stsp }
4745 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4746 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4747 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4748 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4749 369fd7e5 2020-07-23 stsp } else {
4750 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4751 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4752 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4753 369fd7e5 2020-07-23 stsp goto done;
4754 369fd7e5 2020-07-23 stsp }
4755 33aa809d 2019-08-08 stsp }
4756 33aa809d 2019-08-08 stsp } else {
4757 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4758 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4759 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4760 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4761 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4762 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4763 2e1fa222 2020-07-23 stsp } else {
4764 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4765 2e1fa222 2020-07-23 stsp ie->path,
4766 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4767 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4768 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4769 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4770 2e1fa222 2020-07-23 stsp }
4771 a129376b 2019-03-28 stsp if (err)
4772 a129376b 2019-03-28 stsp goto done;
4773 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4774 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4775 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4776 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4777 437adc9d 2020-12-10 yzhong blob->id.sha1,
4778 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4779 2e1fa222 2020-07-23 stsp if (err)
4780 2e1fa222 2020-07-23 stsp goto done;
4781 2e1fa222 2020-07-23 stsp }
4782 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4783 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4784 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4785 33aa809d 2019-08-08 stsp }
4786 a129376b 2019-03-28 stsp }
4787 a129376b 2019-03-28 stsp break;
4788 e20a8b6f 2019-06-04 stsp }
4789 a129376b 2019-03-28 stsp default:
4790 1f1abb7e 2019-08-08 stsp break;
4791 a129376b 2019-03-28 stsp }
4792 a129376b 2019-03-28 stsp done:
4793 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4794 33aa809d 2019-08-08 stsp free(path_content);
4795 e20a8b6f 2019-06-04 stsp free(parent_path);
4796 a129376b 2019-03-28 stsp free(tree_path);
4797 a129376b 2019-03-28 stsp if (blob)
4798 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4799 a129376b 2019-03-28 stsp if (tree)
4800 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4801 a129376b 2019-03-28 stsp free(tree_id);
4802 a44927cc 2022-04-07 stsp if (base_commit)
4803 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4804 e20a8b6f 2019-06-04 stsp return err;
4805 e20a8b6f 2019-06-04 stsp }
4806 e20a8b6f 2019-06-04 stsp
4807 e20a8b6f 2019-06-04 stsp const struct got_error *
4808 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4809 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4810 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4811 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4812 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4813 e20a8b6f 2019-06-04 stsp {
4814 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4815 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4816 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4817 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4818 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4819 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4820 e20a8b6f 2019-06-04 stsp
4821 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4822 e20a8b6f 2019-06-04 stsp if (err)
4823 e20a8b6f 2019-06-04 stsp return err;
4824 e20a8b6f 2019-06-04 stsp
4825 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4826 e20a8b6f 2019-06-04 stsp if (err)
4827 e20a8b6f 2019-06-04 stsp goto done;
4828 e20a8b6f 2019-06-04 stsp
4829 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4830 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4831 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4832 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4833 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4834 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4835 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4836 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4837 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4838 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4839 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4840 e20a8b6f 2019-06-04 stsp if (err)
4841 af12c6b9 2019-06-04 stsp break;
4842 e20a8b6f 2019-06-04 stsp }
4843 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4844 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4845 e20a8b6f 2019-06-04 stsp err = sync_err;
4846 af12c6b9 2019-06-04 stsp done:
4847 fb399478 2019-07-12 stsp free(fileindex_path);
4848 a129376b 2019-03-28 stsp if (fileindex)
4849 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4850 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4851 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4852 a129376b 2019-03-28 stsp err = unlockerr;
4853 c4296144 2019-05-09 stsp return err;
4854 c4296144 2019-05-09 stsp }
4855 c4296144 2019-05-09 stsp
4856 cf066bf8 2019-05-09 stsp static void
4857 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4858 cf066bf8 2019-05-09 stsp {
4859 24519714 2019-05-09 stsp free(ct->path);
4860 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4861 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4862 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4863 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4864 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4865 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4866 cf066bf8 2019-05-09 stsp free(ct);
4867 cf066bf8 2019-05-09 stsp }
4868 24519714 2019-05-09 stsp
4869 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4870 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4871 24519714 2019-05-09 stsp struct got_repository *repo;
4872 24519714 2019-05-09 stsp struct got_worktree *worktree;
4873 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4874 5f8a88c6 2019-08-03 stsp int have_staged_files;
4875 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4876 24519714 2019-05-09 stsp };
4877 cf066bf8 2019-05-09 stsp
4878 c4296144 2019-05-09 stsp static const struct got_error *
4879 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4880 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4881 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4882 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4883 c4296144 2019-05-09 stsp {
4884 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4885 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4886 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4887 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4888 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4889 768aea60 2019-05-09 stsp struct stat sb;
4890 c4296144 2019-05-09 stsp
4891 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4892 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4893 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4894 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4895 5f8a88c6 2019-08-03 stsp return NULL;
4896 5f8a88c6 2019-08-03 stsp } else {
4897 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4898 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4899 c4296144 2019-05-09 stsp
4900 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4901 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4902 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4903 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4904 5f8a88c6 2019-08-03 stsp return NULL;
4905 5f8a88c6 2019-08-03 stsp }
4906 0b5cc0d6 2019-05-09 stsp
4907 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4908 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4909 036813ee 2019-05-09 stsp goto done;
4910 036813ee 2019-05-09 stsp }
4911 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4912 036813ee 2019-05-09 stsp parent_path = strdup("");
4913 69960a46 2019-05-09 stsp if (parent_path == NULL)
4914 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4915 69960a46 2019-05-09 stsp } else {
4916 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4917 69960a46 2019-05-09 stsp if (err)
4918 69960a46 2019-05-09 stsp return err;
4919 69960a46 2019-05-09 stsp }
4920 c4296144 2019-05-09 stsp
4921 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4922 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4923 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4924 c4296144 2019-05-09 stsp goto done;
4925 768aea60 2019-05-09 stsp }
4926 768aea60 2019-05-09 stsp
4927 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4928 768aea60 2019-05-09 stsp relpath) == -1) {
4929 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4930 768aea60 2019-05-09 stsp goto done;
4931 768aea60 2019-05-09 stsp }
4932 0aeb8099 2020-07-23 stsp
4933 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4934 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4935 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4936 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4937 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4938 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4939 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4940 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4941 0aeb8099 2020-07-23 stsp break;
4942 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4943 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4944 0aeb8099 2020-07-23 stsp break;
4945 0aeb8099 2020-07-23 stsp default:
4946 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4947 0aeb8099 2020-07-23 stsp goto done;
4948 0aeb8099 2020-07-23 stsp }
4949 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4950 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4951 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4952 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4953 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4954 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4955 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4956 12463d8b 2019-12-13 stsp ct->ondisk_path);
4957 12463d8b 2019-12-13 stsp goto done;
4958 12463d8b 2019-12-13 stsp }
4959 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4960 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4961 768aea60 2019-05-09 stsp goto done;
4962 768aea60 2019-05-09 stsp }
4963 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4964 c4296144 2019-05-09 stsp }
4965 c4296144 2019-05-09 stsp
4966 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4967 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4968 44d03001 2019-05-09 stsp relpath) == -1) {
4969 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4970 44d03001 2019-05-09 stsp goto done;
4971 44d03001 2019-05-09 stsp }
4972 44d03001 2019-05-09 stsp
4973 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4974 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4975 35213c7c 2020-07-23 stsp int is_bad_symlink;
4976 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4977 35213c7c 2020-07-23 stsp ssize_t target_len;
4978 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4979 35213c7c 2020-07-23 stsp sizeof(target_path));
4980 35213c7c 2020-07-23 stsp if (target_len == -1) {
4981 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4982 35213c7c 2020-07-23 stsp ct->ondisk_path);
4983 35213c7c 2020-07-23 stsp goto done;
4984 35213c7c 2020-07-23 stsp }
4985 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4986 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4987 35213c7c 2020-07-23 stsp if (err)
4988 35213c7c 2020-07-23 stsp goto done;
4989 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4990 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4991 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4992 35213c7c 2020-07-23 stsp goto done;
4993 35213c7c 2020-07-23 stsp }
4994 35213c7c 2020-07-23 stsp }
4995 35213c7c 2020-07-23 stsp
4996 35213c7c 2020-07-23 stsp
4997 cf066bf8 2019-05-09 stsp ct->status = status;
4998 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4999 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5000 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5001 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5002 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5003 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5004 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5005 b416585c 2019-05-13 stsp goto done;
5006 b416585c 2019-05-13 stsp }
5007 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5008 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5009 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5010 f0b75401 2019-08-03 stsp goto done;
5011 f0b75401 2019-08-03 stsp }
5012 f0b75401 2019-08-03 stsp }
5013 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5014 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5015 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5016 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5017 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5018 036813ee 2019-05-09 stsp goto done;
5019 036813ee 2019-05-09 stsp }
5020 ca2503ea 2019-05-09 stsp }
5021 24519714 2019-05-09 stsp ct->path = strdup(path);
5022 24519714 2019-05-09 stsp if (ct->path == NULL) {
5023 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5024 24519714 2019-05-09 stsp goto done;
5025 24519714 2019-05-09 stsp }
5026 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5027 c4296144 2019-05-09 stsp done:
5028 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5029 ed175427 2019-05-09 stsp free_commitable(ct);
5030 24519714 2019-05-09 stsp free(parent_path);
5031 036813ee 2019-05-09 stsp free(path);
5032 a129376b 2019-03-28 stsp return err;
5033 a129376b 2019-03-28 stsp }
5034 c4296144 2019-05-09 stsp
5035 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5036 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5037 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5038 036813ee 2019-05-09 stsp struct got_repository *);
5039 ed175427 2019-05-09 stsp
5040 ed175427 2019-05-09 stsp static const struct got_error *
5041 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5042 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5043 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5044 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5045 afa376bf 2019-05-09 stsp struct got_repository *repo)
5046 ed175427 2019-05-09 stsp {
5047 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5048 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5049 036813ee 2019-05-09 stsp char *subpath;
5050 ed175427 2019-05-09 stsp
5051 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5052 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5053 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5054 ed175427 2019-05-09 stsp
5055 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5056 ed175427 2019-05-09 stsp if (err)
5057 ed175427 2019-05-09 stsp return err;
5058 ed175427 2019-05-09 stsp
5059 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5060 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5061 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5062 036813ee 2019-05-09 stsp free(subpath);
5063 036813ee 2019-05-09 stsp return err;
5064 036813ee 2019-05-09 stsp }
5065 ed175427 2019-05-09 stsp
5066 036813ee 2019-05-09 stsp static const struct got_error *
5067 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5068 036813ee 2019-05-09 stsp {
5069 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5070 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5071 ed175427 2019-05-09 stsp
5072 036813ee 2019-05-09 stsp *match = 0;
5073 ed175427 2019-05-09 stsp
5074 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5075 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5076 0f63689d 2019-05-10 stsp return NULL;
5077 036813ee 2019-05-09 stsp }
5078 0b5cc0d6 2019-05-09 stsp
5079 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5080 0f63689d 2019-05-10 stsp if (err)
5081 0f63689d 2019-05-10 stsp return err;
5082 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5083 036813ee 2019-05-09 stsp free(ct_parent_path);
5084 036813ee 2019-05-09 stsp return err;
5085 036813ee 2019-05-09 stsp }
5086 0b5cc0d6 2019-05-09 stsp
5087 768aea60 2019-05-09 stsp static mode_t
5088 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5089 768aea60 2019-05-09 stsp {
5090 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5091 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5092 3d9a4ec4 2020-07-23 stsp
5093 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5094 768aea60 2019-05-09 stsp }
5095 768aea60 2019-05-09 stsp
5096 036813ee 2019-05-09 stsp static const struct got_error *
5097 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5098 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5099 036813ee 2019-05-09 stsp {
5100 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5101 ca2503ea 2019-05-09 stsp
5102 036813ee 2019-05-09 stsp *new_te = NULL;
5103 0b5cc0d6 2019-05-09 stsp
5104 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5105 036813ee 2019-05-09 stsp if (err)
5106 036813ee 2019-05-09 stsp goto done;
5107 0b5cc0d6 2019-05-09 stsp
5108 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5109 036813ee 2019-05-09 stsp
5110 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5111 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5112 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5113 5f8a88c6 2019-08-03 stsp else
5114 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5115 036813ee 2019-05-09 stsp done:
5116 036813ee 2019-05-09 stsp if (err && *new_te) {
5117 56e0773d 2019-11-28 stsp free(*new_te);
5118 036813ee 2019-05-09 stsp *new_te = NULL;
5119 036813ee 2019-05-09 stsp }
5120 036813ee 2019-05-09 stsp return err;
5121 036813ee 2019-05-09 stsp }
5122 036813ee 2019-05-09 stsp
5123 036813ee 2019-05-09 stsp static const struct got_error *
5124 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5125 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5126 036813ee 2019-05-09 stsp {
5127 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5128 102b254e 2020-10-19 stsp char *ct_name = NULL;
5129 036813ee 2019-05-09 stsp
5130 036813ee 2019-05-09 stsp *new_te = NULL;
5131 036813ee 2019-05-09 stsp
5132 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5133 036813ee 2019-05-09 stsp if (*new_te == NULL)
5134 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5135 036813ee 2019-05-09 stsp
5136 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5137 102b254e 2020-10-19 stsp if (err)
5138 036813ee 2019-05-09 stsp goto done;
5139 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5140 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5141 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5142 036813ee 2019-05-09 stsp goto done;
5143 036813ee 2019-05-09 stsp }
5144 036813ee 2019-05-09 stsp
5145 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5146 036813ee 2019-05-09 stsp
5147 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5148 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5149 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5150 5f8a88c6 2019-08-03 stsp else
5151 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5152 036813ee 2019-05-09 stsp done:
5153 102b254e 2020-10-19 stsp free(ct_name);
5154 036813ee 2019-05-09 stsp if (err && *new_te) {
5155 56e0773d 2019-11-28 stsp free(*new_te);
5156 036813ee 2019-05-09 stsp *new_te = NULL;
5157 036813ee 2019-05-09 stsp }
5158 036813ee 2019-05-09 stsp return err;
5159 036813ee 2019-05-09 stsp }
5160 036813ee 2019-05-09 stsp
5161 036813ee 2019-05-09 stsp static const struct got_error *
5162 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5163 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5164 036813ee 2019-05-09 stsp {
5165 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5166 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5167 036813ee 2019-05-09 stsp
5168 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5169 036813ee 2019-05-09 stsp if (err)
5170 036813ee 2019-05-09 stsp return err;
5171 036813ee 2019-05-09 stsp if (new_pe == NULL)
5172 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5173 036813ee 2019-05-09 stsp return NULL;
5174 afa376bf 2019-05-09 stsp }
5175 afa376bf 2019-05-09 stsp
5176 afa376bf 2019-05-09 stsp static const struct got_error *
5177 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5178 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5179 afa376bf 2019-05-09 stsp {
5180 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5181 5f8a88c6 2019-08-03 stsp unsigned char status;
5182 0ff8d236 2021-09-28 stsp
5183 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5184 0ff8d236 2021-09-28 stsp return NULL;
5185 5f8a88c6 2019-08-03 stsp
5186 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5187 afa376bf 2019-05-09 stsp ct_path++;
5188 5f8a88c6 2019-08-03 stsp
5189 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5190 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5191 5f8a88c6 2019-08-03 stsp else
5192 5f8a88c6 2019-08-03 stsp status = ct->status;
5193 5f8a88c6 2019-08-03 stsp
5194 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5195 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5196 036813ee 2019-05-09 stsp }
5197 036813ee 2019-05-09 stsp
5198 036813ee 2019-05-09 stsp static const struct got_error *
5199 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5200 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5201 44d03001 2019-05-09 stsp {
5202 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5203 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5204 44d03001 2019-05-09 stsp char *te_path;
5205 44d03001 2019-05-09 stsp
5206 44d03001 2019-05-09 stsp *modified = 0;
5207 44d03001 2019-05-09 stsp
5208 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5209 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5210 44d03001 2019-05-09 stsp te->name) == -1)
5211 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5212 44d03001 2019-05-09 stsp
5213 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5214 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5215 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5216 44d03001 2019-05-09 stsp strlen(te_path));
5217 62d463ca 2020-10-20 naddy if (*modified)
5218 44d03001 2019-05-09 stsp break;
5219 44d03001 2019-05-09 stsp }
5220 44d03001 2019-05-09 stsp
5221 44d03001 2019-05-09 stsp free(te_path);
5222 44d03001 2019-05-09 stsp return err;
5223 44d03001 2019-05-09 stsp }
5224 44d03001 2019-05-09 stsp
5225 44d03001 2019-05-09 stsp static const struct got_error *
5226 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5227 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5228 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5229 036813ee 2019-05-09 stsp {
5230 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5231 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5232 036813ee 2019-05-09 stsp
5233 036813ee 2019-05-09 stsp *ctp = NULL;
5234 036813ee 2019-05-09 stsp
5235 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5236 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5237 036813ee 2019-05-09 stsp char *ct_name = NULL;
5238 036813ee 2019-05-09 stsp int path_matches;
5239 036813ee 2019-05-09 stsp
5240 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5241 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5242 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5243 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5244 5f8a88c6 2019-08-03 stsp continue;
5245 5f8a88c6 2019-08-03 stsp } else {
5246 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5247 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5248 5f8a88c6 2019-08-03 stsp continue;
5249 5f8a88c6 2019-08-03 stsp }
5250 036813ee 2019-05-09 stsp
5251 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5252 036813ee 2019-05-09 stsp continue;
5253 036813ee 2019-05-09 stsp
5254 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5255 62d463ca 2020-10-20 naddy if (err)
5256 036813ee 2019-05-09 stsp return err;
5257 036813ee 2019-05-09 stsp if (!path_matches)
5258 036813ee 2019-05-09 stsp continue;
5259 036813ee 2019-05-09 stsp
5260 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5261 d34b633e 2020-10-19 stsp if (err)
5262 d34b633e 2020-10-19 stsp return err;
5263 d34b633e 2020-10-19 stsp
5264 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5265 d34b633e 2020-10-19 stsp free(ct_name);
5266 036813ee 2019-05-09 stsp continue;
5267 d34b633e 2020-10-19 stsp }
5268 d34b633e 2020-10-19 stsp free(ct_name);
5269 036813ee 2019-05-09 stsp
5270 036813ee 2019-05-09 stsp *ctp = ct;
5271 036813ee 2019-05-09 stsp break;
5272 036813ee 2019-05-09 stsp }
5273 2b496619 2019-07-10 stsp
5274 2b496619 2019-07-10 stsp return err;
5275 2b496619 2019-07-10 stsp }
5276 2b496619 2019-07-10 stsp
5277 2b496619 2019-07-10 stsp static const struct got_error *
5278 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5279 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5280 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5281 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5282 2b496619 2019-07-10 stsp struct got_repository *repo)
5283 2b496619 2019-07-10 stsp {
5284 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5285 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5286 2b496619 2019-07-10 stsp char *subtree_path;
5287 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5288 ba580f68 2020-03-22 stsp int nentries;
5289 2b496619 2019-07-10 stsp
5290 2b496619 2019-07-10 stsp *new_tep = NULL;
5291 2b496619 2019-07-10 stsp
5292 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5293 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5294 2b496619 2019-07-10 stsp child_path) == -1)
5295 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5296 036813ee 2019-05-09 stsp
5297 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5298 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5299 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5300 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5301 56e0773d 2019-11-28 stsp
5302 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5303 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5304 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5305 2b496619 2019-07-10 stsp goto done;
5306 2b496619 2019-07-10 stsp }
5307 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5308 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5309 2b496619 2019-07-10 stsp if (err) {
5310 56e0773d 2019-11-28 stsp free(new_te);
5311 2b496619 2019-07-10 stsp goto done;
5312 2b496619 2019-07-10 stsp }
5313 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5314 2b496619 2019-07-10 stsp done:
5315 56e0773d 2019-11-28 stsp free(id);
5316 2b496619 2019-07-10 stsp free(subtree_path);
5317 2b496619 2019-07-10 stsp if (err == NULL)
5318 2b496619 2019-07-10 stsp *new_tep = new_te;
5319 036813ee 2019-05-09 stsp return err;
5320 036813ee 2019-05-09 stsp }
5321 036813ee 2019-05-09 stsp
5322 036813ee 2019-05-09 stsp static const struct got_error *
5323 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5324 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5325 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5326 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5327 036813ee 2019-05-09 stsp struct got_repository *repo)
5328 036813ee 2019-05-09 stsp {
5329 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5330 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5331 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5332 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5333 036813ee 2019-05-09 stsp
5334 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5335 ba580f68 2020-03-22 stsp *nentries = 0;
5336 036813ee 2019-05-09 stsp
5337 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5338 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5339 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5340 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5341 036813ee 2019-05-09 stsp
5342 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5343 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5344 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5345 036813ee 2019-05-09 stsp continue;
5346 036813ee 2019-05-09 stsp
5347 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5348 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5349 036813ee 2019-05-09 stsp continue;
5350 036813ee 2019-05-09 stsp
5351 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5352 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5353 036813ee 2019-05-09 stsp if (err)
5354 036813ee 2019-05-09 stsp goto done;
5355 036813ee 2019-05-09 stsp
5356 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5357 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5358 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5359 036813ee 2019-05-09 stsp if (err)
5360 036813ee 2019-05-09 stsp goto done;
5361 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5362 afa376bf 2019-05-09 stsp if (err)
5363 afa376bf 2019-05-09 stsp goto done;
5364 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5365 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5366 2b496619 2019-07-10 stsp if (err)
5367 2f51b5b3 2019-05-09 stsp goto done;
5368 ba580f68 2020-03-22 stsp (*nentries)++;
5369 2b496619 2019-07-10 stsp } else {
5370 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5371 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5372 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5373 2b496619 2019-07-10 stsp == NULL) {
5374 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5375 2b496619 2019-07-10 stsp child_path, path_base_tree,
5376 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5377 2b496619 2019-07-10 stsp repo);
5378 2b496619 2019-07-10 stsp if (err)
5379 2b496619 2019-07-10 stsp goto done;
5380 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5381 2b496619 2019-07-10 stsp if (err)
5382 2b496619 2019-07-10 stsp goto done;
5383 ba580f68 2020-03-22 stsp (*nentries)++;
5384 9ba0479c 2019-05-10 stsp }
5385 ed175427 2019-05-09 stsp }
5386 2f51b5b3 2019-05-09 stsp }
5387 2f51b5b3 2019-05-09 stsp
5388 2f51b5b3 2019-05-09 stsp if (base_tree) {
5389 56e0773d 2019-11-28 stsp int i, nbase_entries;
5390 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5391 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5392 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5393 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5394 63c5ca5d 2019-08-24 stsp
5395 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5396 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5397 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5398 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5399 63c5ca5d 2019-08-24 stsp if (err)
5400 63c5ca5d 2019-08-24 stsp goto done;
5401 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5402 63c5ca5d 2019-08-24 stsp if (err)
5403 63c5ca5d 2019-08-24 stsp goto done;
5404 ba580f68 2020-03-22 stsp (*nentries)++;
5405 63c5ca5d 2019-08-24 stsp continue;
5406 63c5ca5d 2019-08-24 stsp }
5407 2f51b5b3 2019-05-09 stsp
5408 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5409 44d03001 2019-05-09 stsp int modified;
5410 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5411 036813ee 2019-05-09 stsp if (err)
5412 036813ee 2019-05-09 stsp goto done;
5413 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5414 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5415 2f51b5b3 2019-05-09 stsp if (err)
5416 2f51b5b3 2019-05-09 stsp goto done;
5417 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5418 44d03001 2019-05-09 stsp if (modified) {
5419 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5420 ba580f68 2020-03-22 stsp int nsubentries;
5421 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5422 ba580f68 2020-03-22 stsp &nsubentries, te,
5423 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5424 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5425 44d03001 2019-05-09 stsp if (err)
5426 44d03001 2019-05-09 stsp goto done;
5427 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5428 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5429 ba580f68 2020-03-22 stsp free(new_id);
5430 ba580f68 2020-03-22 stsp continue;
5431 ba580f68 2020-03-22 stsp }
5432 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5433 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5434 56e0773d 2019-11-28 stsp free(new_id);
5435 44d03001 2019-05-09 stsp }
5436 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5437 036813ee 2019-05-09 stsp if (err)
5438 036813ee 2019-05-09 stsp goto done;
5439 ba580f68 2020-03-22 stsp (*nentries)++;
5440 2f51b5b3 2019-05-09 stsp continue;
5441 036813ee 2019-05-09 stsp }
5442 2f51b5b3 2019-05-09 stsp
5443 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5444 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5445 f66c734c 2019-09-22 stsp if (err)
5446 f66c734c 2019-09-22 stsp goto done;
5447 2f51b5b3 2019-05-09 stsp if (ct) {
5448 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5449 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5450 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5451 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5452 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5453 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5454 2f51b5b3 2019-05-09 stsp if (err)
5455 2f51b5b3 2019-05-09 stsp goto done;
5456 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5457 2f51b5b3 2019-05-09 stsp if (err)
5458 2f51b5b3 2019-05-09 stsp goto done;
5459 ba580f68 2020-03-22 stsp (*nentries)++;
5460 2f51b5b3 2019-05-09 stsp }
5461 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5462 b416585c 2019-05-13 stsp status_arg);
5463 afa376bf 2019-05-09 stsp if (err)
5464 afa376bf 2019-05-09 stsp goto done;
5465 2f51b5b3 2019-05-09 stsp } else {
5466 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5467 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5468 2f51b5b3 2019-05-09 stsp if (err)
5469 2f51b5b3 2019-05-09 stsp goto done;
5470 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5471 2f51b5b3 2019-05-09 stsp if (err)
5472 2f51b5b3 2019-05-09 stsp goto done;
5473 ba580f68 2020-03-22 stsp (*nentries)++;
5474 2f51b5b3 2019-05-09 stsp }
5475 ca2503ea 2019-05-09 stsp }
5476 ed175427 2019-05-09 stsp }
5477 0b5cc0d6 2019-05-09 stsp
5478 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5479 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5480 ed175427 2019-05-09 stsp done:
5481 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5482 2e1fa222 2020-07-23 stsp return err;
5483 2e1fa222 2020-07-23 stsp }
5484 2e1fa222 2020-07-23 stsp
5485 2e1fa222 2020-07-23 stsp static const struct got_error *
5486 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5487 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5488 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5489 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5490 ebf99748 2019-05-09 stsp {
5491 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5492 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5493 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5494 ebf99748 2019-05-09 stsp
5495 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5496 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5497 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5498 ebf99748 2019-05-09 stsp
5499 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5500 437adc9d 2020-12-10 yzhong
5501 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5502 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5503 437adc9d 2020-12-10 yzhong if (err)
5504 437adc9d 2020-12-10 yzhong goto done;
5505 437adc9d 2020-12-10 yzhong
5506 ebf99748 2019-05-09 stsp if (ie) {
5507 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5508 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5509 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5510 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5511 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5512 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5513 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5514 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5515 437adc9d 2020-12-10 yzhong
5516 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5517 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5518 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5519 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5520 72fd46fa 2019-09-06 stsp !have_staged_files);
5521 ebf99748 2019-05-09 stsp } else
5522 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5523 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5524 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5525 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5526 72fd46fa 2019-09-06 stsp !have_staged_files);
5527 ebf99748 2019-05-09 stsp } else {
5528 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5529 ebf99748 2019-05-09 stsp if (err)
5530 437adc9d 2020-12-10 yzhong goto done;
5531 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5532 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5533 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5534 3969253a 2020-03-07 stsp if (err) {
5535 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5536 437adc9d 2020-12-10 yzhong goto done;
5537 3969253a 2020-03-07 stsp }
5538 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5539 3969253a 2020-03-07 stsp if (err) {
5540 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5541 437adc9d 2020-12-10 yzhong goto done;
5542 3969253a 2020-03-07 stsp }
5543 ebf99748 2019-05-09 stsp }
5544 437adc9d 2020-12-10 yzhong free(relpath);
5545 437adc9d 2020-12-10 yzhong relpath = NULL;
5546 ebf99748 2019-05-09 stsp }
5547 437adc9d 2020-12-10 yzhong done:
5548 437adc9d 2020-12-10 yzhong free(relpath);
5549 d56d26ce 2019-05-10 stsp return err;
5550 d56d26ce 2019-05-10 stsp }
5551 735ef5ac 2019-08-03 stsp
5552 d56d26ce 2019-05-10 stsp
5553 d56d26ce 2019-05-10 stsp static const struct got_error *
5554 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5555 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5556 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5557 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5558 735ef5ac 2019-08-03 stsp int ood_errcode)
5559 d56d26ce 2019-05-10 stsp {
5560 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5561 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5562 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5563 1a36436d 2019-06-10 stsp
5564 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5565 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5566 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5567 1a36436d 2019-06-10 stsp return NULL;
5568 9bead371 2019-07-28 stsp /*
5569 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5570 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5571 9bead371 2019-07-28 stsp */
5572 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5573 a44927cc 2022-04-07 stsp if (err)
5574 a44927cc 2022-04-07 stsp goto done;
5575 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5576 9bead371 2019-07-28 stsp if (err) {
5577 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5578 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5579 1a36436d 2019-06-10 stsp goto done;
5580 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5581 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5582 1a36436d 2019-06-10 stsp } else {
5583 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5584 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5585 a44927cc 2022-04-07 stsp if (err)
5586 a44927cc 2022-04-07 stsp goto done;
5587 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5588 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5589 1a36436d 2019-06-10 stsp goto done;
5590 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5591 1a36436d 2019-06-10 stsp }
5592 1a36436d 2019-06-10 stsp done:
5593 1a36436d 2019-06-10 stsp free(id);
5594 a44927cc 2022-04-07 stsp if (commit)
5595 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5596 1a36436d 2019-06-10 stsp return err;
5597 ebf99748 2019-05-09 stsp }
5598 ebf99748 2019-05-09 stsp
5599 c4296144 2019-05-09 stsp const struct got_error *
5600 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5601 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5602 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5603 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5604 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5605 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5606 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5607 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5608 afa376bf 2019-05-09 stsp struct got_repository *repo)
5609 c4296144 2019-05-09 stsp {
5610 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5611 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5612 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5613 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5614 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5615 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5616 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5617 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5618 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5619 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5620 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5621 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5622 c4296144 2019-05-09 stsp
5623 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5624 c4296144 2019-05-09 stsp
5625 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5626 675c7539 2019-05-09 stsp
5627 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5628 588edf97 2019-05-10 stsp if (err)
5629 588edf97 2019-05-10 stsp goto done;
5630 de18fc63 2019-05-09 stsp
5631 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5632 588edf97 2019-05-10 stsp if (err)
5633 588edf97 2019-05-10 stsp goto done;
5634 588edf97 2019-05-10 stsp
5635 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5636 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5637 33ad4cbe 2019-05-12 jcs if (err)
5638 33ad4cbe 2019-05-12 jcs goto done;
5639 33ad4cbe 2019-05-12 jcs }
5640 c4296144 2019-05-09 stsp
5641 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5642 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5643 33ad4cbe 2019-05-12 jcs goto done;
5644 33ad4cbe 2019-05-12 jcs }
5645 33ad4cbe 2019-05-12 jcs
5646 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5647 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5648 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5649 cf066bf8 2019-05-09 stsp char *ondisk_path;
5650 cf066bf8 2019-05-09 stsp
5651 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5652 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5653 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5654 5f8a88c6 2019-08-03 stsp continue;
5655 5f8a88c6 2019-08-03 stsp
5656 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5657 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5658 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5659 cf066bf8 2019-05-09 stsp continue;
5660 cf066bf8 2019-05-09 stsp
5661 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5662 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5663 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5664 cf066bf8 2019-05-09 stsp goto done;
5665 cf066bf8 2019-05-09 stsp }
5666 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5667 2e1fa222 2020-07-23 stsp free(ondisk_path);
5668 2e1fa222 2020-07-23 stsp if (err)
5669 cf066bf8 2019-05-09 stsp goto done;
5670 cf066bf8 2019-05-09 stsp }
5671 036813ee 2019-05-09 stsp
5672 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5673 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5674 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5675 036813ee 2019-05-09 stsp if (err)
5676 036813ee 2019-05-09 stsp goto done;
5677 036813ee 2019-05-09 stsp
5678 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5679 de18fc63 2019-05-09 stsp if (err)
5680 de18fc63 2019-05-09 stsp goto done;
5681 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5682 f259c4c1 2021-09-24 stsp nparents++;
5683 f259c4c1 2021-09-24 stsp if (parent_id2) {
5684 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5685 f259c4c1 2021-09-24 stsp if (err)
5686 f259c4c1 2021-09-24 stsp goto done;
5687 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5688 f259c4c1 2021-09-24 stsp nparents++;
5689 f259c4c1 2021-09-24 stsp }
5690 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5691 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5692 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5693 33ad4cbe 2019-05-12 jcs free(logmsg);
5694 9d40349a 2019-05-09 stsp if (err)
5695 9d40349a 2019-05-09 stsp goto done;
5696 9d40349a 2019-05-09 stsp
5697 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5698 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5699 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5700 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5701 09f5bd90 2019-05-09 stsp goto done;
5702 09f5bd90 2019-05-09 stsp }
5703 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5704 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5705 09f5bd90 2019-05-09 stsp if (err)
5706 09f5bd90 2019-05-09 stsp goto done;
5707 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5708 ebf99748 2019-05-09 stsp if (err)
5709 ebf99748 2019-05-09 stsp goto done;
5710 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5711 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5712 09f5bd90 2019-05-09 stsp goto done;
5713 09f5bd90 2019-05-09 stsp }
5714 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5715 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5716 f2c16586 2019-05-09 stsp if (err)
5717 f2c16586 2019-05-09 stsp goto done;
5718 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5719 f2c16586 2019-05-09 stsp if (err)
5720 f2c16586 2019-05-09 stsp goto done;
5721 f2c16586 2019-05-09 stsp
5722 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5723 09f5bd90 2019-05-09 stsp if (err)
5724 09f5bd90 2019-05-09 stsp goto done;
5725 09f5bd90 2019-05-09 stsp
5726 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5727 ebf99748 2019-05-09 stsp if (err)
5728 ebf99748 2019-05-09 stsp goto done;
5729 c4296144 2019-05-09 stsp done:
5730 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5731 588edf97 2019-05-10 stsp if (head_tree)
5732 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5733 588edf97 2019-05-10 stsp if (head_commit)
5734 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5735 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5736 2f17228e 2019-05-12 stsp if (head_ref2) {
5737 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5738 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5739 2f17228e 2019-05-12 stsp err = unlockerr;
5740 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5741 39cd0ff6 2019-07-12 stsp }
5742 39cd0ff6 2019-07-12 stsp return err;
5743 39cd0ff6 2019-07-12 stsp }
5744 5c1e53bc 2019-07-28 stsp
5745 5c1e53bc 2019-07-28 stsp static const struct got_error *
5746 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5747 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5748 5c1e53bc 2019-07-28 stsp {
5749 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5750 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5751 39cd0ff6 2019-07-12 stsp
5752 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5753 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5754 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5755 5c1e53bc 2019-07-28 stsp
5756 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5757 5c1e53bc 2019-07-28 stsp ct_path++;
5758 5c1e53bc 2019-07-28 stsp
5759 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5760 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5761 5c1e53bc 2019-07-28 stsp break;
5762 5c1e53bc 2019-07-28 stsp }
5763 5c1e53bc 2019-07-28 stsp
5764 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5765 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5766 5c1e53bc 2019-07-28 stsp
5767 5c1e53bc 2019-07-28 stsp return NULL;
5768 5c1e53bc 2019-07-28 stsp }
5769 5c1e53bc 2019-07-28 stsp
5770 f0b75401 2019-08-03 stsp static const struct got_error *
5771 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5772 f0b75401 2019-08-03 stsp {
5773 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5774 f0b75401 2019-08-03 stsp
5775 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5776 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5777 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5778 f0b75401 2019-08-03 stsp }
5779 f0b75401 2019-08-03 stsp
5780 f0b75401 2019-08-03 stsp return NULL;
5781 f0b75401 2019-08-03 stsp }
5782 f0b75401 2019-08-03 stsp
5783 5f8a88c6 2019-08-03 stsp static const struct got_error *
5784 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5785 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5786 5f8a88c6 2019-08-03 stsp {
5787 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5788 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5789 5f8a88c6 2019-08-03 stsp
5790 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5791 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5792 5f8a88c6 2019-08-03 stsp continue;
5793 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5794 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5795 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5796 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5797 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5798 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5799 5f8a88c6 2019-08-03 stsp }
5800 5f8a88c6 2019-08-03 stsp
5801 5f8a88c6 2019-08-03 stsp return NULL;
5802 5f8a88c6 2019-08-03 stsp }
5803 5f8a88c6 2019-08-03 stsp
5804 39cd0ff6 2019-07-12 stsp const struct got_error *
5805 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5806 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5807 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5808 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5809 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5810 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5811 39cd0ff6 2019-07-12 stsp {
5812 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5813 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5814 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5815 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5816 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5817 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5818 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5819 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5820 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5821 39cd0ff6 2019-07-12 stsp
5822 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5823 39cd0ff6 2019-07-12 stsp
5824 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5825 39cd0ff6 2019-07-12 stsp
5826 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5827 39cd0ff6 2019-07-12 stsp if (err)
5828 39cd0ff6 2019-07-12 stsp goto done;
5829 39cd0ff6 2019-07-12 stsp
5830 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5831 39cd0ff6 2019-07-12 stsp if (err)
5832 39cd0ff6 2019-07-12 stsp goto done;
5833 39cd0ff6 2019-07-12 stsp
5834 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5835 39cd0ff6 2019-07-12 stsp if (err)
5836 39cd0ff6 2019-07-12 stsp goto done;
5837 39cd0ff6 2019-07-12 stsp
5838 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5839 39cd0ff6 2019-07-12 stsp if (err)
5840 39cd0ff6 2019-07-12 stsp goto done;
5841 39cd0ff6 2019-07-12 stsp
5842 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5843 5f8a88c6 2019-08-03 stsp &have_staged_files);
5844 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5845 5f8a88c6 2019-08-03 stsp goto done;
5846 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5847 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5848 5f8a88c6 2019-08-03 stsp if (err)
5849 5f8a88c6 2019-08-03 stsp goto done;
5850 5f8a88c6 2019-08-03 stsp }
5851 5f8a88c6 2019-08-03 stsp
5852 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5853 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5854 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5855 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5856 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5857 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5858 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5859 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5860 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5861 5c1e53bc 2019-07-28 stsp if (err)
5862 5c1e53bc 2019-07-28 stsp goto done;
5863 5c1e53bc 2019-07-28 stsp }
5864 39cd0ff6 2019-07-12 stsp
5865 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5866 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5867 39cd0ff6 2019-07-12 stsp goto done;
5868 5c1e53bc 2019-07-28 stsp }
5869 5c1e53bc 2019-07-28 stsp
5870 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5871 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5872 5c1e53bc 2019-07-28 stsp if (err)
5873 5c1e53bc 2019-07-28 stsp goto done;
5874 39cd0ff6 2019-07-12 stsp }
5875 f0b75401 2019-08-03 stsp
5876 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5877 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5878 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5879 f0b75401 2019-08-03 stsp
5880 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5881 f0b75401 2019-08-03 stsp ct_path++;
5882 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5883 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5884 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5885 b50cabdf 2019-07-12 stsp if (err)
5886 b50cabdf 2019-07-12 stsp goto done;
5887 f0b75401 2019-08-03 stsp
5888 b50cabdf 2019-07-12 stsp }
5889 b50cabdf 2019-07-12 stsp
5890 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5891 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5892 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5893 39cd0ff6 2019-07-12 stsp if (err)
5894 39cd0ff6 2019-07-12 stsp goto done;
5895 39cd0ff6 2019-07-12 stsp
5896 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5897 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5898 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5899 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5900 39cd0ff6 2019-07-12 stsp err = sync_err;
5901 39cd0ff6 2019-07-12 stsp done:
5902 39cd0ff6 2019-07-12 stsp if (fileindex)
5903 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5904 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5905 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5906 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5907 39cd0ff6 2019-07-12 stsp err = unlockerr;
5908 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5909 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5910 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5911 39cd0ff6 2019-07-12 stsp }
5912 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5913 c4296144 2019-05-09 stsp return err;
5914 8656d6c4 2019-05-20 stsp }
5915 8656d6c4 2019-05-20 stsp
5916 8656d6c4 2019-05-20 stsp const char *
5917 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5918 8656d6c4 2019-05-20 stsp {
5919 8656d6c4 2019-05-20 stsp return ct->path;
5920 8656d6c4 2019-05-20 stsp }
5921 8656d6c4 2019-05-20 stsp
5922 8656d6c4 2019-05-20 stsp unsigned int
5923 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5924 8656d6c4 2019-05-20 stsp {
5925 8656d6c4 2019-05-20 stsp return ct->status;
5926 818c7501 2019-07-11 stsp }
5927 818c7501 2019-07-11 stsp
5928 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5929 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5930 818c7501 2019-07-11 stsp struct got_repository *repo;
5931 818c7501 2019-07-11 stsp };
5932 818c7501 2019-07-11 stsp
5933 818c7501 2019-07-11 stsp static const struct got_error *
5934 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5935 818c7501 2019-07-11 stsp {
5936 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5937 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5938 818c7501 2019-07-11 stsp unsigned char status;
5939 818c7501 2019-07-11 stsp struct stat sb;
5940 818c7501 2019-07-11 stsp char *ondisk_path;
5941 818c7501 2019-07-11 stsp
5942 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5943 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5944 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5945 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5946 818c7501 2019-07-11 stsp
5947 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5948 818c7501 2019-07-11 stsp == -1)
5949 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5950 818c7501 2019-07-11 stsp
5951 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5952 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5953 818c7501 2019-07-11 stsp free(ondisk_path);
5954 818c7501 2019-07-11 stsp if (err)
5955 818c7501 2019-07-11 stsp return err;
5956 818c7501 2019-07-11 stsp
5957 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5958 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5959 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5960 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5961 818c7501 2019-07-11 stsp
5962 818c7501 2019-07-11 stsp return NULL;
5963 818c7501 2019-07-11 stsp }
5964 818c7501 2019-07-11 stsp
5965 818c7501 2019-07-11 stsp const struct got_error *
5966 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5967 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5968 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5969 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5970 818c7501 2019-07-11 stsp {
5971 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5972 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5973 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5974 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5975 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5976 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5977 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5978 818c7501 2019-07-11 stsp
5979 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5980 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5981 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5982 818c7501 2019-07-11 stsp
5983 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5984 818c7501 2019-07-11 stsp if (err)
5985 818c7501 2019-07-11 stsp return err;
5986 818c7501 2019-07-11 stsp
5987 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5988 818c7501 2019-07-11 stsp if (err)
5989 818c7501 2019-07-11 stsp goto done;
5990 818c7501 2019-07-11 stsp
5991 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5992 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5993 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5994 818c7501 2019-07-11 stsp &ok_arg);
5995 818c7501 2019-07-11 stsp if (err)
5996 818c7501 2019-07-11 stsp goto done;
5997 818c7501 2019-07-11 stsp
5998 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5999 818c7501 2019-07-11 stsp if (err)
6000 818c7501 2019-07-11 stsp goto done;
6001 818c7501 2019-07-11 stsp
6002 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6003 818c7501 2019-07-11 stsp if (err)
6004 818c7501 2019-07-11 stsp goto done;
6005 818c7501 2019-07-11 stsp
6006 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6007 818c7501 2019-07-11 stsp if (err)
6008 818c7501 2019-07-11 stsp goto done;
6009 818c7501 2019-07-11 stsp
6010 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6011 818c7501 2019-07-11 stsp 0);
6012 e51d7b55 2020-01-04 stsp if (err)
6013 e51d7b55 2020-01-04 stsp goto done;
6014 e51d7b55 2020-01-04 stsp
6015 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6016 818c7501 2019-07-11 stsp if (err)
6017 818c7501 2019-07-11 stsp goto done;
6018 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6019 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6020 e51d7b55 2020-01-04 stsp goto done;
6021 e51d7b55 2020-01-04 stsp }
6022 818c7501 2019-07-11 stsp
6023 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6024 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6025 818c7501 2019-07-11 stsp if (err)
6026 818c7501 2019-07-11 stsp goto done;
6027 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6028 818c7501 2019-07-11 stsp if (err)
6029 818c7501 2019-07-11 stsp goto done;
6030 818c7501 2019-07-11 stsp
6031 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6032 818c7501 2019-07-11 stsp
6033 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6034 818c7501 2019-07-11 stsp if (err)
6035 818c7501 2019-07-11 stsp goto done;
6036 818c7501 2019-07-11 stsp
6037 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6038 818c7501 2019-07-11 stsp if (err)
6039 818c7501 2019-07-11 stsp goto done;
6040 818c7501 2019-07-11 stsp
6041 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6042 818c7501 2019-07-11 stsp worktree->base_commit_id);
6043 818c7501 2019-07-11 stsp if (err)
6044 818c7501 2019-07-11 stsp goto done;
6045 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6046 818c7501 2019-07-11 stsp if (err)
6047 818c7501 2019-07-11 stsp goto done;
6048 818c7501 2019-07-11 stsp
6049 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6050 818c7501 2019-07-11 stsp if (err)
6051 818c7501 2019-07-11 stsp goto done;
6052 818c7501 2019-07-11 stsp done:
6053 818c7501 2019-07-11 stsp free(fileindex_path);
6054 818c7501 2019-07-11 stsp free(tmp_branch_name);
6055 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6056 818c7501 2019-07-11 stsp free(branch_ref_name);
6057 818c7501 2019-07-11 stsp if (branch_ref)
6058 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6059 818c7501 2019-07-11 stsp if (wt_branch)
6060 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6061 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6062 818c7501 2019-07-11 stsp if (err) {
6063 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6064 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6065 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6066 818c7501 2019-07-11 stsp }
6067 818c7501 2019-07-11 stsp if (*tmp_branch) {
6068 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6069 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6070 818c7501 2019-07-11 stsp }
6071 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6072 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6073 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6074 3e3a69f1 2019-07-25 stsp }
6075 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6076 818c7501 2019-07-11 stsp }
6077 818c7501 2019-07-11 stsp return err;
6078 818c7501 2019-07-11 stsp }
6079 818c7501 2019-07-11 stsp
6080 818c7501 2019-07-11 stsp const struct got_error *
6081 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6082 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6083 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6084 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6085 818c7501 2019-07-11 stsp {
6086 818c7501 2019-07-11 stsp const struct got_error *err;
6087 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6088 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6089 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6090 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6091 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6092 818c7501 2019-07-11 stsp
6093 818c7501 2019-07-11 stsp *commit_id = NULL;
6094 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6095 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6096 3e3a69f1 2019-07-25 stsp *branch = NULL;
6097 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6098 3e3a69f1 2019-07-25 stsp
6099 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6100 3e3a69f1 2019-07-25 stsp if (err)
6101 3e3a69f1 2019-07-25 stsp return err;
6102 818c7501 2019-07-11 stsp
6103 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6104 3e3a69f1 2019-07-25 stsp if (err)
6105 f032f1f7 2019-08-04 stsp goto done;
6106 f032f1f7 2019-08-04 stsp
6107 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6108 f032f1f7 2019-08-04 stsp &have_staged_files);
6109 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6110 f032f1f7 2019-08-04 stsp goto done;
6111 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6112 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6113 3e3a69f1 2019-07-25 stsp goto done;
6114 f032f1f7 2019-08-04 stsp }
6115 3e3a69f1 2019-07-25 stsp
6116 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6117 818c7501 2019-07-11 stsp if (err)
6118 f032f1f7 2019-08-04 stsp goto done;
6119 818c7501 2019-07-11 stsp
6120 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6121 818c7501 2019-07-11 stsp if (err)
6122 818c7501 2019-07-11 stsp goto done;
6123 818c7501 2019-07-11 stsp
6124 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6125 818c7501 2019-07-11 stsp if (err)
6126 818c7501 2019-07-11 stsp goto done;
6127 818c7501 2019-07-11 stsp
6128 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6129 818c7501 2019-07-11 stsp if (err)
6130 818c7501 2019-07-11 stsp goto done;
6131 818c7501 2019-07-11 stsp
6132 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6133 818c7501 2019-07-11 stsp if (err)
6134 818c7501 2019-07-11 stsp goto done;
6135 818c7501 2019-07-11 stsp
6136 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6137 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6138 818c7501 2019-07-11 stsp if (err)
6139 818c7501 2019-07-11 stsp goto done;
6140 818c7501 2019-07-11 stsp
6141 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6142 818c7501 2019-07-11 stsp if (err)
6143 818c7501 2019-07-11 stsp goto done;
6144 818c7501 2019-07-11 stsp
6145 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6146 818c7501 2019-07-11 stsp if (err)
6147 818c7501 2019-07-11 stsp goto done;
6148 818c7501 2019-07-11 stsp
6149 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6150 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6151 818c7501 2019-07-11 stsp if (err)
6152 818c7501 2019-07-11 stsp goto done;
6153 818c7501 2019-07-11 stsp
6154 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6155 818c7501 2019-07-11 stsp if (err)
6156 818c7501 2019-07-11 stsp goto done;
6157 818c7501 2019-07-11 stsp done:
6158 818c7501 2019-07-11 stsp free(commit_ref_name);
6159 818c7501 2019-07-11 stsp free(branch_ref_name);
6160 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6161 818c7501 2019-07-11 stsp if (commit_ref)
6162 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6163 818c7501 2019-07-11 stsp if (branch_ref)
6164 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6165 818c7501 2019-07-11 stsp if (err) {
6166 818c7501 2019-07-11 stsp free(*commit_id);
6167 818c7501 2019-07-11 stsp *commit_id = NULL;
6168 818c7501 2019-07-11 stsp if (*tmp_branch) {
6169 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6170 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6171 818c7501 2019-07-11 stsp }
6172 818c7501 2019-07-11 stsp if (*new_base_branch) {
6173 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6174 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6175 818c7501 2019-07-11 stsp }
6176 818c7501 2019-07-11 stsp if (*branch) {
6177 818c7501 2019-07-11 stsp got_ref_close(*branch);
6178 818c7501 2019-07-11 stsp *branch = NULL;
6179 818c7501 2019-07-11 stsp }
6180 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6181 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6182 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6183 3e3a69f1 2019-07-25 stsp }
6184 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6185 818c7501 2019-07-11 stsp }
6186 818c7501 2019-07-11 stsp return err;
6187 c4296144 2019-05-09 stsp }
6188 818c7501 2019-07-11 stsp
6189 818c7501 2019-07-11 stsp const struct got_error *
6190 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6191 818c7501 2019-07-11 stsp {
6192 818c7501 2019-07-11 stsp const struct got_error *err;
6193 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6194 818c7501 2019-07-11 stsp
6195 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6196 818c7501 2019-07-11 stsp if (err)
6197 818c7501 2019-07-11 stsp return err;
6198 818c7501 2019-07-11 stsp
6199 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6200 818c7501 2019-07-11 stsp free(tmp_branch_name);
6201 818c7501 2019-07-11 stsp return NULL;
6202 818c7501 2019-07-11 stsp }
6203 818c7501 2019-07-11 stsp
6204 818c7501 2019-07-11 stsp static const struct got_error *
6205 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6206 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6207 818c7501 2019-07-11 stsp {
6208 0ebf8283 2019-07-24 stsp *logmsg = arg;
6209 818c7501 2019-07-11 stsp return NULL;
6210 818c7501 2019-07-11 stsp }
6211 818c7501 2019-07-11 stsp
6212 818c7501 2019-07-11 stsp static const struct got_error *
6213 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6214 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6215 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6216 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6217 818c7501 2019-07-11 stsp {
6218 818c7501 2019-07-11 stsp return NULL;
6219 01757395 2019-07-12 stsp }
6220 01757395 2019-07-12 stsp
6221 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6222 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6223 01757395 2019-07-12 stsp void *progress_arg;
6224 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6225 01757395 2019-07-12 stsp };
6226 01757395 2019-07-12 stsp
6227 01757395 2019-07-12 stsp static const struct got_error *
6228 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6229 01757395 2019-07-12 stsp {
6230 01757395 2019-07-12 stsp const struct got_error *err;
6231 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6232 01757395 2019-07-12 stsp char *p;
6233 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6234 01757395 2019-07-12 stsp
6235 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6236 01757395 2019-07-12 stsp if (err)
6237 01757395 2019-07-12 stsp return err;
6238 01757395 2019-07-12 stsp
6239 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6240 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6241 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6242 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6243 01757395 2019-07-12 stsp return NULL;
6244 01757395 2019-07-12 stsp
6245 01757395 2019-07-12 stsp p = strdup(path);
6246 01757395 2019-07-12 stsp if (p == NULL)
6247 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6248 01757395 2019-07-12 stsp
6249 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6250 01757395 2019-07-12 stsp if (err || new == NULL)
6251 01757395 2019-07-12 stsp free(p);
6252 01757395 2019-07-12 stsp return err;
6253 01757395 2019-07-12 stsp }
6254 01757395 2019-07-12 stsp
6255 01757395 2019-07-12 stsp void
6256 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6257 01757395 2019-07-12 stsp {
6258 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6259 01757395 2019-07-12 stsp
6260 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6261 01757395 2019-07-12 stsp free((char *)pe->path);
6262 01757395 2019-07-12 stsp
6263 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6264 818c7501 2019-07-11 stsp }
6265 818c7501 2019-07-11 stsp
6266 0ebf8283 2019-07-24 stsp static const struct got_error *
6267 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6268 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6269 818c7501 2019-07-11 stsp {
6270 818c7501 2019-07-11 stsp const struct got_error *err;
6271 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6272 818c7501 2019-07-11 stsp
6273 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6274 818c7501 2019-07-11 stsp if (err) {
6275 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6276 818c7501 2019-07-11 stsp goto done;
6277 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6278 818c7501 2019-07-11 stsp if (err)
6279 818c7501 2019-07-11 stsp goto done;
6280 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6281 818c7501 2019-07-11 stsp if (err)
6282 818c7501 2019-07-11 stsp goto done;
6283 de05890f 2020-03-05 stsp } else if (is_rebase) {
6284 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6285 818c7501 2019-07-11 stsp int cmp;
6286 818c7501 2019-07-11 stsp
6287 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6288 818c7501 2019-07-11 stsp if (err)
6289 818c7501 2019-07-11 stsp goto done;
6290 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6291 818c7501 2019-07-11 stsp free(stored_id);
6292 818c7501 2019-07-11 stsp if (cmp != 0) {
6293 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6294 818c7501 2019-07-11 stsp goto done;
6295 818c7501 2019-07-11 stsp }
6296 818c7501 2019-07-11 stsp }
6297 0ebf8283 2019-07-24 stsp done:
6298 0ebf8283 2019-07-24 stsp if (commit_ref)
6299 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6300 0ebf8283 2019-07-24 stsp return err;
6301 0ebf8283 2019-07-24 stsp }
6302 0ebf8283 2019-07-24 stsp
6303 0ebf8283 2019-07-24 stsp static const struct got_error *
6304 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6305 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6306 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6307 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6308 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6309 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6310 0ebf8283 2019-07-24 stsp {
6311 0ebf8283 2019-07-24 stsp const struct got_error *err;
6312 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6313 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6314 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6315 818c7501 2019-07-11 stsp
6316 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6317 0ebf8283 2019-07-24 stsp
6318 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6319 0ebf8283 2019-07-24 stsp if (err)
6320 0ebf8283 2019-07-24 stsp return err;
6321 0ebf8283 2019-07-24 stsp
6322 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6323 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6324 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6325 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6326 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6327 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6328 818c7501 2019-07-11 stsp if (commit_ref)
6329 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6330 818c7501 2019-07-11 stsp return err;
6331 818c7501 2019-07-11 stsp }
6332 818c7501 2019-07-11 stsp
6333 818c7501 2019-07-11 stsp const struct got_error *
6334 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6335 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6336 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6337 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6338 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6339 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6340 818c7501 2019-07-11 stsp {
6341 0ebf8283 2019-07-24 stsp const struct got_error *err;
6342 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6343 0ebf8283 2019-07-24 stsp
6344 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6345 0ebf8283 2019-07-24 stsp if (err)
6346 0ebf8283 2019-07-24 stsp return err;
6347 0ebf8283 2019-07-24 stsp
6348 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6349 0ebf8283 2019-07-24 stsp if (err)
6350 0ebf8283 2019-07-24 stsp goto done;
6351 0ebf8283 2019-07-24 stsp
6352 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6353 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6354 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6355 0ebf8283 2019-07-24 stsp done:
6356 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6357 0ebf8283 2019-07-24 stsp return err;
6358 0ebf8283 2019-07-24 stsp }
6359 0ebf8283 2019-07-24 stsp
6360 0ebf8283 2019-07-24 stsp const struct got_error *
6361 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6362 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6363 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6364 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6365 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6366 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6367 0ebf8283 2019-07-24 stsp {
6368 0ebf8283 2019-07-24 stsp const struct got_error *err;
6369 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6370 0ebf8283 2019-07-24 stsp
6371 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6372 0ebf8283 2019-07-24 stsp if (err)
6373 0ebf8283 2019-07-24 stsp return err;
6374 0ebf8283 2019-07-24 stsp
6375 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6376 0ebf8283 2019-07-24 stsp if (err)
6377 0ebf8283 2019-07-24 stsp goto done;
6378 0ebf8283 2019-07-24 stsp
6379 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6380 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6381 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6382 0ebf8283 2019-07-24 stsp done:
6383 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6384 0ebf8283 2019-07-24 stsp return err;
6385 0ebf8283 2019-07-24 stsp }
6386 0ebf8283 2019-07-24 stsp
6387 0ebf8283 2019-07-24 stsp static const struct got_error *
6388 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6389 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6390 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6391 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6392 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6393 0ebf8283 2019-07-24 stsp {
6394 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6395 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6396 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6397 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6398 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6399 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6400 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6401 818c7501 2019-07-11 stsp
6402 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6403 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6404 a0e95631 2019-07-12 stsp
6405 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6406 818c7501 2019-07-11 stsp
6407 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6408 a0e95631 2019-07-12 stsp if (err)
6409 3e3a69f1 2019-07-25 stsp return err;
6410 a0e95631 2019-07-12 stsp
6411 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6412 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6413 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6414 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6415 01757395 2019-07-12 stsp /*
6416 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6417 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6418 6c13b005 2021-09-02 stsp *
6419 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6420 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6421 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6422 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6423 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6424 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6425 01757395 2019-07-12 stsp */
6426 01757395 2019-07-12 stsp if (merged_paths) {
6427 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6428 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6429 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6430 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6431 f2a9dc41 2019-12-13 tracey 0);
6432 01757395 2019-07-12 stsp if (err)
6433 01757395 2019-07-12 stsp goto done;
6434 01757395 2019-07-12 stsp }
6435 01757395 2019-07-12 stsp } else {
6436 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6437 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6438 01757395 2019-07-12 stsp if (err)
6439 01757395 2019-07-12 stsp goto done;
6440 01757395 2019-07-12 stsp }
6441 a0e95631 2019-07-12 stsp
6442 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6443 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6444 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6445 a0e95631 2019-07-12 stsp if (err)
6446 a0e95631 2019-07-12 stsp goto done;
6447 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6448 a0e95631 2019-07-12 stsp goto done;
6449 ff0d2220 2019-07-11 stsp }
6450 818c7501 2019-07-11 stsp
6451 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6452 edd02c5e 2019-07-11 stsp if (err)
6453 edd02c5e 2019-07-11 stsp goto done;
6454 edd02c5e 2019-07-11 stsp
6455 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6456 818c7501 2019-07-11 stsp if (err)
6457 818c7501 2019-07-11 stsp goto done;
6458 818c7501 2019-07-11 stsp
6459 5943eee2 2019-08-13 stsp if (new_logmsg) {
6460 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6461 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6462 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6463 5943eee2 2019-08-13 stsp goto done;
6464 5943eee2 2019-08-13 stsp }
6465 5943eee2 2019-08-13 stsp } else {
6466 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6467 5943eee2 2019-08-13 stsp if (err)
6468 5943eee2 2019-08-13 stsp goto done;
6469 5943eee2 2019-08-13 stsp }
6470 0ebf8283 2019-07-24 stsp
6471 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6472 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6473 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6474 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6475 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6476 a0e95631 2019-07-12 stsp if (err)
6477 a0e95631 2019-07-12 stsp goto done;
6478 a0e95631 2019-07-12 stsp
6479 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6480 a0e95631 2019-07-12 stsp if (err)
6481 a0e95631 2019-07-12 stsp goto done;
6482 a0e95631 2019-07-12 stsp
6483 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6484 a0e95631 2019-07-12 stsp if (err)
6485 a0e95631 2019-07-12 stsp goto done;
6486 a0e95631 2019-07-12 stsp
6487 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6488 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6489 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6490 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6491 a0e95631 2019-07-12 stsp err = sync_err;
6492 818c7501 2019-07-11 stsp done:
6493 a0e95631 2019-07-12 stsp free(fileindex_path);
6494 a0e95631 2019-07-12 stsp free(head_commit_id);
6495 a0e95631 2019-07-12 stsp if (head_ref)
6496 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6497 818c7501 2019-07-11 stsp if (err) {
6498 818c7501 2019-07-11 stsp free(*new_commit_id);
6499 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6500 818c7501 2019-07-11 stsp }
6501 818c7501 2019-07-11 stsp return err;
6502 818c7501 2019-07-11 stsp }
6503 818c7501 2019-07-11 stsp
6504 818c7501 2019-07-11 stsp const struct got_error *
6505 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6506 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6507 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6508 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6509 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6510 0ebf8283 2019-07-24 stsp {
6511 0ebf8283 2019-07-24 stsp const struct got_error *err;
6512 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6513 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6514 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6515 0ebf8283 2019-07-24 stsp
6516 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6517 0ebf8283 2019-07-24 stsp if (err)
6518 0ebf8283 2019-07-24 stsp return err;
6519 0ebf8283 2019-07-24 stsp
6520 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6521 0ebf8283 2019-07-24 stsp if (err)
6522 0ebf8283 2019-07-24 stsp goto done;
6523 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6524 0ebf8283 2019-07-24 stsp if (err)
6525 0ebf8283 2019-07-24 stsp goto done;
6526 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6527 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6528 0ebf8283 2019-07-24 stsp goto done;
6529 0ebf8283 2019-07-24 stsp }
6530 0ebf8283 2019-07-24 stsp
6531 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6532 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6533 0ebf8283 2019-07-24 stsp done:
6534 0ebf8283 2019-07-24 stsp if (commit_ref)
6535 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6536 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6537 0ebf8283 2019-07-24 stsp free(commit_id);
6538 0ebf8283 2019-07-24 stsp return err;
6539 0ebf8283 2019-07-24 stsp }
6540 0ebf8283 2019-07-24 stsp
6541 0ebf8283 2019-07-24 stsp const struct got_error *
6542 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6543 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6544 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6545 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6546 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6547 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6548 0ebf8283 2019-07-24 stsp {
6549 0ebf8283 2019-07-24 stsp const struct got_error *err;
6550 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6551 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6552 0ebf8283 2019-07-24 stsp
6553 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6554 0ebf8283 2019-07-24 stsp if (err)
6555 0ebf8283 2019-07-24 stsp return err;
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6558 0ebf8283 2019-07-24 stsp if (err)
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp
6561 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6562 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6563 0ebf8283 2019-07-24 stsp done:
6564 0ebf8283 2019-07-24 stsp if (commit_ref)
6565 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6566 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6567 0ebf8283 2019-07-24 stsp return err;
6568 0ebf8283 2019-07-24 stsp }
6569 0ebf8283 2019-07-24 stsp
6570 0ebf8283 2019-07-24 stsp const struct got_error *
6571 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6572 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6573 818c7501 2019-07-11 stsp {
6574 3e3a69f1 2019-07-25 stsp if (fileindex)
6575 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6576 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6577 69844fba 2019-07-11 stsp }
6578 69844fba 2019-07-11 stsp
6579 69844fba 2019-07-11 stsp static const struct got_error *
6580 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6581 69844fba 2019-07-11 stsp {
6582 69844fba 2019-07-11 stsp const struct got_error *err;
6583 69844fba 2019-07-11 stsp struct got_reference *ref;
6584 69844fba 2019-07-11 stsp
6585 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6586 69844fba 2019-07-11 stsp if (err) {
6587 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6588 69844fba 2019-07-11 stsp return NULL;
6589 69844fba 2019-07-11 stsp return err;
6590 69844fba 2019-07-11 stsp }
6591 69844fba 2019-07-11 stsp
6592 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6593 69844fba 2019-07-11 stsp got_ref_close(ref);
6594 69844fba 2019-07-11 stsp return err;
6595 818c7501 2019-07-11 stsp }
6596 818c7501 2019-07-11 stsp
6597 69844fba 2019-07-11 stsp static const struct got_error *
6598 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6599 69844fba 2019-07-11 stsp {
6600 69844fba 2019-07-11 stsp const struct got_error *err;
6601 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6602 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6603 69844fba 2019-07-11 stsp
6604 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6605 69844fba 2019-07-11 stsp if (err)
6606 69844fba 2019-07-11 stsp goto done;
6607 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6608 69844fba 2019-07-11 stsp if (err)
6609 69844fba 2019-07-11 stsp goto done;
6610 69844fba 2019-07-11 stsp
6611 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6612 69844fba 2019-07-11 stsp if (err)
6613 69844fba 2019-07-11 stsp goto done;
6614 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6615 69844fba 2019-07-11 stsp if (err)
6616 69844fba 2019-07-11 stsp goto done;
6617 69844fba 2019-07-11 stsp
6618 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6619 69844fba 2019-07-11 stsp if (err)
6620 69844fba 2019-07-11 stsp goto done;
6621 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6622 69844fba 2019-07-11 stsp if (err)
6623 69844fba 2019-07-11 stsp goto done;
6624 69844fba 2019-07-11 stsp
6625 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6626 69844fba 2019-07-11 stsp if (err)
6627 69844fba 2019-07-11 stsp goto done;
6628 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6629 69844fba 2019-07-11 stsp if (err)
6630 69844fba 2019-07-11 stsp goto done;
6631 69844fba 2019-07-11 stsp
6632 69844fba 2019-07-11 stsp done:
6633 69844fba 2019-07-11 stsp free(tmp_branch_name);
6634 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6635 69844fba 2019-07-11 stsp free(branch_ref_name);
6636 69844fba 2019-07-11 stsp free(commit_ref_name);
6637 e600f124 2021-03-21 stsp return err;
6638 e600f124 2021-03-21 stsp }
6639 e600f124 2021-03-21 stsp
6640 e600f124 2021-03-21 stsp const struct got_error *
6641 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6642 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6643 e600f124 2021-03-21 stsp {
6644 e600f124 2021-03-21 stsp const struct got_error *err;
6645 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6646 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6647 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6648 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6649 e600f124 2021-03-21 stsp char *refname = NULL;
6650 e600f124 2021-03-21 stsp
6651 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6652 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6653 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6654 e600f124 2021-03-21 stsp branch_name += 11;
6655 e600f124 2021-03-21 stsp
6656 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6657 e600f124 2021-03-21 stsp if (err)
6658 e600f124 2021-03-21 stsp return err;
6659 e600f124 2021-03-21 stsp
6660 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6661 e600f124 2021-03-21 stsp new_id_str) == -1) {
6662 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6663 e600f124 2021-03-21 stsp goto done;
6664 e600f124 2021-03-21 stsp }
6665 e600f124 2021-03-21 stsp
6666 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6667 e600f124 2021-03-21 stsp if (err)
6668 e600f124 2021-03-21 stsp goto done;
6669 e600f124 2021-03-21 stsp
6670 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6671 e600f124 2021-03-21 stsp if (err)
6672 e600f124 2021-03-21 stsp goto done;
6673 e600f124 2021-03-21 stsp
6674 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6675 e600f124 2021-03-21 stsp done:
6676 e600f124 2021-03-21 stsp free(new_id_str);
6677 e600f124 2021-03-21 stsp free(refname);
6678 e600f124 2021-03-21 stsp free(old_commit_id);
6679 e600f124 2021-03-21 stsp if (ref)
6680 e600f124 2021-03-21 stsp got_ref_close(ref);
6681 69844fba 2019-07-11 stsp return err;
6682 69844fba 2019-07-11 stsp }
6683 69844fba 2019-07-11 stsp
6684 818c7501 2019-07-11 stsp const struct got_error *
6685 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6686 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6687 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6688 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6689 818c7501 2019-07-11 stsp {
6690 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6691 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6692 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6693 818c7501 2019-07-11 stsp
6694 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6695 818c7501 2019-07-11 stsp if (err)
6696 818c7501 2019-07-11 stsp return err;
6697 e600f124 2021-03-21 stsp
6698 e600f124 2021-03-21 stsp if (create_backup) {
6699 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6700 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6701 e600f124 2021-03-21 stsp if (err)
6702 e600f124 2021-03-21 stsp goto done;
6703 e600f124 2021-03-21 stsp }
6704 818c7501 2019-07-11 stsp
6705 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6706 818c7501 2019-07-11 stsp if (err)
6707 818c7501 2019-07-11 stsp goto done;
6708 818c7501 2019-07-11 stsp
6709 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6710 818c7501 2019-07-11 stsp if (err)
6711 818c7501 2019-07-11 stsp goto done;
6712 818c7501 2019-07-11 stsp
6713 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6714 818c7501 2019-07-11 stsp if (err)
6715 818c7501 2019-07-11 stsp goto done;
6716 818c7501 2019-07-11 stsp
6717 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6718 a615e0e7 2020-12-16 stsp if (err)
6719 a615e0e7 2020-12-16 stsp goto done;
6720 a615e0e7 2020-12-16 stsp
6721 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6722 a615e0e7 2020-12-16 stsp if (err)
6723 a615e0e7 2020-12-16 stsp goto done;
6724 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6725 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6726 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6727 a615e0e7 2020-12-16 stsp err = sync_err;
6728 818c7501 2019-07-11 stsp done:
6729 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6730 a615e0e7 2020-12-16 stsp free(fileindex_path);
6731 818c7501 2019-07-11 stsp free(new_head_commit_id);
6732 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6733 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6734 818c7501 2019-07-11 stsp err = unlockerr;
6735 818c7501 2019-07-11 stsp return err;
6736 818c7501 2019-07-11 stsp }
6737 818c7501 2019-07-11 stsp
6738 818c7501 2019-07-11 stsp const struct got_error *
6739 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6740 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6741 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6742 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6743 818c7501 2019-07-11 stsp {
6744 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6745 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6746 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6747 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6748 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6749 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6750 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6751 818c7501 2019-07-11 stsp
6752 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6753 818c7501 2019-07-11 stsp if (err)
6754 818c7501 2019-07-11 stsp return err;
6755 818c7501 2019-07-11 stsp
6756 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
6757 a44927cc 2022-04-07 stsp worktree->base_commit_id);
6758 a44927cc 2022-04-07 stsp if (err)
6759 a44927cc 2022-04-07 stsp goto done;
6760 a44927cc 2022-04-07 stsp
6761 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6762 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6763 818c7501 2019-07-11 stsp if (err)
6764 818c7501 2019-07-11 stsp goto done;
6765 818c7501 2019-07-11 stsp
6766 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6767 818c7501 2019-07-11 stsp if (err)
6768 818c7501 2019-07-11 stsp goto done;
6769 818c7501 2019-07-11 stsp
6770 818c7501 2019-07-11 stsp /*
6771 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6772 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6773 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6774 818c7501 2019-07-11 stsp */
6775 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6776 818c7501 2019-07-11 stsp if (err)
6777 818c7501 2019-07-11 stsp goto done;
6778 818c7501 2019-07-11 stsp
6779 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6780 818c7501 2019-07-11 stsp if (err)
6781 818c7501 2019-07-11 stsp goto done;
6782 818c7501 2019-07-11 stsp
6783 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
6784 a44927cc 2022-04-07 stsp worktree->path_prefix);
6785 ca355955 2019-07-12 stsp if (err)
6786 ca355955 2019-07-12 stsp goto done;
6787 ca355955 2019-07-12 stsp
6788 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6789 ca355955 2019-07-12 stsp if (err)
6790 ca355955 2019-07-12 stsp goto done;
6791 ca355955 2019-07-12 stsp
6792 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6793 818c7501 2019-07-11 stsp if (err)
6794 818c7501 2019-07-11 stsp goto done;
6795 818c7501 2019-07-11 stsp
6796 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6797 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6798 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6799 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6800 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6801 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6802 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6803 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6804 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6805 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6806 55bd499d 2019-07-12 stsp if (err)
6807 1f1abb7e 2019-08-08 stsp goto sync;
6808 55bd499d 2019-07-12 stsp
6809 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6810 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6811 ca355955 2019-07-12 stsp sync:
6812 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6813 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6814 ca355955 2019-07-12 stsp err = sync_err;
6815 818c7501 2019-07-11 stsp done:
6816 818c7501 2019-07-11 stsp got_ref_close(resolved);
6817 a3a2faf2 2019-07-12 stsp free(tree_id);
6818 818c7501 2019-07-11 stsp free(commit_id);
6819 a44927cc 2022-04-07 stsp if (commit)
6820 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6821 0ebf8283 2019-07-24 stsp if (fileindex)
6822 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6823 0ebf8283 2019-07-24 stsp free(fileindex_path);
6824 0ebf8283 2019-07-24 stsp
6825 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6826 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6827 0ebf8283 2019-07-24 stsp err = unlockerr;
6828 0ebf8283 2019-07-24 stsp return err;
6829 0ebf8283 2019-07-24 stsp }
6830 0ebf8283 2019-07-24 stsp
6831 0ebf8283 2019-07-24 stsp const struct got_error *
6832 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6833 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6834 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6835 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6836 0ebf8283 2019-07-24 stsp {
6837 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6838 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6839 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6840 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6841 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6842 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6843 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6844 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6845 0ebf8283 2019-07-24 stsp
6846 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6847 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6848 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6849 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6850 0ebf8283 2019-07-24 stsp
6851 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6852 0ebf8283 2019-07-24 stsp if (err)
6853 0ebf8283 2019-07-24 stsp return err;
6854 0ebf8283 2019-07-24 stsp
6855 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6856 0ebf8283 2019-07-24 stsp if (err)
6857 0ebf8283 2019-07-24 stsp goto done;
6858 0ebf8283 2019-07-24 stsp
6859 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6860 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6861 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6862 0ebf8283 2019-07-24 stsp &ok_arg);
6863 0ebf8283 2019-07-24 stsp if (err)
6864 0ebf8283 2019-07-24 stsp goto done;
6865 0ebf8283 2019-07-24 stsp
6866 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6867 0ebf8283 2019-07-24 stsp if (err)
6868 0ebf8283 2019-07-24 stsp goto done;
6869 0ebf8283 2019-07-24 stsp
6870 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6871 0ebf8283 2019-07-24 stsp if (err)
6872 0ebf8283 2019-07-24 stsp goto done;
6873 0ebf8283 2019-07-24 stsp
6874 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6875 0ebf8283 2019-07-24 stsp worktree);
6876 0ebf8283 2019-07-24 stsp if (err)
6877 0ebf8283 2019-07-24 stsp goto done;
6878 0ebf8283 2019-07-24 stsp
6879 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6880 0ebf8283 2019-07-24 stsp 0);
6881 0ebf8283 2019-07-24 stsp if (err)
6882 0ebf8283 2019-07-24 stsp goto done;
6883 0ebf8283 2019-07-24 stsp
6884 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6885 0ebf8283 2019-07-24 stsp if (err)
6886 0ebf8283 2019-07-24 stsp goto done;
6887 0ebf8283 2019-07-24 stsp
6888 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6889 0ebf8283 2019-07-24 stsp if (err)
6890 0ebf8283 2019-07-24 stsp goto done;
6891 0ebf8283 2019-07-24 stsp
6892 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6893 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6894 0ebf8283 2019-07-24 stsp if (err)
6895 0ebf8283 2019-07-24 stsp goto done;
6896 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6897 0ebf8283 2019-07-24 stsp if (err)
6898 0ebf8283 2019-07-24 stsp goto done;
6899 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6900 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6901 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6902 0ebf8283 2019-07-24 stsp goto done;
6903 0ebf8283 2019-07-24 stsp }
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6906 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6907 0ebf8283 2019-07-24 stsp if (err)
6908 0ebf8283 2019-07-24 stsp goto done;
6909 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6910 0ebf8283 2019-07-24 stsp if (err)
6911 0ebf8283 2019-07-24 stsp goto done;
6912 0ebf8283 2019-07-24 stsp
6913 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6914 0ebf8283 2019-07-24 stsp if (err)
6915 0ebf8283 2019-07-24 stsp goto done;
6916 0ebf8283 2019-07-24 stsp done:
6917 0ebf8283 2019-07-24 stsp free(fileindex_path);
6918 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6919 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6920 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6921 0ebf8283 2019-07-24 stsp if (wt_branch)
6922 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6923 0ebf8283 2019-07-24 stsp if (err) {
6924 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6925 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6926 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6927 0ebf8283 2019-07-24 stsp }
6928 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6929 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6930 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6931 0ebf8283 2019-07-24 stsp }
6932 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6933 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6934 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6935 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6936 3e3a69f1 2019-07-25 stsp }
6937 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6938 0ebf8283 2019-07-24 stsp }
6939 0ebf8283 2019-07-24 stsp return err;
6940 0ebf8283 2019-07-24 stsp }
6941 0ebf8283 2019-07-24 stsp
6942 0ebf8283 2019-07-24 stsp const struct got_error *
6943 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6944 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6945 0ebf8283 2019-07-24 stsp {
6946 3e3a69f1 2019-07-25 stsp if (fileindex)
6947 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6948 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6949 0ebf8283 2019-07-24 stsp }
6950 0ebf8283 2019-07-24 stsp
6951 0ebf8283 2019-07-24 stsp const struct got_error *
6952 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6953 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6954 0ebf8283 2019-07-24 stsp {
6955 0ebf8283 2019-07-24 stsp const struct got_error *err;
6956 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6957 0ebf8283 2019-07-24 stsp
6958 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6959 0ebf8283 2019-07-24 stsp if (err)
6960 0ebf8283 2019-07-24 stsp return err;
6961 0ebf8283 2019-07-24 stsp
6962 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6963 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6964 0ebf8283 2019-07-24 stsp return NULL;
6965 0ebf8283 2019-07-24 stsp }
6966 0ebf8283 2019-07-24 stsp
6967 0ebf8283 2019-07-24 stsp const struct got_error *
6968 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6969 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6970 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6971 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6972 0ebf8283 2019-07-24 stsp {
6973 0ebf8283 2019-07-24 stsp const struct got_error *err;
6974 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6975 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6976 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6977 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6978 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6979 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6980 0ebf8283 2019-07-24 stsp
6981 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6982 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6983 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6984 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6985 0ebf8283 2019-07-24 stsp
6986 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6987 3e3a69f1 2019-07-25 stsp if (err)
6988 3e3a69f1 2019-07-25 stsp return err;
6989 3e3a69f1 2019-07-25 stsp
6990 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6991 3e3a69f1 2019-07-25 stsp if (err)
6992 f032f1f7 2019-08-04 stsp goto done;
6993 f032f1f7 2019-08-04 stsp
6994 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6995 f032f1f7 2019-08-04 stsp &have_staged_files);
6996 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6997 f032f1f7 2019-08-04 stsp goto done;
6998 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6999 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7000 3e3a69f1 2019-07-25 stsp goto done;
7001 f032f1f7 2019-08-04 stsp }
7002 3e3a69f1 2019-07-25 stsp
7003 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 0ebf8283 2019-07-24 stsp if (err)
7005 f032f1f7 2019-08-04 stsp goto done;
7006 0ebf8283 2019-07-24 stsp
7007 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7008 0ebf8283 2019-07-24 stsp if (err)
7009 0ebf8283 2019-07-24 stsp goto done;
7010 0ebf8283 2019-07-24 stsp
7011 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7012 0ebf8283 2019-07-24 stsp if (err)
7013 0ebf8283 2019-07-24 stsp goto done;
7014 0ebf8283 2019-07-24 stsp
7015 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7016 0ebf8283 2019-07-24 stsp worktree);
7017 0ebf8283 2019-07-24 stsp if (err)
7018 0ebf8283 2019-07-24 stsp goto done;
7019 0ebf8283 2019-07-24 stsp
7020 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7021 0ebf8283 2019-07-24 stsp if (err)
7022 0ebf8283 2019-07-24 stsp goto done;
7023 0ebf8283 2019-07-24 stsp
7024 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7025 0ebf8283 2019-07-24 stsp if (err)
7026 0ebf8283 2019-07-24 stsp goto done;
7027 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7028 0ebf8283 2019-07-24 stsp if (err)
7029 0ebf8283 2019-07-24 stsp goto done;
7030 0ebf8283 2019-07-24 stsp
7031 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7032 0ebf8283 2019-07-24 stsp if (err)
7033 0ebf8283 2019-07-24 stsp goto done;
7034 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7035 0ebf8283 2019-07-24 stsp if (err)
7036 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp
7038 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7039 0ebf8283 2019-07-24 stsp if (err)
7040 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp done:
7042 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7043 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7044 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7045 0ebf8283 2019-07-24 stsp if (commit_ref)
7046 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7047 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7048 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7049 0ebf8283 2019-07-24 stsp if (err) {
7050 0ebf8283 2019-07-24 stsp free(*commit_id);
7051 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7052 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7053 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7054 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7055 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7056 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7057 0ebf8283 2019-07-24 stsp }
7058 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7059 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7060 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7061 3e3a69f1 2019-07-25 stsp }
7062 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7063 0ebf8283 2019-07-24 stsp }
7064 0ebf8283 2019-07-24 stsp return err;
7065 0ebf8283 2019-07-24 stsp }
7066 0ebf8283 2019-07-24 stsp
7067 0ebf8283 2019-07-24 stsp static const struct got_error *
7068 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7069 0ebf8283 2019-07-24 stsp {
7070 0ebf8283 2019-07-24 stsp const struct got_error *err;
7071 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7072 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7073 0ebf8283 2019-07-24 stsp
7074 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7075 0ebf8283 2019-07-24 stsp if (err)
7076 0ebf8283 2019-07-24 stsp goto done;
7077 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7078 0ebf8283 2019-07-24 stsp if (err)
7079 0ebf8283 2019-07-24 stsp goto done;
7080 0ebf8283 2019-07-24 stsp
7081 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7082 0ebf8283 2019-07-24 stsp worktree);
7083 0ebf8283 2019-07-24 stsp if (err)
7084 0ebf8283 2019-07-24 stsp goto done;
7085 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7086 0ebf8283 2019-07-24 stsp if (err)
7087 0ebf8283 2019-07-24 stsp goto done;
7088 0ebf8283 2019-07-24 stsp
7089 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7090 0ebf8283 2019-07-24 stsp if (err)
7091 0ebf8283 2019-07-24 stsp goto done;
7092 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
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 err = delete_ref(commit_ref_name, repo);
7100 0ebf8283 2019-07-24 stsp if (err)
7101 0ebf8283 2019-07-24 stsp goto done;
7102 0ebf8283 2019-07-24 stsp done:
7103 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7104 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7105 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7106 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7107 0ebf8283 2019-07-24 stsp return err;
7108 0ebf8283 2019-07-24 stsp }
7109 0ebf8283 2019-07-24 stsp
7110 0ebf8283 2019-07-24 stsp const struct got_error *
7111 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7112 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7113 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7114 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7115 0ebf8283 2019-07-24 stsp {
7116 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7117 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7118 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7119 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7120 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7121 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7122 0ebf8283 2019-07-24 stsp
7123 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7124 0ebf8283 2019-07-24 stsp if (err)
7125 0ebf8283 2019-07-24 stsp return err;
7126 0ebf8283 2019-07-24 stsp
7127 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7128 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7129 a44927cc 2022-04-07 stsp if (err)
7130 a44927cc 2022-04-07 stsp goto done;
7131 a44927cc 2022-04-07 stsp
7132 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7133 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7134 0ebf8283 2019-07-24 stsp if (err)
7135 0ebf8283 2019-07-24 stsp goto done;
7136 0ebf8283 2019-07-24 stsp
7137 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7138 0ebf8283 2019-07-24 stsp if (err)
7139 0ebf8283 2019-07-24 stsp goto done;
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp goto done;
7144 0ebf8283 2019-07-24 stsp
7145 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7146 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7147 0ebf8283 2019-07-24 stsp if (err)
7148 0ebf8283 2019-07-24 stsp goto done;
7149 0ebf8283 2019-07-24 stsp
7150 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7151 0ebf8283 2019-07-24 stsp if (err)
7152 0ebf8283 2019-07-24 stsp goto done;
7153 0ebf8283 2019-07-24 stsp
7154 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7155 0ebf8283 2019-07-24 stsp if (err)
7156 0ebf8283 2019-07-24 stsp goto done;
7157 0ebf8283 2019-07-24 stsp
7158 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7159 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7160 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7161 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7162 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7163 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7164 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7165 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7166 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7167 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7168 0ebf8283 2019-07-24 stsp if (err)
7169 1f1abb7e 2019-08-08 stsp goto sync;
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7172 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7173 0ebf8283 2019-07-24 stsp sync:
7174 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7175 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7176 0ebf8283 2019-07-24 stsp err = sync_err;
7177 0ebf8283 2019-07-24 stsp done:
7178 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7179 0ebf8283 2019-07-24 stsp free(tree_id);
7180 818c7501 2019-07-11 stsp free(fileindex_path);
7181 818c7501 2019-07-11 stsp
7182 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7183 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7184 818c7501 2019-07-11 stsp err = unlockerr;
7185 818c7501 2019-07-11 stsp return err;
7186 818c7501 2019-07-11 stsp }
7187 0ebf8283 2019-07-24 stsp
7188 0ebf8283 2019-07-24 stsp const struct got_error *
7189 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7190 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7191 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7192 0ebf8283 2019-07-24 stsp {
7193 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7194 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7195 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7196 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7197 0ebf8283 2019-07-24 stsp
7198 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7199 0ebf8283 2019-07-24 stsp if (err)
7200 0ebf8283 2019-07-24 stsp return err;
7201 0ebf8283 2019-07-24 stsp
7202 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7203 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7204 e600f124 2021-03-21 stsp if (err)
7205 e600f124 2021-03-21 stsp goto done;
7206 e600f124 2021-03-21 stsp
7207 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7208 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp goto done;
7211 0ebf8283 2019-07-24 stsp
7212 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7213 0ebf8283 2019-07-24 stsp if (err)
7214 0ebf8283 2019-07-24 stsp goto done;
7215 0ebf8283 2019-07-24 stsp
7216 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7217 0ebf8283 2019-07-24 stsp if (err)
7218 0ebf8283 2019-07-24 stsp goto done;
7219 0ebf8283 2019-07-24 stsp
7220 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7221 0ebf8283 2019-07-24 stsp if (err)
7222 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp
7224 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7225 a615e0e7 2020-12-16 stsp if (err)
7226 a615e0e7 2020-12-16 stsp goto done;
7227 a615e0e7 2020-12-16 stsp
7228 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7229 a615e0e7 2020-12-16 stsp if (err)
7230 a615e0e7 2020-12-16 stsp goto done;
7231 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7232 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7233 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7234 a615e0e7 2020-12-16 stsp err = sync_err;
7235 0ebf8283 2019-07-24 stsp done:
7236 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7237 a615e0e7 2020-12-16 stsp free(fileindex_path);
7238 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7239 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7240 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7241 0ebf8283 2019-07-24 stsp err = unlockerr;
7242 0ebf8283 2019-07-24 stsp return err;
7243 0ebf8283 2019-07-24 stsp }
7244 0ebf8283 2019-07-24 stsp
7245 0ebf8283 2019-07-24 stsp const struct got_error *
7246 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7247 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7248 0ebf8283 2019-07-24 stsp {
7249 0ebf8283 2019-07-24 stsp const struct got_error *err;
7250 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7251 0ebf8283 2019-07-24 stsp
7252 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7253 0ebf8283 2019-07-24 stsp if (err)
7254 0ebf8283 2019-07-24 stsp return err;
7255 0ebf8283 2019-07-24 stsp
7256 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7257 0ebf8283 2019-07-24 stsp if (err)
7258 0ebf8283 2019-07-24 stsp goto done;
7259 0ebf8283 2019-07-24 stsp
7260 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7261 0ebf8283 2019-07-24 stsp done:
7262 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7263 2822a352 2019-10-15 stsp return err;
7264 2822a352 2019-10-15 stsp }
7265 2822a352 2019-10-15 stsp
7266 2822a352 2019-10-15 stsp const struct got_error *
7267 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7268 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7269 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7270 2822a352 2019-10-15 stsp struct got_repository *repo)
7271 2822a352 2019-10-15 stsp {
7272 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7273 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7274 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7275 2822a352 2019-10-15 stsp
7276 2822a352 2019-10-15 stsp *fileindex = NULL;
7277 2822a352 2019-10-15 stsp *branch_ref = NULL;
7278 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7279 2822a352 2019-10-15 stsp
7280 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7281 2822a352 2019-10-15 stsp if (err)
7282 2822a352 2019-10-15 stsp return err;
7283 2822a352 2019-10-15 stsp
7284 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7285 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7286 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7287 2822a352 2019-10-15 stsp "update -b or different branch name required");
7288 2822a352 2019-10-15 stsp goto done;
7289 2822a352 2019-10-15 stsp }
7290 2822a352 2019-10-15 stsp
7291 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7292 2822a352 2019-10-15 stsp if (err)
7293 2822a352 2019-10-15 stsp goto done;
7294 2822a352 2019-10-15 stsp
7295 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7296 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7297 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7298 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7299 2822a352 2019-10-15 stsp &ok_arg);
7300 2822a352 2019-10-15 stsp if (err)
7301 2822a352 2019-10-15 stsp goto done;
7302 2822a352 2019-10-15 stsp
7303 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7304 2822a352 2019-10-15 stsp if (err)
7305 2822a352 2019-10-15 stsp goto done;
7306 2822a352 2019-10-15 stsp
7307 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7308 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7309 2822a352 2019-10-15 stsp done:
7310 2822a352 2019-10-15 stsp if (err) {
7311 2822a352 2019-10-15 stsp if (*branch_ref) {
7312 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7313 2822a352 2019-10-15 stsp *branch_ref = NULL;
7314 2822a352 2019-10-15 stsp }
7315 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7316 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7317 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7318 2822a352 2019-10-15 stsp }
7319 2822a352 2019-10-15 stsp if (*fileindex) {
7320 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7321 2822a352 2019-10-15 stsp *fileindex = NULL;
7322 2822a352 2019-10-15 stsp }
7323 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7324 2822a352 2019-10-15 stsp }
7325 0cb83759 2019-08-03 stsp return err;
7326 0cb83759 2019-08-03 stsp }
7327 0cb83759 2019-08-03 stsp
7328 2822a352 2019-10-15 stsp const struct got_error *
7329 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7330 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7331 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7332 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7333 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7334 2822a352 2019-10-15 stsp {
7335 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7336 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7337 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7338 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7339 2822a352 2019-10-15 stsp
7340 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7341 2822a352 2019-10-15 stsp if (err)
7342 2822a352 2019-10-15 stsp goto done;
7343 2822a352 2019-10-15 stsp
7344 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7345 2822a352 2019-10-15 stsp if (err)
7346 2822a352 2019-10-15 stsp goto done;
7347 2822a352 2019-10-15 stsp
7348 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7349 a44927cc 2022-04-07 stsp if (err)
7350 a44927cc 2022-04-07 stsp goto done;
7351 a44927cc 2022-04-07 stsp
7352 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7353 2822a352 2019-10-15 stsp worktree->path_prefix);
7354 2822a352 2019-10-15 stsp if (err)
7355 2822a352 2019-10-15 stsp goto done;
7356 2822a352 2019-10-15 stsp
7357 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7358 2822a352 2019-10-15 stsp if (err)
7359 2822a352 2019-10-15 stsp goto done;
7360 2822a352 2019-10-15 stsp
7361 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7362 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7363 2822a352 2019-10-15 stsp if (err)
7364 2822a352 2019-10-15 stsp goto sync;
7365 2822a352 2019-10-15 stsp
7366 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7367 2822a352 2019-10-15 stsp if (err)
7368 2822a352 2019-10-15 stsp goto sync;
7369 2822a352 2019-10-15 stsp
7370 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7371 6e210706 2021-01-22 stsp if (err)
7372 6e210706 2021-01-22 stsp goto sync;
7373 6e210706 2021-01-22 stsp
7374 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7375 2822a352 2019-10-15 stsp sync:
7376 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7377 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7378 2822a352 2019-10-15 stsp err = sync_err;
7379 2822a352 2019-10-15 stsp
7380 2822a352 2019-10-15 stsp done:
7381 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7382 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7383 2822a352 2019-10-15 stsp err = unlockerr;
7384 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7385 2822a352 2019-10-15 stsp
7386 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7387 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7388 2822a352 2019-10-15 stsp err = unlockerr;
7389 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7390 2822a352 2019-10-15 stsp
7391 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7392 2822a352 2019-10-15 stsp free(fileindex_path);
7393 2822a352 2019-10-15 stsp free(tree_id);
7394 a44927cc 2022-04-07 stsp if (commit)
7395 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7396 2822a352 2019-10-15 stsp
7397 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7398 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7399 2822a352 2019-10-15 stsp err = unlockerr;
7400 2822a352 2019-10-15 stsp return err;
7401 2822a352 2019-10-15 stsp }
7402 2822a352 2019-10-15 stsp
7403 2822a352 2019-10-15 stsp const struct got_error *
7404 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7405 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7406 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7407 2822a352 2019-10-15 stsp {
7408 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7409 8b692cd0 2019-10-21 stsp
7410 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7411 8b692cd0 2019-10-21 stsp
7412 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7413 8b692cd0 2019-10-21 stsp
7414 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7415 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7416 8b692cd0 2019-10-21 stsp err = unlockerr;
7417 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7418 8b692cd0 2019-10-21 stsp
7419 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7420 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7421 8b692cd0 2019-10-21 stsp err = unlockerr;
7422 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7423 f259c4c1 2021-09-24 stsp
7424 f259c4c1 2021-09-24 stsp return err;
7425 f259c4c1 2021-09-24 stsp }
7426 f259c4c1 2021-09-24 stsp
7427 f259c4c1 2021-09-24 stsp const struct got_error *
7428 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7429 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7430 f259c4c1 2021-09-24 stsp {
7431 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7432 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7433 f259c4c1 2021-09-24 stsp
7434 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7435 f259c4c1 2021-09-24 stsp if (err)
7436 f259c4c1 2021-09-24 stsp goto done;
7437 8b692cd0 2019-10-21 stsp
7438 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7439 f259c4c1 2021-09-24 stsp
7440 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7441 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7442 f259c4c1 2021-09-24 stsp err = sync_err;
7443 f259c4c1 2021-09-24 stsp done:
7444 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7445 f259c4c1 2021-09-24 stsp free(fileindex_path);
7446 8b692cd0 2019-10-21 stsp return err;
7447 2822a352 2019-10-15 stsp }
7448 2822a352 2019-10-15 stsp
7449 f259c4c1 2021-09-24 stsp static const struct got_error *
7450 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7451 f259c4c1 2021-09-24 stsp {
7452 f259c4c1 2021-09-24 stsp const struct got_error *err;
7453 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7454 f259c4c1 2021-09-24 stsp
7455 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7456 f259c4c1 2021-09-24 stsp if (err)
7457 f259c4c1 2021-09-24 stsp goto done;
7458 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7459 f259c4c1 2021-09-24 stsp if (err)
7460 f259c4c1 2021-09-24 stsp goto done;
7461 f259c4c1 2021-09-24 stsp
7462 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7463 f259c4c1 2021-09-24 stsp if (err)
7464 f259c4c1 2021-09-24 stsp goto done;
7465 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7466 f259c4c1 2021-09-24 stsp if (err)
7467 f259c4c1 2021-09-24 stsp goto done;
7468 f259c4c1 2021-09-24 stsp
7469 f259c4c1 2021-09-24 stsp done:
7470 f259c4c1 2021-09-24 stsp free(branch_refname);
7471 f259c4c1 2021-09-24 stsp free(commit_refname);
7472 f259c4c1 2021-09-24 stsp return err;
7473 f259c4c1 2021-09-24 stsp }
7474 f259c4c1 2021-09-24 stsp
7475 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7476 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7477 f259c4c1 2021-09-24 stsp const char *branch_name;
7478 f259c4c1 2021-09-24 stsp };
7479 f259c4c1 2021-09-24 stsp
7480 f259c4c1 2021-09-24 stsp static const struct got_error *
7481 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7482 f259c4c1 2021-09-24 stsp void *arg)
7483 f259c4c1 2021-09-24 stsp {
7484 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7485 f259c4c1 2021-09-24 stsp
7486 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7487 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7488 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7489 f259c4c1 2021-09-24 stsp
7490 f259c4c1 2021-09-24 stsp return NULL;
7491 f259c4c1 2021-09-24 stsp }
7492 f259c4c1 2021-09-24 stsp
7493 f259c4c1 2021-09-24 stsp
7494 f259c4c1 2021-09-24 stsp const struct got_error *
7495 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7496 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7497 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7498 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7499 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7500 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7501 f259c4c1 2021-09-24 stsp {
7502 f259c4c1 2021-09-24 stsp const struct got_error *err;
7503 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7504 f259c4c1 2021-09-24 stsp
7505 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7506 f259c4c1 2021-09-24 stsp if (err)
7507 f259c4c1 2021-09-24 stsp goto done;
7508 f259c4c1 2021-09-24 stsp
7509 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7510 f259c4c1 2021-09-24 stsp worktree);
7511 f259c4c1 2021-09-24 stsp if (err)
7512 f259c4c1 2021-09-24 stsp goto done;
7513 f259c4c1 2021-09-24 stsp
7514 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7515 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7516 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7517 f259c4c1 2021-09-24 stsp done:
7518 f259c4c1 2021-09-24 stsp free(fileindex_path);
7519 f259c4c1 2021-09-24 stsp return err;
7520 f259c4c1 2021-09-24 stsp }
7521 f259c4c1 2021-09-24 stsp
7522 f259c4c1 2021-09-24 stsp const struct got_error *
7523 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7524 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7525 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7526 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7527 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7528 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7529 0ff8d236 2021-09-28 stsp
7530 f259c4c1 2021-09-24 stsp {
7531 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7532 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7533 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7534 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7535 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7536 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7537 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7538 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7539 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7540 f259c4c1 2021-09-24 stsp
7541 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7542 f259c4c1 2021-09-24 stsp
7543 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7544 f259c4c1 2021-09-24 stsp
7545 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7546 f259c4c1 2021-09-24 stsp if (err)
7547 f259c4c1 2021-09-24 stsp goto done;
7548 f259c4c1 2021-09-24 stsp
7549 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7550 f259c4c1 2021-09-24 stsp if (err)
7551 f259c4c1 2021-09-24 stsp goto done;
7552 f259c4c1 2021-09-24 stsp
7553 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7554 f259c4c1 2021-09-24 stsp if (err)
7555 f259c4c1 2021-09-24 stsp goto done;
7556 f259c4c1 2021-09-24 stsp
7557 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7558 f259c4c1 2021-09-24 stsp &have_staged_files);
7559 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7560 f259c4c1 2021-09-24 stsp goto done;
7561 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7562 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7563 f259c4c1 2021-09-24 stsp goto done;
7564 f259c4c1 2021-09-24 stsp }
7565 f259c4c1 2021-09-24 stsp
7566 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7567 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7568 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7569 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7570 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7571 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7572 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7573 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7574 f259c4c1 2021-09-24 stsp if (err)
7575 f259c4c1 2021-09-24 stsp goto done;
7576 f259c4c1 2021-09-24 stsp
7577 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7578 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7579 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7580 f259c4c1 2021-09-24 stsp goto done;
7581 f259c4c1 2021-09-24 stsp }
7582 f259c4c1 2021-09-24 stsp
7583 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7584 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7585 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7586 f259c4c1 2021-09-24 stsp
7587 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7588 f259c4c1 2021-09-24 stsp ct_path++;
7589 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7590 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7591 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7592 f259c4c1 2021-09-24 stsp if (err)
7593 f259c4c1 2021-09-24 stsp goto done;
7594 f259c4c1 2021-09-24 stsp
7595 f259c4c1 2021-09-24 stsp }
7596 f259c4c1 2021-09-24 stsp
7597 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7598 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7599 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7600 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7601 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7602 f259c4c1 2021-09-24 stsp if (err)
7603 f259c4c1 2021-09-24 stsp goto done;
7604 f259c4c1 2021-09-24 stsp
7605 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7606 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7607 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7608 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7609 f259c4c1 2021-09-24 stsp err = sync_err;
7610 f259c4c1 2021-09-24 stsp done:
7611 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7612 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7613 f259c4c1 2021-09-24 stsp free_commitable(ct);
7614 f259c4c1 2021-09-24 stsp }
7615 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7616 f259c4c1 2021-09-24 stsp free(fileindex_path);
7617 f259c4c1 2021-09-24 stsp return err;
7618 f259c4c1 2021-09-24 stsp }
7619 f259c4c1 2021-09-24 stsp
7620 f259c4c1 2021-09-24 stsp const struct got_error *
7621 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7622 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7623 f259c4c1 2021-09-24 stsp {
7624 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7625 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7626 f259c4c1 2021-09-24 stsp
7627 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7628 f259c4c1 2021-09-24 stsp if (err)
7629 f259c4c1 2021-09-24 stsp goto done;
7630 f259c4c1 2021-09-24 stsp
7631 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7632 f259c4c1 2021-09-24 stsp if (err)
7633 f259c4c1 2021-09-24 stsp goto done;
7634 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7635 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7636 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7637 f259c4c1 2021-09-24 stsp err = sync_err;
7638 f259c4c1 2021-09-24 stsp done:
7639 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7640 f259c4c1 2021-09-24 stsp free(fileindex_path);
7641 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7642 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7643 f259c4c1 2021-09-24 stsp err = unlockerr;
7644 f259c4c1 2021-09-24 stsp return err;
7645 f259c4c1 2021-09-24 stsp }
7646 f259c4c1 2021-09-24 stsp
7647 f259c4c1 2021-09-24 stsp const struct got_error *
7648 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7649 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7650 f259c4c1 2021-09-24 stsp {
7651 f259c4c1 2021-09-24 stsp const struct got_error *err;
7652 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7653 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7654 f259c4c1 2021-09-24 stsp
7655 f259c4c1 2021-09-24 stsp *in_progress = 0;
7656 f259c4c1 2021-09-24 stsp
7657 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7658 f259c4c1 2021-09-24 stsp if (err)
7659 f259c4c1 2021-09-24 stsp return err;
7660 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7661 793fcac3 2021-09-24 stsp free(branch_refname);
7662 f259c4c1 2021-09-24 stsp if (err) {
7663 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7664 f259c4c1 2021-09-24 stsp return err;
7665 f259c4c1 2021-09-24 stsp } else
7666 f259c4c1 2021-09-24 stsp *in_progress = 1;
7667 f259c4c1 2021-09-24 stsp
7668 f259c4c1 2021-09-24 stsp return NULL;
7669 f259c4c1 2021-09-24 stsp }
7670 f259c4c1 2021-09-24 stsp
7671 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7672 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7673 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7674 f259c4c1 2021-09-24 stsp {
7675 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7676 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7677 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7678 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7679 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7680 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7681 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7682 f259c4c1 2021-09-24 stsp
7683 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7684 f259c4c1 2021-09-24 stsp
7685 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7686 f259c4c1 2021-09-24 stsp if (err)
7687 f259c4c1 2021-09-24 stsp return err;
7688 f259c4c1 2021-09-24 stsp
7689 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7690 f259c4c1 2021-09-24 stsp if (err)
7691 f259c4c1 2021-09-24 stsp goto done;
7692 f259c4c1 2021-09-24 stsp
7693 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7694 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7695 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7696 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7697 f259c4c1 2021-09-24 stsp &ok_arg);
7698 f259c4c1 2021-09-24 stsp if (err)
7699 f259c4c1 2021-09-24 stsp goto done;
7700 f259c4c1 2021-09-24 stsp
7701 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7702 f259c4c1 2021-09-24 stsp if (err)
7703 f259c4c1 2021-09-24 stsp return err;
7704 f259c4c1 2021-09-24 stsp
7705 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7706 f259c4c1 2021-09-24 stsp if (err)
7707 f259c4c1 2021-09-24 stsp return err;
7708 f259c4c1 2021-09-24 stsp
7709 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7710 f259c4c1 2021-09-24 stsp 0);
7711 f259c4c1 2021-09-24 stsp if (err)
7712 f259c4c1 2021-09-24 stsp goto done;
7713 f259c4c1 2021-09-24 stsp
7714 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7715 f259c4c1 2021-09-24 stsp if (err)
7716 f259c4c1 2021-09-24 stsp goto done;
7717 f259c4c1 2021-09-24 stsp
7718 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7719 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7720 f259c4c1 2021-09-24 stsp goto done;
7721 f259c4c1 2021-09-24 stsp }
7722 f259c4c1 2021-09-24 stsp
7723 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7724 f259c4c1 2021-09-24 stsp if (err)
7725 f259c4c1 2021-09-24 stsp goto done;
7726 f259c4c1 2021-09-24 stsp
7727 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7728 f259c4c1 2021-09-24 stsp if (err)
7729 f259c4c1 2021-09-24 stsp goto done;
7730 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7731 f259c4c1 2021-09-24 stsp if (err)
7732 f259c4c1 2021-09-24 stsp goto done;
7733 f259c4c1 2021-09-24 stsp
7734 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7735 f259c4c1 2021-09-24 stsp if (err)
7736 f259c4c1 2021-09-24 stsp goto done;
7737 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7738 f259c4c1 2021-09-24 stsp if (err)
7739 f259c4c1 2021-09-24 stsp goto done;
7740 f259c4c1 2021-09-24 stsp
7741 f259c4c1 2021-09-24 stsp done:
7742 f259c4c1 2021-09-24 stsp free(branch_refname);
7743 f259c4c1 2021-09-24 stsp free(commit_refname);
7744 f259c4c1 2021-09-24 stsp free(fileindex_path);
7745 f259c4c1 2021-09-24 stsp if (branch_ref)
7746 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7747 f259c4c1 2021-09-24 stsp if (commit_ref)
7748 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7749 f259c4c1 2021-09-24 stsp if (wt_branch)
7750 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7751 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7752 f259c4c1 2021-09-24 stsp if (err) {
7753 f259c4c1 2021-09-24 stsp if (*fileindex) {
7754 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7755 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7756 f259c4c1 2021-09-24 stsp }
7757 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7758 f259c4c1 2021-09-24 stsp }
7759 f259c4c1 2021-09-24 stsp return err;
7760 f259c4c1 2021-09-24 stsp }
7761 f259c4c1 2021-09-24 stsp
7762 f259c4c1 2021-09-24 stsp const struct got_error *
7763 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7764 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7765 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7766 f259c4c1 2021-09-24 stsp {
7767 f259c4c1 2021-09-24 stsp const struct got_error *err;
7768 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7769 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7770 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7771 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7772 f259c4c1 2021-09-24 stsp
7773 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7774 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7775 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7776 f259c4c1 2021-09-24 stsp
7777 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7778 f259c4c1 2021-09-24 stsp if (err)
7779 f259c4c1 2021-09-24 stsp return err;
7780 f259c4c1 2021-09-24 stsp
7781 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7782 f259c4c1 2021-09-24 stsp if (err)
7783 f259c4c1 2021-09-24 stsp goto done;
7784 f259c4c1 2021-09-24 stsp
7785 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7786 f259c4c1 2021-09-24 stsp &have_staged_files);
7787 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7788 f259c4c1 2021-09-24 stsp goto done;
7789 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7790 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7791 f259c4c1 2021-09-24 stsp goto done;
7792 f259c4c1 2021-09-24 stsp }
7793 f259c4c1 2021-09-24 stsp
7794 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7795 f259c4c1 2021-09-24 stsp if (err)
7796 f259c4c1 2021-09-24 stsp goto done;
7797 f259c4c1 2021-09-24 stsp
7798 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7799 f259c4c1 2021-09-24 stsp if (err)
7800 f259c4c1 2021-09-24 stsp goto done;
7801 f259c4c1 2021-09-24 stsp
7802 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7803 f259c4c1 2021-09-24 stsp if (err)
7804 f259c4c1 2021-09-24 stsp goto done;
7805 f259c4c1 2021-09-24 stsp
7806 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7807 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7808 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7809 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7810 f259c4c1 2021-09-24 stsp goto done;
7811 f259c4c1 2021-09-24 stsp }
7812 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7813 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7814 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7815 f259c4c1 2021-09-24 stsp goto done;
7816 f259c4c1 2021-09-24 stsp }
7817 f259c4c1 2021-09-24 stsp
7818 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7819 f259c4c1 2021-09-24 stsp if (err)
7820 f259c4c1 2021-09-24 stsp goto done;
7821 f259c4c1 2021-09-24 stsp
7822 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7823 f259c4c1 2021-09-24 stsp if (err)
7824 f259c4c1 2021-09-24 stsp goto done;
7825 f259c4c1 2021-09-24 stsp done:
7826 f259c4c1 2021-09-24 stsp free(commit_refname);
7827 f259c4c1 2021-09-24 stsp free(branch_refname);
7828 f259c4c1 2021-09-24 stsp free(fileindex_path);
7829 f259c4c1 2021-09-24 stsp if (commit_ref)
7830 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7831 f259c4c1 2021-09-24 stsp if (branch_ref)
7832 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7833 f259c4c1 2021-09-24 stsp if (err) {
7834 f259c4c1 2021-09-24 stsp if (*branch_name) {
7835 f259c4c1 2021-09-24 stsp free(*branch_name);
7836 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7837 f259c4c1 2021-09-24 stsp }
7838 f259c4c1 2021-09-24 stsp free(*branch_tip);
7839 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7840 f259c4c1 2021-09-24 stsp if (*fileindex) {
7841 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7842 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7843 f259c4c1 2021-09-24 stsp }
7844 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7845 f259c4c1 2021-09-24 stsp }
7846 f259c4c1 2021-09-24 stsp return err;
7847 f259c4c1 2021-09-24 stsp }
7848 f259c4c1 2021-09-24 stsp
7849 f259c4c1 2021-09-24 stsp const struct got_error *
7850 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7851 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7852 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7853 f259c4c1 2021-09-24 stsp {
7854 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7855 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7856 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7857 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7858 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7859 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7860 f259c4c1 2021-09-24 stsp
7861 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7862 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7863 a44927cc 2022-04-07 stsp if (err)
7864 a44927cc 2022-04-07 stsp goto done;
7865 a44927cc 2022-04-07 stsp
7866 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7867 a44927cc 2022-04-07 stsp worktree->path_prefix);
7868 f259c4c1 2021-09-24 stsp if (err)
7869 f259c4c1 2021-09-24 stsp goto done;
7870 f259c4c1 2021-09-24 stsp
7871 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7872 f259c4c1 2021-09-24 stsp if (err)
7873 f259c4c1 2021-09-24 stsp goto done;
7874 f259c4c1 2021-09-24 stsp
7875 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7876 f259c4c1 2021-09-24 stsp if (err)
7877 f259c4c1 2021-09-24 stsp goto done;
7878 f259c4c1 2021-09-24 stsp
7879 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7880 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7881 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7882 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7883 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7884 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7885 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7886 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7887 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7888 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7889 f259c4c1 2021-09-24 stsp if (err)
7890 f259c4c1 2021-09-24 stsp goto sync;
7891 f259c4c1 2021-09-24 stsp
7892 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7893 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7894 f259c4c1 2021-09-24 stsp sync:
7895 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7896 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7897 f259c4c1 2021-09-24 stsp err = sync_err;
7898 f259c4c1 2021-09-24 stsp done:
7899 f259c4c1 2021-09-24 stsp free(tree_id);
7900 f259c4c1 2021-09-24 stsp free(commit_id);
7901 a44927cc 2022-04-07 stsp if (commit)
7902 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7903 f259c4c1 2021-09-24 stsp if (fileindex)
7904 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7905 f259c4c1 2021-09-24 stsp free(fileindex_path);
7906 f259c4c1 2021-09-24 stsp
7907 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7908 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7909 f259c4c1 2021-09-24 stsp err = unlockerr;
7910 f259c4c1 2021-09-24 stsp return err;
7911 f259c4c1 2021-09-24 stsp }
7912 f259c4c1 2021-09-24 stsp
7913 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7914 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7915 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7916 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7917 2db2652d 2019-08-07 stsp struct got_repository *repo;
7918 2db2652d 2019-08-07 stsp int have_changes;
7919 2db2652d 2019-08-07 stsp };
7920 2db2652d 2019-08-07 stsp
7921 2db2652d 2019-08-07 stsp const struct got_error *
7922 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7923 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7924 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7925 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7926 735ef5ac 2019-08-03 stsp {
7927 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7928 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7929 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7930 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7931 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7932 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7933 8b13ce36 2019-08-08 stsp
7934 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7935 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7936 8b13ce36 2019-08-08 stsp return NULL;
7937 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7938 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7939 735ef5ac 2019-08-03 stsp
7940 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7941 735ef5ac 2019-08-03 stsp if (ie == NULL)
7942 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7943 735ef5ac 2019-08-03 stsp
7944 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7945 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7946 735ef5ac 2019-08-03 stsp relpath) == -1)
7947 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7948 735ef5ac 2019-08-03 stsp
7949 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7950 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7951 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7952 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7953 735ef5ac 2019-08-03 stsp }
7954 735ef5ac 2019-08-03 stsp
7955 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7956 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7957 3aa5969e 2019-08-06 stsp goto done;
7958 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7959 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7960 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7961 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7962 735ef5ac 2019-08-03 stsp goto done;
7963 3aa5969e 2019-08-06 stsp }
7964 735ef5ac 2019-08-03 stsp
7965 2db2652d 2019-08-07 stsp a->have_changes = 1;
7966 2db2652d 2019-08-07 stsp
7967 735ef5ac 2019-08-03 stsp p = in_repo_path;
7968 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7969 735ef5ac 2019-08-03 stsp p++;
7970 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7971 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7972 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7973 735ef5ac 2019-08-03 stsp done:
7974 735ef5ac 2019-08-03 stsp free(in_repo_path);
7975 dc424a06 2019-08-07 stsp return err;
7976 dc424a06 2019-08-07 stsp }
7977 dc424a06 2019-08-07 stsp
7978 2db2652d 2019-08-07 stsp struct stage_path_arg {
7979 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7980 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7981 2db2652d 2019-08-07 stsp struct got_repository *repo;
7982 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7983 2db2652d 2019-08-07 stsp void *status_arg;
7984 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7985 2db2652d 2019-08-07 stsp void *patch_arg;
7986 7b5dc508 2019-10-28 stsp int staged_something;
7987 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7988 2db2652d 2019-08-07 stsp };
7989 2db2652d 2019-08-07 stsp
7990 2db2652d 2019-08-07 stsp static const struct got_error *
7991 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7992 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7993 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7994 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7995 0cb83759 2019-08-03 stsp {
7996 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7997 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7998 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7999 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8000 0cb83759 2019-08-03 stsp uint32_t stage;
8001 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8002 0aeb8099 2020-07-23 stsp struct stat sb;
8003 8b13ce36 2019-08-08 stsp
8004 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8005 8b13ce36 2019-08-08 stsp return NULL;
8006 0cb83759 2019-08-03 stsp
8007 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8008 d3e7c587 2019-08-03 stsp if (ie == NULL)
8009 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8010 0cb83759 2019-08-03 stsp
8011 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8012 2db2652d 2019-08-07 stsp relpath)== -1)
8013 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8014 0cb83759 2019-08-03 stsp
8015 0cb83759 2019-08-03 stsp switch (status) {
8016 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8017 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8018 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8019 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8020 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8021 0aeb8099 2020-07-23 stsp break;
8022 0aeb8099 2020-07-23 stsp }
8023 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8024 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8025 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8026 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8027 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8028 dc424a06 2019-08-07 stsp if (err)
8029 dc424a06 2019-08-07 stsp break;
8030 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8031 dc424a06 2019-08-07 stsp break;
8032 dc424a06 2019-08-07 stsp } else {
8033 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8034 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8035 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8036 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8037 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8038 dc424a06 2019-08-07 stsp break;
8039 dc424a06 2019-08-07 stsp }
8040 dc424a06 2019-08-07 stsp }
8041 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8042 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8043 0cb83759 2019-08-03 stsp if (err)
8044 dc424a06 2019-08-07 stsp break;
8045 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8046 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8047 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8048 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8049 0cb83759 2019-08-03 stsp else
8050 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8051 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8052 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8053 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8054 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8055 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8056 35213c7c 2020-07-23 stsp ssize_t target_len;
8057 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8058 35213c7c 2020-07-23 stsp sizeof(target_path));
8059 35213c7c 2020-07-23 stsp if (target_len == -1) {
8060 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8061 35213c7c 2020-07-23 stsp ondisk_path);
8062 35213c7c 2020-07-23 stsp break;
8063 35213c7c 2020-07-23 stsp }
8064 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8065 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8066 35213c7c 2020-07-23 stsp a->worktree->root_path);
8067 35213c7c 2020-07-23 stsp if (err)
8068 35213c7c 2020-07-23 stsp break;
8069 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8070 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8071 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8072 35213c7c 2020-07-23 stsp break;
8073 35213c7c 2020-07-23 stsp }
8074 35213c7c 2020-07-23 stsp }
8075 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8076 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8077 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8078 35213c7c 2020-07-23 stsp else
8079 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8080 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8081 0aeb8099 2020-07-23 stsp } else {
8082 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8083 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8084 0aeb8099 2020-07-23 stsp }
8085 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8086 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8087 dc424a06 2019-08-07 stsp break;
8088 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8089 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8090 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8091 9fdde394 2022-06-04 op if (err)
8092 9fdde394 2022-06-04 op break;
8093 9fdde394 2022-06-04 op /*
8094 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8095 9fdde394 2022-06-04 op * implicitly unstage the file.
8096 9fdde394 2022-06-04 op */
8097 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8098 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8099 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8100 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8101 9fdde394 2022-06-04 op }
8102 0cb83759 2019-08-03 stsp break;
8103 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8104 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8105 d3e7c587 2019-08-03 stsp break;
8106 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8107 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8108 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8109 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8110 dc424a06 2019-08-07 stsp if (err)
8111 dc424a06 2019-08-07 stsp break;
8112 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8113 88f33a19 2019-08-08 stsp break;
8114 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8115 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8116 dc424a06 2019-08-07 stsp break;
8117 88f33a19 2019-08-08 stsp }
8118 dc424a06 2019-08-07 stsp }
8119 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8120 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8121 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8122 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8123 dc424a06 2019-08-07 stsp break;
8124 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8125 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8126 12463d8b 2019-12-13 stsp de_name);
8127 0cb83759 2019-08-03 stsp break;
8128 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8129 d3e7c587 2019-08-03 stsp break;
8130 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8131 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8132 ebf48fd5 2019-08-03 stsp break;
8133 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8134 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8135 2a06fe5f 2019-08-24 stsp break;
8136 0cb83759 2019-08-03 stsp default:
8137 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8138 537ac44b 2019-08-03 stsp break;
8139 0cb83759 2019-08-03 stsp }
8140 dc424a06 2019-08-07 stsp
8141 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8142 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8143 dc424a06 2019-08-07 stsp free(path_content);
8144 2db2652d 2019-08-07 stsp free(ondisk_path);
8145 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8146 0ebf8283 2019-07-24 stsp return err;
8147 0ebf8283 2019-07-24 stsp }
8148 0cb83759 2019-08-03 stsp
8149 0cb83759 2019-08-03 stsp const struct got_error *
8150 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8151 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8152 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8153 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8154 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8155 0cb83759 2019-08-03 stsp {
8156 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8157 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8158 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8159 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8160 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8161 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8162 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8163 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8164 0cb83759 2019-08-03 stsp
8165 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8166 0cb83759 2019-08-03 stsp if (err)
8167 0cb83759 2019-08-03 stsp return err;
8168 0cb83759 2019-08-03 stsp
8169 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8170 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8171 735ef5ac 2019-08-03 stsp if (err)
8172 735ef5ac 2019-08-03 stsp goto done;
8173 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8174 735ef5ac 2019-08-03 stsp if (err)
8175 735ef5ac 2019-08-03 stsp goto done;
8176 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8177 0cb83759 2019-08-03 stsp if (err)
8178 0cb83759 2019-08-03 stsp goto done;
8179 0cb83759 2019-08-03 stsp
8180 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8181 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8182 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8183 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8184 2db2652d 2019-08-07 stsp oka.repo = repo;
8185 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8186 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8187 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8188 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8189 735ef5ac 2019-08-03 stsp if (err)
8190 735ef5ac 2019-08-03 stsp goto done;
8191 735ef5ac 2019-08-03 stsp }
8192 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8193 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8194 2db2652d 2019-08-07 stsp goto done;
8195 2db2652d 2019-08-07 stsp }
8196 735ef5ac 2019-08-03 stsp
8197 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8198 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8199 2db2652d 2019-08-07 stsp spa.repo = repo;
8200 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8201 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8202 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8203 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8204 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8205 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8206 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8207 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8208 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8209 42005733 2019-08-03 stsp if (err)
8210 2db2652d 2019-08-07 stsp goto done;
8211 7b5dc508 2019-10-28 stsp }
8212 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8213 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8214 7b5dc508 2019-10-28 stsp goto done;
8215 0cb83759 2019-08-03 stsp }
8216 0cb83759 2019-08-03 stsp
8217 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8218 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8219 0cb83759 2019-08-03 stsp err = sync_err;
8220 0cb83759 2019-08-03 stsp done:
8221 735ef5ac 2019-08-03 stsp if (head_ref)
8222 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8223 735ef5ac 2019-08-03 stsp free(head_commit_id);
8224 0cb83759 2019-08-03 stsp free(fileindex_path);
8225 0cb83759 2019-08-03 stsp if (fileindex)
8226 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8227 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8228 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8229 0cb83759 2019-08-03 stsp err = unlockerr;
8230 0cb83759 2019-08-03 stsp return err;
8231 0cb83759 2019-08-03 stsp }
8232 ad493afc 2019-08-03 stsp
8233 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8234 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8235 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8236 ad493afc 2019-08-03 stsp struct got_repository *repo;
8237 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8238 ad493afc 2019-08-03 stsp void *progress_arg;
8239 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8240 2e1f37b0 2019-08-08 stsp void *patch_arg;
8241 ad493afc 2019-08-03 stsp };
8242 2e1f37b0 2019-08-08 stsp
8243 2e1f37b0 2019-08-08 stsp static const struct got_error *
8244 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8245 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8246 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8247 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8248 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8249 2e1f37b0 2019-08-08 stsp {
8250 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8251 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8252 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8253 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8254 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8255 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8256 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8257 2e1f37b0 2019-08-08 stsp
8258 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8259 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8260 2e1f37b0 2019-08-08 stsp
8261 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8262 2e1f37b0 2019-08-08 stsp if (err)
8263 2e1f37b0 2019-08-08 stsp return err;
8264 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8265 2e1f37b0 2019-08-08 stsp if (err)
8266 2e1f37b0 2019-08-08 stsp goto done;
8267 2e1f37b0 2019-08-08 stsp
8268 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8269 2e1f37b0 2019-08-08 stsp if (err)
8270 2e1f37b0 2019-08-08 stsp goto done;
8271 2e1f37b0 2019-08-08 stsp
8272 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8273 2e1f37b0 2019-08-08 stsp if (err)
8274 2e1f37b0 2019-08-08 stsp goto done;
8275 2e1f37b0 2019-08-08 stsp
8276 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8277 2e1f37b0 2019-08-08 stsp if (err)
8278 2e1f37b0 2019-08-08 stsp goto done;
8279 2e1f37b0 2019-08-08 stsp
8280 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8281 2e1f37b0 2019-08-08 stsp if (err)
8282 2e1f37b0 2019-08-08 stsp goto done;
8283 ad493afc 2019-08-03 stsp
8284 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8285 2e1f37b0 2019-08-08 stsp if (err)
8286 2e1f37b0 2019-08-08 stsp goto done;
8287 2e1f37b0 2019-08-08 stsp
8288 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8289 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8290 2e1f37b0 2019-08-08 stsp if (err)
8291 2e1f37b0 2019-08-08 stsp goto done;
8292 2e1f37b0 2019-08-08 stsp
8293 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8294 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8295 2e1f37b0 2019-08-08 stsp if (err)
8296 2e1f37b0 2019-08-08 stsp goto done;
8297 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8298 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8299 2e1f37b0 2019-08-08 stsp if (err)
8300 2e1f37b0 2019-08-08 stsp goto done;
8301 2e1f37b0 2019-08-08 stsp
8302 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8303 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8304 2e1f37b0 2019-08-08 stsp goto done;
8305 2e1f37b0 2019-08-08 stsp }
8306 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8307 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8308 2e1f37b0 2019-08-08 stsp goto done;
8309 2e1f37b0 2019-08-08 stsp }
8310 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8311 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8312 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8313 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8314 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8315 fe621944 2020-11-10 stsp nchanges++;
8316 fe621944 2020-11-10 stsp }
8317 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8318 2e1f37b0 2019-08-08 stsp int choice;
8319 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8320 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8321 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8322 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8323 2e1f37b0 2019-08-08 stsp if (err)
8324 2e1f37b0 2019-08-08 stsp goto done;
8325 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8326 2e1f37b0 2019-08-08 stsp have_content = 1;
8327 19e4b907 2019-08-08 stsp else
8328 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8329 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8330 2e1f37b0 2019-08-08 stsp break;
8331 2e1f37b0 2019-08-08 stsp }
8332 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8333 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8334 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8335 2e1f37b0 2019-08-08 stsp done:
8336 2e1f37b0 2019-08-08 stsp free(label1);
8337 2e1f37b0 2019-08-08 stsp if (blob)
8338 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8339 2e1f37b0 2019-08-08 stsp if (staged_blob)
8340 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8341 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8342 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8343 fe621944 2020-11-10 stsp err = free_err;
8344 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8345 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8346 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8347 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8348 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8349 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8350 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8351 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8352 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8353 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8354 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8355 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8356 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8357 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8358 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8359 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8360 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8361 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8362 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8363 2e1f37b0 2019-08-08 stsp }
8364 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8365 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8366 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8367 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8368 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8369 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8370 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8371 2e1f37b0 2019-08-08 stsp }
8372 2e1f37b0 2019-08-08 stsp free(path1);
8373 2e1f37b0 2019-08-08 stsp free(path2);
8374 fda8017d 2020-07-23 stsp return err;
8375 fda8017d 2020-07-23 stsp }
8376 fda8017d 2020-07-23 stsp
8377 fda8017d 2020-07-23 stsp static const struct got_error *
8378 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8379 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8380 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8381 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8382 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8383 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8384 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8385 fda8017d 2020-07-23 stsp {
8386 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8387 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8388 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8389 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8390 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8391 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8392 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8393 fda8017d 2020-07-23 stsp struct stat sb;
8394 fda8017d 2020-07-23 stsp
8395 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8396 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8397 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8398 fda8017d 2020-07-23 stsp if (err)
8399 fda8017d 2020-07-23 stsp return err;
8400 fda8017d 2020-07-23 stsp
8401 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8402 fda8017d 2020-07-23 stsp return NULL;
8403 fda8017d 2020-07-23 stsp
8404 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8405 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8406 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8407 fda8017d 2020-07-23 stsp if (err)
8408 fda8017d 2020-07-23 stsp goto done;
8409 fda8017d 2020-07-23 stsp }
8410 fda8017d 2020-07-23 stsp
8411 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8412 fda8017d 2020-07-23 stsp if (f == NULL) {
8413 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8414 fda8017d 2020-07-23 stsp path_unstaged_content);
8415 fda8017d 2020-07-23 stsp goto done;
8416 fda8017d 2020-07-23 stsp }
8417 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8418 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8419 fda8017d 2020-07-23 stsp goto done;
8420 fda8017d 2020-07-23 stsp }
8421 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8422 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8423 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8424 fda8017d 2020-07-23 stsp size_t r;
8425 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8426 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8427 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8428 fda8017d 2020-07-23 stsp goto done;
8429 fda8017d 2020-07-23 stsp }
8430 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8431 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8432 fda8017d 2020-07-23 stsp goto done;
8433 fda8017d 2020-07-23 stsp }
8434 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8435 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8436 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8437 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8438 fda8017d 2020-07-23 stsp progress_arg);
8439 fda8017d 2020-07-23 stsp } else {
8440 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8441 67a66647 2021-05-31 stsp
8442 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8443 67a66647 2021-05-31 stsp if (err)
8444 67a66647 2021-05-31 stsp return err;
8445 67a66647 2021-05-31 stsp
8446 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8447 67a66647 2021-05-31 stsp parent) == -1) {
8448 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8449 67a66647 2021-05-31 stsp base_path = NULL;
8450 67a66647 2021-05-31 stsp goto done;
8451 67a66647 2021-05-31 stsp }
8452 67a66647 2021-05-31 stsp
8453 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8454 67a66647 2021-05-31 stsp if (err)
8455 67a66647 2021-05-31 stsp goto done;
8456 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8457 67a66647 2021-05-31 stsp blob_base);
8458 67a66647 2021-05-31 stsp if (err)
8459 67a66647 2021-05-31 stsp goto done;
8460 67a66647 2021-05-31 stsp
8461 eec2f5a9 2021-06-03 stsp /*
8462 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8463 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8464 eec2f5a9 2021-06-03 stsp */
8465 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8466 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8467 eec2f5a9 2021-06-03 stsp ondisk_path);
8468 eec2f5a9 2021-06-03 stsp if (err)
8469 eec2f5a9 2021-06-03 stsp goto done;
8470 eec2f5a9 2021-06-03 stsp } else {
8471 eec2f5a9 2021-06-03 stsp int fd;
8472 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8473 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8474 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8475 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8476 eec2f5a9 2021-06-03 stsp goto done;
8477 eec2f5a9 2021-06-03 stsp }
8478 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8479 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8480 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8481 eec2f5a9 2021-06-03 stsp close(fd);
8482 eec2f5a9 2021-06-03 stsp goto done;
8483 eec2f5a9 2021-06-03 stsp }
8484 eec2f5a9 2021-06-03 stsp }
8485 eec2f5a9 2021-06-03 stsp
8486 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8487 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8488 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8489 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8490 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8491 fda8017d 2020-07-23 stsp }
8492 fda8017d 2020-07-23 stsp if (err)
8493 fda8017d 2020-07-23 stsp goto done;
8494 fda8017d 2020-07-23 stsp
8495 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8496 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8497 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8498 2ac8aa02 2020-11-09 stsp } else {
8499 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8500 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8501 2ac8aa02 2020-11-09 stsp }
8502 fda8017d 2020-07-23 stsp done:
8503 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8504 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8505 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8506 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8507 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8508 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8509 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8510 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8511 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8512 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8513 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8514 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8515 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8516 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8517 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8518 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8519 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8520 67a66647 2021-05-31 stsp free(blob_base_path);
8521 67a66647 2021-05-31 stsp free(parent);
8522 67a66647 2021-05-31 stsp free(base_path);
8523 2e1f37b0 2019-08-08 stsp return err;
8524 2e1f37b0 2019-08-08 stsp }
8525 2e1f37b0 2019-08-08 stsp
8526 ad493afc 2019-08-03 stsp static const struct got_error *
8527 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8528 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8529 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8530 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8531 ad493afc 2019-08-03 stsp {
8532 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8533 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8534 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8535 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8536 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8537 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8538 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8539 9bc94a15 2019-08-03 stsp struct stat sb;
8540 ad493afc 2019-08-03 stsp
8541 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8542 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8543 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8544 2e1f37b0 2019-08-08 stsp return NULL;
8545 2e1f37b0 2019-08-08 stsp
8546 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8547 ad493afc 2019-08-03 stsp if (ie == NULL)
8548 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8549 9bc94a15 2019-08-03 stsp
8550 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8551 9bc94a15 2019-08-03 stsp == -1)
8552 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8553 ad493afc 2019-08-03 stsp
8554 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8555 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8556 f69721c3 2019-10-21 stsp if (err)
8557 f69721c3 2019-10-21 stsp goto done;
8558 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8559 f69721c3 2019-10-21 stsp id_str) == -1) {
8560 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8561 f69721c3 2019-10-21 stsp goto done;
8562 f69721c3 2019-10-21 stsp }
8563 f69721c3 2019-10-21 stsp
8564 ad493afc 2019-08-03 stsp switch (staged_status) {
8565 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8566 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8567 ad493afc 2019-08-03 stsp blob_id, 8192);
8568 ad493afc 2019-08-03 stsp if (err)
8569 ad493afc 2019-08-03 stsp break;
8570 ad493afc 2019-08-03 stsp /* fall through */
8571 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8572 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8573 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8574 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8575 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8576 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8577 2e1f37b0 2019-08-08 stsp if (err)
8578 2e1f37b0 2019-08-08 stsp break;
8579 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8580 2e1f37b0 2019-08-08 stsp break;
8581 2e1f37b0 2019-08-08 stsp } else {
8582 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8583 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8584 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8585 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8586 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8587 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8588 2e1f37b0 2019-08-08 stsp }
8589 2e1f37b0 2019-08-08 stsp }
8590 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8591 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8592 ad493afc 2019-08-03 stsp if (err)
8593 ad493afc 2019-08-03 stsp break;
8594 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8595 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8596 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8597 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8598 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8599 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8600 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8601 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8602 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8603 ea7786be 2020-07-23 stsp break;
8604 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8605 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8606 36bf999c 2020-07-23 stsp char *staged_target;
8607 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8608 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8609 36bf999c 2020-07-23 stsp if (err)
8610 36bf999c 2020-07-23 stsp goto done;
8611 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8612 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8613 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8614 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8615 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8616 36bf999c 2020-07-23 stsp free(staged_target);
8617 dfe9fba0 2020-07-23 stsp } else {
8618 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8619 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8620 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8621 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8622 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8623 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8624 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8625 dfe9fba0 2020-07-23 stsp }
8626 ea7786be 2020-07-23 stsp break;
8627 ea7786be 2020-07-23 stsp default:
8628 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8629 ea7786be 2020-07-23 stsp break;
8630 ea7786be 2020-07-23 stsp }
8631 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8632 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8633 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8634 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8635 2ac8aa02 2020-11-09 stsp }
8636 ad493afc 2019-08-03 stsp break;
8637 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8638 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8639 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8640 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8641 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8642 2e1f37b0 2019-08-08 stsp if (err)
8643 2e1f37b0 2019-08-08 stsp break;
8644 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8645 2e1f37b0 2019-08-08 stsp break;
8646 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8647 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8648 2e1f37b0 2019-08-08 stsp break;
8649 2e1f37b0 2019-08-08 stsp }
8650 2e1f37b0 2019-08-08 stsp }
8651 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8652 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8653 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8654 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8655 9bc94a15 2019-08-03 stsp if (err)
8656 9bc94a15 2019-08-03 stsp break;
8657 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8658 ad493afc 2019-08-03 stsp break;
8659 ad493afc 2019-08-03 stsp }
8660 f69721c3 2019-10-21 stsp done:
8661 ad493afc 2019-08-03 stsp free(ondisk_path);
8662 ad493afc 2019-08-03 stsp if (blob_base)
8663 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8664 ad493afc 2019-08-03 stsp if (blob_staged)
8665 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8666 f69721c3 2019-10-21 stsp free(id_str);
8667 f69721c3 2019-10-21 stsp free(label_orig);
8668 ad493afc 2019-08-03 stsp return err;
8669 ad493afc 2019-08-03 stsp }
8670 ad493afc 2019-08-03 stsp
8671 ad493afc 2019-08-03 stsp const struct got_error *
8672 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8673 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8674 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8675 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8676 ad493afc 2019-08-03 stsp struct got_repository *repo)
8677 ad493afc 2019-08-03 stsp {
8678 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8679 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8680 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8681 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8682 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8683 ad493afc 2019-08-03 stsp
8684 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8685 ad493afc 2019-08-03 stsp if (err)
8686 ad493afc 2019-08-03 stsp return err;
8687 ad493afc 2019-08-03 stsp
8688 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8689 ad493afc 2019-08-03 stsp if (err)
8690 ad493afc 2019-08-03 stsp goto done;
8691 ad493afc 2019-08-03 stsp
8692 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8693 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8694 ad493afc 2019-08-03 stsp upa.repo = repo;
8695 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8696 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8697 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8698 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8699 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8700 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8701 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8702 ad493afc 2019-08-03 stsp if (err)
8703 ad493afc 2019-08-03 stsp goto done;
8704 ad493afc 2019-08-03 stsp }
8705 ad493afc 2019-08-03 stsp
8706 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8707 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8708 ad493afc 2019-08-03 stsp err = sync_err;
8709 ad493afc 2019-08-03 stsp done:
8710 ad493afc 2019-08-03 stsp free(fileindex_path);
8711 ad493afc 2019-08-03 stsp if (fileindex)
8712 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8713 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8714 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8715 ad493afc 2019-08-03 stsp err = unlockerr;
8716 ad493afc 2019-08-03 stsp return err;
8717 ad493afc 2019-08-03 stsp }
8718 b2118c49 2020-07-28 stsp
8719 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8720 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8721 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8722 b2118c49 2020-07-28 stsp void *info_arg;
8723 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8724 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8725 b2118c49 2020-07-28 stsp void *cancel_arg;
8726 b2118c49 2020-07-28 stsp };
8727 b2118c49 2020-07-28 stsp
8728 b2118c49 2020-07-28 stsp static const struct got_error *
8729 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8730 b2118c49 2020-07-28 stsp {
8731 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8732 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8733 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8734 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8735 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8736 b2118c49 2020-07-28 stsp int stage;
8737 b2118c49 2020-07-28 stsp
8738 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8739 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8740 b2118c49 2020-07-28 stsp
8741 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8742 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8743 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8744 b2118c49 2020-07-28 stsp break;
8745 b2118c49 2020-07-28 stsp }
8746 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8747 b2118c49 2020-07-28 stsp return NULL;
8748 b2118c49 2020-07-28 stsp
8749 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8750 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8751 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8752 b2118c49 2020-07-28 stsp }
8753 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8754 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8755 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8756 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8757 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8758 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8759 b2118c49 2020-07-28 stsp }
8760 b2118c49 2020-07-28 stsp
8761 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8762 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8763 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8764 b2118c49 2020-07-28 stsp }
8765 b2118c49 2020-07-28 stsp
8766 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8767 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8768 b2118c49 2020-07-28 stsp }
8769 b2118c49 2020-07-28 stsp
8770 b2118c49 2020-07-28 stsp const struct got_error *
8771 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8772 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8773 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8774 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8775 b2118c49 2020-07-28 stsp
8776 b2118c49 2020-07-28 stsp {
8777 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8778 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8779 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8780 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8781 b2118c49 2020-07-28 stsp
8782 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8783 b2118c49 2020-07-28 stsp if (err)
8784 b2118c49 2020-07-28 stsp return err;
8785 b2118c49 2020-07-28 stsp
8786 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8787 b2118c49 2020-07-28 stsp if (err)
8788 b2118c49 2020-07-28 stsp goto done;
8789 b2118c49 2020-07-28 stsp
8790 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8791 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8792 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8793 b2118c49 2020-07-28 stsp arg.paths = paths;
8794 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8795 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8796 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8797 b2118c49 2020-07-28 stsp &arg);
8798 b2118c49 2020-07-28 stsp done:
8799 b2118c49 2020-07-28 stsp free(fileindex_path);
8800 b2118c49 2020-07-28 stsp if (fileindex)
8801 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8802 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8803 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8804 b2118c49 2020-07-28 stsp err = unlockerr;
8805 b2118c49 2020-07-28 stsp return err;
8806 b2118c49 2020-07-28 stsp }
8807 78f5ac24 2022-03-19 op
8808 78f5ac24 2022-03-19 op static const struct got_error *
8809 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
8810 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
8811 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
8812 78f5ac24 2022-03-19 op {
8813 78f5ac24 2022-03-19 op const struct got_error *err;
8814 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
8815 78f5ac24 2022-03-19 op struct stat sb;
8816 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
8817 78f5ac24 2022-03-19 op
8818 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
8819 78f5ac24 2022-03-19 op if (err)
8820 78f5ac24 2022-03-19 op return err;
8821 78f5ac24 2022-03-19 op
8822 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8823 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
8824 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
8825 78f5ac24 2022-03-19 op
8826 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8827 78f5ac24 2022-03-19 op if (ie) {
8828 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
8829 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8830 a05fb460 2022-04-23 op repo);
8831 78f5ac24 2022-03-19 op if (err)
8832 78f5ac24 2022-03-19 op goto done;
8833 78f5ac24 2022-03-19 op } else {
8834 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
8835 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
8836 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
8837 78f5ac24 2022-03-19 op if (errno != ENOENT) {
8838 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
8839 78f5ac24 2022-03-19 op ondisk_path);
8840 78f5ac24 2022-03-19 op goto done;
8841 78f5ac24 2022-03-19 op }
8842 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
8843 78f5ac24 2022-03-19 op }
8844 78f5ac24 2022-03-19 op }
8845 78f5ac24 2022-03-19 op
8846 78f5ac24 2022-03-19 op done:
8847 78f5ac24 2022-03-19 op free(ondisk_path);
8848 78f5ac24 2022-03-19 op return err;
8849 78f5ac24 2022-03-19 op }
8850 78f5ac24 2022-03-19 op
8851 78f5ac24 2022-03-19 op static const struct got_error *
8852 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
8853 78f5ac24 2022-03-19 op unsigned char staged_status)
8854 78f5ac24 2022-03-19 op {
8855 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8856 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8857 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8858 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8859 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
8860 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
8861 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8862 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
8863 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8864 78f5ac24 2022-03-19 op return NULL;
8865 78f5ac24 2022-03-19 op }
8866 78f5ac24 2022-03-19 op
8867 78f5ac24 2022-03-19 op static const struct got_error *
8868 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
8869 78f5ac24 2022-03-19 op {
8870 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
8871 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8872 78f5ac24 2022-03-19 op return NULL;
8873 78f5ac24 2022-03-19 op }
8874 78f5ac24 2022-03-19 op
8875 78f5ac24 2022-03-19 op static const struct got_error *
8876 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
8877 78f5ac24 2022-03-19 op unsigned char staged_status)
8878 78f5ac24 2022-03-19 op {
8879 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
8880 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
8881 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
8882 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
8883 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
8884 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8885 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
8886 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
8887 78f5ac24 2022-03-19 op return NULL;
8888 78f5ac24 2022-03-19 op }
8889 78f5ac24 2022-03-19 op
8890 78f5ac24 2022-03-19 op const struct got_error *
8891 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
8892 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
8893 78f5ac24 2022-03-19 op {
8894 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
8895 78f5ac24 2022-03-19 op }
8896 78f5ac24 2022-03-19 op
8897 78f5ac24 2022-03-19 op const struct got_error *
8898 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
8899 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
8900 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
8901 78f5ac24 2022-03-19 op {
8902 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
8903 78f5ac24 2022-03-19 op int file_renamed = 0;
8904 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
8905 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
8906 78f5ac24 2022-03-19 op
8907 78f5ac24 2022-03-19 op *oldpath = NULL;
8908 78f5ac24 2022-03-19 op *newpath = NULL;
8909 78f5ac24 2022-03-19 op
8910 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
8911 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
8912 78f5ac24 2022-03-19 op if (err)
8913 78f5ac24 2022-03-19 op goto done;
8914 78f5ac24 2022-03-19 op
8915 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
8916 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
8917 78f5ac24 2022-03-19 op if (err)
8918 78f5ac24 2022-03-19 op goto done;
8919 78f5ac24 2022-03-19 op
8920 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
8921 78f5ac24 2022-03-19 op file_renamed = 1;
8922 78f5ac24 2022-03-19 op
8923 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
8924 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
8925 78f5ac24 2022-03-19 op else if (file_renamed) {
8926 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
8927 78f5ac24 2022-03-19 op if (err == NULL)
8928 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
8929 78f5ac24 2022-03-19 op } else if (old == NULL)
8930 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
8931 78f5ac24 2022-03-19 op else
8932 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
8933 78f5ac24 2022-03-19 op
8934 78f5ac24 2022-03-19 op done:
8935 78f5ac24 2022-03-19 op if (err) {
8936 78f5ac24 2022-03-19 op free(*oldpath);
8937 78f5ac24 2022-03-19 op *oldpath = NULL;
8938 78f5ac24 2022-03-19 op free(*newpath);
8939 78f5ac24 2022-03-19 op *newpath = NULL;
8940 78f5ac24 2022-03-19 op }
8941 78f5ac24 2022-03-19 op return err;
8942 78f5ac24 2022-03-19 op }
8943 78f5ac24 2022-03-19 op
8944 78f5ac24 2022-03-19 op const struct got_error *
8945 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
8946 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
8947 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
8948 78f5ac24 2022-03-19 op {
8949 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
8950 f2dd7807 2022-05-17 op
8951 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
8952 f2dd7807 2022-05-17 op saa.worktree = worktree;
8953 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
8954 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
8955 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
8956 f2dd7807 2022-05-17 op saa.repo = repo;
8957 f2dd7807 2022-05-17 op
8958 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
8959 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
8960 78f5ac24 2022-03-19 op }
8961 f2dd7807 2022-05-17 op
8962 f2dd7807 2022-05-17 op const struct got_error *
8963 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
8964 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
8965 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
8966 f2dd7807 2022-05-17 op {
8967 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
8968 f2dd7807 2022-05-17 op
8969 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
8970 f2dd7807 2022-05-17 op sda.worktree = worktree;
8971 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
8972 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
8973 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
8974 f2dd7807 2022-05-17 op sda.repo = repo;
8975 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
8976 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
8977 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
8978 f2dd7807 2022-05-17 op sda.status_codes = NULL;
8979 f2dd7807 2022-05-17 op
8980 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
8981 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
8982 f2dd7807 2022-05-17 op }
8983 f2dd7807 2022-05-17 op
8984 f2dd7807 2022-05-17 op const struct got_error *
8985 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
8986 4bcdc895 2022-05-17 op const char *fileindex_path)
8987 f2dd7807 2022-05-17 op {
8988 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
8989 f2dd7807 2022-05-17 op
8990 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
8991 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
8992 f2dd7807 2022-05-17 op
8993 f2dd7807 2022-05-17 op return err;
8994 f2dd7807 2022-05-17 op }