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 86c3caaf 2018-03-09 stsp
19 d1f6d47b 2019-02-04 stsp #include <dirent.h>
20 12ce7a6c 2019-08-12 stsp #include <limits.h>
21 f5c49f82 2019-01-06 stsp #include <stddef.h>
22 86c3caaf 2018-03-09 stsp #include <string.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 303e14b5 2019-09-23 stsp #include <time.h>
26 86c3caaf 2018-03-09 stsp #include <fcntl.h>
27 86c3caaf 2018-03-09 stsp #include <errno.h>
28 86c3caaf 2018-03-09 stsp #include <unistd.h>
29 9d31a1d8 2018-03-11 stsp #include <zlib.h>
30 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
31 512f0d0e 2019-01-02 stsp #include <libgen.h>
32 92a9e85d 2021-09-24 thomas #include <uuid.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 86c3caaf 2018-03-09 stsp
65 99724ed4 2018-03-10 stsp static const struct got_error *
66 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
67 99724ed4 2018-03-10 stsp {
68 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
69 99724ed4 2018-03-10 stsp char *path;
70 99724ed4 2018-03-10 stsp
71 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
73 99724ed4 2018-03-10 stsp
74 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 99724ed4 2018-03-10 stsp free(path);
76 507dc3bb 2018-12-29 stsp return err;
77 507dc3bb 2018-12-29 stsp }
78 507dc3bb 2018-12-29 stsp
79 507dc3bb 2018-12-29 stsp static const struct got_error *
80 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
81 507dc3bb 2018-12-29 stsp {
82 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
83 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
84 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
85 507dc3bb 2018-12-29 stsp char *path = NULL;
86 507dc3bb 2018-12-29 stsp
87 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
89 507dc3bb 2018-12-29 stsp path = NULL;
90 507dc3bb 2018-12-29 stsp goto done;
91 507dc3bb 2018-12-29 stsp }
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
94 507dc3bb 2018-12-29 stsp if (err)
95 507dc3bb 2018-12-29 stsp goto done;
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp if (content) {
98 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
99 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
100 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp goto done;
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp
105 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
106 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
107 2a57020b 2019-02-20 stsp unlink(tmppath);
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp }
110 507dc3bb 2018-12-29 stsp
111 507dc3bb 2018-12-29 stsp done:
112 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
114 230a42bd 2019-05-11 jcs free(tmppath);
115 09fe317a 2018-03-11 stsp return err;
116 09fe317a 2018-03-11 stsp }
117 09fe317a 2018-03-11 stsp
118 024e9686 2019-05-14 stsp static const struct got_error *
119 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
120 024e9686 2019-05-14 stsp {
121 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
122 024e9686 2019-05-14 stsp char *refstr = NULL;
123 024e9686 2019-05-14 stsp
124 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
125 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
126 024e9686 2019-05-14 stsp if (refstr == NULL)
127 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
128 024e9686 2019-05-14 stsp } else {
129 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
132 024e9686 2019-05-14 stsp }
133 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
134 024e9686 2019-05-14 stsp free(refstr);
135 024e9686 2019-05-14 stsp return err;
136 024e9686 2019-05-14 stsp }
137 024e9686 2019-05-14 stsp
138 86c3caaf 2018-03-09 stsp const struct got_error *
139 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
140 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
141 86c3caaf 2018-03-09 stsp {
142 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
143 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
144 ec22038e 2019-03-10 stsp uuid_t uuid;
145 ec22038e 2019-03-10 stsp uint32_t uuid_status;
146 65596e15 2018-12-24 stsp int obj_type;
147 7ac97322 2018-03-11 stsp char *path_got = NULL;
148 1451e70d 2018-03-10 stsp char *formatstr = NULL;
149 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
150 65596e15 2018-12-24 stsp char *basestr = NULL;
151 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
152 65596e15 2018-12-24 stsp
153 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
154 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
155 0c48fee2 2019-03-11 stsp goto done;
156 0c48fee2 2019-03-11 stsp }
157 0c48fee2 2019-03-11 stsp
158 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
159 65596e15 2018-12-24 stsp if (err)
160 65596e15 2018-12-24 stsp return err;
161 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
162 65596e15 2018-12-24 stsp if (err)
163 65596e15 2018-12-24 stsp return err;
164 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
165 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
166 86c3caaf 2018-03-09 stsp
167 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
168 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
169 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
170 0bb8a95e 2018-03-12 stsp }
171 577ec78f 2018-03-11 stsp
172 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
173 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
175 86c3caaf 2018-03-09 stsp goto done;
176 86c3caaf 2018-03-09 stsp }
177 86c3caaf 2018-03-09 stsp
178 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
179 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
180 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
181 86c3caaf 2018-03-09 stsp goto done;
182 86c3caaf 2018-03-09 stsp }
183 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 86c3caaf 2018-03-09 stsp
188 056e7441 2018-03-11 stsp /* Create an empty lock file. */
189 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
190 056e7441 2018-03-11 stsp if (err)
191 056e7441 2018-03-11 stsp goto done;
192 056e7441 2018-03-11 stsp
193 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
194 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
195 99724ed4 2018-03-10 stsp if (err)
196 86c3caaf 2018-03-09 stsp goto done;
197 86c3caaf 2018-03-09 stsp
198 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
199 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
200 65596e15 2018-12-24 stsp if (err)
201 65596e15 2018-12-24 stsp goto done;
202 65596e15 2018-12-24 stsp
203 65596e15 2018-12-24 stsp /* Record our base commit. */
204 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
205 65596e15 2018-12-24 stsp if (err)
206 65596e15 2018-12-24 stsp goto done;
207 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
208 99724ed4 2018-03-10 stsp if (err)
209 86c3caaf 2018-03-09 stsp goto done;
210 86c3caaf 2018-03-09 stsp
211 1451e70d 2018-03-10 stsp /* Store path to repository. */
212 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
213 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
214 99724ed4 2018-03-10 stsp if (err)
215 86c3caaf 2018-03-09 stsp goto done;
216 86c3caaf 2018-03-09 stsp
217 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
218 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
219 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
220 ec22038e 2019-03-10 stsp if (err)
221 ec22038e 2019-03-10 stsp goto done;
222 ec22038e 2019-03-10 stsp
223 ec22038e 2019-03-10 stsp /* Generate UUID. */
224 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
225 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
226 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
227 ec22038e 2019-03-10 stsp goto done;
228 ec22038e 2019-03-10 stsp }
229 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
230 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
231 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
232 ec22038e 2019-03-10 stsp goto done;
233 ec22038e 2019-03-10 stsp }
234 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
235 577ec78f 2018-03-11 stsp if (err)
236 577ec78f 2018-03-11 stsp goto done;
237 577ec78f 2018-03-11 stsp
238 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
239 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
241 1451e70d 2018-03-10 stsp goto done;
242 1451e70d 2018-03-10 stsp }
243 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
244 99724ed4 2018-03-10 stsp if (err)
245 1451e70d 2018-03-10 stsp goto done;
246 1451e70d 2018-03-10 stsp
247 86c3caaf 2018-03-09 stsp done:
248 65596e15 2018-12-24 stsp free(commit_id);
249 7ac97322 2018-03-11 stsp free(path_got);
250 1451e70d 2018-03-10 stsp free(formatstr);
251 0bb8a95e 2018-03-12 stsp free(absprefix);
252 65596e15 2018-12-24 stsp free(basestr);
253 ec22038e 2019-03-10 stsp free(uuidstr);
254 86c3caaf 2018-03-09 stsp return err;
255 86c3caaf 2018-03-09 stsp }
256 86c3caaf 2018-03-09 stsp
257 247140b2 2019-02-05 stsp const struct got_error *
258 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
259 e5dc7198 2018-12-29 stsp const char *path_prefix)
260 e5dc7198 2018-12-29 stsp {
261 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
262 e5dc7198 2018-12-29 stsp
263 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
264 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
265 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
266 e5dc7198 2018-12-29 stsp }
267 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
268 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
269 e5dc7198 2018-12-29 stsp free(absprefix);
270 e5dc7198 2018-12-29 stsp return NULL;
271 86c3caaf 2018-03-09 stsp }
272 86c3caaf 2018-03-09 stsp
273 bc70eb79 2019-05-09 stsp const char *
274 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 86c3caaf 2018-03-09 stsp {
276 36a38700 2019-05-10 stsp return worktree->head_ref_name;
277 024e9686 2019-05-14 stsp }
278 024e9686 2019-05-14 stsp
279 024e9686 2019-05-14 stsp const struct got_error *
280 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
281 024e9686 2019-05-14 stsp struct got_reference *head_ref)
282 024e9686 2019-05-14 stsp {
283 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
284 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
285 024e9686 2019-05-14 stsp
286 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
287 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
288 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
289 024e9686 2019-05-14 stsp path_got = NULL;
290 024e9686 2019-05-14 stsp goto done;
291 024e9686 2019-05-14 stsp }
292 024e9686 2019-05-14 stsp
293 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
294 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
295 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
296 024e9686 2019-05-14 stsp goto done;
297 024e9686 2019-05-14 stsp }
298 024e9686 2019-05-14 stsp
299 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
300 024e9686 2019-05-14 stsp if (err)
301 024e9686 2019-05-14 stsp goto done;
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
304 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
305 024e9686 2019-05-14 stsp done:
306 024e9686 2019-05-14 stsp free(path_got);
307 024e9686 2019-05-14 stsp if (err)
308 024e9686 2019-05-14 stsp free(head_ref_name);
309 024e9686 2019-05-14 stsp return err;
310 507dc3bb 2018-12-29 stsp }
311 507dc3bb 2018-12-29 stsp
312 b72f483a 2019-02-05 stsp struct got_object_id *
313 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 507dc3bb 2018-12-29 stsp {
315 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
316 86c3caaf 2018-03-09 stsp }
317 86c3caaf 2018-03-09 stsp
318 507dc3bb 2018-12-29 stsp const struct got_error *
319 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
320 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
321 507dc3bb 2018-12-29 stsp {
322 507dc3bb 2018-12-29 stsp const struct got_error *err;
323 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
324 507dc3bb 2018-12-29 stsp char *id_str = NULL;
325 507dc3bb 2018-12-29 stsp char *path_got = NULL;
326 507dc3bb 2018-12-29 stsp
327 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
328 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
329 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
330 507dc3bb 2018-12-29 stsp path_got = NULL;
331 507dc3bb 2018-12-29 stsp goto done;
332 507dc3bb 2018-12-29 stsp }
333 507dc3bb 2018-12-29 stsp
334 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
335 507dc3bb 2018-12-29 stsp if (err)
336 507dc3bb 2018-12-29 stsp return err;
337 507dc3bb 2018-12-29 stsp
338 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
339 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
340 507dc3bb 2018-12-29 stsp goto done;
341 507dc3bb 2018-12-29 stsp }
342 507dc3bb 2018-12-29 stsp
343 507dc3bb 2018-12-29 stsp /* Record our base commit. */
344 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
345 507dc3bb 2018-12-29 stsp if (err)
346 507dc3bb 2018-12-29 stsp goto done;
347 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
348 507dc3bb 2018-12-29 stsp if (err)
349 507dc3bb 2018-12-29 stsp goto done;
350 507dc3bb 2018-12-29 stsp
351 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
352 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
353 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
354 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
355 507dc3bb 2018-12-29 stsp goto done;
356 507dc3bb 2018-12-29 stsp }
357 507dc3bb 2018-12-29 stsp done:
358 507dc3bb 2018-12-29 stsp if (obj)
359 507dc3bb 2018-12-29 stsp got_object_close(obj);
360 507dc3bb 2018-12-29 stsp free(id_str);
361 507dc3bb 2018-12-29 stsp free(path_got);
362 507dc3bb 2018-12-29 stsp return err;
363 50b0790e 2020-09-11 stsp }
364 50b0790e 2020-09-11 stsp
365 50b0790e 2020-09-11 stsp const struct got_gotconfig *
366 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
367 50b0790e 2020-09-11 stsp {
368 50b0790e 2020-09-11 stsp return worktree->gotconfig;
369 507dc3bb 2018-12-29 stsp }
370 507dc3bb 2018-12-29 stsp
371 9d31a1d8 2018-03-11 stsp static const struct got_error *
372 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
373 86c3caaf 2018-03-09 stsp {
374 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
375 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
376 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
377 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
378 86c3caaf 2018-03-09 stsp return NULL;
379 21908da4 2019-01-13 stsp }
380 21908da4 2019-01-13 stsp
381 21908da4 2019-01-13 stsp static const struct got_error *
382 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 4a1ddfc2 2019-01-12 stsp {
384 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
385 4a1ddfc2 2019-01-12 stsp char *abspath;
386 4a1ddfc2 2019-01-12 stsp
387 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
388 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
389 4a1ddfc2 2019-01-12 stsp
390 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
391 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
392 ddcd8544 2019-03-11 stsp struct stat sb;
393 ddcd8544 2019-03-11 stsp err = NULL;
394 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
395 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
396 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
397 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
398 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
399 ddcd8544 2019-03-11 stsp }
400 ddcd8544 2019-03-11 stsp }
401 4a1ddfc2 2019-01-12 stsp free(abspath);
402 68c76935 2019-02-19 stsp return err;
403 68c76935 2019-02-19 stsp }
404 68c76935 2019-02-19 stsp
405 68c76935 2019-02-19 stsp static const struct got_error *
406 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 68c76935 2019-02-19 stsp {
408 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
409 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
410 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
411 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
412 68c76935 2019-02-19 stsp
413 68c76935 2019-02-19 stsp *same = 1;
414 68c76935 2019-02-19 stsp
415 230a42bd 2019-05-11 jcs for (;;) {
416 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
417 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
418 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
419 68c76935 2019-02-19 stsp break;
420 68c76935 2019-02-19 stsp }
421 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
422 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
423 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
424 68c76935 2019-02-19 stsp break;
425 68c76935 2019-02-19 stsp }
426 68c76935 2019-02-19 stsp if (flen1 == 0) {
427 68c76935 2019-02-19 stsp if (flen2 != 0)
428 68c76935 2019-02-19 stsp *same = 0;
429 68c76935 2019-02-19 stsp break;
430 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
431 68c76935 2019-02-19 stsp if (flen1 != 0)
432 68c76935 2019-02-19 stsp *same = 0;
433 68c76935 2019-02-19 stsp break;
434 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
435 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
436 68c76935 2019-02-19 stsp *same = 0;
437 68c76935 2019-02-19 stsp break;
438 68c76935 2019-02-19 stsp }
439 68c76935 2019-02-19 stsp } else {
440 68c76935 2019-02-19 stsp *same = 0;
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp }
444 68c76935 2019-02-19 stsp
445 68c76935 2019-02-19 stsp return err;
446 68c76935 2019-02-19 stsp }
447 68c76935 2019-02-19 stsp
448 68c76935 2019-02-19 stsp static const struct got_error *
449 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
450 68c76935 2019-02-19 stsp {
451 68c76935 2019-02-19 stsp struct stat sb;
452 68c76935 2019-02-19 stsp size_t size1, size2;
453 68c76935 2019-02-19 stsp
454 68c76935 2019-02-19 stsp *same = 1;
455 68c76935 2019-02-19 stsp
456 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
457 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
458 68c76935 2019-02-19 stsp size1 = sb.st_size;
459 68c76935 2019-02-19 stsp
460 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size2 = sb.st_size;
463 68c76935 2019-02-19 stsp
464 68c76935 2019-02-19 stsp if (size1 != size2) {
465 68c76935 2019-02-19 stsp *same = 0;
466 68c76935 2019-02-19 stsp return NULL;
467 68c76935 2019-02-19 stsp }
468 68c76935 2019-02-19 stsp
469 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
470 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
471 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
473 68c76935 2019-02-19 stsp
474 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
475 24f136e0 2021-11-20 thomas }
476 24f136e0 2021-11-20 thomas
477 24f136e0 2021-11-20 thomas static const struct got_error *
478 24f136e0 2021-11-20 thomas copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 24f136e0 2021-11-20 thomas {
480 24f136e0 2021-11-20 thomas uint8_t fbuf[65536];
481 24f136e0 2021-11-20 thomas size_t flen;
482 24f136e0 2021-11-20 thomas ssize_t outlen;
483 24f136e0 2021-11-20 thomas
484 24f136e0 2021-11-20 thomas *outsize = 0;
485 24f136e0 2021-11-20 thomas
486 24f136e0 2021-11-20 thomas if (fseek(f, 0L, SEEK_SET) == -1)
487 24f136e0 2021-11-20 thomas return got_ferror(f, GOT_ERR_IO);
488 24f136e0 2021-11-20 thomas
489 24f136e0 2021-11-20 thomas for (;;) {
490 24f136e0 2021-11-20 thomas flen = fread(fbuf, 1, sizeof(fbuf), f);
491 24f136e0 2021-11-20 thomas if (flen == 0) {
492 24f136e0 2021-11-20 thomas if (ferror(f))
493 24f136e0 2021-11-20 thomas return got_error_from_errno("fread");
494 24f136e0 2021-11-20 thomas if (feof(f))
495 24f136e0 2021-11-20 thomas break;
496 24f136e0 2021-11-20 thomas }
497 24f136e0 2021-11-20 thomas outlen = write(outfd, fbuf, flen);
498 24f136e0 2021-11-20 thomas if (outlen == -1)
499 24f136e0 2021-11-20 thomas return got_error_from_errno("write");
500 24f136e0 2021-11-20 thomas if (outlen != flen)
501 24f136e0 2021-11-20 thomas return got_error(GOT_ERR_IO);
502 24f136e0 2021-11-20 thomas *outsize += outlen;
503 24f136e0 2021-11-20 thomas }
504 24f136e0 2021-11-20 thomas
505 24f136e0 2021-11-20 thomas return NULL;
506 24f136e0 2021-11-20 thomas }
507 24f136e0 2021-11-20 thomas
508 24f136e0 2021-11-20 thomas static const struct got_error *
509 24f136e0 2021-11-20 thomas merge_binary_file(int *overlapcnt, int merged_fd,
510 24f136e0 2021-11-20 thomas FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
511 24f136e0 2021-11-20 thomas const char *label_deriv, const char *label_orig, const char *label_deriv2,
512 24f136e0 2021-11-20 thomas const char *ondisk_path)
513 24f136e0 2021-11-20 thomas {
514 24f136e0 2021-11-20 thomas const struct got_error *err = NULL;
515 24f136e0 2021-11-20 thomas int same_content, changed_deriv, changed_deriv2;
516 24f136e0 2021-11-20 thomas int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
517 24f136e0 2021-11-20 thomas off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
518 24f136e0 2021-11-20 thomas char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
519 24f136e0 2021-11-20 thomas char *base_path_orig = NULL, *base_path_deriv = NULL;
520 24f136e0 2021-11-20 thomas char *base_path_deriv2 = NULL;
521 24f136e0 2021-11-20 thomas
522 24f136e0 2021-11-20 thomas *overlapcnt = 0;
523 24f136e0 2021-11-20 thomas
524 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_deriv2);
525 24f136e0 2021-11-20 thomas if (err)
526 24f136e0 2021-11-20 thomas return err;
527 24f136e0 2021-11-20 thomas
528 24f136e0 2021-11-20 thomas if (same_content)
529 24f136e0 2021-11-20 thomas return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 24f136e0 2021-11-20 thomas
531 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv, f_orig);
532 24f136e0 2021-11-20 thomas if (err)
533 24f136e0 2021-11-20 thomas return err;
534 24f136e0 2021-11-20 thomas changed_deriv = !same_content;
535 24f136e0 2021-11-20 thomas err = check_files_equal(&same_content, f_deriv2, f_orig);
536 24f136e0 2021-11-20 thomas if (err)
537 24f136e0 2021-11-20 thomas return err;
538 24f136e0 2021-11-20 thomas changed_deriv2 = !same_content;
539 24f136e0 2021-11-20 thomas
540 24f136e0 2021-11-20 thomas if (changed_deriv && changed_deriv2) {
541 24f136e0 2021-11-20 thomas *overlapcnt = 1;
542 24f136e0 2021-11-20 thomas if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
543 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
544 24f136e0 2021-11-20 thomas goto done;
545 24f136e0 2021-11-20 thomas }
546 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
547 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
548 24f136e0 2021-11-20 thomas goto done;
549 24f136e0 2021-11-20 thomas }
550 24f136e0 2021-11-20 thomas if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
551 24f136e0 2021-11-20 thomas err = got_error_from_errno("asprintf");
552 24f136e0 2021-11-20 thomas goto done;
553 24f136e0 2021-11-20 thomas }
554 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_orig, &fd_orig,
555 24f136e0 2021-11-20 thomas base_path_orig);
556 24f136e0 2021-11-20 thomas if (err)
557 24f136e0 2021-11-20 thomas goto done;
558 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
559 24f136e0 2021-11-20 thomas base_path_deriv);
560 24f136e0 2021-11-20 thomas if (err)
561 24f136e0 2021-11-20 thomas goto done;
562 24f136e0 2021-11-20 thomas err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
563 24f136e0 2021-11-20 thomas base_path_deriv2);
564 24f136e0 2021-11-20 thomas if (err)
565 24f136e0 2021-11-20 thomas goto done;
566 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
567 24f136e0 2021-11-20 thomas if (err)
568 24f136e0 2021-11-20 thomas goto done;
569 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
570 24f136e0 2021-11-20 thomas if (err)
571 24f136e0 2021-11-20 thomas goto done;
572 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
573 24f136e0 2021-11-20 thomas if (err)
574 24f136e0 2021-11-20 thomas goto done;
575 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "Binary files differ and cannot be "
576 24f136e0 2021-11-20 thomas "merged automatically:\n") < 0) {
577 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
578 24f136e0 2021-11-20 thomas goto done;
579 24f136e0 2021-11-20 thomas }
580 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
581 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_BEGIN,
582 24f136e0 2021-11-20 thomas label_deriv ? " " : "",
583 24f136e0 2021-11-20 thomas label_deriv ? label_deriv : "",
584 24f136e0 2021-11-20 thomas path_deriv) < 0) {
585 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
586 24f136e0 2021-11-20 thomas goto done;
587 24f136e0 2021-11-20 thomas }
588 24f136e0 2021-11-20 thomas if (size_orig > 0) {
589 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
590 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_ORIG,
591 24f136e0 2021-11-20 thomas label_orig ? " " : "",
592 24f136e0 2021-11-20 thomas label_orig ? label_orig : "",
593 24f136e0 2021-11-20 thomas path_orig) < 0) {
594 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
595 24f136e0 2021-11-20 thomas goto done;
596 24f136e0 2021-11-20 thomas }
597 24f136e0 2021-11-20 thomas }
598 24f136e0 2021-11-20 thomas if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
599 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_SEP,
600 24f136e0 2021-11-20 thomas path_deriv2,
601 24f136e0 2021-11-20 thomas GOT_DIFF_CONFLICT_MARKER_END,
602 24f136e0 2021-11-20 thomas label_deriv2 ? " " : "",
603 24f136e0 2021-11-20 thomas label_deriv2 ? label_deriv2 : "") < 0) {
604 24f136e0 2021-11-20 thomas err = got_error_from_errno("dprintf");
605 24f136e0 2021-11-20 thomas goto done;
606 24f136e0 2021-11-20 thomas }
607 24f136e0 2021-11-20 thomas } else if (changed_deriv)
608 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
609 24f136e0 2021-11-20 thomas else if (changed_deriv2)
610 24f136e0 2021-11-20 thomas err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
611 24f136e0 2021-11-20 thomas done:
612 24f136e0 2021-11-20 thomas if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
613 24f136e0 2021-11-20 thomas err == NULL)
614 24f136e0 2021-11-20 thomas err = got_error_from_errno2("unlink", path_orig);
615 24f136e0 2021-11-20 thomas if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
616 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_orig);
617 24f136e0 2021-11-20 thomas if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
618 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv);
619 24f136e0 2021-11-20 thomas if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
620 24f136e0 2021-11-20 thomas err = got_error_from_errno2("close", path_deriv2);
621 24f136e0 2021-11-20 thomas free(path_orig);
622 24f136e0 2021-11-20 thomas free(path_deriv);
623 24f136e0 2021-11-20 thomas free(path_deriv2);
624 24f136e0 2021-11-20 thomas free(base_path_orig);
625 24f136e0 2021-11-20 thomas free(base_path_deriv);
626 24f136e0 2021-11-20 thomas free(base_path_deriv2);
627 24f136e0 2021-11-20 thomas return err;
628 6353ad76 2019-02-08 stsp }
629 6353ad76 2019-02-08 stsp
630 6353ad76 2019-02-08 stsp /*
631 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
632 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
633 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
634 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
635 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
636 6353ad76 2019-02-08 stsp */
637 6353ad76 2019-02-08 stsp static const struct got_error *
638 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
639 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
640 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
641 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
642 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
643 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
644 6353ad76 2019-02-08 stsp {
645 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
646 6353ad76 2019-02-08 stsp int merged_fd = -1;
647 db590691 2021-06-02 stsp FILE *f_merged = NULL;
648 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
649 234035bc 2019-06-01 stsp int overlapcnt = 0;
650 3524bbf9 2020-10-20 stsp char *parent = NULL;
651 6353ad76 2019-02-08 stsp
652 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
653 234035bc 2019-06-01 stsp
654 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
655 3524bbf9 2020-10-20 stsp if (err)
656 3524bbf9 2020-10-20 stsp return err;
657 af54ae4a 2019-02-19 stsp
658 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
659 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
660 3524bbf9 2020-10-20 stsp goto done;
661 3524bbf9 2020-10-20 stsp }
662 af54ae4a 2019-02-19 stsp
663 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
664 6353ad76 2019-02-08 stsp if (err)
665 6353ad76 2019-02-08 stsp goto done;
666 3b9f0f87 2020-07-23 stsp
667 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
668 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
669 24f136e0 2021-11-20 thomas if (err) {
670 24f136e0 2021-11-20 thomas if (err->code != GOT_ERR_FILE_BINARY)
671 24f136e0 2021-11-20 thomas goto done;
672 24f136e0 2021-11-20 thomas err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
673 24f136e0 2021-11-20 thomas f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
674 24f136e0 2021-11-20 thomas ondisk_path);
675 24f136e0 2021-11-20 thomas if (err)
676 24f136e0 2021-11-20 thomas goto done;
677 24f136e0 2021-11-20 thomas }
678 6353ad76 2019-02-08 stsp
679 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
680 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
681 1ee397ad 2019-07-12 stsp if (err)
682 1ee397ad 2019-07-12 stsp goto done;
683 6353ad76 2019-02-08 stsp
684 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
685 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
686 816dc654 2019-02-16 stsp goto done;
687 68c76935 2019-02-19 stsp }
688 68c76935 2019-02-19 stsp
689 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
690 db590691 2021-06-02 stsp if (f_merged == NULL) {
691 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
692 db590691 2021-06-02 stsp goto done;
693 db590691 2021-06-02 stsp }
694 db590691 2021-06-02 stsp merged_fd = -1;
695 db590691 2021-06-02 stsp
696 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
697 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
698 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
699 db590691 2021-06-02 stsp f_merged);
700 68c76935 2019-02-19 stsp if (err)
701 68c76935 2019-02-19 stsp goto done;
702 816dc654 2019-02-16 stsp }
703 6353ad76 2019-02-08 stsp
704 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
705 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
706 70a0c8ec 2019-02-20 stsp goto done;
707 70a0c8ec 2019-02-20 stsp }
708 70a0c8ec 2019-02-20 stsp
709 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
710 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
711 230a42bd 2019-05-11 jcs ondisk_path);
712 6353ad76 2019-02-08 stsp goto done;
713 6353ad76 2019-02-08 stsp }
714 6353ad76 2019-02-08 stsp done:
715 fdcb7daf 2019-12-15 stsp if (err) {
716 fdcb7daf 2019-12-15 stsp if (merged_path)
717 fdcb7daf 2019-12-15 stsp unlink(merged_path);
718 3b9f0f87 2020-07-23 stsp }
719 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
721 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
723 6353ad76 2019-02-08 stsp free(merged_path);
724 af54ae4a 2019-02-19 stsp free(base_path);
725 3524bbf9 2020-10-20 stsp free(parent);
726 af57b12a 2020-07-23 stsp return err;
727 af57b12a 2020-07-23 stsp }
728 af57b12a 2020-07-23 stsp
729 af57b12a 2020-07-23 stsp static const struct got_error *
730 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
731 af57b12a 2020-07-23 stsp size_t target_len)
732 af57b12a 2020-07-23 stsp {
733 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
734 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
735 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
736 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
738 af57b12a 2020-07-23 stsp ondisk_path);
739 af57b12a 2020-07-23 stsp return NULL;
740 af57b12a 2020-07-23 stsp }
741 af57b12a 2020-07-23 stsp
742 af57b12a 2020-07-23 stsp /*
743 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
744 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
745 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
746 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
747 993e2a1b 2020-07-23 stsp *
748 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
749 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
750 993e2a1b 2020-07-23 stsp *
751 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
752 993e2a1b 2020-07-23 stsp * has deleted this symlink.
753 11cc08c1 2020-07-23 stsp */
754 11cc08c1 2020-07-23 stsp static const struct got_error *
755 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
756 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
757 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
758 11cc08c1 2020-07-23 stsp {
759 11cc08c1 2020-07-23 stsp const struct got_error *err;
760 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
761 11cc08c1 2020-07-23 stsp FILE *f = NULL;
762 11cc08c1 2020-07-23 stsp
763 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
764 11cc08c1 2020-07-23 stsp if (err)
765 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
766 11cc08c1 2020-07-23 stsp
767 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
768 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
769 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
770 11cc08c1 2020-07-23 stsp goto done;
771 11cc08c1 2020-07-23 stsp }
772 11cc08c1 2020-07-23 stsp
773 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
774 11cc08c1 2020-07-23 stsp if (err)
775 3818e3c4 2020-11-01 naddy goto done;
776 3818e3c4 2020-11-01 naddy
777 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
778 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
779 11cc08c1 2020-07-23 stsp goto done;
780 3818e3c4 2020-11-01 naddy }
781 11cc08c1 2020-07-23 stsp
782 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
783 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
784 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
785 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
786 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
787 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
790 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
791 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
792 11cc08c1 2020-07-23 stsp goto done;
793 11cc08c1 2020-07-23 stsp }
794 11cc08c1 2020-07-23 stsp
795 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
796 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
797 11cc08c1 2020-07-23 stsp goto done;
798 11cc08c1 2020-07-23 stsp }
799 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
801 11cc08c1 2020-07-23 stsp goto done;
802 11cc08c1 2020-07-23 stsp }
803 11cc08c1 2020-07-23 stsp done:
804 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
805 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
806 11cc08c1 2020-07-23 stsp free(path);
807 11cc08c1 2020-07-23 stsp free(id_str);
808 11cc08c1 2020-07-23 stsp free(label_deriv);
809 11cc08c1 2020-07-23 stsp return err;
810 11cc08c1 2020-07-23 stsp }
811 d219f183 2020-07-23 stsp
812 d219f183 2020-07-23 stsp /* forward declaration */
813 d219f183 2020-07-23 stsp static const struct got_error *
814 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
815 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
816 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
817 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
818 11cc08c1 2020-07-23 stsp
819 11cc08c1 2020-07-23 stsp /*
820 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
821 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
822 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
823 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
824 af57b12a 2020-07-23 stsp */
825 af57b12a 2020-07-23 stsp static const struct got_error *
826 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
827 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
828 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
829 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
830 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
831 af57b12a 2020-07-23 stsp {
832 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
833 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
834 af57b12a 2020-07-23 stsp struct stat sb;
835 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
836 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
837 11cc08c1 2020-07-23 stsp int have_local_change = 0;
838 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
839 af57b12a 2020-07-23 stsp
840 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
842 af57b12a 2020-07-23 stsp
843 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
844 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
845 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
846 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
847 af57b12a 2020-07-23 stsp ondisk_path);
848 af57b12a 2020-07-23 stsp goto done;
849 af57b12a 2020-07-23 stsp }
850 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
851 af57b12a 2020-07-23 stsp
852 526a746f 2020-07-23 stsp if (blob_orig) {
853 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
854 526a746f 2020-07-23 stsp if (err)
855 526a746f 2020-07-23 stsp goto done;
856 526a746f 2020-07-23 stsp }
857 af57b12a 2020-07-23 stsp
858 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
859 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
860 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
861 11cc08c1 2020-07-23 stsp have_local_change = 1;
862 11cc08c1 2020-07-23 stsp
863 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
864 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
865 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
866 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
867 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
868 11cc08c1 2020-07-23 stsp
869 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
870 11cc08c1 2020-07-23 stsp if (ancestor_target) {
871 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
872 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
873 11cc08c1 2020-07-23 stsp path);
874 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
875 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
876 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
877 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 11cc08c1 2020-07-23 stsp path);
879 11cc08c1 2020-07-23 stsp } else {
880 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
881 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
882 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
883 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
884 11cc08c1 2020-07-23 stsp if (err)
885 11cc08c1 2020-07-23 stsp goto done;
886 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
887 11cc08c1 2020-07-23 stsp path);
888 11cc08c1 2020-07-23 stsp }
889 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
890 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
891 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
892 af57b12a 2020-07-23 stsp strlen(deriv_target));
893 af57b12a 2020-07-23 stsp if (err)
894 af57b12a 2020-07-23 stsp goto done;
895 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
896 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
897 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
898 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
899 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
900 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
901 ea7786be 2020-07-23 stsp path);
902 ea7786be 2020-07-23 stsp } else {
903 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
904 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
905 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
906 ea7786be 2020-07-23 stsp if (err)
907 ea7786be 2020-07-23 stsp goto done;
908 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
909 ea7786be 2020-07-23 stsp path);
910 ea7786be 2020-07-23 stsp }
911 6353ad76 2019-02-08 stsp }
912 11cc08c1 2020-07-23 stsp
913 af57b12a 2020-07-23 stsp done:
914 af57b12a 2020-07-23 stsp free(ancestor_target);
915 eec2f5a9 2021-06-03 stsp return err;
916 eec2f5a9 2021-06-03 stsp }
917 eec2f5a9 2021-06-03 stsp
918 eec2f5a9 2021-06-03 stsp static const struct got_error *
919 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 eec2f5a9 2021-06-03 stsp {
921 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
922 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
923 eec2f5a9 2021-06-03 stsp ssize_t target_len;
924 eec2f5a9 2021-06-03 stsp size_t n;
925 eec2f5a9 2021-06-03 stsp FILE *f;
926 eec2f5a9 2021-06-03 stsp
927 eec2f5a9 2021-06-03 stsp *outfile = NULL;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp f = got_opentemp();
930 eec2f5a9 2021-06-03 stsp if (f == NULL)
931 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
932 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
933 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
934 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
935 eec2f5a9 2021-06-03 stsp goto done;
936 eec2f5a9 2021-06-03 stsp }
937 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
938 eec2f5a9 2021-06-03 stsp if (n != target_len) {
939 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
940 eec2f5a9 2021-06-03 stsp goto done;
941 eec2f5a9 2021-06-03 stsp }
942 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
943 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
944 eec2f5a9 2021-06-03 stsp goto done;
945 eec2f5a9 2021-06-03 stsp }
946 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
947 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
948 eec2f5a9 2021-06-03 stsp goto done;
949 eec2f5a9 2021-06-03 stsp }
950 eec2f5a9 2021-06-03 stsp done:
951 eec2f5a9 2021-06-03 stsp if (err)
952 eec2f5a9 2021-06-03 stsp fclose(f);
953 eec2f5a9 2021-06-03 stsp else
954 eec2f5a9 2021-06-03 stsp *outfile = f;
955 14c901f1 2019-08-08 stsp return err;
956 14c901f1 2019-08-08 stsp }
957 14c901f1 2019-08-08 stsp
958 14c901f1 2019-08-08 stsp /*
959 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
960 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
961 14c901f1 2019-08-08 stsp * acts as the second derived version.
962 14c901f1 2019-08-08 stsp */
963 14c901f1 2019-08-08 stsp static const struct got_error *
964 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
965 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
966 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
967 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
968 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
969 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
970 14c901f1 2019-08-08 stsp {
971 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
972 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
973 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
974 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
975 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
976 14c901f1 2019-08-08 stsp
977 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
978 14c901f1 2019-08-08 stsp
979 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
980 ed6b5030 2020-10-20 stsp if (err)
981 ed6b5030 2020-10-20 stsp return err;
982 14c901f1 2019-08-08 stsp
983 67a66647 2021-05-31 stsp if (blob_orig) {
984 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
985 67a66647 2021-05-31 stsp parent) == -1) {
986 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
987 67a66647 2021-05-31 stsp base_path = NULL;
988 67a66647 2021-05-31 stsp goto done;
989 67a66647 2021-05-31 stsp }
990 67a66647 2021-05-31 stsp
991 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
992 67a66647 2021-05-31 stsp if (err)
993 67a66647 2021-05-31 stsp goto done;
994 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
995 67a66647 2021-05-31 stsp blob_orig);
996 67a66647 2021-05-31 stsp if (err)
997 67a66647 2021-05-31 stsp goto done;
998 67a66647 2021-05-31 stsp free(base_path);
999 db590691 2021-06-02 stsp } else {
1000 db590691 2021-06-02 stsp /*
1001 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1002 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1003 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1004 db590691 2021-06-02 stsp */
1005 db590691 2021-06-02 stsp f_orig = got_opentemp();
1006 db590691 2021-06-02 stsp if (f_orig == NULL) {
1007 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1008 db590691 2021-06-02 stsp goto done;
1009 db590691 2021-06-02 stsp }
1010 67a66647 2021-05-31 stsp }
1011 67a66647 2021-05-31 stsp
1012 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1013 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1014 14c901f1 2019-08-08 stsp base_path = NULL;
1015 14c901f1 2019-08-08 stsp goto done;
1016 14c901f1 2019-08-08 stsp }
1017 14c901f1 2019-08-08 stsp
1018 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1019 14c901f1 2019-08-08 stsp if (err)
1020 14c901f1 2019-08-08 stsp goto done;
1021 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1022 14c901f1 2019-08-08 stsp blob_deriv);
1023 14c901f1 2019-08-08 stsp if (err)
1024 14c901f1 2019-08-08 stsp goto done;
1025 14c901f1 2019-08-08 stsp
1026 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1027 14c901f1 2019-08-08 stsp if (err)
1028 14c901f1 2019-08-08 stsp goto done;
1029 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1030 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1031 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1032 14c901f1 2019-08-08 stsp goto done;
1033 eec2f5a9 2021-06-03 stsp }
1034 eec2f5a9 2021-06-03 stsp
1035 eec2f5a9 2021-06-03 stsp /*
1036 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1037 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1038 eec2f5a9 2021-06-03 stsp */
1039 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1040 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1041 eec2f5a9 2021-06-03 stsp if (err)
1042 eec2f5a9 2021-06-03 stsp goto done;
1043 eec2f5a9 2021-06-03 stsp } else {
1044 eec2f5a9 2021-06-03 stsp int fd;
1045 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1046 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1047 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1048 eec2f5a9 2021-06-03 stsp goto done;
1049 eec2f5a9 2021-06-03 stsp }
1050 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1051 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp close(fd);
1054 eec2f5a9 2021-06-03 stsp goto done;
1055 eec2f5a9 2021-06-03 stsp }
1056 14c901f1 2019-08-08 stsp }
1057 14c901f1 2019-08-08 stsp
1058 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1059 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1060 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1061 14c901f1 2019-08-08 stsp done:
1062 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1063 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1064 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1066 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1067 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1068 14c901f1 2019-08-08 stsp free(base_path);
1069 67a66647 2021-05-31 stsp if (blob_orig_path) {
1070 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1071 67a66647 2021-05-31 stsp free(blob_orig_path);
1072 67a66647 2021-05-31 stsp }
1073 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1074 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1075 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1076 14c901f1 2019-08-08 stsp }
1077 6353ad76 2019-02-08 stsp free(id_str);
1078 818c7501 2019-07-11 stsp free(label_deriv);
1079 ed6b5030 2020-10-20 stsp free(parent);
1080 4a1ddfc2 2019-01-12 stsp return err;
1081 4a1ddfc2 2019-01-12 stsp }
1082 4a1ddfc2 2019-01-12 stsp
1083 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1084 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1085 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1086 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1087 13d9040b 2019-03-27 stsp {
1088 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1089 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1090 13d9040b 2019-03-27 stsp
1091 65b05cec 2020-07-23 stsp *new_iep = NULL;
1092 65b05cec 2020-07-23 stsp
1093 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1094 054041d0 2020-06-24 stsp if (err)
1095 054041d0 2020-06-24 stsp return err;
1096 054041d0 2020-06-24 stsp
1097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1098 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1099 054041d0 2020-06-24 stsp if (err)
1100 054041d0 2020-06-24 stsp goto done;
1101 054041d0 2020-06-24 stsp
1102 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1103 054041d0 2020-06-24 stsp done:
1104 054041d0 2020-06-24 stsp if (err)
1105 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1106 65b05cec 2020-07-23 stsp else
1107 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1108 13d9040b 2019-03-27 stsp return err;
1109 1ebedb77 2019-10-19 stsp }
1110 1ebedb77 2019-10-19 stsp
1111 1ebedb77 2019-10-19 stsp static mode_t
1112 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1113 1ebedb77 2019-10-19 stsp {
1114 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1115 1ebedb77 2019-10-19 stsp
1116 1ebedb77 2019-10-19 stsp if (executable) {
1117 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1118 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1119 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1122 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1123 1ebedb77 2019-10-19 stsp }
1124 1ebedb77 2019-10-19 stsp
1125 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1126 13d9040b 2019-03-27 stsp }
1127 13d9040b 2019-03-27 stsp
1128 8ba819a3 2020-07-23 stsp /* forward declaration */
1129 13d9040b 2019-03-27 stsp static const struct got_error *
1130 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1131 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1132 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1133 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1134 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1135 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1136 8ba819a3 2020-07-23 stsp
1137 5a1fbc73 2020-07-23 stsp /*
1138 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1139 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1140 5a1fbc73 2020-07-23 stsp */
1141 8ba819a3 2020-07-23 stsp static const struct got_error *
1142 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1143 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1144 5a1fbc73 2020-07-23 stsp {
1145 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1146 5a1fbc73 2020-07-23 stsp ssize_t elen;
1147 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1148 5a1fbc73 2020-07-23 stsp int fd;
1149 5a1fbc73 2020-07-23 stsp
1150 c6e8a826 2021-04-05 stsp *did_something = 0;
1151 c6e8a826 2021-04-05 stsp
1152 5a1fbc73 2020-07-23 stsp /*
1153 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1154 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1155 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1156 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1157 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1158 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1159 5a1fbc73 2020-07-23 stsp */
1160 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1161 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1162 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
1163 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1164 5a1fbc73 2020-07-23 stsp
1165 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1166 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1167 5a1fbc73 2020-07-23 stsp if (elen == -1)
1168 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1169 5a1fbc73 2020-07-23 stsp
1170 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1171 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1172 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1173 5a1fbc73 2020-07-23 stsp }
1174 5a1fbc73 2020-07-23 stsp
1175 c6e8a826 2021-04-05 stsp *did_something = 1;
1176 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1177 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1178 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp return err;
1180 3c1ec782 2020-07-23 stsp }
1181 3c1ec782 2020-07-23 stsp
1182 3c1ec782 2020-07-23 stsp static const struct got_error *
1183 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1184 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 3c1ec782 2020-07-23 stsp {
1186 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1187 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1188 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1189 3c1ec782 2020-07-23 stsp
1190 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1191 3c1ec782 2020-07-23 stsp
1192 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1193 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1194 3c1ec782 2020-07-23 stsp return NULL;
1195 3c1ec782 2020-07-23 stsp }
1196 3c1ec782 2020-07-23 stsp
1197 3c1ec782 2020-07-23 stsp /*
1198 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1199 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1200 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1201 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1202 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1203 3c1ec782 2020-07-23 stsp */
1204 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1205 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1206 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1207 ce031e9e 2020-10-20 stsp if (err)
1208 ce031e9e 2020-10-20 stsp return err;
1209 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1210 ce031e9e 2020-10-20 stsp free(parent);
1211 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1212 ce031e9e 2020-10-20 stsp }
1213 ce031e9e 2020-10-20 stsp free(parent);
1214 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1215 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1216 3c1ec782 2020-07-23 stsp free(abspath);
1217 3c1ec782 2020-07-23 stsp return err;
1218 3c1ec782 2020-07-23 stsp }
1219 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1220 3c1ec782 2020-07-23 stsp free(abspath);
1221 3c1ec782 2020-07-23 stsp if (err)
1222 3c1ec782 2020-07-23 stsp return err;
1223 3c1ec782 2020-07-23 stsp } else {
1224 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1225 3c1ec782 2020-07-23 stsp if (err)
1226 3c1ec782 2020-07-23 stsp return err;
1227 3c1ec782 2020-07-23 stsp }
1228 3c1ec782 2020-07-23 stsp
1229 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1230 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1231 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1232 3c1ec782 2020-07-23 stsp return NULL;
1233 3c1ec782 2020-07-23 stsp }
1234 3c1ec782 2020-07-23 stsp
1235 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1236 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1237 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1238 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1239 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1240 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1241 3c1ec782 2020-07-23 stsp
1242 3c1ec782 2020-07-23 stsp free(path_got);
1243 3c1ec782 2020-07-23 stsp return NULL;
1244 5a1fbc73 2020-07-23 stsp }
1245 5a1fbc73 2020-07-23 stsp
1246 5a1fbc73 2020-07-23 stsp static const struct got_error *
1247 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1248 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1249 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1250 ace90326 2021-09-27 thomas int path_is_unversioned, int allow_bad_symlinks,
1251 ace90326 2021-09-27 thomas struct got_repository *repo,
1252 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 9d31a1d8 2018-03-11 stsp {
1254 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1255 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1256 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1257 906c123b 2020-07-23 stsp char *path_got = NULL;
1258 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1259 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 8ba819a3 2020-07-23 stsp
1261 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1262 2e1fa222 2020-07-23 stsp
1263 8ba819a3 2020-07-23 stsp /*
1264 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1265 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1266 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1267 8ba819a3 2020-07-23 stsp * in the blob object.
1268 8ba819a3 2020-07-23 stsp */
1269 8ba819a3 2020-07-23 stsp do {
1270 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1271 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1272 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1273 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1274 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1275 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1276 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1277 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1278 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1279 3b9f0f87 2020-07-23 stsp progress_arg);
1280 8ba819a3 2020-07-23 stsp }
1281 8ba819a3 2020-07-23 stsp if (len > 0) {
1282 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1283 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1284 8ba819a3 2020-07-23 stsp len - hdrlen);
1285 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1286 8ba819a3 2020-07-23 stsp hdrlen = 0;
1287 8ba819a3 2020-07-23 stsp }
1288 8ba819a3 2020-07-23 stsp } while (len != 0);
1289 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1290 8ba819a3 2020-07-23 stsp
1291 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1292 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1293 3c1ec782 2020-07-23 stsp if (err)
1294 3c1ec782 2020-07-23 stsp return err;
1295 8ba819a3 2020-07-23 stsp
1296 ace90326 2021-09-27 thomas if (*is_bad_symlink && !allow_bad_symlinks) {
1297 906c123b 2020-07-23 stsp /* install as a regular file */
1298 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1299 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1300 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1301 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1302 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1303 906c123b 2020-07-23 stsp goto done;
1304 906c123b 2020-07-23 stsp }
1305 906c123b 2020-07-23 stsp
1306 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1307 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1308 c6e8a826 2021-04-05 stsp int symlink_replaced;
1309 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1310 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1311 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1312 c90c8ce3 2020-07-23 stsp goto done;
1313 c90c8ce3 2020-07-23 stsp }
1314 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1315 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1316 5a1fbc73 2020-07-23 stsp if (err)
1317 f35fa46a 2020-07-23 stsp goto done;
1318 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1319 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1320 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1321 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1322 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1323 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1324 c6e8a826 2021-04-05 stsp } else {
1325 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1326 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1327 c6e8a826 2021-04-05 stsp }
1328 f35fa46a 2020-07-23 stsp }
1329 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1330 f35fa46a 2020-07-23 stsp }
1331 f35fa46a 2020-07-23 stsp
1332 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1333 f4994adc 2020-10-20 stsp char *parent;
1334 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1335 f4994adc 2020-10-20 stsp if (err)
1336 f4994adc 2020-10-20 stsp goto done;
1337 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1338 f4994adc 2020-10-20 stsp free(parent);
1339 f35fa46a 2020-07-23 stsp if (err)
1340 f35fa46a 2020-07-23 stsp goto done;
1341 f35fa46a 2020-07-23 stsp /*
1342 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1343 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1344 f35fa46a 2020-07-23 stsp */
1345 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1346 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1347 f35fa46a 2020-07-23 stsp goto done;
1348 f35fa46a 2020-07-23 stsp }
1349 f35fa46a 2020-07-23 stsp }
1350 f35fa46a 2020-07-23 stsp
1351 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1352 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1353 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1354 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1355 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1356 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1357 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1358 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1359 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1360 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1361 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1362 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1363 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1364 8ba819a3 2020-07-23 stsp } else {
1365 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1366 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1367 8ba819a3 2020-07-23 stsp }
1368 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1369 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1370 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1371 8ba819a3 2020-07-23 stsp done:
1372 906c123b 2020-07-23 stsp free(path_got);
1373 8ba819a3 2020-07-23 stsp return err;
1374 8ba819a3 2020-07-23 stsp }
1375 8ba819a3 2020-07-23 stsp
1376 8ba819a3 2020-07-23 stsp static const struct got_error *
1377 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1378 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1379 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1380 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1381 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1382 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 8ba819a3 2020-07-23 stsp {
1384 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1385 507dc3bb 2018-12-29 stsp int fd = -1;
1386 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1387 507dc3bb 2018-12-29 stsp int update = 0;
1388 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1389 8ba819a3 2020-07-23 stsp
1390 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1391 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1392 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1393 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1394 f5375317 2020-10-20 stsp char *parent;
1395 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1396 f5375317 2020-10-20 stsp if (err)
1397 f5375317 2020-10-20 stsp return err;
1398 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1399 f5375317 2020-10-20 stsp free(parent);
1400 21908da4 2019-01-13 stsp if (err)
1401 21908da4 2019-01-13 stsp return err;
1402 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1403 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1404 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1405 21908da4 2019-01-13 stsp if (fd == -1)
1406 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1407 230a42bd 2019-05-11 jcs ondisk_path);
1408 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1409 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1410 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1411 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1412 3b9f0f87 2020-07-23 stsp goto done;
1413 3b9f0f87 2020-07-23 stsp }
1414 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1415 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1416 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1417 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1418 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1419 507dc3bb 2018-12-29 stsp goto done;
1420 d70b8e30 2018-12-27 stsp } else {
1421 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1422 507dc3bb 2018-12-29 stsp ondisk_path);
1423 507dc3bb 2018-12-29 stsp if (err)
1424 507dc3bb 2018-12-29 stsp goto done;
1425 507dc3bb 2018-12-29 stsp update = 1;
1426 9d31a1d8 2018-03-11 stsp }
1427 507dc3bb 2018-12-29 stsp } else
1428 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1429 9d31a1d8 2018-03-11 stsp }
1430 9d31a1d8 2018-03-11 stsp
1431 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1432 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1433 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1434 3818e3c4 2020-11-01 naddy goto done;
1435 3818e3c4 2020-11-01 naddy }
1436 3818e3c4 2020-11-01 naddy
1437 bd6aa359 2020-07-23 stsp if (progress_cb) {
1438 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1439 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1440 bd6aa359 2020-07-23 stsp path);
1441 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1442 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1443 bd6aa359 2020-07-23 stsp path);
1444 bd6aa359 2020-07-23 stsp else
1445 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1446 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1447 bd6aa359 2020-07-23 stsp if (err)
1448 bd6aa359 2020-07-23 stsp goto done;
1449 bd6aa359 2020-07-23 stsp }
1450 d7b62c98 2018-12-27 stsp
1451 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1452 9d31a1d8 2018-03-11 stsp do {
1453 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1454 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1455 9d31a1d8 2018-03-11 stsp if (err)
1456 9d31a1d8 2018-03-11 stsp break;
1457 9d31a1d8 2018-03-11 stsp if (len > 0) {
1458 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1459 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1460 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1461 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1462 b87c6f83 2018-12-24 stsp goto done;
1463 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1464 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1465 b87c6f83 2018-12-24 stsp goto done;
1466 9d31a1d8 2018-03-11 stsp }
1467 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1468 9d31a1d8 2018-03-11 stsp }
1469 9d31a1d8 2018-03-11 stsp } while (len != 0);
1470 9d31a1d8 2018-03-11 stsp
1471 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1472 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1473 816dc654 2019-02-16 stsp goto done;
1474 816dc654 2019-02-16 stsp }
1475 9d31a1d8 2018-03-11 stsp
1476 507dc3bb 2018-12-29 stsp if (update) {
1477 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1478 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1479 f6d8c0ac 2020-11-09 stsp goto done;
1480 f6d8c0ac 2020-11-09 stsp }
1481 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1482 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1483 230a42bd 2019-05-11 jcs ondisk_path);
1484 68ed9ba5 2019-02-10 stsp goto done;
1485 68ed9ba5 2019-02-10 stsp }
1486 63df146d 2020-11-09 stsp free(tmppath);
1487 63df146d 2020-11-09 stsp tmppath = NULL;
1488 507dc3bb 2018-12-29 stsp }
1489 507dc3bb 2018-12-29 stsp
1490 9d31a1d8 2018-03-11 stsp done:
1491 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1493 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1494 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1495 507dc3bb 2018-12-29 stsp free(tmppath);
1496 6353ad76 2019-02-08 stsp return err;
1497 6353ad76 2019-02-08 stsp }
1498 6353ad76 2019-02-08 stsp
1499 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1500 6353ad76 2019-02-08 stsp static const struct got_error *
1501 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1502 7154f6ce 2019-03-27 stsp {
1503 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1504 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1505 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1506 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1508 7154f6ce 2019-03-27 stsp };
1509 7154f6ce 2019-03-27 stsp int i = 0;
1510 9bdd68dd 2020-01-02 naddy char *line = NULL;
1511 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1512 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1513 7154f6ce 2019-03-27 stsp
1514 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1515 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1516 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1517 7154f6ce 2019-03-27 stsp if (feof(f))
1518 7154f6ce 2019-03-27 stsp break;
1519 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1520 7154f6ce 2019-03-27 stsp break;
1521 7154f6ce 2019-03-27 stsp }
1522 7154f6ce 2019-03-27 stsp
1523 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1524 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1525 19332e6d 2019-05-13 stsp == 0)
1526 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1527 7154f6ce 2019-03-27 stsp else
1528 7154f6ce 2019-03-27 stsp i++;
1529 7154f6ce 2019-03-27 stsp }
1530 7154f6ce 2019-03-27 stsp }
1531 9bdd68dd 2020-01-02 naddy free(line);
1532 7154f6ce 2019-03-27 stsp
1533 7154f6ce 2019-03-27 stsp return err;
1534 e2b1e152 2019-07-13 stsp }
1535 e2b1e152 2019-07-13 stsp
1536 e2b1e152 2019-07-13 stsp static int
1537 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 1ebedb77 2019-10-19 stsp {
1539 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1540 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1541 1ebedb77 2019-10-19 stsp }
1542 1ebedb77 2019-10-19 stsp
1543 1ebedb77 2019-10-19 stsp static int
1544 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 e2b1e152 2019-07-13 stsp {
1546 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1547 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1548 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1550 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1551 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1552 c363b2c1 2019-08-03 stsp }
1553 c363b2c1 2019-08-03 stsp
1554 c363b2c1 2019-08-03 stsp static unsigned char
1555 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1556 c363b2c1 2019-08-03 stsp {
1557 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1558 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1559 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1564 c363b2c1 2019-08-03 stsp default:
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1566 a919d5c4 2020-07-23 stsp }
1567 a919d5c4 2020-07-23 stsp }
1568 a919d5c4 2020-07-23 stsp
1569 a919d5c4 2020-07-23 stsp static const struct got_error *
1570 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1571 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1572 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1573 a919d5c4 2020-07-23 stsp {
1574 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1575 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1576 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1577 a919d5c4 2020-07-23 stsp ssize_t elen;
1578 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1579 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 a919d5c4 2020-07-23 stsp
1582 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1583 a919d5c4 2020-07-23 stsp
1584 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1585 a919d5c4 2020-07-23 stsp do {
1586 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1587 a919d5c4 2020-07-23 stsp if (err)
1588 a919d5c4 2020-07-23 stsp return err;
1589 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1590 a919d5c4 2020-07-23 stsp /*
1591 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1592 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1593 a919d5c4 2020-07-23 stsp */
1594 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1595 a919d5c4 2020-07-23 stsp }
1596 a919d5c4 2020-07-23 stsp if (len > 0) {
1597 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1598 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1599 a919d5c4 2020-07-23 stsp len - hdrlen);
1600 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1601 a919d5c4 2020-07-23 stsp hdrlen = 0;
1602 a919d5c4 2020-07-23 stsp }
1603 a919d5c4 2020-07-23 stsp } while (len != 0);
1604 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1605 a919d5c4 2020-07-23 stsp
1606 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1607 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1608 a919d5c4 2020-07-23 stsp if (elen == -1)
1609 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1610 a919d5c4 2020-07-23 stsp } else {
1611 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1612 a919d5c4 2020-07-23 stsp if (elen == -1)
1613 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1614 c363b2c1 2019-08-03 stsp }
1615 a919d5c4 2020-07-23 stsp
1616 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1617 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1618 a919d5c4 2020-07-23 stsp
1619 a919d5c4 2020-07-23 stsp return NULL;
1620 7154f6ce 2019-03-27 stsp }
1621 7154f6ce 2019-03-27 stsp
1622 7154f6ce 2019-03-27 stsp static const struct got_error *
1623 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1624 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1625 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1626 6353ad76 2019-02-08 stsp {
1627 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1628 6353ad76 2019-02-08 stsp struct got_object_id id;
1629 6353ad76 2019-02-08 stsp size_t hdrlen;
1630 1338848f 2019-12-13 stsp int fd = -1;
1631 6353ad76 2019-02-08 stsp FILE *f = NULL;
1632 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1633 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1634 6353ad76 2019-02-08 stsp size_t flen, blen;
1635 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1636 6353ad76 2019-02-08 stsp
1637 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1638 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1639 6353ad76 2019-02-08 stsp
1640 7f91a133 2019-12-13 stsp /*
1641 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1642 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1643 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1644 7f91a133 2019-12-13 stsp */
1645 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1646 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1647 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1648 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1649 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1650 882ef1b9 2019-12-13 stsp else
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1652 882ef1b9 2019-12-13 stsp goto done;
1653 882ef1b9 2019-12-13 stsp }
1654 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1655 3d35a492 2019-12-13 stsp goto done;
1656 3d35a492 2019-12-13 stsp }
1657 7f91a133 2019-12-13 stsp } else {
1658 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1659 3dc1dc04 2021-09-27 thomas if (fd == -1 && errno != ENOENT &&
1660 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
1661 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1662 3dc1dc04 2021-09-27 thomas else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1663 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1664 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1665 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1666 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1667 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1668 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1669 3d35a492 2019-12-13 stsp else
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1671 3d35a492 2019-12-13 stsp goto done;
1672 3d35a492 2019-12-13 stsp }
1673 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1674 1338848f 2019-12-13 stsp goto done;
1675 a378724f 2019-02-10 stsp }
1676 a378724f 2019-02-10 stsp }
1677 6353ad76 2019-02-08 stsp
1678 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1679 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1680 1338848f 2019-12-13 stsp goto done;
1681 3f148bc6 2019-07-27 stsp }
1682 efdd40df 2019-07-27 stsp
1683 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1684 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1685 1338848f 2019-12-13 stsp goto done;
1686 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1687 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1688 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1689 1338848f 2019-12-13 stsp goto done;
1690 d00136be 2019-03-26 stsp }
1691 b8f41171 2019-02-10 stsp
1692 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1693 f179e66d 2020-07-23 stsp goto done;
1694 f179e66d 2020-07-23 stsp
1695 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1696 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1697 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1698 1338848f 2019-12-13 stsp goto done;
1699 f179e66d 2020-07-23 stsp }
1700 6353ad76 2019-02-08 stsp
1701 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1702 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1703 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1704 c363b2c1 2019-08-03 stsp else
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 c363b2c1 2019-08-03 stsp
1707 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1708 6353ad76 2019-02-08 stsp if (err)
1709 1338848f 2019-12-13 stsp goto done;
1710 6353ad76 2019-02-08 stsp
1711 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1712 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1713 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1714 a919d5c4 2020-07-23 stsp goto done;
1715 a919d5c4 2020-07-23 stsp }
1716 a919d5c4 2020-07-23 stsp
1717 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1718 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1719 ab0d4361 2019-12-13 stsp if (fd == -1) {
1720 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1721 ab0d4361 2019-12-13 stsp goto done;
1722 ab0d4361 2019-12-13 stsp }
1723 3d35a492 2019-12-13 stsp }
1724 3d35a492 2019-12-13 stsp
1725 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1726 6353ad76 2019-02-08 stsp if (f == NULL) {
1727 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1728 6353ad76 2019-02-08 stsp goto done;
1729 6353ad76 2019-02-08 stsp }
1730 1338848f 2019-12-13 stsp fd = -1;
1731 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1732 656b1f76 2019-05-11 jcs for (;;) {
1733 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1734 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1735 6353ad76 2019-02-08 stsp if (err)
1736 7154f6ce 2019-03-27 stsp goto done;
1737 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1738 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1739 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1740 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1741 7154f6ce 2019-03-27 stsp goto done;
1742 80c5c120 2019-02-19 stsp }
1743 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1744 6353ad76 2019-02-08 stsp if (flen != 0)
1745 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1746 6353ad76 2019-02-08 stsp break;
1747 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1748 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1749 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1750 6353ad76 2019-02-08 stsp break;
1751 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1752 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1753 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1754 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1755 6353ad76 2019-02-08 stsp break;
1756 6353ad76 2019-02-08 stsp }
1757 6353ad76 2019-02-08 stsp } else {
1758 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1759 6353ad76 2019-02-08 stsp break;
1760 6353ad76 2019-02-08 stsp }
1761 6353ad76 2019-02-08 stsp hdrlen = 0;
1762 6353ad76 2019-02-08 stsp }
1763 7154f6ce 2019-03-27 stsp
1764 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1765 7154f6ce 2019-03-27 stsp rewind(f);
1766 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1767 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1768 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1769 6353ad76 2019-02-08 stsp done:
1770 6353ad76 2019-02-08 stsp if (blob)
1771 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1772 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1773 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1774 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1775 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1776 9d31a1d8 2018-03-11 stsp return err;
1777 e2b1e152 2019-07-13 stsp }
1778 e2b1e152 2019-07-13 stsp
1779 e2b1e152 2019-07-13 stsp /*
1780 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1781 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1782 e2b1e152 2019-07-13 stsp */
1783 e2b1e152 2019-07-13 stsp static const struct got_error *
1784 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1785 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1786 e2b1e152 2019-07-13 stsp {
1787 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1788 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1789 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1790 e2b1e152 2019-07-13 stsp
1791 e2b1e152 2019-07-13 stsp return NULL;
1792 9d31a1d8 2018-03-11 stsp }
1793 9d31a1d8 2018-03-11 stsp
1794 9d31a1d8 2018-03-11 stsp static const struct got_error *
1795 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1796 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1797 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1798 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1799 0584f854 2019-04-06 stsp void *progress_arg)
1800 9d31a1d8 2018-03-11 stsp {
1801 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1802 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1803 6353ad76 2019-02-08 stsp char *ondisk_path;
1804 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1805 b8f41171 2019-02-10 stsp struct stat sb;
1806 9d31a1d8 2018-03-11 stsp
1807 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1808 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1809 6353ad76 2019-02-08 stsp
1810 abb4604f 2019-07-27 stsp if (ie) {
1811 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1812 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1813 a76c42e6 2019-08-03 stsp goto done;
1814 a76c42e6 2019-08-03 stsp }
1815 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1816 7f91a133 2019-12-13 stsp repo);
1817 abb4604f 2019-07-27 stsp if (err)
1818 abb4604f 2019-07-27 stsp goto done;
1819 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1820 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1821 c90c8ce3 2020-07-23 stsp } else {
1822 e6f4ba31 2021-09-24 thomas if (stat(ondisk_path, &sb) == -1) {
1823 e6f4ba31 2021-09-24 thomas if (errno != ENOENT) {
1824 e6f4ba31 2021-09-24 thomas err = got_error_from_errno2("stat",
1825 e6f4ba31 2021-09-24 thomas ondisk_path);
1826 e6f4ba31 2021-09-24 thomas goto done;
1827 e6f4ba31 2021-09-24 thomas }
1828 e6f4ba31 2021-09-24 thomas sb.st_mode = GOT_DEFAULT_FILE_MODE;
1829 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1830 e6f4ba31 2021-09-24 thomas } else {
1831 e6f4ba31 2021-09-24 thomas if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1832 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_UNVERSIONED;
1833 e6f4ba31 2021-09-24 thomas else
1834 e6f4ba31 2021-09-24 thomas status = GOT_STATUS_OBSTRUCTED;
1835 e6f4ba31 2021-09-24 thomas }
1836 c90c8ce3 2020-07-23 stsp }
1837 6353ad76 2019-02-08 stsp
1838 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1839 2c41dce7 2021-06-27 stsp if (ie)
1840 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1841 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1842 b8f41171 2019-02-10 stsp goto done;
1843 b8f41171 2019-02-10 stsp }
1844 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1845 a769b60b 2021-06-27 stsp if (ie)
1846 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1847 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1848 5036ab18 2020-04-18 stsp path);
1849 5036ab18 2020-04-18 stsp goto done;
1850 5036ab18 2020-04-18 stsp }
1851 b8f41171 2019-02-10 stsp
1852 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1853 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1854 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 c6e8a826 2021-04-05 stsp /*
1856 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1857 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1858 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1859 c6e8a826 2021-04-05 stsp * updating contents of this file.
1860 c6e8a826 2021-04-05 stsp */
1861 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1862 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1863 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1864 c6e8a826 2021-04-05 stsp /* Same commit. */
1865 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1866 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1867 e2b1e152 2019-07-13 stsp if (err)
1868 e2b1e152 2019-07-13 stsp goto done;
1869 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1870 b8f41171 2019-02-10 stsp path);
1871 6353ad76 2019-02-08 stsp goto done;
1872 a378724f 2019-02-10 stsp }
1873 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1874 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1875 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1876 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1877 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1878 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1879 0f58026f 2021-04-05 stsp if (err)
1880 0f58026f 2021-04-05 stsp goto done;
1881 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1882 0f58026f 2021-04-05 stsp path);
1883 b8f41171 2019-02-10 stsp goto done;
1884 e2b1e152 2019-07-13 stsp }
1885 9d31a1d8 2018-03-11 stsp }
1886 9d31a1d8 2018-03-11 stsp
1887 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1888 8da9e5f4 2019-01-12 stsp if (err)
1889 6353ad76 2019-02-08 stsp goto done;
1890 512f0d0e 2019-01-02 stsp
1891 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1892 234035bc 2019-06-01 stsp int update_timestamps;
1893 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1894 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1895 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1896 234035bc 2019-06-01 stsp struct got_object_id id2;
1897 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1898 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1899 234035bc 2019-06-01 stsp if (err)
1900 f69721c3 2019-10-21 stsp goto done;
1901 f69721c3 2019-10-21 stsp }
1902 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1903 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1904 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1905 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1906 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1907 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1908 f69721c3 2019-10-21 stsp goto done;
1909 f69721c3 2019-10-21 stsp }
1910 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1911 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1912 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1913 234035bc 2019-06-01 stsp goto done;
1914 f69721c3 2019-10-21 stsp }
1915 234035bc 2019-06-01 stsp }
1916 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1917 36bf999c 2020-07-23 stsp char *link_target;
1918 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1919 36bf999c 2020-07-23 stsp if (err)
1920 36bf999c 2020-07-23 stsp goto done;
1921 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1922 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1923 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1924 36bf999c 2020-07-23 stsp free(link_target);
1925 993e2a1b 2020-07-23 stsp } else {
1926 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1927 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1928 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1929 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1930 993e2a1b 2020-07-23 stsp }
1931 f69721c3 2019-10-21 stsp free(label_orig);
1932 234035bc 2019-06-01 stsp if (blob2)
1933 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1934 909d120e 2019-09-22 stsp if (err)
1935 909d120e 2019-09-22 stsp goto done;
1936 234035bc 2019-06-01 stsp /*
1937 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1938 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1939 234035bc 2019-06-01 stsp * unmodified files again.
1940 234035bc 2019-06-01 stsp */
1941 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1942 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1943 234035bc 2019-06-01 stsp update_timestamps);
1944 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1945 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1946 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1947 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1948 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1949 1ee397ad 2019-07-12 stsp if (err)
1950 1ee397ad 2019-07-12 stsp goto done;
1951 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1952 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1953 13d9040b 2019-03-27 stsp if (err)
1954 13d9040b 2019-03-27 stsp goto done;
1955 13d9040b 2019-03-27 stsp } else {
1956 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1957 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1958 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1959 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1960 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1961 ace90326 2021-09-27 thomas status == GOT_STATUS_UNVERSIONED, 0,
1962 ace90326 2021-09-27 thomas repo, progress_cb, progress_arg);
1963 2e1fa222 2020-07-23 stsp } else {
1964 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1965 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1966 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1967 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1968 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1969 2e1fa222 2020-07-23 stsp }
1970 13d9040b 2019-03-27 stsp if (err)
1971 13d9040b 2019-03-27 stsp goto done;
1972 65b05cec 2020-07-23 stsp
1973 054041d0 2020-06-24 stsp if (ie) {
1974 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1975 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1976 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1977 054041d0 2020-06-24 stsp } else {
1978 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1979 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
1980 054041d0 2020-06-24 stsp &blob->id);
1981 054041d0 2020-06-24 stsp }
1982 13d9040b 2019-03-27 stsp if (err)
1983 13d9040b 2019-03-27 stsp goto done;
1984 65b05cec 2020-07-23 stsp
1985 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1986 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1987 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1988 2e1fa222 2020-07-23 stsp }
1989 13d9040b 2019-03-27 stsp }
1990 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1991 6353ad76 2019-02-08 stsp done:
1992 6353ad76 2019-02-08 stsp free(ondisk_path);
1993 8da9e5f4 2019-01-12 stsp return err;
1994 90285c3b 2019-01-08 stsp }
1995 90285c3b 2019-01-08 stsp
1996 90285c3b 2019-01-08 stsp static const struct got_error *
1997 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1998 90285c3b 2019-01-08 stsp {
1999 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2000 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2001 90285c3b 2019-01-08 stsp
2002 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2003 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2004 90285c3b 2019-01-08 stsp
2005 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2006 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2007 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2008 c97665ae 2019-01-13 stsp } else {
2009 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2010 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2011 1c4cdd89 2021-06-20 stsp if (err)
2012 1c4cdd89 2021-06-20 stsp goto done;
2013 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2014 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2015 fddefe3b 2020-10-20 stsp free(ondisk_path);
2016 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2017 1c4cdd89 2021-06-20 stsp parent = NULL;
2018 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2019 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2020 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2021 fddefe3b 2020-10-20 stsp ondisk_path);
2022 90285c3b 2019-01-08 stsp break;
2023 90285c3b 2019-01-08 stsp }
2024 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2025 1c4cdd89 2021-06-20 stsp if (err)
2026 1c4cdd89 2021-06-20 stsp break;
2027 1c4cdd89 2021-06-20 stsp }
2028 90285c3b 2019-01-08 stsp }
2029 1c4cdd89 2021-06-20 stsp done:
2030 90285c3b 2019-01-08 stsp free(ondisk_path);
2031 1c4cdd89 2021-06-20 stsp free(parent);
2032 90285c3b 2019-01-08 stsp return err;
2033 512f0d0e 2019-01-02 stsp }
2034 512f0d0e 2019-01-02 stsp
2035 708d8e67 2019-03-27 stsp static const struct got_error *
2036 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2037 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2038 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 708d8e67 2019-03-27 stsp {
2040 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2041 708d8e67 2019-03-27 stsp unsigned char status;
2042 708d8e67 2019-03-27 stsp struct stat sb;
2043 708d8e67 2019-03-27 stsp char *ondisk_path;
2044 708d8e67 2019-03-27 stsp
2045 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2046 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 a76c42e6 2019-08-03 stsp
2048 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2049 708d8e67 2019-03-27 stsp == -1)
2050 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2051 708d8e67 2019-03-27 stsp
2052 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2053 708d8e67 2019-03-27 stsp if (err)
2054 5a58a424 2020-06-23 stsp goto done;
2055 708d8e67 2019-03-27 stsp
2056 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2057 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2058 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2059 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2060 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2061 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2062 993e2a1b 2020-07-23 stsp goto done;
2063 993e2a1b 2020-07-23 stsp }
2064 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2065 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2066 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2067 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2068 993e2a1b 2020-07-23 stsp if (err)
2069 993e2a1b 2020-07-23 stsp goto done;
2070 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2071 993e2a1b 2020-07-23 stsp ie->path);
2072 993e2a1b 2020-07-23 stsp goto done;
2073 993e2a1b 2020-07-23 stsp }
2074 993e2a1b 2020-07-23 stsp
2075 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2076 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2077 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2078 1ee397ad 2019-07-12 stsp if (err)
2079 5a58a424 2020-06-23 stsp goto done;
2080 fc6346c4 2019-03-27 stsp /*
2081 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2082 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2083 fc6346c4 2019-03-27 stsp */
2084 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2085 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2086 fc6346c4 2019-03-27 stsp } else {
2087 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2088 1ee397ad 2019-07-12 stsp if (err)
2089 5a58a424 2020-06-23 stsp goto done;
2090 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2091 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2092 fc6346c4 2019-03-27 stsp if (err)
2093 5a58a424 2020-06-23 stsp goto done;
2094 fc6346c4 2019-03-27 stsp }
2095 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2096 708d8e67 2019-03-27 stsp }
2097 5a58a424 2020-06-23 stsp done:
2098 5a58a424 2020-06-23 stsp free(ondisk_path);
2099 fc6346c4 2019-03-27 stsp return err;
2100 708d8e67 2019-03-27 stsp }
2101 708d8e67 2019-03-27 stsp
2102 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2103 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2104 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2105 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2106 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2107 8da9e5f4 2019-01-12 stsp void *progress_arg;
2108 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2109 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2110 8da9e5f4 2019-01-12 stsp };
2111 8da9e5f4 2019-01-12 stsp
2112 512f0d0e 2019-01-02 stsp static const struct got_error *
2113 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2114 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2115 512f0d0e 2019-01-02 stsp {
2116 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2117 0584f854 2019-04-06 stsp
2118 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2119 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2120 512f0d0e 2019-01-02 stsp
2121 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2122 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2123 8da9e5f4 2019-01-12 stsp }
2124 512f0d0e 2019-01-02 stsp
2125 8da9e5f4 2019-01-12 stsp static const struct got_error *
2126 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 8da9e5f4 2019-01-12 stsp {
2128 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2129 7a9df742 2019-01-08 stsp
2130 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2131 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2132 0584f854 2019-04-06 stsp
2133 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2134 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2135 9d31a1d8 2018-03-11 stsp }
2136 9d31a1d8 2018-03-11 stsp
2137 9d31a1d8 2018-03-11 stsp static const struct got_error *
2138 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 9d31a1d8 2018-03-11 stsp {
2140 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2141 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2142 8da9e5f4 2019-01-12 stsp char *path;
2143 9d31a1d8 2018-03-11 stsp
2144 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2145 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2146 0584f854 2019-04-06 stsp
2147 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2148 63c5ca5d 2019-08-24 stsp return NULL;
2149 63c5ca5d 2019-08-24 stsp
2150 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2151 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2152 8da9e5f4 2019-01-12 stsp == -1)
2153 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2154 9d31a1d8 2018-03-11 stsp
2155 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2156 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2157 8da9e5f4 2019-01-12 stsp else
2158 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2159 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2160 9d31a1d8 2018-03-11 stsp
2161 8da9e5f4 2019-01-12 stsp free(path);
2162 0cd1c46a 2019-03-11 stsp return err;
2163 0cd1c46a 2019-03-11 stsp }
2164 0cd1c46a 2019-03-11 stsp
2165 b2118c49 2020-07-28 stsp const struct got_error *
2166 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 b2118c49 2020-07-28 stsp {
2168 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2169 b2118c49 2020-07-28 stsp
2170 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2171 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2172 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2173 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2174 b2118c49 2020-07-28 stsp }
2175 b2118c49 2020-07-28 stsp
2176 b2118c49 2020-07-28 stsp return NULL;
2177 b2118c49 2020-07-28 stsp }
2178 b2118c49 2020-07-28 stsp
2179 818c7501 2019-07-11 stsp static const struct got_error *
2180 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 0cd1c46a 2019-03-11 stsp {
2182 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2183 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2184 0cd1c46a 2019-03-11 stsp
2185 517bab73 2019-03-11 stsp *refname = NULL;
2186 517bab73 2019-03-11 stsp
2187 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2188 b2118c49 2020-07-28 stsp if (err)
2189 b2118c49 2020-07-28 stsp return err;
2190 0cd1c46a 2019-03-11 stsp
2191 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2192 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2193 0647c563 2019-03-11 stsp *refname = NULL;
2194 0cd1c46a 2019-03-11 stsp }
2195 517bab73 2019-03-11 stsp free(uuidstr);
2196 517bab73 2019-03-11 stsp return err;
2197 517bab73 2019-03-11 stsp }
2198 517bab73 2019-03-11 stsp
2199 818c7501 2019-07-11 stsp const struct got_error *
2200 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 818c7501 2019-07-11 stsp {
2202 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2203 818c7501 2019-07-11 stsp }
2204 818c7501 2019-07-11 stsp
2205 818c7501 2019-07-11 stsp static const struct got_error *
2206 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 818c7501 2019-07-11 stsp {
2208 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2209 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2210 818c7501 2019-07-11 stsp }
2211 818c7501 2019-07-11 stsp
2212 818c7501 2019-07-11 stsp static const struct got_error *
2213 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 818c7501 2019-07-11 stsp {
2215 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2216 818c7501 2019-07-11 stsp }
2217 818c7501 2019-07-11 stsp
2218 818c7501 2019-07-11 stsp static const struct got_error *
2219 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 818c7501 2019-07-11 stsp {
2221 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2222 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2223 818c7501 2019-07-11 stsp }
2224 818c7501 2019-07-11 stsp
2225 818c7501 2019-07-11 stsp static const struct got_error *
2226 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 818c7501 2019-07-11 stsp {
2228 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2229 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2230 0ebf8283 2019-07-24 stsp }
2231 0ebf8283 2019-07-24 stsp
2232 0ebf8283 2019-07-24 stsp static const struct got_error *
2233 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 0ebf8283 2019-07-24 stsp {
2235 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2236 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2237 0ebf8283 2019-07-24 stsp }
2238 0ebf8283 2019-07-24 stsp
2239 0ebf8283 2019-07-24 stsp static const struct got_error *
2240 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 0ebf8283 2019-07-24 stsp {
2242 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2243 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2244 0ebf8283 2019-07-24 stsp }
2245 0ebf8283 2019-07-24 stsp
2246 0ebf8283 2019-07-24 stsp static const struct got_error *
2247 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 0ebf8283 2019-07-24 stsp {
2249 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2250 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2251 818c7501 2019-07-11 stsp }
2252 818c7501 2019-07-11 stsp
2253 0ebf8283 2019-07-24 stsp static const struct got_error *
2254 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 0ebf8283 2019-07-24 stsp {
2256 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2257 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2258 0ebf8283 2019-07-24 stsp }
2259 818c7501 2019-07-11 stsp
2260 0ebf8283 2019-07-24 stsp const struct got_error *
2261 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2262 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2263 0ebf8283 2019-07-24 stsp {
2264 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2265 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2266 0ebf8283 2019-07-24 stsp *path = NULL;
2267 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2268 0ebf8283 2019-07-24 stsp }
2269 0ebf8283 2019-07-24 stsp return NULL;
2270 10604dce 2021-09-24 thomas }
2271 10604dce 2021-09-24 thomas
2272 10604dce 2021-09-24 thomas static const struct got_error *
2273 10604dce 2021-09-24 thomas get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 10604dce 2021-09-24 thomas {
2275 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2276 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2277 0ebf8283 2019-07-24 stsp }
2278 0ebf8283 2019-07-24 stsp
2279 10604dce 2021-09-24 thomas static const struct got_error *
2280 10604dce 2021-09-24 thomas get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 10604dce 2021-09-24 thomas {
2282 10604dce 2021-09-24 thomas return get_ref_name(refname, worktree,
2283 10604dce 2021-09-24 thomas GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2284 10604dce 2021-09-24 thomas }
2285 10604dce 2021-09-24 thomas
2286 517bab73 2019-03-11 stsp /*
2287 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2288 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2289 517bab73 2019-03-11 stsp */
2290 517bab73 2019-03-11 stsp static const struct got_error *
2291 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 517bab73 2019-03-11 stsp {
2293 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2294 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2295 517bab73 2019-03-11 stsp char *refname;
2296 517bab73 2019-03-11 stsp
2297 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2298 517bab73 2019-03-11 stsp if (err)
2299 517bab73 2019-03-11 stsp return err;
2300 0cd1c46a 2019-03-11 stsp
2301 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2302 0cd1c46a 2019-03-11 stsp if (err)
2303 0cd1c46a 2019-03-11 stsp goto done;
2304 0cd1c46a 2019-03-11 stsp
2305 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2306 0cd1c46a 2019-03-11 stsp done:
2307 0cd1c46a 2019-03-11 stsp free(refname);
2308 0cd1c46a 2019-03-11 stsp if (ref)
2309 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2310 3e3a69f1 2019-07-25 stsp return err;
2311 3e3a69f1 2019-07-25 stsp }
2312 3e3a69f1 2019-07-25 stsp
2313 3e3a69f1 2019-07-25 stsp static const struct got_error *
2314 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 3e3a69f1 2019-07-25 stsp {
2316 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2317 3e3a69f1 2019-07-25 stsp
2318 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2319 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2320 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2321 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2322 3e3a69f1 2019-07-25 stsp }
2323 9d31a1d8 2018-03-11 stsp return err;
2324 9d31a1d8 2018-03-11 stsp }
2325 9d31a1d8 2018-03-11 stsp
2326 3e3a69f1 2019-07-25 stsp
2327 ebf99748 2019-05-09 stsp static const struct got_error *
2328 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2329 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2330 ebf99748 2019-05-09 stsp {
2331 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2332 ebf99748 2019-05-09 stsp FILE *index = NULL;
2333 0cd1c46a 2019-03-11 stsp
2334 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2335 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2336 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2337 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2338 ebf99748 2019-05-09 stsp
2339 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2340 3e3a69f1 2019-07-25 stsp if (err)
2341 ebf99748 2019-05-09 stsp goto done;
2342 ebf99748 2019-05-09 stsp
2343 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2344 ebf99748 2019-05-09 stsp if (index == NULL) {
2345 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2346 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2347 ebf99748 2019-05-09 stsp } else {
2348 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2349 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2350 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2351 ebf99748 2019-05-09 stsp }
2352 ebf99748 2019-05-09 stsp done:
2353 ebf99748 2019-05-09 stsp if (err) {
2354 ebf99748 2019-05-09 stsp free(*fileindex_path);
2355 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2356 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2357 ebf99748 2019-05-09 stsp *fileindex = NULL;
2358 ebf99748 2019-05-09 stsp }
2359 ebf99748 2019-05-09 stsp return err;
2360 ebf99748 2019-05-09 stsp }
2361 c932eeeb 2019-05-22 stsp
2362 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2363 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2364 c932eeeb 2019-05-22 stsp const char *path;
2365 c932eeeb 2019-05-22 stsp size_t path_len;
2366 c932eeeb 2019-05-22 stsp const char *entry_name;
2367 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2368 a484d721 2019-06-10 stsp void *progress_arg;
2369 c932eeeb 2019-05-22 stsp };
2370 ebf99748 2019-05-09 stsp
2371 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2372 c932eeeb 2019-05-22 stsp static const struct got_error *
2373 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 c932eeeb 2019-05-22 stsp {
2375 1ee397ad 2019-07-12 stsp const struct got_error *err;
2376 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2377 c932eeeb 2019-05-22 stsp
2378 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2379 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2380 c932eeeb 2019-05-22 stsp return NULL;
2381 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2382 102ff934 2019-06-11 stsp return NULL;
2383 102ff934 2019-06-11 stsp
2384 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2385 a769b60b 2021-06-27 stsp return NULL;
2386 a769b60b 2021-06-27 stsp
2387 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2388 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2389 c932eeeb 2019-05-22 stsp return NULL;
2390 c932eeeb 2019-05-22 stsp
2391 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2392 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2393 edd02c5e 2019-07-11 stsp ie->path);
2394 1ee397ad 2019-07-12 stsp if (err)
2395 1ee397ad 2019-07-12 stsp return err;
2396 1ee397ad 2019-07-12 stsp }
2397 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2398 c932eeeb 2019-05-22 stsp return NULL;
2399 a615e0e7 2020-12-16 stsp }
2400 a615e0e7 2020-12-16 stsp
2401 a615e0e7 2020-12-16 stsp static const struct got_error *
2402 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2403 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2404 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 a615e0e7 2020-12-16 stsp {
2406 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2407 a615e0e7 2020-12-16 stsp
2408 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2409 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2410 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2411 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2412 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2413 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2414 a615e0e7 2020-12-16 stsp
2415 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2416 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2417 9c6338c4 2019-06-02 stsp }
2418 9c6338c4 2019-06-02 stsp
2419 9c6338c4 2019-06-02 stsp static const struct got_error *
2420 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 9c6338c4 2019-06-02 stsp {
2422 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2423 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2424 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2425 867630bb 2020-01-17 stsp struct timespec timeout;
2426 9c6338c4 2019-06-02 stsp
2427 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2428 9c6338c4 2019-06-02 stsp fileindex_path);
2429 9c6338c4 2019-06-02 stsp if (err)
2430 9c6338c4 2019-06-02 stsp goto done;
2431 9c6338c4 2019-06-02 stsp
2432 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2433 9c6338c4 2019-06-02 stsp if (err)
2434 9c6338c4 2019-06-02 stsp goto done;
2435 9c6338c4 2019-06-02 stsp
2436 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2437 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2438 9c6338c4 2019-06-02 stsp fileindex_path);
2439 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2440 9c6338c4 2019-06-02 stsp }
2441 867630bb 2020-01-17 stsp
2442 867630bb 2020-01-17 stsp /*
2443 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2444 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2445 867630bb 2020-01-17 stsp * was recorded in the file index.
2446 867630bb 2020-01-17 stsp */
2447 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2448 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2449 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2450 9c6338c4 2019-06-02 stsp done:
2451 9c6338c4 2019-06-02 stsp if (new_index)
2452 9c6338c4 2019-06-02 stsp fclose(new_index);
2453 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2454 9c6338c4 2019-06-02 stsp return err;
2455 c932eeeb 2019-05-22 stsp }
2456 6ced4176 2019-07-12 stsp
2457 6ced4176 2019-07-12 stsp static const struct got_error *
2458 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2459 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2460 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2461 6ced4176 2019-07-12 stsp {
2462 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2463 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2464 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2465 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2466 6ced4176 2019-07-12 stsp
2467 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2468 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2469 6ced4176 2019-07-12 stsp *tree_id = NULL;
2470 6ced4176 2019-07-12 stsp
2471 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2472 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2473 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2474 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2475 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2476 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2477 6ced4176 2019-07-12 stsp goto done;
2478 6ced4176 2019-07-12 stsp }
2479 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2480 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2481 6ced4176 2019-07-12 stsp if (err)
2482 6ced4176 2019-07-12 stsp goto done;
2483 6ced4176 2019-07-12 stsp return NULL;
2484 6ced4176 2019-07-12 stsp }
2485 6ced4176 2019-07-12 stsp
2486 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2487 6ced4176 2019-07-12 stsp
2488 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2491 6ced4176 2019-07-12 stsp goto done;
2492 6ced4176 2019-07-12 stsp }
2493 6ced4176 2019-07-12 stsp
2494 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2495 6ced4176 2019-07-12 stsp in_repo_path);
2496 6ced4176 2019-07-12 stsp if (err)
2497 6ced4176 2019-07-12 stsp goto done;
2498 6ced4176 2019-07-12 stsp
2499 6ced4176 2019-07-12 stsp free(in_repo_path);
2500 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2501 6ced4176 2019-07-12 stsp
2502 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2503 6ced4176 2019-07-12 stsp if (err)
2504 6ced4176 2019-07-12 stsp goto done;
2505 c932eeeb 2019-05-22 stsp
2506 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2507 6ced4176 2019-07-12 stsp /* Check out a single file. */
2508 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2509 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2510 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2511 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2512 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2513 6ced4176 2019-07-12 stsp goto done;
2514 6ced4176 2019-07-12 stsp }
2515 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2516 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2517 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2518 6ced4176 2019-07-12 stsp goto done;
2519 6ced4176 2019-07-12 stsp }
2520 6ced4176 2019-07-12 stsp } else {
2521 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2522 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2523 6ced4176 2019-07-12 stsp if (err)
2524 6ced4176 2019-07-12 stsp return err;
2525 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2526 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2527 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2528 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2529 6ced4176 2019-07-12 stsp goto done;
2530 6ced4176 2019-07-12 stsp }
2531 6ced4176 2019-07-12 stsp }
2532 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2533 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2534 6ced4176 2019-07-12 stsp } else {
2535 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2536 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2537 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2538 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2539 6ced4176 2019-07-12 stsp goto done;
2540 6ced4176 2019-07-12 stsp }
2541 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2542 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2543 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2544 6ced4176 2019-07-12 stsp goto done;
2545 6ced4176 2019-07-12 stsp }
2546 6ced4176 2019-07-12 stsp }
2547 6ced4176 2019-07-12 stsp done:
2548 6ced4176 2019-07-12 stsp free(id);
2549 6ced4176 2019-07-12 stsp free(in_repo_path);
2550 6ced4176 2019-07-12 stsp if (err) {
2551 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2552 6ced4176 2019-07-12 stsp free(*tree_relpath);
2553 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2554 6ced4176 2019-07-12 stsp free(*tree_id);
2555 6ced4176 2019-07-12 stsp *tree_id = NULL;
2556 6ced4176 2019-07-12 stsp }
2557 07ed472d 2019-07-12 stsp return err;
2558 07ed472d 2019-07-12 stsp }
2559 07ed472d 2019-07-12 stsp
2560 07ed472d 2019-07-12 stsp static const struct got_error *
2561 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2562 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2563 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2564 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 07ed472d 2019-07-12 stsp {
2566 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2567 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2568 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2569 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2570 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2571 07ed472d 2019-07-12 stsp
2572 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2573 7f47418f 2019-12-20 stsp if (err) {
2574 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2575 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2576 7f47418f 2019-12-20 stsp goto done;
2577 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2578 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2579 7f47418f 2019-12-20 stsp if (err)
2580 7f47418f 2019-12-20 stsp return err;
2581 7f47418f 2019-12-20 stsp }
2582 07ed472d 2019-07-12 stsp
2583 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2584 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2585 07ed472d 2019-07-12 stsp if (err)
2586 07ed472d 2019-07-12 stsp goto done;
2587 07ed472d 2019-07-12 stsp
2588 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2589 07ed472d 2019-07-12 stsp if (err)
2590 07ed472d 2019-07-12 stsp goto done;
2591 07ed472d 2019-07-12 stsp
2592 07ed472d 2019-07-12 stsp if (entry_name &&
2593 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2594 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2595 07ed472d 2019-07-12 stsp goto done;
2596 07ed472d 2019-07-12 stsp }
2597 07ed472d 2019-07-12 stsp
2598 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2599 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2600 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2601 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2602 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2603 07ed472d 2019-07-12 stsp arg.repo = repo;
2604 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2605 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2606 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2607 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2608 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2609 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2610 07ed472d 2019-07-12 stsp done:
2611 07ed472d 2019-07-12 stsp if (tree)
2612 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2613 07ed472d 2019-07-12 stsp if (commit)
2614 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2615 6ced4176 2019-07-12 stsp return err;
2616 6ced4176 2019-07-12 stsp }
2617 6ced4176 2019-07-12 stsp
2618 9d31a1d8 2018-03-11 stsp const struct got_error *
2619 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2620 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2621 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2622 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2623 9d31a1d8 2018-03-11 stsp {
2624 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2625 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2626 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2627 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2628 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2629 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2630 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2631 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2632 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2633 f2ea84fa 2019-07-27 stsp int entry_type;
2634 f2ea84fa 2019-07-27 stsp char *relpath;
2635 f2ea84fa 2019-07-27 stsp char *entry_name;
2636 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2637 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 9d31a1d8 2018-03-11 stsp
2639 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2640 f2ea84fa 2019-07-27 stsp
2641 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2642 9d31a1d8 2018-03-11 stsp if (err)
2643 9d31a1d8 2018-03-11 stsp return err;
2644 93a30277 2018-12-24 stsp
2645 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2646 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2647 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2648 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2649 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2650 f2ea84fa 2019-07-27 stsp goto done;
2651 f2ea84fa 2019-07-27 stsp }
2652 6ced4176 2019-07-12 stsp
2653 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2654 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2655 f2ea84fa 2019-07-27 stsp if (err) {
2656 f2ea84fa 2019-07-27 stsp free(tpd);
2657 6ced4176 2019-07-12 stsp goto done;
2658 6ced4176 2019-07-12 stsp }
2659 f2ea84fa 2019-07-27 stsp
2660 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2661 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2662 f2ea84fa 2019-07-27 stsp if (err) {
2663 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2664 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2665 f2ea84fa 2019-07-27 stsp free(tpd);
2666 f2ea84fa 2019-07-27 stsp goto done;
2667 f2ea84fa 2019-07-27 stsp }
2668 f2ea84fa 2019-07-27 stsp } else
2669 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2670 f2ea84fa 2019-07-27 stsp
2671 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2672 6ced4176 2019-07-12 stsp }
2673 6ced4176 2019-07-12 stsp
2674 51514078 2018-12-25 stsp /*
2675 51514078 2018-12-25 stsp * Read the file index.
2676 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2677 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2678 51514078 2018-12-25 stsp */
2679 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2680 8da9e5f4 2019-01-12 stsp if (err)
2681 c4cdcb68 2019-04-03 stsp goto done;
2682 c4cdcb68 2019-04-03 stsp
2683 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2684 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2685 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2686 f2ea84fa 2019-07-27 stsp
2687 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2688 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2689 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2690 f2ea84fa 2019-07-27 stsp if (err)
2691 f2ea84fa 2019-07-27 stsp break;
2692 f2ea84fa 2019-07-27 stsp
2693 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2694 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2695 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2696 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2697 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2698 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2699 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2700 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2701 f2ea84fa 2019-07-27 stsp if (err)
2702 f2ea84fa 2019-07-27 stsp break;
2703 f2ea84fa 2019-07-27 stsp
2704 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2705 711d9cd9 2019-07-12 stsp }
2706 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2707 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2708 af12c6b9 2019-06-04 stsp err = sync_err;
2709 9d31a1d8 2018-03-11 stsp done:
2710 9c6338c4 2019-06-02 stsp free(fileindex_path);
2711 edfa7d7f 2018-09-11 stsp if (tree)
2712 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2713 9d31a1d8 2018-03-11 stsp if (commit)
2714 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2715 5ade8233 2019-07-12 stsp if (fileindex)
2716 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2717 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2718 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2719 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2720 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2721 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2722 f2ea84fa 2019-07-27 stsp free(tpd);
2723 f2ea84fa 2019-07-27 stsp }
2724 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2725 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2726 9d31a1d8 2018-03-11 stsp err = unlockerr;
2727 f8d1f275 2019-02-04 stsp return err;
2728 f8d1f275 2019-02-04 stsp }
2729 f8d1f275 2019-02-04 stsp
2730 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2731 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2732 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2733 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2734 234035bc 2019-06-01 stsp void *progress_arg;
2735 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2736 234035bc 2019-06-01 stsp void *cancel_arg;
2737 f69721c3 2019-10-21 stsp const char *label_orig;
2738 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2739 ace90326 2021-09-27 thomas int allow_bad_symlinks;
2740 234035bc 2019-06-01 stsp };
2741 234035bc 2019-06-01 stsp
2742 234035bc 2019-06-01 stsp static const struct got_error *
2743 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2744 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2745 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2746 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2747 234035bc 2019-06-01 stsp {
2748 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2749 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2750 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2751 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2752 234035bc 2019-06-01 stsp struct stat sb;
2753 234035bc 2019-06-01 stsp unsigned char status;
2754 234035bc 2019-06-01 stsp int local_changes_subsumed;
2755 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2756 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2757 234035bc 2019-06-01 stsp
2758 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2759 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2760 d6c87207 2019-08-02 stsp strlen(path2));
2761 1ee397ad 2019-07-12 stsp if (ie == NULL)
2762 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2763 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2764 234035bc 2019-06-01 stsp
2765 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2766 234035bc 2019-06-01 stsp path2) == -1)
2767 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2768 234035bc 2019-06-01 stsp
2769 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2770 7f91a133 2019-12-13 stsp repo);
2771 234035bc 2019-06-01 stsp if (err)
2772 234035bc 2019-06-01 stsp goto done;
2773 234035bc 2019-06-01 stsp
2774 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2775 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2776 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2777 234035bc 2019-06-01 stsp goto done;
2778 234035bc 2019-06-01 stsp }
2779 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2780 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2781 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2782 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2783 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2784 234035bc 2019-06-01 stsp goto done;
2785 234035bc 2019-06-01 stsp }
2786 234035bc 2019-06-01 stsp
2787 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2788 36bf999c 2020-07-23 stsp char *link_target2;
2789 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2790 36bf999c 2020-07-23 stsp if (err)
2791 36bf999c 2020-07-23 stsp goto done;
2792 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2793 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2794 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2795 36bf999c 2020-07-23 stsp free(link_target2);
2796 af57b12a 2020-07-23 stsp } else {
2797 1af628f4 2021-06-03 stsp int fd;
2798 1af628f4 2021-06-03 stsp
2799 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2800 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2801 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2802 1af628f4 2021-06-03 stsp goto done;
2803 1af628f4 2021-06-03 stsp }
2804 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2805 1af628f4 2021-06-03 stsp f_orig, blob1);
2806 1af628f4 2021-06-03 stsp if (err)
2807 1af628f4 2021-06-03 stsp goto done;
2808 1af628f4 2021-06-03 stsp
2809 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2810 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2811 1af628f4 2021-06-03 stsp goto done;
2812 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2813 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2814 1af628f4 2021-06-03 stsp if (err)
2815 1af628f4 2021-06-03 stsp goto done;
2816 1af628f4 2021-06-03 stsp
2817 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2818 1af628f4 2021-06-03 stsp if (fd == -1) {
2819 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2820 1af628f4 2021-06-03 stsp ondisk_path);
2821 1af628f4 2021-06-03 stsp goto done;
2822 1af628f4 2021-06-03 stsp }
2823 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2824 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2825 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2826 1af628f4 2021-06-03 stsp ondisk_path);
2827 1af628f4 2021-06-03 stsp close(fd);
2828 1af628f4 2021-06-03 stsp goto done;
2829 1af628f4 2021-06-03 stsp }
2830 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2831 1af628f4 2021-06-03 stsp if (err)
2832 1af628f4 2021-06-03 stsp goto done;
2833 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2834 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2835 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2836 1af628f4 2021-06-03 stsp goto done;
2837 1af628f4 2021-06-03 stsp }
2838 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2839 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2840 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2841 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2842 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2843 af57b12a 2020-07-23 stsp }
2844 234035bc 2019-06-01 stsp } else if (blob1) {
2845 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2846 d6c87207 2019-08-02 stsp strlen(path1));
2847 1ee397ad 2019-07-12 stsp if (ie == NULL)
2848 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2849 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2850 2b92fad7 2019-06-02 stsp
2851 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 2b92fad7 2019-06-02 stsp path1) == -1)
2853 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2854 2b92fad7 2019-06-02 stsp
2855 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2856 7f91a133 2019-12-13 stsp repo);
2857 2b92fad7 2019-06-02 stsp if (err)
2858 2b92fad7 2019-06-02 stsp goto done;
2859 2b92fad7 2019-06-02 stsp
2860 2b92fad7 2019-06-02 stsp switch (status) {
2861 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2862 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2863 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2864 1ee397ad 2019-07-12 stsp if (err)
2865 1ee397ad 2019-07-12 stsp goto done;
2866 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2867 2b92fad7 2019-06-02 stsp if (err)
2868 2b92fad7 2019-06-02 stsp goto done;
2869 2b92fad7 2019-06-02 stsp if (ie)
2870 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2871 2b92fad7 2019-06-02 stsp break;
2872 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2873 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2874 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2875 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2876 1ee397ad 2019-07-12 stsp if (err)
2877 1ee397ad 2019-07-12 stsp goto done;
2878 2b92fad7 2019-06-02 stsp if (ie)
2879 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2880 2b92fad7 2019-06-02 stsp break;
2881 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2882 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2883 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2884 0a22ca1a 2020-09-23 stsp /*
2885 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2886 0a22ca1a 2020-09-23 stsp * exists in the repository.
2887 0a22ca1a 2020-09-23 stsp */
2888 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2889 0a22ca1a 2020-09-23 stsp if (err)
2890 0a22ca1a 2020-09-23 stsp goto done;
2891 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2892 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2893 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2894 0a22ca1a 2020-09-23 stsp if (err)
2895 0a22ca1a 2020-09-23 stsp goto done;
2896 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2897 0a22ca1a 2020-09-23 stsp path1);
2898 0a22ca1a 2020-09-23 stsp if (err)
2899 0a22ca1a 2020-09-23 stsp goto done;
2900 0a22ca1a 2020-09-23 stsp if (ie)
2901 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2902 0a22ca1a 2020-09-23 stsp ie);
2903 0a22ca1a 2020-09-23 stsp } else {
2904 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2905 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2906 0a22ca1a 2020-09-23 stsp }
2907 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2908 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2909 0a22ca1a 2020-09-23 stsp free(id);
2910 0a22ca1a 2020-09-23 stsp if (err)
2911 0a22ca1a 2020-09-23 stsp goto done;
2912 0a22ca1a 2020-09-23 stsp break;
2913 0a22ca1a 2020-09-23 stsp }
2914 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2915 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2916 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2917 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2918 1ee397ad 2019-07-12 stsp if (err)
2919 1ee397ad 2019-07-12 stsp goto done;
2920 2b92fad7 2019-06-02 stsp break;
2921 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2922 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2923 1ee397ad 2019-07-12 stsp if (err)
2924 1ee397ad 2019-07-12 stsp goto done;
2925 2b92fad7 2019-06-02 stsp break;
2926 2b92fad7 2019-06-02 stsp default:
2927 2b92fad7 2019-06-02 stsp break;
2928 2b92fad7 2019-06-02 stsp }
2929 234035bc 2019-06-01 stsp } else if (blob2) {
2930 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2931 234035bc 2019-06-01 stsp path2) == -1)
2932 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2933 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2934 d6c87207 2019-08-02 stsp strlen(path2));
2935 234035bc 2019-06-01 stsp if (ie) {
2936 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2937 7f91a133 2019-12-13 stsp -1, NULL, repo);
2938 234035bc 2019-06-01 stsp if (err)
2939 234035bc 2019-06-01 stsp goto done;
2940 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2941 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2942 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2943 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2944 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2945 1ee397ad 2019-07-12 stsp status, path2);
2946 234035bc 2019-06-01 stsp goto done;
2947 234035bc 2019-06-01 stsp }
2948 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2949 36bf999c 2020-07-23 stsp char *link_target2;
2950 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2951 36bf999c 2020-07-23 stsp blob2);
2952 36bf999c 2020-07-23 stsp if (err)
2953 36bf999c 2020-07-23 stsp goto done;
2954 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2955 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2956 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2957 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2958 36bf999c 2020-07-23 stsp free(link_target2);
2959 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2960 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2961 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2962 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2963 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2964 526a746f 2020-07-23 stsp a->progress_arg);
2965 dfe9fba0 2020-07-23 stsp } else {
2966 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2967 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2968 526a746f 2020-07-23 stsp }
2969 dfe9fba0 2020-07-23 stsp if (err)
2970 dfe9fba0 2020-07-23 stsp goto done;
2971 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2972 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2973 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2974 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2975 234035bc 2019-06-01 stsp if (err)
2976 234035bc 2019-06-01 stsp goto done;
2977 234035bc 2019-06-01 stsp }
2978 234035bc 2019-06-01 stsp } else {
2979 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2980 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2981 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2982 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2983 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2984 ace90326 2021-09-27 thomas 0, 1, a->allow_bad_symlinks, repo,
2985 ace90326 2021-09-27 thomas a->progress_cb, a->progress_arg);
2986 2e1fa222 2020-07-23 stsp } else {
2987 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2988 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2989 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2990 2e1fa222 2020-07-23 stsp }
2991 234035bc 2019-06-01 stsp if (err)
2992 234035bc 2019-06-01 stsp goto done;
2993 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2994 234035bc 2019-06-01 stsp if (err)
2995 234035bc 2019-06-01 stsp goto done;
2996 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2997 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
2998 3969253a 2020-03-07 stsp if (err) {
2999 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3000 3969253a 2020-03-07 stsp goto done;
3001 3969253a 2020-03-07 stsp }
3002 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3003 2b92fad7 2019-06-02 stsp if (err) {
3004 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3005 2b92fad7 2019-06-02 stsp goto done;
3006 2b92fad7 2019-06-02 stsp }
3007 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3008 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3009 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3010 2e1fa222 2020-07-23 stsp }
3011 234035bc 2019-06-01 stsp }
3012 234035bc 2019-06-01 stsp }
3013 234035bc 2019-06-01 stsp done:
3014 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3015 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3016 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3017 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3018 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3019 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3020 1af628f4 2021-06-03 stsp free(id_str);
3021 54d5be07 2021-06-03 stsp free(label_deriv2);
3022 234035bc 2019-06-01 stsp free(ondisk_path);
3023 234035bc 2019-06-01 stsp return err;
3024 234035bc 2019-06-01 stsp }
3025 234035bc 2019-06-01 stsp
3026 69de9dd4 2021-09-03 stsp static const struct got_error *
3027 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3028 69de9dd4 2021-09-03 stsp {
3029 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3030 69de9dd4 2021-09-03 stsp
3031 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3032 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3033 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3034 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3035 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3036 69de9dd4 2021-09-03 stsp
3037 69de9dd4 2021-09-03 stsp return NULL;
3038 69de9dd4 2021-09-03 stsp }
3039 69de9dd4 2021-09-03 stsp
3040 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3041 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3042 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3043 234035bc 2019-06-01 stsp struct got_repository *repo;
3044 234035bc 2019-06-01 stsp };
3045 234035bc 2019-06-01 stsp
3046 234035bc 2019-06-01 stsp static const struct got_error *
3047 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3048 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3049 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3050 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3051 234035bc 2019-06-01 stsp {
3052 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3053 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3054 234035bc 2019-06-01 stsp unsigned char status;
3055 234035bc 2019-06-01 stsp struct stat sb;
3056 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3057 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3058 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3059 234035bc 2019-06-01 stsp char *ondisk_path;
3060 234035bc 2019-06-01 stsp
3061 69de9dd4 2021-09-03 stsp if (id == NULL)
3062 69de9dd4 2021-09-03 stsp return NULL;
3063 234035bc 2019-06-01 stsp
3064 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3065 69de9dd4 2021-09-03 stsp if (ie == NULL)
3066 69de9dd4 2021-09-03 stsp return NULL;
3067 69de9dd4 2021-09-03 stsp
3068 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3069 234035bc 2019-06-01 stsp == -1)
3070 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3071 234035bc 2019-06-01 stsp
3072 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3073 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3074 5546d466 2021-09-02 stsp free(ondisk_path);
3075 234035bc 2019-06-01 stsp if (err)
3076 234035bc 2019-06-01 stsp return err;
3077 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3078 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3079 234035bc 2019-06-01 stsp
3080 234035bc 2019-06-01 stsp return NULL;
3081 234035bc 2019-06-01 stsp }
3082 234035bc 2019-06-01 stsp
3083 818c7501 2019-07-11 stsp static const struct got_error *
3084 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3085 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3086 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3087 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3088 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3089 234035bc 2019-06-01 stsp {
3090 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3091 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3092 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3093 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3094 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3095 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3096 234035bc 2019-06-01 stsp
3097 03415a1a 2019-06-02 stsp if (commit_id1) {
3098 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3099 03415a1a 2019-06-02 stsp worktree->path_prefix);
3100 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3101 03415a1a 2019-06-02 stsp goto done;
3102 69d57f3d 2020-07-31 stsp }
3103 69d57f3d 2020-07-31 stsp if (tree_id1) {
3104 69d57f3d 2020-07-31 stsp char *id_str;
3105 03415a1a 2019-06-02 stsp
3106 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3107 03415a1a 2019-06-02 stsp if (err)
3108 03415a1a 2019-06-02 stsp goto done;
3109 f69721c3 2019-10-21 stsp
3110 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3111 f69721c3 2019-10-21 stsp if (err)
3112 f69721c3 2019-10-21 stsp goto done;
3113 f69721c3 2019-10-21 stsp
3114 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3115 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3116 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3117 f69721c3 2019-10-21 stsp free(id_str);
3118 f69721c3 2019-10-21 stsp goto done;
3119 f69721c3 2019-10-21 stsp }
3120 f69721c3 2019-10-21 stsp free(id_str);
3121 03415a1a 2019-06-02 stsp }
3122 234035bc 2019-06-01 stsp
3123 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3124 234035bc 2019-06-01 stsp worktree->path_prefix);
3125 234035bc 2019-06-01 stsp if (err)
3126 234035bc 2019-06-01 stsp goto done;
3127 234035bc 2019-06-01 stsp
3128 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3129 234035bc 2019-06-01 stsp if (err)
3130 234035bc 2019-06-01 stsp goto done;
3131 234035bc 2019-06-01 stsp
3132 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3133 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3134 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3135 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3136 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3137 69de9dd4 2021-09-03 stsp if (err)
3138 69de9dd4 2021-09-03 stsp goto done;
3139 69de9dd4 2021-09-03 stsp
3140 234035bc 2019-06-01 stsp arg.worktree = worktree;
3141 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3142 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3143 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3144 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3145 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3146 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3147 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3148 ace90326 2021-09-27 thomas arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3149 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3150 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3151 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3152 af12c6b9 2019-06-04 stsp err = sync_err;
3153 234035bc 2019-06-01 stsp done:
3154 234035bc 2019-06-01 stsp if (tree1)
3155 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3156 234035bc 2019-06-01 stsp if (tree2)
3157 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3158 f69721c3 2019-10-21 stsp free(label_orig);
3159 818c7501 2019-07-11 stsp return err;
3160 818c7501 2019-07-11 stsp }
3161 234035bc 2019-06-01 stsp
3162 818c7501 2019-07-11 stsp const struct got_error *
3163 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3164 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3165 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3166 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3167 818c7501 2019-07-11 stsp {
3168 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3169 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3170 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3171 818c7501 2019-07-11 stsp
3172 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3173 818c7501 2019-07-11 stsp if (err)
3174 818c7501 2019-07-11 stsp return err;
3175 818c7501 2019-07-11 stsp
3176 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3177 818c7501 2019-07-11 stsp if (err)
3178 818c7501 2019-07-11 stsp goto done;
3179 818c7501 2019-07-11 stsp
3180 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3181 69de9dd4 2021-09-03 stsp worktree);
3182 818c7501 2019-07-11 stsp if (err)
3183 818c7501 2019-07-11 stsp goto done;
3184 818c7501 2019-07-11 stsp
3185 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3186 10604dce 2021-09-24 thomas commit_id2, repo, progress_cb, progress_arg,
3187 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
3188 818c7501 2019-07-11 stsp done:
3189 818c7501 2019-07-11 stsp if (fileindex)
3190 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3191 818c7501 2019-07-11 stsp free(fileindex_path);
3192 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3193 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3194 234035bc 2019-06-01 stsp err = unlockerr;
3195 234035bc 2019-06-01 stsp return err;
3196 234035bc 2019-06-01 stsp }
3197 234035bc 2019-06-01 stsp
3198 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3199 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3200 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3201 927df6b7 2019-02-10 stsp const char *status_path;
3202 927df6b7 2019-02-10 stsp size_t status_path_len;
3203 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3204 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3205 f8d1f275 2019-02-04 stsp void *status_arg;
3206 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3207 f8d1f275 2019-02-04 stsp void *cancel_arg;
3208 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3209 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3210 f2a9dc41 2019-12-13 tracey int report_unchanged;
3211 3143d852 2020-06-25 stsp int no_ignores;
3212 f8d1f275 2019-02-04 stsp };
3213 88d0e355 2019-08-03 stsp
3214 f8d1f275 2019-02-04 stsp static const struct got_error *
3215 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3216 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3217 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3218 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3219 927df6b7 2019-02-10 stsp {
3220 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3221 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3222 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3223 927df6b7 2019-02-10 stsp struct stat sb;
3224 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3225 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3226 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3227 927df6b7 2019-02-10 stsp
3228 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3229 98eaaa12 2019-08-03 stsp if (err)
3230 98eaaa12 2019-08-03 stsp return err;
3231 98eaaa12 2019-08-03 stsp
3232 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3233 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3234 98eaaa12 2019-08-03 stsp return NULL;
3235 98eaaa12 2019-08-03 stsp
3236 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3237 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3238 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3239 98eaaa12 2019-08-03 stsp }
3240 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3241 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3242 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3243 927df6b7 2019-02-10 stsp }
3244 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3245 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3246 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3247 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3248 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3249 98eaaa12 2019-08-03 stsp }
3250 98eaaa12 2019-08-03 stsp
3251 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3252 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3253 927df6b7 2019-02-10 stsp }
3254 927df6b7 2019-02-10 stsp
3255 927df6b7 2019-02-10 stsp static const struct got_error *
3256 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3257 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3258 f8d1f275 2019-02-04 stsp {
3259 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3260 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3261 f8d1f275 2019-02-04 stsp char *abspath;
3262 f8d1f275 2019-02-04 stsp
3263 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3264 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3265 0584f854 2019-04-06 stsp
3266 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3267 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3268 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3269 927df6b7 2019-02-10 stsp return NULL;
3270 927df6b7 2019-02-10 stsp
3271 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3272 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3273 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3274 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3275 f8d1f275 2019-02-04 stsp } else {
3276 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3277 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3278 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3279 f8d1f275 2019-02-04 stsp }
3280 f8d1f275 2019-02-04 stsp
3281 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3282 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3283 f8d1f275 2019-02-04 stsp free(abspath);
3284 9d31a1d8 2018-03-11 stsp return err;
3285 9d31a1d8 2018-03-11 stsp }
3286 f8d1f275 2019-02-04 stsp
3287 f8d1f275 2019-02-04 stsp static const struct got_error *
3288 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3289 f8d1f275 2019-02-04 stsp {
3290 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3291 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3292 2ec1f75b 2019-03-26 stsp unsigned char status;
3293 927df6b7 2019-02-10 stsp
3294 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3295 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3296 0584f854 2019-04-06 stsp
3297 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3298 927df6b7 2019-02-10 stsp return NULL;
3299 927df6b7 2019-02-10 stsp
3300 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3301 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3302 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3303 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3304 2ec1f75b 2019-03-26 stsp else
3305 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3306 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3307 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3308 6841da00 2019-08-08 stsp }
3309 6841da00 2019-08-08 stsp
3310 6841da00 2019-08-08 stsp void
3311 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3312 6841da00 2019-08-08 stsp {
3313 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3314 6841da00 2019-08-08 stsp
3315 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3316 6841da00 2019-08-08 stsp free((char *)pe->path);
3317 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3318 6841da00 2019-08-08 stsp }
3319 6841da00 2019-08-08 stsp
3320 6841da00 2019-08-08 stsp void
3321 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3322 6841da00 2019-08-08 stsp {
3323 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3324 6841da00 2019-08-08 stsp
3325 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3326 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3327 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3328 6841da00 2019-08-08 stsp free((char *)pe->path);
3329 6841da00 2019-08-08 stsp }
3330 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3331 6841da00 2019-08-08 stsp }
3332 6841da00 2019-08-08 stsp
3333 6841da00 2019-08-08 stsp static const struct got_error *
3334 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3335 6841da00 2019-08-08 stsp {
3336 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3337 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3338 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3339 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3340 6841da00 2019-08-08 stsp size_t linesize = 0;
3341 6841da00 2019-08-08 stsp ssize_t linelen;
3342 6841da00 2019-08-08 stsp
3343 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3344 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3345 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3346 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3347 6841da00 2019-08-08 stsp
3348 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3349 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3350 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3351 bd8de430 2019-10-04 stsp
3352 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3353 bd8de430 2019-10-04 stsp if (line[0] == '#')
3354 bd8de430 2019-10-04 stsp continue;
3355 bd8de430 2019-10-04 stsp
3356 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3357 bd8de430 2019-10-04 stsp if (line[0] == '!')
3358 bd8de430 2019-10-04 stsp continue;
3359 bd8de430 2019-10-04 stsp
3360 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3361 6841da00 2019-08-08 stsp line) == -1) {
3362 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3363 6841da00 2019-08-08 stsp goto done;
3364 6841da00 2019-08-08 stsp }
3365 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3366 6841da00 2019-08-08 stsp if (err)
3367 6841da00 2019-08-08 stsp goto done;
3368 6841da00 2019-08-08 stsp }
3369 6841da00 2019-08-08 stsp if (ferror(f)) {
3370 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3371 6841da00 2019-08-08 stsp goto done;
3372 6841da00 2019-08-08 stsp }
3373 6841da00 2019-08-08 stsp
3374 6841da00 2019-08-08 stsp dirpath = strdup(path);
3375 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3376 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3377 6841da00 2019-08-08 stsp goto done;
3378 6841da00 2019-08-08 stsp }
3379 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3380 6841da00 2019-08-08 stsp done:
3381 6841da00 2019-08-08 stsp free(line);
3382 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3383 6841da00 2019-08-08 stsp free(dirpath);
3384 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3385 6841da00 2019-08-08 stsp }
3386 6841da00 2019-08-08 stsp return err;
3387 6841da00 2019-08-08 stsp }
3388 6841da00 2019-08-08 stsp
3389 6841da00 2019-08-08 stsp int
3390 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3391 6841da00 2019-08-08 stsp {
3392 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3393 bd8de430 2019-10-04 stsp
3394 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3395 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3396 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3397 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3398 bd8de430 2019-10-04 stsp
3399 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3400 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3401 6841da00 2019-08-08 stsp
3402 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3403 bd8de430 2019-10-04 stsp continue;
3404 bd8de430 2019-10-04 stsp pattern += 3;
3405 bd8de430 2019-10-04 stsp p = path;
3406 bd8de430 2019-10-04 stsp while (*p) {
3407 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3408 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3409 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3410 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3411 bd8de430 2019-10-04 stsp p++;
3412 bd8de430 2019-10-04 stsp while (*p == '/')
3413 bd8de430 2019-10-04 stsp p++;
3414 bd8de430 2019-10-04 stsp continue;
3415 bd8de430 2019-10-04 stsp }
3416 bd8de430 2019-10-04 stsp return 1;
3417 bd8de430 2019-10-04 stsp }
3418 bd8de430 2019-10-04 stsp }
3419 bd8de430 2019-10-04 stsp }
3420 bd8de430 2019-10-04 stsp
3421 6841da00 2019-08-08 stsp /*
3422 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3423 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3424 6841da00 2019-08-08 stsp * ignores backwards.
3425 6841da00 2019-08-08 stsp */
3426 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3427 6841da00 2019-08-08 stsp while (pe) {
3428 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3429 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3430 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3431 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3432 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3433 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3434 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3435 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3436 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3437 6841da00 2019-08-08 stsp continue;
3438 6841da00 2019-08-08 stsp return 1;
3439 6841da00 2019-08-08 stsp }
3440 6841da00 2019-08-08 stsp }
3441 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3442 6841da00 2019-08-08 stsp }
3443 6841da00 2019-08-08 stsp
3444 6841da00 2019-08-08 stsp return 0;
3445 6841da00 2019-08-08 stsp }
3446 6841da00 2019-08-08 stsp
3447 6841da00 2019-08-08 stsp static const struct got_error *
3448 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3449 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3450 6841da00 2019-08-08 stsp {
3451 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3452 6841da00 2019-08-08 stsp char *ignorespath;
3453 886cec17 2019-12-15 stsp int fd = -1;
3454 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3455 6841da00 2019-08-08 stsp
3456 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3457 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3458 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3459 6841da00 2019-08-08 stsp
3460 886cec17 2019-12-15 stsp if (dirfd != -1) {
3461 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3462 886cec17 2019-12-15 stsp if (fd == -1) {
3463 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3464 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3465 886cec17 2019-12-15 stsp ignorespath);
3466 886cec17 2019-12-15 stsp } else {
3467 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3468 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3469 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3470 886cec17 2019-12-15 stsp ignorespath);
3471 886cec17 2019-12-15 stsp else {
3472 886cec17 2019-12-15 stsp fd = -1;
3473 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3474 886cec17 2019-12-15 stsp }
3475 886cec17 2019-12-15 stsp }
3476 886cec17 2019-12-15 stsp } else {
3477 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3478 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3479 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3480 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3481 886cec17 2019-12-15 stsp ignorespath);
3482 886cec17 2019-12-15 stsp } else
3483 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3484 886cec17 2019-12-15 stsp }
3485 6841da00 2019-08-08 stsp
3486 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3487 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3488 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3489 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3490 6841da00 2019-08-08 stsp free(ignorespath);
3491 6841da00 2019-08-08 stsp return err;
3492 f8d1f275 2019-02-04 stsp }
3493 f8d1f275 2019-02-04 stsp
3494 f8d1f275 2019-02-04 stsp static const struct got_error *
3495 6092c299 2021-10-04 thomas status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3496 6092c299 2021-10-04 thomas int dirfd)
3497 f8d1f275 2019-02-04 stsp {
3498 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3499 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3500 f8d1f275 2019-02-04 stsp char *path = NULL;
3501 6092c299 2021-10-04 thomas
3502 6092c299 2021-10-04 thomas if (ignore != NULL)
3503 6092c299 2021-10-04 thomas *ignore = 0;
3504 f8d1f275 2019-02-04 stsp
3505 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3507 0584f854 2019-04-06 stsp
3508 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3509 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3510 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3511 f8d1f275 2019-02-04 stsp } else {
3512 f8d1f275 2019-02-04 stsp path = de->d_name;
3513 f8d1f275 2019-02-04 stsp }
3514 f8d1f275 2019-02-04 stsp
3515 6092c299 2021-10-04 thomas if (de->d_type == DT_DIR) {
3516 6092c299 2021-10-04 thomas if (!a->no_ignores && ignore != NULL &&
3517 6092c299 2021-10-04 thomas match_ignores(a->ignores, path))
3518 6092c299 2021-10-04 thomas *ignore = 1;
3519 6092c299 2021-10-04 thomas } else if (!match_ignores(a->ignores, path) &&
3520 6092c299 2021-10-04 thomas got_path_is_child(path, a->status_path, a->status_path_len))
3521 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3522 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3523 f8d1f275 2019-02-04 stsp if (parent_path[0])
3524 f8d1f275 2019-02-04 stsp free(path);
3525 b72f483a 2019-02-05 stsp return err;
3526 f8d1f275 2019-02-04 stsp }
3527 f8d1f275 2019-02-04 stsp
3528 347d1d3e 2019-07-12 stsp static const struct got_error *
3529 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3530 3143d852 2020-06-25 stsp {
3531 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3532 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3533 3143d852 2020-06-25 stsp
3534 3143d852 2020-06-25 stsp if (a->no_ignores)
3535 3143d852 2020-06-25 stsp return NULL;
3536 3143d852 2020-06-25 stsp
3537 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3538 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3539 3143d852 2020-06-25 stsp if (err)
3540 3143d852 2020-06-25 stsp return err;
3541 3143d852 2020-06-25 stsp
3542 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3543 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3544 3143d852 2020-06-25 stsp
3545 3143d852 2020-06-25 stsp return err;
3546 3143d852 2020-06-25 stsp }
3547 3143d852 2020-06-25 stsp
3548 3143d852 2020-06-25 stsp static const struct got_error *
3549 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3550 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3551 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3552 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3553 abb4604f 2019-07-27 stsp {
3554 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3555 abb4604f 2019-07-27 stsp struct stat sb;
3556 abb4604f 2019-07-27 stsp
3557 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3558 ff56836b 2021-07-08 stsp return NULL;
3559 ff56836b 2021-07-08 stsp
3560 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3561 abb4604f 2019-07-27 stsp if (ie)
3562 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3563 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3564 abb4604f 2019-07-27 stsp
3565 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3566 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3567 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3568 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3569 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3570 abb4604f 2019-07-27 stsp return NULL;
3571 abb4604f 2019-07-27 stsp }
3572 abb4604f 2019-07-27 stsp
3573 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3574 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3575 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3576 abb4604f 2019-07-27 stsp
3577 abb4604f 2019-07-27 stsp return NULL;
3578 3143d852 2020-06-25 stsp }
3579 3143d852 2020-06-25 stsp
3580 3143d852 2020-06-25 stsp static const struct got_error *
3581 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3582 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3583 3143d852 2020-06-25 stsp {
3584 3143d852 2020-06-25 stsp const struct got_error *err;
3585 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3586 3143d852 2020-06-25 stsp
3587 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3588 3143d852 2020-06-25 stsp ".cvsignore");
3589 3143d852 2020-06-25 stsp if (err)
3590 3143d852 2020-06-25 stsp return err;
3591 3143d852 2020-06-25 stsp
3592 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3593 3143d852 2020-06-25 stsp ".gitignore");
3594 3143d852 2020-06-25 stsp if (err)
3595 3143d852 2020-06-25 stsp return err;
3596 3143d852 2020-06-25 stsp
3597 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3598 3143d852 2020-06-25 stsp if (err) {
3599 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3600 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3601 3143d852 2020-06-25 stsp return err;
3602 3143d852 2020-06-25 stsp }
3603 3143d852 2020-06-25 stsp for (;;) {
3604 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3605 3143d852 2020-06-25 stsp ".cvsignore");
3606 3143d852 2020-06-25 stsp if (err)
3607 3143d852 2020-06-25 stsp break;
3608 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3609 3143d852 2020-06-25 stsp ".gitignore");
3610 3143d852 2020-06-25 stsp if (err)
3611 3143d852 2020-06-25 stsp break;
3612 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3613 3143d852 2020-06-25 stsp if (err) {
3614 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3615 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3616 3143d852 2020-06-25 stsp break;
3617 3143d852 2020-06-25 stsp }
3618 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3619 ff56836b 2021-07-08 stsp break;
3620 b737c85a 2020-06-26 stsp free(parent_path);
3621 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3622 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3623 3143d852 2020-06-25 stsp }
3624 3143d852 2020-06-25 stsp
3625 b737c85a 2020-06-26 stsp free(parent_path);
3626 b737c85a 2020-06-26 stsp free(next_parent_path);
3627 3143d852 2020-06-25 stsp return err;
3628 abb4604f 2019-07-27 stsp }
3629 abb4604f 2019-07-27 stsp
3630 abb4604f 2019-07-27 stsp static const struct got_error *
3631 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3632 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3633 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3634 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3635 f2a9dc41 2019-12-13 tracey int report_unchanged)
3636 f8d1f275 2019-02-04 stsp {
3637 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3638 6fc93f37 2019-12-13 stsp int fd = -1;
3639 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3640 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3641 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3642 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3643 3143d852 2020-06-25 stsp
3644 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3645 f8d1f275 2019-02-04 stsp
3646 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3647 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3648 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3649 8dc303cc 2019-07-27 stsp
3650 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3651 6fc93f37 2019-12-13 stsp if (fd == -1) {
3652 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3653 3dc1dc04 2021-09-27 thomas !got_err_open_nofollow_on_symlink())
3654 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3655 ff56836b 2021-07-08 stsp else {
3656 ff56836b 2021-07-08 stsp if (!no_ignores) {
3657 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3658 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3659 ff56836b 2021-07-08 stsp if (err)
3660 ff56836b 2021-07-08 stsp goto done;
3661 ff56836b 2021-07-08 stsp }
3662 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3663 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3664 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3665 ff56836b 2021-07-08 stsp }
3666 abb4604f 2019-07-27 stsp } else {
3667 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3668 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3669 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3670 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3671 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3672 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3673 abb4604f 2019-07-27 stsp arg.status_path = path;
3674 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3675 abb4604f 2019-07-27 stsp arg.repo = repo;
3676 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3677 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3678 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3679 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3680 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3681 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3682 022fae89 2019-12-06 tracey if (!no_ignores) {
3683 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3684 3143d852 2020-06-25 stsp worktree->root_path, path);
3685 3143d852 2020-06-25 stsp if (err)
3686 3143d852 2020-06-25 stsp goto done;
3687 022fae89 2019-12-06 tracey }
3688 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3689 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3690 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3691 927df6b7 2019-02-10 stsp }
3692 3143d852 2020-06-25 stsp done:
3693 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3694 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3695 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3696 927df6b7 2019-02-10 stsp free(ondisk_path);
3697 347d1d3e 2019-07-12 stsp return err;
3698 347d1d3e 2019-07-12 stsp }
3699 347d1d3e 2019-07-12 stsp
3700 347d1d3e 2019-07-12 stsp const struct got_error *
3701 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3702 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3703 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3704 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3705 347d1d3e 2019-07-12 stsp {
3706 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3707 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3708 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3709 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3710 347d1d3e 2019-07-12 stsp
3711 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3712 347d1d3e 2019-07-12 stsp if (err)
3713 347d1d3e 2019-07-12 stsp return err;
3714 347d1d3e 2019-07-12 stsp
3715 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3716 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3717 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3718 f6343036 2021-06-22 stsp no_ignores, 0);
3719 72ea6654 2019-07-27 stsp if (err)
3720 72ea6654 2019-07-27 stsp break;
3721 72ea6654 2019-07-27 stsp }
3722 f8d1f275 2019-02-04 stsp free(fileindex_path);
3723 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3724 f8d1f275 2019-02-04 stsp return err;
3725 f8d1f275 2019-02-04 stsp }
3726 6c7ab921 2019-03-18 stsp
3727 6c7ab921 2019-03-18 stsp const struct got_error *
3728 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3729 6c7ab921 2019-03-18 stsp const char *arg)
3730 6c7ab921 2019-03-18 stsp {
3731 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3732 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3733 6c7ab921 2019-03-18 stsp size_t len;
3734 00bb5ea0 2020-07-23 stsp struct stat sb;
3735 01740607 2020-11-04 stsp char *abspath = NULL;
3736 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3737 6c7ab921 2019-03-18 stsp
3738 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3739 6c7ab921 2019-03-18 stsp
3740 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3741 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3742 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3743 00bb5ea0 2020-07-23 stsp
3744 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3745 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3746 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3747 00bb5ea0 2020-07-23 stsp goto done;
3748 00bb5ea0 2020-07-23 stsp }
3749 727173c3 2020-11-06 stsp sb.st_mode = 0;
3750 00bb5ea0 2020-07-23 stsp }
3751 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3752 00bb5ea0 2020-07-23 stsp /*
3753 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3754 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3755 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3756 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3757 00bb5ea0 2020-07-23 stsp */
3758 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3759 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3760 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3761 00bb5ea0 2020-07-23 stsp goto done;
3762 00bb5ea0 2020-07-23 stsp }
3763 00bb5ea0 2020-07-23 stsp
3764 00bb5ea0 2020-07-23 stsp }
3765 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3766 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3767 00bb5ea0 2020-07-23 stsp if (err)
3768 d0710d08 2019-07-22 stsp goto done;
3769 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3770 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3771 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3772 00bb5ea0 2020-07-23 stsp goto done;
3773 00bb5ea0 2020-07-23 stsp }
3774 00bb5ea0 2020-07-23 stsp } else {
3775 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3776 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3777 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3778 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3779 00bb5ea0 2020-07-23 stsp goto done;
3780 00bb5ea0 2020-07-23 stsp }
3781 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3782 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3783 01740607 2020-11-04 stsp goto done;
3784 01740607 2020-11-04 stsp }
3785 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3786 01740607 2020-11-04 stsp sizeof(canonpath));
3787 01740607 2020-11-04 stsp if (err)
3788 00bb5ea0 2020-07-23 stsp goto done;
3789 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3790 01740607 2020-11-04 stsp if (resolved == NULL) {
3791 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3792 01740607 2020-11-04 stsp goto done;
3793 00bb5ea0 2020-07-23 stsp }
3794 d0710d08 2019-07-22 stsp }
3795 d0710d08 2019-07-22 stsp }
3796 6c7ab921 2019-03-18 stsp
3797 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3798 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3799 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3800 6c7ab921 2019-03-18 stsp goto done;
3801 6c7ab921 2019-03-18 stsp }
3802 6c7ab921 2019-03-18 stsp
3803 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3804 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3805 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3806 f6d88e1a 2019-05-29 stsp if (err)
3807 f6d88e1a 2019-05-29 stsp goto done;
3808 f6d88e1a 2019-05-29 stsp } else {
3809 f6d88e1a 2019-05-29 stsp path = strdup("");
3810 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3811 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3812 f6d88e1a 2019-05-29 stsp goto done;
3813 f6d88e1a 2019-05-29 stsp }
3814 6c7ab921 2019-03-18 stsp }
3815 6c7ab921 2019-03-18 stsp
3816 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3817 6c7ab921 2019-03-18 stsp len = strlen(path);
3818 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3819 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3820 6c7ab921 2019-03-18 stsp len--;
3821 6c7ab921 2019-03-18 stsp }
3822 6c7ab921 2019-03-18 stsp done:
3823 01740607 2020-11-04 stsp free(abspath);
3824 6c7ab921 2019-03-18 stsp free(resolved);
3825 d0710d08 2019-07-22 stsp free(cwd);
3826 6c7ab921 2019-03-18 stsp if (err == NULL)
3827 6c7ab921 2019-03-18 stsp *wt_path = path;
3828 6c7ab921 2019-03-18 stsp else
3829 6c7ab921 2019-03-18 stsp free(path);
3830 d00136be 2019-03-26 stsp return err;
3831 d00136be 2019-03-26 stsp }
3832 d00136be 2019-03-26 stsp
3833 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3834 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3835 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3836 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3837 4e68cba3 2019-11-23 stsp void *progress_arg;
3838 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3839 4e68cba3 2019-11-23 stsp };
3840 4e68cba3 2019-11-23 stsp
3841 4e68cba3 2019-11-23 stsp static const struct got_error *
3842 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3843 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3844 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3845 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3846 07f5b47a 2019-06-02 stsp {
3847 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3848 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3849 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3850 6d022e97 2019-08-04 stsp struct stat sb;
3851 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3852 07f5b47a 2019-06-02 stsp
3853 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3854 dbb83fbd 2019-12-12 stsp relpath) == -1)
3855 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3856 dbb83fbd 2019-12-12 stsp
3857 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3858 6d022e97 2019-08-04 stsp if (ie) {
3859 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3860 12463d8b 2019-12-13 stsp de_name, a->repo);
3861 6d022e97 2019-08-04 stsp if (err)
3862 dbb83fbd 2019-12-12 stsp goto done;
3863 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3864 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3865 dbb83fbd 2019-12-12 stsp goto done;
3866 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3867 dbb83fbd 2019-12-12 stsp if (err)
3868 dbb83fbd 2019-12-12 stsp goto done;
3869 6d022e97 2019-08-04 stsp }
3870 07f5b47a 2019-06-02 stsp
3871 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3872 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3873 dbb83fbd 2019-12-12 stsp goto done;
3874 4e68cba3 2019-11-23 stsp }
3875 07f5b47a 2019-06-02 stsp
3876 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3877 4e68cba3 2019-11-23 stsp if (err)
3878 4e68cba3 2019-11-23 stsp goto done;
3879 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3880 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3881 3969253a 2020-03-07 stsp if (err) {
3882 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3883 3969253a 2020-03-07 stsp goto done;
3884 3969253a 2020-03-07 stsp }
3885 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3886 07f5b47a 2019-06-02 stsp if (err) {
3887 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3888 4e68cba3 2019-11-23 stsp goto done;
3889 07f5b47a 2019-06-02 stsp }
3890 4e68cba3 2019-11-23 stsp done:
3891 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3892 dbb83fbd 2019-12-12 stsp if (err)
3893 dbb83fbd 2019-12-12 stsp return err;
3894 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3895 dbb83fbd 2019-12-12 stsp return NULL;
3896 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3897 07f5b47a 2019-06-02 stsp }
3898 07f5b47a 2019-06-02 stsp
3899 d00136be 2019-03-26 stsp const struct got_error *
3900 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3901 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3902 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3903 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3904 d00136be 2019-03-26 stsp {
3905 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3906 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3907 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3908 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3909 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3910 d00136be 2019-03-26 stsp
3911 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3912 d00136be 2019-03-26 stsp if (err)
3913 d00136be 2019-03-26 stsp return err;
3914 d00136be 2019-03-26 stsp
3915 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3916 d00136be 2019-03-26 stsp if (err)
3917 d00136be 2019-03-26 stsp goto done;
3918 d00136be 2019-03-26 stsp
3919 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3920 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3921 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3922 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3923 4e68cba3 2019-11-23 stsp saa.repo = repo;
3924 4e68cba3 2019-11-23 stsp
3925 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3926 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3927 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3928 1dd54920 2019-05-11 stsp if (err)
3929 af12c6b9 2019-06-04 stsp break;
3930 1dd54920 2019-05-11 stsp }
3931 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3932 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3933 af12c6b9 2019-06-04 stsp err = sync_err;
3934 d00136be 2019-03-26 stsp done:
3935 fb399478 2019-07-12 stsp free(fileindex_path);
3936 d00136be 2019-03-26 stsp if (fileindex)
3937 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3938 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3939 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3940 d00136be 2019-03-26 stsp err = unlockerr;
3941 6c7ab921 2019-03-18 stsp return err;
3942 6c7ab921 2019-03-18 stsp }
3943 17ed4618 2019-06-02 stsp
3944 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3945 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3946 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3947 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3948 f2a9dc41 2019-12-13 tracey void *progress_arg;
3949 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3950 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3951 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3952 766841c2 2020-08-13 stsp const char *status_codes;
3953 f2a9dc41 2019-12-13 tracey };
3954 f2a9dc41 2019-12-13 tracey
3955 f2a9dc41 2019-12-13 tracey static const struct got_error *
3956 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3957 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3958 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3959 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3960 17ed4618 2019-06-02 stsp {
3961 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3962 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3963 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3964 17ed4618 2019-06-02 stsp struct stat sb;
3965 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3966 17ed4618 2019-06-02 stsp
3967 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3968 17ed4618 2019-06-02 stsp if (ie == NULL)
3969 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3970 2ec1f75b 2019-03-26 stsp
3971 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3972 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3973 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3974 9acbc4fa 2019-08-03 stsp return NULL;
3975 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3976 9acbc4fa 2019-08-03 stsp }
3977 9acbc4fa 2019-08-03 stsp
3978 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3979 f2a9dc41 2019-12-13 tracey relpath) == -1)
3980 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3981 f2a9dc41 2019-12-13 tracey
3982 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3983 12463d8b 2019-12-13 stsp a->repo);
3984 17ed4618 2019-06-02 stsp if (err)
3985 f2a9dc41 2019-12-13 tracey goto done;
3986 17ed4618 2019-06-02 stsp
3987 766841c2 2020-08-13 stsp if (a->status_codes) {
3988 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3989 766841c2 2020-08-13 stsp int i;
3990 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3991 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3992 766841c2 2020-08-13 stsp break;
3993 766841c2 2020-08-13 stsp }
3994 766841c2 2020-08-13 stsp if (i == ncodes) {
3995 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3996 766841c2 2020-08-13 stsp free(ondisk_path);
3997 766841c2 2020-08-13 stsp return NULL;
3998 766841c2 2020-08-13 stsp }
3999 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4000 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4001 766841c2 2020-08-13 stsp static char msg[64];
4002 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4003 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4004 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4005 766841c2 2020-08-13 stsp goto done;
4006 766841c2 2020-08-13 stsp }
4007 766841c2 2020-08-13 stsp }
4008 766841c2 2020-08-13 stsp
4009 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4010 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4011 f2a9dc41 2019-12-13 tracey goto done;
4012 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4013 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4014 f2a9dc41 2019-12-13 tracey goto done;
4015 f2a9dc41 2019-12-13 tracey }
4016 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4017 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4018 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4019 f2a9dc41 2019-12-13 tracey goto done;
4020 f2a9dc41 2019-12-13 tracey }
4021 17ed4618 2019-06-02 stsp }
4022 17ed4618 2019-06-02 stsp
4023 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4024 20a2ad1c 2020-10-20 stsp size_t root_len;
4025 20a2ad1c 2020-10-20 stsp
4026 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4027 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4028 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4029 12463d8b 2019-12-13 stsp ondisk_path);
4030 12463d8b 2019-12-13 stsp goto done;
4031 12463d8b 2019-12-13 stsp }
4032 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4033 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4034 12463d8b 2019-12-13 stsp goto done;
4035 15341bfd 2020-03-05 tracey }
4036 15341bfd 2020-03-05 tracey
4037 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4038 20a2ad1c 2020-10-20 stsp do {
4039 20a2ad1c 2020-10-20 stsp char *parent;
4040 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4041 20a2ad1c 2020-10-20 stsp if (err)
4042 2513f20a 2020-10-20 stsp goto done;
4043 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4044 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4045 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4046 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4047 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4048 20a2ad1c 2020-10-20 stsp ondisk_path);
4049 15341bfd 2020-03-05 tracey break;
4050 15341bfd 2020-03-05 tracey }
4051 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4052 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4053 f2a9dc41 2019-12-13 tracey }
4054 17ed4618 2019-06-02 stsp
4055 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4056 f2a9dc41 2019-12-13 tracey done:
4057 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4058 f2a9dc41 2019-12-13 tracey if (err)
4059 f2a9dc41 2019-12-13 tracey return err;
4060 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4061 f2a9dc41 2019-12-13 tracey return NULL;
4062 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4063 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4064 17ed4618 2019-06-02 stsp }
4065 17ed4618 2019-06-02 stsp
4066 2ec1f75b 2019-03-26 stsp const struct got_error *
4067 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4068 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4069 766841c2 2020-08-13 stsp const char *status_codes,
4070 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4071 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4072 2ec1f75b 2019-03-26 stsp {
4073 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4074 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4075 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4076 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4077 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4078 2ec1f75b 2019-03-26 stsp
4079 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4080 2ec1f75b 2019-03-26 stsp if (err)
4081 2ec1f75b 2019-03-26 stsp return err;
4082 2ec1f75b 2019-03-26 stsp
4083 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4084 2ec1f75b 2019-03-26 stsp if (err)
4085 2ec1f75b 2019-03-26 stsp goto done;
4086 f2a9dc41 2019-12-13 tracey
4087 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4088 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4089 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4090 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4091 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4092 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4093 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4094 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4095 2ec1f75b 2019-03-26 stsp
4096 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4097 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4098 6092c299 2021-10-04 thomas schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4099 17ed4618 2019-06-02 stsp if (err)
4100 af12c6b9 2019-06-04 stsp break;
4101 2ec1f75b 2019-03-26 stsp }
4102 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4103 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4104 af12c6b9 2019-06-04 stsp err = sync_err;
4105 2ec1f75b 2019-03-26 stsp done:
4106 fb399478 2019-07-12 stsp free(fileindex_path);
4107 2ec1f75b 2019-03-26 stsp if (fileindex)
4108 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4109 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4110 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4111 2ec1f75b 2019-03-26 stsp err = unlockerr;
4112 33aa809d 2019-08-08 stsp return err;
4113 33aa809d 2019-08-08 stsp }
4114 33aa809d 2019-08-08 stsp
4115 33aa809d 2019-08-08 stsp static const struct got_error *
4116 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4117 33aa809d 2019-08-08 stsp {
4118 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4119 33aa809d 2019-08-08 stsp char *line = NULL;
4120 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4121 33aa809d 2019-08-08 stsp ssize_t linelen;
4122 33aa809d 2019-08-08 stsp
4123 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4124 33aa809d 2019-08-08 stsp if (linelen == -1) {
4125 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4126 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4127 33aa809d 2019-08-08 stsp goto done;
4128 33aa809d 2019-08-08 stsp }
4129 33aa809d 2019-08-08 stsp return NULL;
4130 33aa809d 2019-08-08 stsp }
4131 33aa809d 2019-08-08 stsp if (outfile) {
4132 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4133 33aa809d 2019-08-08 stsp if (n != linelen) {
4134 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4135 33aa809d 2019-08-08 stsp goto done;
4136 33aa809d 2019-08-08 stsp }
4137 33aa809d 2019-08-08 stsp }
4138 33aa809d 2019-08-08 stsp if (rejectfile) {
4139 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4140 33aa809d 2019-08-08 stsp if (n != linelen)
4141 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4142 33aa809d 2019-08-08 stsp }
4143 33aa809d 2019-08-08 stsp done:
4144 33aa809d 2019-08-08 stsp free(line);
4145 2ec1f75b 2019-03-26 stsp return err;
4146 2ec1f75b 2019-03-26 stsp }
4147 1f1abb7e 2019-08-08 stsp
4148 33aa809d 2019-08-08 stsp static const struct got_error *
4149 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4150 33aa809d 2019-08-08 stsp {
4151 33aa809d 2019-08-08 stsp char *line = NULL;
4152 33aa809d 2019-08-08 stsp size_t linesize = 0;
4153 33aa809d 2019-08-08 stsp ssize_t linelen;
4154 33aa809d 2019-08-08 stsp
4155 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4156 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4157 500467ff 2019-09-25 hiltjo if (ferror(f))
4158 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4159 500467ff 2019-09-25 hiltjo return NULL;
4160 500467ff 2019-09-25 hiltjo }
4161 33aa809d 2019-08-08 stsp free(line);
4162 33aa809d 2019-08-08 stsp return NULL;
4163 33aa809d 2019-08-08 stsp }
4164 33aa809d 2019-08-08 stsp
4165 33aa809d 2019-08-08 stsp static const struct got_error *
4166 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4167 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4168 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4169 abc59930 2021-09-05 naddy {
4170 33aa809d 2019-08-08 stsp const struct got_error *err;
4171 33aa809d 2019-08-08 stsp
4172 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4173 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4174 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4175 33aa809d 2019-08-08 stsp if (err)
4176 33aa809d 2019-08-08 stsp return err;
4177 33aa809d 2019-08-08 stsp (*line_cur1)++;
4178 33aa809d 2019-08-08 stsp }
4179 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4180 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4181 33aa809d 2019-08-08 stsp if (rejectfile)
4182 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4183 33aa809d 2019-08-08 stsp else
4184 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4185 33aa809d 2019-08-08 stsp if (err)
4186 33aa809d 2019-08-08 stsp return err;
4187 33aa809d 2019-08-08 stsp (*line_cur2)++;
4188 33aa809d 2019-08-08 stsp }
4189 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4190 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4191 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4192 33aa809d 2019-08-08 stsp if (err)
4193 33aa809d 2019-08-08 stsp return err;
4194 33aa809d 2019-08-08 stsp (*line_cur2)++;
4195 33aa809d 2019-08-08 stsp }
4196 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4197 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4198 33aa809d 2019-08-08 stsp if (rejectfile)
4199 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4200 33aa809d 2019-08-08 stsp else
4201 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4202 33aa809d 2019-08-08 stsp if (err)
4203 33aa809d 2019-08-08 stsp return err;
4204 33aa809d 2019-08-08 stsp (*line_cur1)++;
4205 33aa809d 2019-08-08 stsp }
4206 f1e81a05 2019-08-10 stsp
4207 f1e81a05 2019-08-10 stsp return NULL;
4208 f1e81a05 2019-08-10 stsp }
4209 f1e81a05 2019-08-10 stsp
4210 f1e81a05 2019-08-10 stsp static const struct got_error *
4211 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4212 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4213 f1e81a05 2019-08-10 stsp {
4214 f1e81a05 2019-08-10 stsp const struct got_error *err;
4215 f1e81a05 2019-08-10 stsp
4216 f1e81a05 2019-08-10 stsp if (outfile) {
4217 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4218 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4219 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4220 f1e81a05 2019-08-10 stsp if (err)
4221 f1e81a05 2019-08-10 stsp return err;
4222 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4223 f1e81a05 2019-08-10 stsp }
4224 f1e81a05 2019-08-10 stsp }
4225 f1e81a05 2019-08-10 stsp if (rejectfile) {
4226 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4227 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4228 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4229 f1e81a05 2019-08-10 stsp if (err)
4230 f1e81a05 2019-08-10 stsp return err;
4231 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4232 f1e81a05 2019-08-10 stsp }
4233 33aa809d 2019-08-08 stsp }
4234 33aa809d 2019-08-08 stsp
4235 33aa809d 2019-08-08 stsp return NULL;
4236 33aa809d 2019-08-08 stsp }
4237 33aa809d 2019-08-08 stsp
4238 33aa809d 2019-08-08 stsp static const struct got_error *
4239 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4240 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4241 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4242 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4243 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4244 33aa809d 2019-08-08 stsp {
4245 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4246 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4247 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4248 33aa809d 2019-08-08 stsp FILE *hunkfile;
4249 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4250 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4251 fe621944 2020-11-10 stsp int rc;
4252 33aa809d 2019-08-08 stsp
4253 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4254 33aa809d 2019-08-08 stsp
4255 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4256 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4257 fe621944 2020-11-10 stsp start_old = cc.left.start;
4258 fe621944 2020-11-10 stsp end_old = cc.left.end;
4259 fe621944 2020-11-10 stsp start_new = cc.right.start;
4260 fe621944 2020-11-10 stsp end_new = cc.right.end;
4261 33aa809d 2019-08-08 stsp
4262 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4263 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4264 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4265 33aa809d 2019-08-08 stsp
4266 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4267 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4268 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4269 33aa809d 2019-08-08 stsp
4270 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4271 fe621944 2020-11-10 stsp if (diff_state == NULL)
4272 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4273 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4274 fe621944 2020-11-10 stsp
4275 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4276 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4277 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4278 33aa809d 2019-08-08 stsp goto done;
4279 33aa809d 2019-08-08 stsp }
4280 fe621944 2020-11-10 stsp
4281 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4282 fe621944 2020-11-10 stsp diff_result, &cc);
4283 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4284 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4285 33aa809d 2019-08-08 stsp goto done;
4286 33aa809d 2019-08-08 stsp }
4287 fe621944 2020-11-10 stsp
4288 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4289 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4290 33aa809d 2019-08-08 stsp goto done;
4291 33aa809d 2019-08-08 stsp }
4292 33aa809d 2019-08-08 stsp
4293 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4294 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4295 33aa809d 2019-08-08 stsp if (err)
4296 33aa809d 2019-08-08 stsp goto done;
4297 33aa809d 2019-08-08 stsp
4298 33aa809d 2019-08-08 stsp switch (*choice) {
4299 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4300 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4301 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4302 33aa809d 2019-08-08 stsp break;
4303 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4304 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4305 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4306 33aa809d 2019-08-08 stsp break;
4307 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4308 33aa809d 2019-08-08 stsp break;
4309 33aa809d 2019-08-08 stsp default:
4310 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4311 33aa809d 2019-08-08 stsp break;
4312 33aa809d 2019-08-08 stsp }
4313 33aa809d 2019-08-08 stsp done:
4314 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4315 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4316 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4317 33aa809d 2019-08-08 stsp return err;
4318 33aa809d 2019-08-08 stsp }
4319 33aa809d 2019-08-08 stsp
4320 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4321 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4322 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4323 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4324 1f1abb7e 2019-08-08 stsp void *progress_arg;
4325 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4326 33aa809d 2019-08-08 stsp void *patch_arg;
4327 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4328 10604dce 2021-09-24 thomas int unlink_added_files;
4329 1f1abb7e 2019-08-08 stsp };
4330 a129376b 2019-03-28 stsp
4331 e20a8b6f 2019-06-04 stsp static const struct got_error *
4332 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4333 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4334 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4335 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4336 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4337 33aa809d 2019-08-08 stsp {
4338 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4339 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4340 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4341 1ebedb77 2019-10-19 stsp int fd2 = -1;
4342 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4343 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4344 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4345 fe621944 2020-11-10 stsp struct stat sb2;
4346 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4347 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4348 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4349 33aa809d 2019-08-08 stsp
4350 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4351 33aa809d 2019-08-08 stsp
4352 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4353 33aa809d 2019-08-08 stsp if (err)
4354 33aa809d 2019-08-08 stsp return err;
4355 33aa809d 2019-08-08 stsp
4356 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4357 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4358 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4359 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4360 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4361 fa3cef63 2020-07-23 stsp goto done;
4362 fa3cef63 2020-07-23 stsp }
4363 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4364 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4365 fa3cef63 2020-07-23 stsp if (link_len == -1)
4366 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4367 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4368 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4369 12463d8b 2019-12-13 stsp }
4370 12463d8b 2019-12-13 stsp } else {
4371 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4372 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4373 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink()) {
4374 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4375 fa3cef63 2020-07-23 stsp goto done;
4376 fa3cef63 2020-07-23 stsp }
4377 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4378 fa3cef63 2020-07-23 stsp sizeof(link_target));
4379 fa3cef63 2020-07-23 stsp if (link_len == -1)
4380 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4381 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4382 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4383 12463d8b 2019-12-13 stsp }
4384 1ebedb77 2019-10-19 stsp }
4385 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4386 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4387 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4388 fa3cef63 2020-07-23 stsp goto done;
4389 fa3cef63 2020-07-23 stsp }
4390 1ebedb77 2019-10-19 stsp
4391 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4392 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4393 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4394 fa3cef63 2020-07-23 stsp goto done;
4395 fa3cef63 2020-07-23 stsp }
4396 fa3cef63 2020-07-23 stsp fd2 = -1;
4397 fa3cef63 2020-07-23 stsp } else {
4398 fa3cef63 2020-07-23 stsp size_t n;
4399 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4400 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4401 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4402 fa3cef63 2020-07-23 stsp goto done;
4403 fa3cef63 2020-07-23 stsp }
4404 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4405 fa3cef63 2020-07-23 stsp if (n != link_len) {
4406 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4407 fa3cef63 2020-07-23 stsp goto done;
4408 fa3cef63 2020-07-23 stsp }
4409 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4410 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4411 fa3cef63 2020-07-23 stsp goto done;
4412 fa3cef63 2020-07-23 stsp }
4413 fa3cef63 2020-07-23 stsp rewind(f2);
4414 33aa809d 2019-08-08 stsp }
4415 33aa809d 2019-08-08 stsp
4416 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4417 33aa809d 2019-08-08 stsp if (err)
4418 33aa809d 2019-08-08 stsp goto done;
4419 33aa809d 2019-08-08 stsp
4420 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4421 33aa809d 2019-08-08 stsp if (err)
4422 33aa809d 2019-08-08 stsp goto done;
4423 33aa809d 2019-08-08 stsp
4424 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4425 33aa809d 2019-08-08 stsp if (err)
4426 33aa809d 2019-08-08 stsp goto done;
4427 33aa809d 2019-08-08 stsp
4428 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4429 fe621944 2020-11-10 stsp NULL);
4430 33aa809d 2019-08-08 stsp if (err)
4431 33aa809d 2019-08-08 stsp goto done;
4432 33aa809d 2019-08-08 stsp
4433 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4434 33aa809d 2019-08-08 stsp if (err)
4435 33aa809d 2019-08-08 stsp goto done;
4436 33aa809d 2019-08-08 stsp
4437 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4438 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4439 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4440 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4441 fe621944 2020-11-10 stsp
4442 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4443 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4444 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4445 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4446 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4447 fe621944 2020-11-10 stsp nchanges++;
4448 fe621944 2020-11-10 stsp }
4449 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4450 33aa809d 2019-08-08 stsp int choice;
4451 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4452 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4453 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4454 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4455 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4456 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4457 33aa809d 2019-08-08 stsp if (err)
4458 33aa809d 2019-08-08 stsp goto done;
4459 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4460 33aa809d 2019-08-08 stsp have_content = 1;
4461 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4462 33aa809d 2019-08-08 stsp break;
4463 33aa809d 2019-08-08 stsp }
4464 1ebedb77 2019-10-19 stsp if (have_content) {
4465 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4466 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4467 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4468 1ebedb77 2019-10-19 stsp if (err)
4469 1ebedb77 2019-10-19 stsp goto done;
4470 1ebedb77 2019-10-19 stsp
4471 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4472 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4473 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4474 fa3cef63 2020-07-23 stsp goto done;
4475 fa3cef63 2020-07-23 stsp }
4476 1ebedb77 2019-10-19 stsp }
4477 1ebedb77 2019-10-19 stsp }
4478 33aa809d 2019-08-08 stsp done:
4479 33aa809d 2019-08-08 stsp free(id_str);
4480 33aa809d 2019-08-08 stsp if (blob)
4481 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4482 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4483 fe621944 2020-11-10 stsp if (err == NULL)
4484 fe621944 2020-11-10 stsp err = free_err;
4485 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4486 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4487 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4488 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4489 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4490 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4491 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4492 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4493 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4494 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4495 33aa809d 2019-08-08 stsp if (err || !have_content) {
4496 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4497 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4498 33aa809d 2019-08-08 stsp free(*path_outfile);
4499 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4500 33aa809d 2019-08-08 stsp }
4501 33aa809d 2019-08-08 stsp free(path1);
4502 33aa809d 2019-08-08 stsp return err;
4503 33aa809d 2019-08-08 stsp }
4504 33aa809d 2019-08-08 stsp
4505 33aa809d 2019-08-08 stsp static const struct got_error *
4506 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4507 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4508 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4509 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4510 a129376b 2019-03-28 stsp {
4511 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4512 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4513 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4514 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4515 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4516 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4517 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4518 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4519 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4520 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4521 a129376b 2019-03-28 stsp
4522 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4523 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4524 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4525 d3bcc3d1 2019-08-08 stsp return NULL;
4526 3d69ad8d 2019-08-17 semarie
4527 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4528 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4529 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4530 d3bcc3d1 2019-08-08 stsp
4531 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4532 65084dad 2019-08-08 stsp if (ie == NULL)
4533 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4534 a129376b 2019-03-28 stsp
4535 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4536 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4537 a129376b 2019-03-28 stsp if (err) {
4538 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4539 a129376b 2019-03-28 stsp goto done;
4540 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4541 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4542 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4543 e20a8b6f 2019-06-04 stsp goto done;
4544 e20a8b6f 2019-06-04 stsp }
4545 a129376b 2019-03-28 stsp }
4546 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4547 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4548 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4549 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4550 a129376b 2019-03-28 stsp goto done;
4551 a129376b 2019-03-28 stsp }
4552 a129376b 2019-03-28 stsp } else {
4553 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4554 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4555 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4556 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4557 a129376b 2019-03-28 stsp goto done;
4558 a129376b 2019-03-28 stsp }
4559 a129376b 2019-03-28 stsp } else {
4560 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4561 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4562 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4563 a129376b 2019-03-28 stsp goto done;
4564 a129376b 2019-03-28 stsp }
4565 a129376b 2019-03-28 stsp }
4566 a129376b 2019-03-28 stsp }
4567 a129376b 2019-03-28 stsp
4568 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4569 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4570 a9fa2909 2019-07-27 stsp if (err) {
4571 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4572 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4573 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4574 a9fa2909 2019-07-27 stsp goto done;
4575 a9fa2909 2019-07-27 stsp } else {
4576 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4577 a9fa2909 2019-07-27 stsp if (err)
4578 a9fa2909 2019-07-27 stsp goto done;
4579 a9fa2909 2019-07-27 stsp
4580 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4581 1233e6b6 2020-10-19 stsp if (err)
4582 a9fa2909 2019-07-27 stsp goto done;
4583 a9fa2909 2019-07-27 stsp
4584 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4585 1233e6b6 2020-10-19 stsp free(te_name);
4586 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4587 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4588 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4589 a9fa2909 2019-07-27 stsp goto done;
4590 a9fa2909 2019-07-27 stsp }
4591 a129376b 2019-03-28 stsp }
4592 a129376b 2019-03-28 stsp
4593 a129376b 2019-03-28 stsp switch (status) {
4594 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4595 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4596 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4597 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4598 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4599 33aa809d 2019-08-08 stsp if (err)
4600 33aa809d 2019-08-08 stsp goto done;
4601 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4602 33aa809d 2019-08-08 stsp break;
4603 33aa809d 2019-08-08 stsp }
4604 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4605 1f1abb7e 2019-08-08 stsp ie->path);
4606 1ee397ad 2019-07-12 stsp if (err)
4607 1ee397ad 2019-07-12 stsp goto done;
4608 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4609 10604dce 2021-09-24 thomas if (a->unlink_added_files) {
4610 10604dce 2021-09-24 thomas if (asprintf(&ondisk_path, "%s/%s",
4611 10604dce 2021-09-24 thomas got_worktree_get_root_path(a->worktree),
4612 10604dce 2021-09-24 thomas relpath) == -1) {
4613 10604dce 2021-09-24 thomas err = got_error_from_errno("asprintf");
4614 10604dce 2021-09-24 thomas goto done;
4615 10604dce 2021-09-24 thomas }
4616 10604dce 2021-09-24 thomas if (unlink(ondisk_path) == -1) {
4617 10604dce 2021-09-24 thomas err = got_error_from_errno2("unlink",
4618 10604dce 2021-09-24 thomas ondisk_path);
4619 10604dce 2021-09-24 thomas break;
4620 10604dce 2021-09-24 thomas }
4621 10604dce 2021-09-24 thomas }
4622 a129376b 2019-03-28 stsp break;
4623 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4624 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4625 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4626 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4627 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4628 33aa809d 2019-08-08 stsp if (err)
4629 33aa809d 2019-08-08 stsp goto done;
4630 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4631 33aa809d 2019-08-08 stsp break;
4632 33aa809d 2019-08-08 stsp }
4633 33aa809d 2019-08-08 stsp /* fall through */
4634 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4635 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4636 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4637 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4638 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4639 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4640 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4641 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4642 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4643 24278f30 2019-08-03 stsp } else
4644 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4645 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4646 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4647 a129376b 2019-03-28 stsp if (err)
4648 65084dad 2019-08-08 stsp goto done;
4649 65084dad 2019-08-08 stsp
4650 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4651 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4652 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4653 a129376b 2019-03-28 stsp goto done;
4654 65084dad 2019-08-08 stsp }
4655 65084dad 2019-08-08 stsp
4656 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4657 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4658 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4659 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4660 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4661 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4662 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4663 33aa809d 2019-08-08 stsp break;
4664 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4665 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4666 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4667 369fd7e5 2020-07-23 stsp path_content);
4668 369fd7e5 2020-07-23 stsp break;
4669 369fd7e5 2020-07-23 stsp }
4670 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4671 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4672 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4673 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4674 369fd7e5 2020-07-23 stsp } else {
4675 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4676 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4677 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4678 369fd7e5 2020-07-23 stsp goto done;
4679 369fd7e5 2020-07-23 stsp }
4680 33aa809d 2019-08-08 stsp }
4681 33aa809d 2019-08-08 stsp } else {
4682 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4683 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4684 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4685 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4686 ace90326 2021-09-27 thomas blob, 0, 1, 0, 0, a->repo,
4687 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4688 2e1fa222 2020-07-23 stsp } else {
4689 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4690 2e1fa222 2020-07-23 stsp ie->path,
4691 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4692 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4693 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4694 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4695 2e1fa222 2020-07-23 stsp }
4696 a129376b 2019-03-28 stsp if (err)
4697 a129376b 2019-03-28 stsp goto done;
4698 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4699 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4700 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4701 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4702 437adc9d 2020-12-10 yzhong blob->id.sha1,
4703 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4704 2e1fa222 2020-07-23 stsp if (err)
4705 2e1fa222 2020-07-23 stsp goto done;
4706 2e1fa222 2020-07-23 stsp }
4707 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4708 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4709 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4710 33aa809d 2019-08-08 stsp }
4711 a129376b 2019-03-28 stsp }
4712 a129376b 2019-03-28 stsp break;
4713 e20a8b6f 2019-06-04 stsp }
4714 a129376b 2019-03-28 stsp default:
4715 1f1abb7e 2019-08-08 stsp break;
4716 a129376b 2019-03-28 stsp }
4717 a129376b 2019-03-28 stsp done:
4718 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4719 33aa809d 2019-08-08 stsp free(path_content);
4720 e20a8b6f 2019-06-04 stsp free(parent_path);
4721 a129376b 2019-03-28 stsp free(tree_path);
4722 a129376b 2019-03-28 stsp if (blob)
4723 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4724 a129376b 2019-03-28 stsp if (tree)
4725 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4726 a129376b 2019-03-28 stsp free(tree_id);
4727 e20a8b6f 2019-06-04 stsp return err;
4728 e20a8b6f 2019-06-04 stsp }
4729 e20a8b6f 2019-06-04 stsp
4730 e20a8b6f 2019-06-04 stsp const struct got_error *
4731 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4732 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4733 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4734 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4735 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4736 e20a8b6f 2019-06-04 stsp {
4737 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4738 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4739 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4740 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4741 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4742 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4743 e20a8b6f 2019-06-04 stsp
4744 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4745 e20a8b6f 2019-06-04 stsp if (err)
4746 e20a8b6f 2019-06-04 stsp return err;
4747 e20a8b6f 2019-06-04 stsp
4748 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4749 e20a8b6f 2019-06-04 stsp if (err)
4750 e20a8b6f 2019-06-04 stsp goto done;
4751 e20a8b6f 2019-06-04 stsp
4752 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4753 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4754 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4755 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4756 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4757 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4758 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4759 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
4760 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4761 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4762 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
4763 e20a8b6f 2019-06-04 stsp if (err)
4764 af12c6b9 2019-06-04 stsp break;
4765 e20a8b6f 2019-06-04 stsp }
4766 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4767 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4768 e20a8b6f 2019-06-04 stsp err = sync_err;
4769 af12c6b9 2019-06-04 stsp done:
4770 fb399478 2019-07-12 stsp free(fileindex_path);
4771 a129376b 2019-03-28 stsp if (fileindex)
4772 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4773 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4774 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4775 a129376b 2019-03-28 stsp err = unlockerr;
4776 c4296144 2019-05-09 stsp return err;
4777 c4296144 2019-05-09 stsp }
4778 c4296144 2019-05-09 stsp
4779 cf066bf8 2019-05-09 stsp static void
4780 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4781 cf066bf8 2019-05-09 stsp {
4782 24519714 2019-05-09 stsp free(ct->path);
4783 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4784 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4785 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4786 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4787 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4788 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4789 cf066bf8 2019-05-09 stsp free(ct);
4790 cf066bf8 2019-05-09 stsp }
4791 24519714 2019-05-09 stsp
4792 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4793 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4794 24519714 2019-05-09 stsp struct got_repository *repo;
4795 24519714 2019-05-09 stsp struct got_worktree *worktree;
4796 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4797 5f8a88c6 2019-08-03 stsp int have_staged_files;
4798 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4799 24519714 2019-05-09 stsp };
4800 cf066bf8 2019-05-09 stsp
4801 c4296144 2019-05-09 stsp static const struct got_error *
4802 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4803 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4804 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4805 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4806 c4296144 2019-05-09 stsp {
4807 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4808 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4809 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4810 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4811 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4812 768aea60 2019-05-09 stsp struct stat sb;
4813 c4296144 2019-05-09 stsp
4814 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4815 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4816 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4817 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4818 5f8a88c6 2019-08-03 stsp return NULL;
4819 5f8a88c6 2019-08-03 stsp } else {
4820 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4821 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4822 c4296144 2019-05-09 stsp
4823 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4824 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4825 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4826 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4827 5f8a88c6 2019-08-03 stsp return NULL;
4828 5f8a88c6 2019-08-03 stsp }
4829 0b5cc0d6 2019-05-09 stsp
4830 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4831 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4832 036813ee 2019-05-09 stsp goto done;
4833 036813ee 2019-05-09 stsp }
4834 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4835 036813ee 2019-05-09 stsp parent_path = strdup("");
4836 69960a46 2019-05-09 stsp if (parent_path == NULL)
4837 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4838 69960a46 2019-05-09 stsp } else {
4839 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4840 69960a46 2019-05-09 stsp if (err)
4841 69960a46 2019-05-09 stsp return err;
4842 69960a46 2019-05-09 stsp }
4843 c4296144 2019-05-09 stsp
4844 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4845 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4846 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4847 c4296144 2019-05-09 stsp goto done;
4848 768aea60 2019-05-09 stsp }
4849 768aea60 2019-05-09 stsp
4850 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4851 768aea60 2019-05-09 stsp relpath) == -1) {
4852 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4853 768aea60 2019-05-09 stsp goto done;
4854 768aea60 2019-05-09 stsp }
4855 0aeb8099 2020-07-23 stsp
4856 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4857 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4858 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4859 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4860 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4861 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4862 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4863 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4864 0aeb8099 2020-07-23 stsp break;
4865 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4866 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4867 0aeb8099 2020-07-23 stsp break;
4868 0aeb8099 2020-07-23 stsp default:
4869 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4870 0aeb8099 2020-07-23 stsp goto done;
4871 0aeb8099 2020-07-23 stsp }
4872 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4873 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4874 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4875 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4876 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4877 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4878 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4879 12463d8b 2019-12-13 stsp ct->ondisk_path);
4880 12463d8b 2019-12-13 stsp goto done;
4881 12463d8b 2019-12-13 stsp }
4882 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4883 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4884 768aea60 2019-05-09 stsp goto done;
4885 768aea60 2019-05-09 stsp }
4886 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4887 c4296144 2019-05-09 stsp }
4888 c4296144 2019-05-09 stsp
4889 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4890 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4891 44d03001 2019-05-09 stsp relpath) == -1) {
4892 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4893 44d03001 2019-05-09 stsp goto done;
4894 44d03001 2019-05-09 stsp }
4895 44d03001 2019-05-09 stsp
4896 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4897 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4898 35213c7c 2020-07-23 stsp int is_bad_symlink;
4899 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4900 35213c7c 2020-07-23 stsp ssize_t target_len;
4901 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4902 35213c7c 2020-07-23 stsp sizeof(target_path));
4903 35213c7c 2020-07-23 stsp if (target_len == -1) {
4904 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4905 35213c7c 2020-07-23 stsp ct->ondisk_path);
4906 35213c7c 2020-07-23 stsp goto done;
4907 35213c7c 2020-07-23 stsp }
4908 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4909 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4910 35213c7c 2020-07-23 stsp if (err)
4911 35213c7c 2020-07-23 stsp goto done;
4912 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4913 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4914 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4915 35213c7c 2020-07-23 stsp goto done;
4916 35213c7c 2020-07-23 stsp }
4917 35213c7c 2020-07-23 stsp }
4918 35213c7c 2020-07-23 stsp
4919 35213c7c 2020-07-23 stsp
4920 cf066bf8 2019-05-09 stsp ct->status = status;
4921 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4922 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4923 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4924 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4925 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4926 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4927 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4928 b416585c 2019-05-13 stsp goto done;
4929 b416585c 2019-05-13 stsp }
4930 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4931 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4932 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4933 f0b75401 2019-08-03 stsp goto done;
4934 f0b75401 2019-08-03 stsp }
4935 f0b75401 2019-08-03 stsp }
4936 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4937 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4938 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4939 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4940 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4941 036813ee 2019-05-09 stsp goto done;
4942 036813ee 2019-05-09 stsp }
4943 ca2503ea 2019-05-09 stsp }
4944 24519714 2019-05-09 stsp ct->path = strdup(path);
4945 24519714 2019-05-09 stsp if (ct->path == NULL) {
4946 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4947 24519714 2019-05-09 stsp goto done;
4948 24519714 2019-05-09 stsp }
4949 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4950 c4296144 2019-05-09 stsp done:
4951 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4952 ed175427 2019-05-09 stsp free_commitable(ct);
4953 24519714 2019-05-09 stsp free(parent_path);
4954 036813ee 2019-05-09 stsp free(path);
4955 a129376b 2019-03-28 stsp return err;
4956 a129376b 2019-03-28 stsp }
4957 c4296144 2019-05-09 stsp
4958 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4959 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4960 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4961 036813ee 2019-05-09 stsp struct got_repository *);
4962 ed175427 2019-05-09 stsp
4963 ed175427 2019-05-09 stsp static const struct got_error *
4964 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4965 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4966 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4967 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4968 afa376bf 2019-05-09 stsp struct got_repository *repo)
4969 ed175427 2019-05-09 stsp {
4970 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4971 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4972 036813ee 2019-05-09 stsp char *subpath;
4973 ed175427 2019-05-09 stsp
4974 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4975 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4976 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4977 ed175427 2019-05-09 stsp
4978 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4979 ed175427 2019-05-09 stsp if (err)
4980 ed175427 2019-05-09 stsp return err;
4981 ed175427 2019-05-09 stsp
4982 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4983 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4984 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4985 036813ee 2019-05-09 stsp free(subpath);
4986 036813ee 2019-05-09 stsp return err;
4987 036813ee 2019-05-09 stsp }
4988 ed175427 2019-05-09 stsp
4989 036813ee 2019-05-09 stsp static const struct got_error *
4990 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4991 036813ee 2019-05-09 stsp {
4992 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4993 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4994 ed175427 2019-05-09 stsp
4995 036813ee 2019-05-09 stsp *match = 0;
4996 ed175427 2019-05-09 stsp
4997 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4998 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4999 0f63689d 2019-05-10 stsp return NULL;
5000 036813ee 2019-05-09 stsp }
5001 0b5cc0d6 2019-05-09 stsp
5002 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5003 0f63689d 2019-05-10 stsp if (err)
5004 0f63689d 2019-05-10 stsp return err;
5005 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5006 036813ee 2019-05-09 stsp free(ct_parent_path);
5007 036813ee 2019-05-09 stsp return err;
5008 036813ee 2019-05-09 stsp }
5009 0b5cc0d6 2019-05-09 stsp
5010 768aea60 2019-05-09 stsp static mode_t
5011 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5012 768aea60 2019-05-09 stsp {
5013 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5014 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5015 3d9a4ec4 2020-07-23 stsp
5016 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5017 768aea60 2019-05-09 stsp }
5018 768aea60 2019-05-09 stsp
5019 036813ee 2019-05-09 stsp static const struct got_error *
5020 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5021 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5022 036813ee 2019-05-09 stsp {
5023 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5024 ca2503ea 2019-05-09 stsp
5025 036813ee 2019-05-09 stsp *new_te = NULL;
5026 0b5cc0d6 2019-05-09 stsp
5027 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5028 036813ee 2019-05-09 stsp if (err)
5029 036813ee 2019-05-09 stsp goto done;
5030 0b5cc0d6 2019-05-09 stsp
5031 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5032 036813ee 2019-05-09 stsp
5033 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5034 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5035 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5036 5f8a88c6 2019-08-03 stsp else
5037 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5038 036813ee 2019-05-09 stsp done:
5039 036813ee 2019-05-09 stsp if (err && *new_te) {
5040 56e0773d 2019-11-28 stsp free(*new_te);
5041 036813ee 2019-05-09 stsp *new_te = NULL;
5042 036813ee 2019-05-09 stsp }
5043 036813ee 2019-05-09 stsp return err;
5044 036813ee 2019-05-09 stsp }
5045 036813ee 2019-05-09 stsp
5046 036813ee 2019-05-09 stsp static const struct got_error *
5047 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5048 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5049 036813ee 2019-05-09 stsp {
5050 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5051 102b254e 2020-10-19 stsp char *ct_name = NULL;
5052 036813ee 2019-05-09 stsp
5053 036813ee 2019-05-09 stsp *new_te = NULL;
5054 036813ee 2019-05-09 stsp
5055 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5056 036813ee 2019-05-09 stsp if (*new_te == NULL)
5057 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5058 036813ee 2019-05-09 stsp
5059 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5060 102b254e 2020-10-19 stsp if (err)
5061 036813ee 2019-05-09 stsp goto done;
5062 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5063 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5064 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5065 036813ee 2019-05-09 stsp goto done;
5066 036813ee 2019-05-09 stsp }
5067 036813ee 2019-05-09 stsp
5068 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5069 036813ee 2019-05-09 stsp
5070 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5071 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5072 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5073 5f8a88c6 2019-08-03 stsp else
5074 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5075 036813ee 2019-05-09 stsp done:
5076 102b254e 2020-10-19 stsp free(ct_name);
5077 036813ee 2019-05-09 stsp if (err && *new_te) {
5078 56e0773d 2019-11-28 stsp free(*new_te);
5079 036813ee 2019-05-09 stsp *new_te = NULL;
5080 036813ee 2019-05-09 stsp }
5081 036813ee 2019-05-09 stsp return err;
5082 036813ee 2019-05-09 stsp }
5083 036813ee 2019-05-09 stsp
5084 036813ee 2019-05-09 stsp static const struct got_error *
5085 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5086 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5087 036813ee 2019-05-09 stsp {
5088 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5089 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5090 036813ee 2019-05-09 stsp
5091 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5092 036813ee 2019-05-09 stsp if (err)
5093 036813ee 2019-05-09 stsp return err;
5094 036813ee 2019-05-09 stsp if (new_pe == NULL)
5095 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5096 036813ee 2019-05-09 stsp return NULL;
5097 afa376bf 2019-05-09 stsp }
5098 afa376bf 2019-05-09 stsp
5099 afa376bf 2019-05-09 stsp static const struct got_error *
5100 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5101 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5102 afa376bf 2019-05-09 stsp {
5103 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5104 5f8a88c6 2019-08-03 stsp unsigned char status;
5105 ae1e948a 2021-09-28 thomas
5106 ae1e948a 2021-09-28 thomas if (status_cb == NULL) /* no commit progress output desired */
5107 ae1e948a 2021-09-28 thomas return NULL;
5108 5f8a88c6 2019-08-03 stsp
5109 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5110 afa376bf 2019-05-09 stsp ct_path++;
5111 5f8a88c6 2019-08-03 stsp
5112 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5113 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5114 5f8a88c6 2019-08-03 stsp else
5115 5f8a88c6 2019-08-03 stsp status = ct->status;
5116 5f8a88c6 2019-08-03 stsp
5117 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5118 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5119 036813ee 2019-05-09 stsp }
5120 036813ee 2019-05-09 stsp
5121 036813ee 2019-05-09 stsp static const struct got_error *
5122 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5123 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5124 44d03001 2019-05-09 stsp {
5125 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5126 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5127 44d03001 2019-05-09 stsp char *te_path;
5128 44d03001 2019-05-09 stsp
5129 44d03001 2019-05-09 stsp *modified = 0;
5130 44d03001 2019-05-09 stsp
5131 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5132 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5133 44d03001 2019-05-09 stsp te->name) == -1)
5134 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5135 44d03001 2019-05-09 stsp
5136 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5137 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5138 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5139 44d03001 2019-05-09 stsp strlen(te_path));
5140 62d463ca 2020-10-20 naddy if (*modified)
5141 44d03001 2019-05-09 stsp break;
5142 44d03001 2019-05-09 stsp }
5143 44d03001 2019-05-09 stsp
5144 44d03001 2019-05-09 stsp free(te_path);
5145 44d03001 2019-05-09 stsp return err;
5146 44d03001 2019-05-09 stsp }
5147 44d03001 2019-05-09 stsp
5148 44d03001 2019-05-09 stsp static const struct got_error *
5149 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5150 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5151 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5152 036813ee 2019-05-09 stsp {
5153 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5154 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5155 036813ee 2019-05-09 stsp
5156 036813ee 2019-05-09 stsp *ctp = NULL;
5157 036813ee 2019-05-09 stsp
5158 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5159 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5160 036813ee 2019-05-09 stsp char *ct_name = NULL;
5161 036813ee 2019-05-09 stsp int path_matches;
5162 036813ee 2019-05-09 stsp
5163 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5164 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5165 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5166 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5167 5f8a88c6 2019-08-03 stsp continue;
5168 5f8a88c6 2019-08-03 stsp } else {
5169 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5170 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5171 5f8a88c6 2019-08-03 stsp continue;
5172 5f8a88c6 2019-08-03 stsp }
5173 036813ee 2019-05-09 stsp
5174 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5175 036813ee 2019-05-09 stsp continue;
5176 036813ee 2019-05-09 stsp
5177 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5178 62d463ca 2020-10-20 naddy if (err)
5179 036813ee 2019-05-09 stsp return err;
5180 036813ee 2019-05-09 stsp if (!path_matches)
5181 036813ee 2019-05-09 stsp continue;
5182 036813ee 2019-05-09 stsp
5183 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5184 d34b633e 2020-10-19 stsp if (err)
5185 d34b633e 2020-10-19 stsp return err;
5186 d34b633e 2020-10-19 stsp
5187 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5188 d34b633e 2020-10-19 stsp free(ct_name);
5189 036813ee 2019-05-09 stsp continue;
5190 d34b633e 2020-10-19 stsp }
5191 d34b633e 2020-10-19 stsp free(ct_name);
5192 036813ee 2019-05-09 stsp
5193 036813ee 2019-05-09 stsp *ctp = ct;
5194 036813ee 2019-05-09 stsp break;
5195 036813ee 2019-05-09 stsp }
5196 2b496619 2019-07-10 stsp
5197 2b496619 2019-07-10 stsp return err;
5198 2b496619 2019-07-10 stsp }
5199 2b496619 2019-07-10 stsp
5200 2b496619 2019-07-10 stsp static const struct got_error *
5201 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5202 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5203 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5204 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5205 2b496619 2019-07-10 stsp struct got_repository *repo)
5206 2b496619 2019-07-10 stsp {
5207 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5208 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5209 2b496619 2019-07-10 stsp char *subtree_path;
5210 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5211 ba580f68 2020-03-22 stsp int nentries;
5212 2b496619 2019-07-10 stsp
5213 2b496619 2019-07-10 stsp *new_tep = NULL;
5214 2b496619 2019-07-10 stsp
5215 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5216 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5217 2b496619 2019-07-10 stsp child_path) == -1)
5218 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5219 036813ee 2019-05-09 stsp
5220 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5221 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5222 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5223 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5224 56e0773d 2019-11-28 stsp
5225 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5226 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5227 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5228 2b496619 2019-07-10 stsp goto done;
5229 2b496619 2019-07-10 stsp }
5230 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5231 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5232 2b496619 2019-07-10 stsp if (err) {
5233 56e0773d 2019-11-28 stsp free(new_te);
5234 2b496619 2019-07-10 stsp goto done;
5235 2b496619 2019-07-10 stsp }
5236 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5237 2b496619 2019-07-10 stsp done:
5238 56e0773d 2019-11-28 stsp free(id);
5239 2b496619 2019-07-10 stsp free(subtree_path);
5240 2b496619 2019-07-10 stsp if (err == NULL)
5241 2b496619 2019-07-10 stsp *new_tep = new_te;
5242 036813ee 2019-05-09 stsp return err;
5243 036813ee 2019-05-09 stsp }
5244 036813ee 2019-05-09 stsp
5245 036813ee 2019-05-09 stsp static const struct got_error *
5246 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5247 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5248 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5249 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5250 036813ee 2019-05-09 stsp struct got_repository *repo)
5251 036813ee 2019-05-09 stsp {
5252 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5253 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5254 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5255 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5256 036813ee 2019-05-09 stsp
5257 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5258 ba580f68 2020-03-22 stsp *nentries = 0;
5259 036813ee 2019-05-09 stsp
5260 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5261 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5262 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5263 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5264 036813ee 2019-05-09 stsp
5265 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5266 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5267 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5268 036813ee 2019-05-09 stsp continue;
5269 036813ee 2019-05-09 stsp
5270 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5271 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5272 036813ee 2019-05-09 stsp continue;
5273 036813ee 2019-05-09 stsp
5274 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5275 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5276 036813ee 2019-05-09 stsp if (err)
5277 036813ee 2019-05-09 stsp goto done;
5278 036813ee 2019-05-09 stsp
5279 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5280 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5281 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5282 036813ee 2019-05-09 stsp if (err)
5283 036813ee 2019-05-09 stsp goto done;
5284 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5285 afa376bf 2019-05-09 stsp if (err)
5286 afa376bf 2019-05-09 stsp goto done;
5287 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5288 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5289 2b496619 2019-07-10 stsp if (err)
5290 2f51b5b3 2019-05-09 stsp goto done;
5291 ba580f68 2020-03-22 stsp (*nentries)++;
5292 2b496619 2019-07-10 stsp } else {
5293 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5294 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5295 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5296 2b496619 2019-07-10 stsp == NULL) {
5297 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5298 2b496619 2019-07-10 stsp child_path, path_base_tree,
5299 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5300 2b496619 2019-07-10 stsp repo);
5301 2b496619 2019-07-10 stsp if (err)
5302 2b496619 2019-07-10 stsp goto done;
5303 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5304 2b496619 2019-07-10 stsp if (err)
5305 2b496619 2019-07-10 stsp goto done;
5306 ba580f68 2020-03-22 stsp (*nentries)++;
5307 9ba0479c 2019-05-10 stsp }
5308 ed175427 2019-05-09 stsp }
5309 2f51b5b3 2019-05-09 stsp }
5310 2f51b5b3 2019-05-09 stsp
5311 2f51b5b3 2019-05-09 stsp if (base_tree) {
5312 56e0773d 2019-11-28 stsp int i, nbase_entries;
5313 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5314 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5315 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5316 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5317 63c5ca5d 2019-08-24 stsp
5318 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5319 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5320 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5321 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5322 63c5ca5d 2019-08-24 stsp if (err)
5323 63c5ca5d 2019-08-24 stsp goto done;
5324 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5325 63c5ca5d 2019-08-24 stsp if (err)
5326 63c5ca5d 2019-08-24 stsp goto done;
5327 ba580f68 2020-03-22 stsp (*nentries)++;
5328 63c5ca5d 2019-08-24 stsp continue;
5329 63c5ca5d 2019-08-24 stsp }
5330 2f51b5b3 2019-05-09 stsp
5331 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5332 44d03001 2019-05-09 stsp int modified;
5333 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5334 036813ee 2019-05-09 stsp if (err)
5335 036813ee 2019-05-09 stsp goto done;
5336 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5337 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5338 2f51b5b3 2019-05-09 stsp if (err)
5339 2f51b5b3 2019-05-09 stsp goto done;
5340 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5341 44d03001 2019-05-09 stsp if (modified) {
5342 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5343 ba580f68 2020-03-22 stsp int nsubentries;
5344 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5345 ba580f68 2020-03-22 stsp &nsubentries, te,
5346 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5347 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5348 44d03001 2019-05-09 stsp if (err)
5349 44d03001 2019-05-09 stsp goto done;
5350 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5351 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5352 ba580f68 2020-03-22 stsp free(new_id);
5353 ba580f68 2020-03-22 stsp continue;
5354 ba580f68 2020-03-22 stsp }
5355 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5356 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5357 56e0773d 2019-11-28 stsp free(new_id);
5358 44d03001 2019-05-09 stsp }
5359 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5360 036813ee 2019-05-09 stsp if (err)
5361 036813ee 2019-05-09 stsp goto done;
5362 ba580f68 2020-03-22 stsp (*nentries)++;
5363 2f51b5b3 2019-05-09 stsp continue;
5364 036813ee 2019-05-09 stsp }
5365 2f51b5b3 2019-05-09 stsp
5366 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5367 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5368 f66c734c 2019-09-22 stsp if (err)
5369 f66c734c 2019-09-22 stsp goto done;
5370 2f51b5b3 2019-05-09 stsp if (ct) {
5371 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5372 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5373 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5374 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5375 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5376 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5377 2f51b5b3 2019-05-09 stsp if (err)
5378 2f51b5b3 2019-05-09 stsp goto done;
5379 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5380 2f51b5b3 2019-05-09 stsp if (err)
5381 2f51b5b3 2019-05-09 stsp goto done;
5382 ba580f68 2020-03-22 stsp (*nentries)++;
5383 2f51b5b3 2019-05-09 stsp }
5384 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5385 b416585c 2019-05-13 stsp status_arg);
5386 afa376bf 2019-05-09 stsp if (err)
5387 afa376bf 2019-05-09 stsp goto done;
5388 2f51b5b3 2019-05-09 stsp } else {
5389 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5390 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5391 2f51b5b3 2019-05-09 stsp if (err)
5392 2f51b5b3 2019-05-09 stsp goto done;
5393 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5394 2f51b5b3 2019-05-09 stsp if (err)
5395 2f51b5b3 2019-05-09 stsp goto done;
5396 ba580f68 2020-03-22 stsp (*nentries)++;
5397 2f51b5b3 2019-05-09 stsp }
5398 ca2503ea 2019-05-09 stsp }
5399 ed175427 2019-05-09 stsp }
5400 0b5cc0d6 2019-05-09 stsp
5401 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5402 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5403 ed175427 2019-05-09 stsp done:
5404 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5405 2e1fa222 2020-07-23 stsp return err;
5406 2e1fa222 2020-07-23 stsp }
5407 2e1fa222 2020-07-23 stsp
5408 2e1fa222 2020-07-23 stsp static const struct got_error *
5409 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5410 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5411 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5412 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5413 ebf99748 2019-05-09 stsp {
5414 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5415 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5416 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5417 ebf99748 2019-05-09 stsp
5418 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5419 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5420 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5421 ebf99748 2019-05-09 stsp
5422 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5423 437adc9d 2020-12-10 yzhong
5424 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5425 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5426 437adc9d 2020-12-10 yzhong if (err)
5427 437adc9d 2020-12-10 yzhong goto done;
5428 437adc9d 2020-12-10 yzhong
5429 ebf99748 2019-05-09 stsp if (ie) {
5430 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5431 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5432 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5433 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5434 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5435 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5436 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5437 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5438 437adc9d 2020-12-10 yzhong
5439 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5440 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5441 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5442 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5443 72fd46fa 2019-09-06 stsp !have_staged_files);
5444 ebf99748 2019-05-09 stsp } else
5445 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5446 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5447 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5448 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5449 72fd46fa 2019-09-06 stsp !have_staged_files);
5450 ebf99748 2019-05-09 stsp } else {
5451 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5452 ebf99748 2019-05-09 stsp if (err)
5453 437adc9d 2020-12-10 yzhong goto done;
5454 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5455 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5456 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5457 3969253a 2020-03-07 stsp if (err) {
5458 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5459 437adc9d 2020-12-10 yzhong goto done;
5460 3969253a 2020-03-07 stsp }
5461 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5462 3969253a 2020-03-07 stsp if (err) {
5463 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5464 437adc9d 2020-12-10 yzhong goto done;
5465 3969253a 2020-03-07 stsp }
5466 ebf99748 2019-05-09 stsp }
5467 437adc9d 2020-12-10 yzhong free(relpath);
5468 437adc9d 2020-12-10 yzhong relpath = NULL;
5469 ebf99748 2019-05-09 stsp }
5470 437adc9d 2020-12-10 yzhong done:
5471 437adc9d 2020-12-10 yzhong free(relpath);
5472 d56d26ce 2019-05-10 stsp return err;
5473 d56d26ce 2019-05-10 stsp }
5474 735ef5ac 2019-08-03 stsp
5475 d56d26ce 2019-05-10 stsp
5476 d56d26ce 2019-05-10 stsp static const struct got_error *
5477 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5478 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5479 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5480 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5481 735ef5ac 2019-08-03 stsp int ood_errcode)
5482 d56d26ce 2019-05-10 stsp {
5483 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5484 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5485 1a36436d 2019-06-10 stsp
5486 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5487 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5488 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5489 1a36436d 2019-06-10 stsp return NULL;
5490 9bead371 2019-07-28 stsp /*
5491 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5492 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5493 9bead371 2019-07-28 stsp */
5494 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5495 735ef5ac 2019-08-03 stsp in_repo_path);
5496 9bead371 2019-07-28 stsp if (err) {
5497 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5498 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5499 1a36436d 2019-06-10 stsp goto done;
5500 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5501 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5502 1a36436d 2019-06-10 stsp } else {
5503 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5504 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5505 735ef5ac 2019-08-03 stsp in_repo_path);
5506 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5507 1a36436d 2019-06-10 stsp goto done;
5508 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5509 1a36436d 2019-06-10 stsp }
5510 1a36436d 2019-06-10 stsp done:
5511 1a36436d 2019-06-10 stsp free(id);
5512 1a36436d 2019-06-10 stsp return err;
5513 ebf99748 2019-05-09 stsp }
5514 ebf99748 2019-05-09 stsp
5515 c4296144 2019-05-09 stsp const struct got_error *
5516 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5517 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5518 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id,
5519 10604dce 2021-09-24 thomas struct got_object_id *parent_id2,
5520 10604dce 2021-09-24 thomas struct got_worktree *worktree,
5521 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5522 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5523 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5524 afa376bf 2019-05-09 stsp struct got_repository *repo)
5525 c4296144 2019-05-09 stsp {
5526 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5527 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5528 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5529 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5530 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5531 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5532 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5533 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5534 10604dce 2021-09-24 thomas int nentries, nparents = 0;
5535 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5536 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5537 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5538 c4296144 2019-05-09 stsp
5539 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5540 c4296144 2019-05-09 stsp
5541 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5542 675c7539 2019-05-09 stsp
5543 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5544 588edf97 2019-05-10 stsp if (err)
5545 588edf97 2019-05-10 stsp goto done;
5546 de18fc63 2019-05-09 stsp
5547 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5548 588edf97 2019-05-10 stsp if (err)
5549 588edf97 2019-05-10 stsp goto done;
5550 588edf97 2019-05-10 stsp
5551 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5552 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5553 33ad4cbe 2019-05-12 jcs if (err)
5554 33ad4cbe 2019-05-12 jcs goto done;
5555 33ad4cbe 2019-05-12 jcs }
5556 c4296144 2019-05-09 stsp
5557 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5558 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5559 33ad4cbe 2019-05-12 jcs goto done;
5560 33ad4cbe 2019-05-12 jcs }
5561 33ad4cbe 2019-05-12 jcs
5562 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5563 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5564 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5565 cf066bf8 2019-05-09 stsp char *ondisk_path;
5566 cf066bf8 2019-05-09 stsp
5567 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5568 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5569 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5570 5f8a88c6 2019-08-03 stsp continue;
5571 5f8a88c6 2019-08-03 stsp
5572 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5573 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5574 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5575 cf066bf8 2019-05-09 stsp continue;
5576 cf066bf8 2019-05-09 stsp
5577 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5578 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5580 cf066bf8 2019-05-09 stsp goto done;
5581 cf066bf8 2019-05-09 stsp }
5582 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5583 2e1fa222 2020-07-23 stsp free(ondisk_path);
5584 2e1fa222 2020-07-23 stsp if (err)
5585 cf066bf8 2019-05-09 stsp goto done;
5586 cf066bf8 2019-05-09 stsp }
5587 036813ee 2019-05-09 stsp
5588 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5589 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5590 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5591 036813ee 2019-05-09 stsp if (err)
5592 036813ee 2019-05-09 stsp goto done;
5593 036813ee 2019-05-09 stsp
5594 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5595 de18fc63 2019-05-09 stsp if (err)
5596 de18fc63 2019-05-09 stsp goto done;
5597 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5598 10604dce 2021-09-24 thomas nparents++;
5599 10604dce 2021-09-24 thomas if (parent_id2) {
5600 10604dce 2021-09-24 thomas err = got_object_qid_alloc(&pid, parent_id2);
5601 10604dce 2021-09-24 thomas if (err)
5602 10604dce 2021-09-24 thomas goto done;
5603 10604dce 2021-09-24 thomas STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5604 10604dce 2021-09-24 thomas nparents++;
5605 10604dce 2021-09-24 thomas }
5606 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5607 10604dce 2021-09-24 thomas nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5608 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5609 33ad4cbe 2019-05-12 jcs free(logmsg);
5610 9d40349a 2019-05-09 stsp if (err)
5611 9d40349a 2019-05-09 stsp goto done;
5612 9d40349a 2019-05-09 stsp
5613 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5614 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5615 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5616 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5617 09f5bd90 2019-05-09 stsp goto done;
5618 09f5bd90 2019-05-09 stsp }
5619 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5620 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5621 09f5bd90 2019-05-09 stsp if (err)
5622 09f5bd90 2019-05-09 stsp goto done;
5623 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5624 ebf99748 2019-05-09 stsp if (err)
5625 ebf99748 2019-05-09 stsp goto done;
5626 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5627 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5628 09f5bd90 2019-05-09 stsp goto done;
5629 09f5bd90 2019-05-09 stsp }
5630 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5631 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5632 f2c16586 2019-05-09 stsp if (err)
5633 f2c16586 2019-05-09 stsp goto done;
5634 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5635 f2c16586 2019-05-09 stsp if (err)
5636 f2c16586 2019-05-09 stsp goto done;
5637 f2c16586 2019-05-09 stsp
5638 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5639 09f5bd90 2019-05-09 stsp if (err)
5640 09f5bd90 2019-05-09 stsp goto done;
5641 09f5bd90 2019-05-09 stsp
5642 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5643 ebf99748 2019-05-09 stsp if (err)
5644 ebf99748 2019-05-09 stsp goto done;
5645 c4296144 2019-05-09 stsp done:
5646 10604dce 2021-09-24 thomas got_object_id_queue_free(&parent_ids);
5647 588edf97 2019-05-10 stsp if (head_tree)
5648 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5649 588edf97 2019-05-10 stsp if (head_commit)
5650 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5651 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5652 2f17228e 2019-05-12 stsp if (head_ref2) {
5653 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5654 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5655 2f17228e 2019-05-12 stsp err = unlockerr;
5656 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5657 39cd0ff6 2019-07-12 stsp }
5658 39cd0ff6 2019-07-12 stsp return err;
5659 39cd0ff6 2019-07-12 stsp }
5660 5c1e53bc 2019-07-28 stsp
5661 5c1e53bc 2019-07-28 stsp static const struct got_error *
5662 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5663 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5664 5c1e53bc 2019-07-28 stsp {
5665 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5666 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5667 39cd0ff6 2019-07-12 stsp
5668 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5669 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5670 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5671 5c1e53bc 2019-07-28 stsp
5672 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5673 5c1e53bc 2019-07-28 stsp ct_path++;
5674 5c1e53bc 2019-07-28 stsp
5675 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5676 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5677 5c1e53bc 2019-07-28 stsp break;
5678 5c1e53bc 2019-07-28 stsp }
5679 5c1e53bc 2019-07-28 stsp
5680 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5681 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5682 5c1e53bc 2019-07-28 stsp
5683 5c1e53bc 2019-07-28 stsp return NULL;
5684 5c1e53bc 2019-07-28 stsp }
5685 5c1e53bc 2019-07-28 stsp
5686 f0b75401 2019-08-03 stsp static const struct got_error *
5687 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5688 f0b75401 2019-08-03 stsp {
5689 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5690 f0b75401 2019-08-03 stsp
5691 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5692 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5693 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5694 f0b75401 2019-08-03 stsp }
5695 f0b75401 2019-08-03 stsp
5696 f0b75401 2019-08-03 stsp return NULL;
5697 f0b75401 2019-08-03 stsp }
5698 f0b75401 2019-08-03 stsp
5699 5f8a88c6 2019-08-03 stsp static const struct got_error *
5700 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5701 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5702 5f8a88c6 2019-08-03 stsp {
5703 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5704 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5705 5f8a88c6 2019-08-03 stsp
5706 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5707 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5708 5f8a88c6 2019-08-03 stsp continue;
5709 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5710 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5711 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5712 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5713 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5714 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5715 5f8a88c6 2019-08-03 stsp }
5716 5f8a88c6 2019-08-03 stsp
5717 5f8a88c6 2019-08-03 stsp return NULL;
5718 5f8a88c6 2019-08-03 stsp }
5719 5f8a88c6 2019-08-03 stsp
5720 39cd0ff6 2019-07-12 stsp const struct got_error *
5721 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5722 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5723 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5724 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5725 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5726 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5727 39cd0ff6 2019-07-12 stsp {
5728 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5729 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5730 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5731 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5732 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5733 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5734 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5735 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5736 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5737 39cd0ff6 2019-07-12 stsp
5738 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5739 39cd0ff6 2019-07-12 stsp
5740 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5741 39cd0ff6 2019-07-12 stsp
5742 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5743 39cd0ff6 2019-07-12 stsp if (err)
5744 39cd0ff6 2019-07-12 stsp goto done;
5745 39cd0ff6 2019-07-12 stsp
5746 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5747 39cd0ff6 2019-07-12 stsp if (err)
5748 39cd0ff6 2019-07-12 stsp goto done;
5749 39cd0ff6 2019-07-12 stsp
5750 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5751 39cd0ff6 2019-07-12 stsp if (err)
5752 39cd0ff6 2019-07-12 stsp goto done;
5753 39cd0ff6 2019-07-12 stsp
5754 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5755 39cd0ff6 2019-07-12 stsp if (err)
5756 39cd0ff6 2019-07-12 stsp goto done;
5757 39cd0ff6 2019-07-12 stsp
5758 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5759 5f8a88c6 2019-08-03 stsp &have_staged_files);
5760 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5761 5f8a88c6 2019-08-03 stsp goto done;
5762 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5763 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5764 5f8a88c6 2019-08-03 stsp if (err)
5765 5f8a88c6 2019-08-03 stsp goto done;
5766 5f8a88c6 2019-08-03 stsp }
5767 5f8a88c6 2019-08-03 stsp
5768 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5769 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5770 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5771 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5772 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5773 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5774 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5775 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5776 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5777 5c1e53bc 2019-07-28 stsp if (err)
5778 5c1e53bc 2019-07-28 stsp goto done;
5779 5c1e53bc 2019-07-28 stsp }
5780 39cd0ff6 2019-07-12 stsp
5781 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5782 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5783 39cd0ff6 2019-07-12 stsp goto done;
5784 5c1e53bc 2019-07-28 stsp }
5785 5c1e53bc 2019-07-28 stsp
5786 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5787 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5788 5c1e53bc 2019-07-28 stsp if (err)
5789 5c1e53bc 2019-07-28 stsp goto done;
5790 39cd0ff6 2019-07-12 stsp }
5791 f0b75401 2019-08-03 stsp
5792 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5793 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5794 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5795 f0b75401 2019-08-03 stsp
5796 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5797 f0b75401 2019-08-03 stsp ct_path++;
5798 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5799 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5800 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5801 b50cabdf 2019-07-12 stsp if (err)
5802 b50cabdf 2019-07-12 stsp goto done;
5803 f0b75401 2019-08-03 stsp
5804 b50cabdf 2019-07-12 stsp }
5805 b50cabdf 2019-07-12 stsp
5806 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5807 10604dce 2021-09-24 thomas head_commit_id, NULL, worktree, author, committer,
5808 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5809 39cd0ff6 2019-07-12 stsp if (err)
5810 39cd0ff6 2019-07-12 stsp goto done;
5811 39cd0ff6 2019-07-12 stsp
5812 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5813 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5814 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5815 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5816 39cd0ff6 2019-07-12 stsp err = sync_err;
5817 39cd0ff6 2019-07-12 stsp done:
5818 39cd0ff6 2019-07-12 stsp if (fileindex)
5819 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5820 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5821 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5822 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5823 39cd0ff6 2019-07-12 stsp err = unlockerr;
5824 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5825 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5826 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5827 39cd0ff6 2019-07-12 stsp }
5828 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5829 c4296144 2019-05-09 stsp return err;
5830 8656d6c4 2019-05-20 stsp }
5831 8656d6c4 2019-05-20 stsp
5832 8656d6c4 2019-05-20 stsp const char *
5833 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5834 8656d6c4 2019-05-20 stsp {
5835 8656d6c4 2019-05-20 stsp return ct->path;
5836 8656d6c4 2019-05-20 stsp }
5837 8656d6c4 2019-05-20 stsp
5838 8656d6c4 2019-05-20 stsp unsigned int
5839 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5840 8656d6c4 2019-05-20 stsp {
5841 8656d6c4 2019-05-20 stsp return ct->status;
5842 818c7501 2019-07-11 stsp }
5843 818c7501 2019-07-11 stsp
5844 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5845 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5846 818c7501 2019-07-11 stsp struct got_repository *repo;
5847 818c7501 2019-07-11 stsp };
5848 818c7501 2019-07-11 stsp
5849 818c7501 2019-07-11 stsp static const struct got_error *
5850 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5851 818c7501 2019-07-11 stsp {
5852 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5853 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5854 818c7501 2019-07-11 stsp unsigned char status;
5855 818c7501 2019-07-11 stsp struct stat sb;
5856 818c7501 2019-07-11 stsp char *ondisk_path;
5857 818c7501 2019-07-11 stsp
5858 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5859 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5860 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5861 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5862 818c7501 2019-07-11 stsp
5863 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5864 818c7501 2019-07-11 stsp == -1)
5865 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5866 818c7501 2019-07-11 stsp
5867 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5868 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5869 818c7501 2019-07-11 stsp free(ondisk_path);
5870 818c7501 2019-07-11 stsp if (err)
5871 818c7501 2019-07-11 stsp return err;
5872 818c7501 2019-07-11 stsp
5873 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5874 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5875 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5876 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5877 818c7501 2019-07-11 stsp
5878 818c7501 2019-07-11 stsp return NULL;
5879 818c7501 2019-07-11 stsp }
5880 818c7501 2019-07-11 stsp
5881 818c7501 2019-07-11 stsp const struct got_error *
5882 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5883 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5884 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5885 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5886 818c7501 2019-07-11 stsp {
5887 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5888 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5889 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5890 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5891 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5892 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5893 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5894 818c7501 2019-07-11 stsp
5895 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5896 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5897 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5898 818c7501 2019-07-11 stsp
5899 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5900 818c7501 2019-07-11 stsp if (err)
5901 818c7501 2019-07-11 stsp return err;
5902 818c7501 2019-07-11 stsp
5903 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5904 818c7501 2019-07-11 stsp if (err)
5905 818c7501 2019-07-11 stsp goto done;
5906 818c7501 2019-07-11 stsp
5907 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5908 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5909 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5910 818c7501 2019-07-11 stsp &ok_arg);
5911 818c7501 2019-07-11 stsp if (err)
5912 818c7501 2019-07-11 stsp goto done;
5913 818c7501 2019-07-11 stsp
5914 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5915 818c7501 2019-07-11 stsp if (err)
5916 818c7501 2019-07-11 stsp goto done;
5917 818c7501 2019-07-11 stsp
5918 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5919 818c7501 2019-07-11 stsp if (err)
5920 818c7501 2019-07-11 stsp goto done;
5921 818c7501 2019-07-11 stsp
5922 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5923 818c7501 2019-07-11 stsp if (err)
5924 818c7501 2019-07-11 stsp goto done;
5925 818c7501 2019-07-11 stsp
5926 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5927 818c7501 2019-07-11 stsp 0);
5928 e51d7b55 2020-01-04 stsp if (err)
5929 e51d7b55 2020-01-04 stsp goto done;
5930 e51d7b55 2020-01-04 stsp
5931 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5932 818c7501 2019-07-11 stsp if (err)
5933 818c7501 2019-07-11 stsp goto done;
5934 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5935 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5936 e51d7b55 2020-01-04 stsp goto done;
5937 e51d7b55 2020-01-04 stsp }
5938 818c7501 2019-07-11 stsp
5939 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5940 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5941 818c7501 2019-07-11 stsp if (err)
5942 818c7501 2019-07-11 stsp goto done;
5943 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5944 818c7501 2019-07-11 stsp if (err)
5945 818c7501 2019-07-11 stsp goto done;
5946 818c7501 2019-07-11 stsp
5947 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5948 818c7501 2019-07-11 stsp
5949 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5950 818c7501 2019-07-11 stsp if (err)
5951 818c7501 2019-07-11 stsp goto done;
5952 818c7501 2019-07-11 stsp
5953 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5954 818c7501 2019-07-11 stsp if (err)
5955 818c7501 2019-07-11 stsp goto done;
5956 818c7501 2019-07-11 stsp
5957 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5958 818c7501 2019-07-11 stsp worktree->base_commit_id);
5959 818c7501 2019-07-11 stsp if (err)
5960 818c7501 2019-07-11 stsp goto done;
5961 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5962 818c7501 2019-07-11 stsp if (err)
5963 818c7501 2019-07-11 stsp goto done;
5964 818c7501 2019-07-11 stsp
5965 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5966 818c7501 2019-07-11 stsp if (err)
5967 818c7501 2019-07-11 stsp goto done;
5968 818c7501 2019-07-11 stsp done:
5969 818c7501 2019-07-11 stsp free(fileindex_path);
5970 818c7501 2019-07-11 stsp free(tmp_branch_name);
5971 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5972 818c7501 2019-07-11 stsp free(branch_ref_name);
5973 818c7501 2019-07-11 stsp if (branch_ref)
5974 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5975 818c7501 2019-07-11 stsp if (wt_branch)
5976 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5977 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5978 818c7501 2019-07-11 stsp if (err) {
5979 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5980 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5981 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5982 818c7501 2019-07-11 stsp }
5983 818c7501 2019-07-11 stsp if (*tmp_branch) {
5984 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5985 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5986 818c7501 2019-07-11 stsp }
5987 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5988 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5989 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5990 3e3a69f1 2019-07-25 stsp }
5991 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5992 818c7501 2019-07-11 stsp }
5993 818c7501 2019-07-11 stsp return err;
5994 818c7501 2019-07-11 stsp }
5995 818c7501 2019-07-11 stsp
5996 818c7501 2019-07-11 stsp const struct got_error *
5997 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5998 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5999 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6000 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6001 818c7501 2019-07-11 stsp {
6002 818c7501 2019-07-11 stsp const struct got_error *err;
6003 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6004 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6006 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6007 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6008 818c7501 2019-07-11 stsp
6009 818c7501 2019-07-11 stsp *commit_id = NULL;
6010 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6011 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6012 3e3a69f1 2019-07-25 stsp *branch = NULL;
6013 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6014 3e3a69f1 2019-07-25 stsp
6015 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6016 3e3a69f1 2019-07-25 stsp if (err)
6017 3e3a69f1 2019-07-25 stsp return err;
6018 818c7501 2019-07-11 stsp
6019 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6020 3e3a69f1 2019-07-25 stsp if (err)
6021 f032f1f7 2019-08-04 stsp goto done;
6022 f032f1f7 2019-08-04 stsp
6023 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 f032f1f7 2019-08-04 stsp &have_staged_files);
6025 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6026 f032f1f7 2019-08-04 stsp goto done;
6027 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6028 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6029 3e3a69f1 2019-07-25 stsp goto done;
6030 f032f1f7 2019-08-04 stsp }
6031 3e3a69f1 2019-07-25 stsp
6032 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6033 818c7501 2019-07-11 stsp if (err)
6034 f032f1f7 2019-08-04 stsp goto done;
6035 818c7501 2019-07-11 stsp
6036 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6037 818c7501 2019-07-11 stsp if (err)
6038 818c7501 2019-07-11 stsp goto done;
6039 818c7501 2019-07-11 stsp
6040 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6041 818c7501 2019-07-11 stsp if (err)
6042 818c7501 2019-07-11 stsp goto done;
6043 818c7501 2019-07-11 stsp
6044 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6045 818c7501 2019-07-11 stsp if (err)
6046 818c7501 2019-07-11 stsp goto done;
6047 818c7501 2019-07-11 stsp
6048 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6049 818c7501 2019-07-11 stsp if (err)
6050 818c7501 2019-07-11 stsp goto done;
6051 818c7501 2019-07-11 stsp
6052 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6053 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6054 818c7501 2019-07-11 stsp if (err)
6055 818c7501 2019-07-11 stsp goto done;
6056 818c7501 2019-07-11 stsp
6057 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6058 818c7501 2019-07-11 stsp if (err)
6059 818c7501 2019-07-11 stsp goto done;
6060 818c7501 2019-07-11 stsp
6061 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6062 818c7501 2019-07-11 stsp if (err)
6063 818c7501 2019-07-11 stsp goto done;
6064 818c7501 2019-07-11 stsp
6065 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6066 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6067 818c7501 2019-07-11 stsp if (err)
6068 818c7501 2019-07-11 stsp goto done;
6069 818c7501 2019-07-11 stsp
6070 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6071 818c7501 2019-07-11 stsp if (err)
6072 818c7501 2019-07-11 stsp goto done;
6073 818c7501 2019-07-11 stsp done:
6074 818c7501 2019-07-11 stsp free(commit_ref_name);
6075 818c7501 2019-07-11 stsp free(branch_ref_name);
6076 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6077 818c7501 2019-07-11 stsp if (commit_ref)
6078 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6079 818c7501 2019-07-11 stsp if (branch_ref)
6080 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6081 818c7501 2019-07-11 stsp if (err) {
6082 818c7501 2019-07-11 stsp free(*commit_id);
6083 818c7501 2019-07-11 stsp *commit_id = NULL;
6084 818c7501 2019-07-11 stsp if (*tmp_branch) {
6085 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6086 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6087 818c7501 2019-07-11 stsp }
6088 818c7501 2019-07-11 stsp if (*new_base_branch) {
6089 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6090 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6091 818c7501 2019-07-11 stsp }
6092 818c7501 2019-07-11 stsp if (*branch) {
6093 818c7501 2019-07-11 stsp got_ref_close(*branch);
6094 818c7501 2019-07-11 stsp *branch = NULL;
6095 818c7501 2019-07-11 stsp }
6096 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6097 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6098 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6099 3e3a69f1 2019-07-25 stsp }
6100 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6101 818c7501 2019-07-11 stsp }
6102 818c7501 2019-07-11 stsp return err;
6103 c4296144 2019-05-09 stsp }
6104 818c7501 2019-07-11 stsp
6105 818c7501 2019-07-11 stsp const struct got_error *
6106 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6107 818c7501 2019-07-11 stsp {
6108 818c7501 2019-07-11 stsp const struct got_error *err;
6109 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6110 818c7501 2019-07-11 stsp
6111 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6112 818c7501 2019-07-11 stsp if (err)
6113 818c7501 2019-07-11 stsp return err;
6114 818c7501 2019-07-11 stsp
6115 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6116 818c7501 2019-07-11 stsp free(tmp_branch_name);
6117 818c7501 2019-07-11 stsp return NULL;
6118 818c7501 2019-07-11 stsp }
6119 818c7501 2019-07-11 stsp
6120 818c7501 2019-07-11 stsp static const struct got_error *
6121 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6122 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6123 818c7501 2019-07-11 stsp {
6124 0ebf8283 2019-07-24 stsp *logmsg = arg;
6125 818c7501 2019-07-11 stsp return NULL;
6126 818c7501 2019-07-11 stsp }
6127 818c7501 2019-07-11 stsp
6128 818c7501 2019-07-11 stsp static const struct got_error *
6129 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6130 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6131 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6132 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6133 818c7501 2019-07-11 stsp {
6134 818c7501 2019-07-11 stsp return NULL;
6135 01757395 2019-07-12 stsp }
6136 01757395 2019-07-12 stsp
6137 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6138 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6139 01757395 2019-07-12 stsp void *progress_arg;
6140 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6141 01757395 2019-07-12 stsp };
6142 01757395 2019-07-12 stsp
6143 01757395 2019-07-12 stsp static const struct got_error *
6144 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6145 01757395 2019-07-12 stsp {
6146 01757395 2019-07-12 stsp const struct got_error *err;
6147 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6148 01757395 2019-07-12 stsp char *p;
6149 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6150 01757395 2019-07-12 stsp
6151 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6152 01757395 2019-07-12 stsp if (err)
6153 01757395 2019-07-12 stsp return err;
6154 01757395 2019-07-12 stsp
6155 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6156 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6157 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6158 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6159 01757395 2019-07-12 stsp return NULL;
6160 01757395 2019-07-12 stsp
6161 01757395 2019-07-12 stsp p = strdup(path);
6162 01757395 2019-07-12 stsp if (p == NULL)
6163 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6164 01757395 2019-07-12 stsp
6165 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6166 01757395 2019-07-12 stsp if (err || new == NULL)
6167 01757395 2019-07-12 stsp free(p);
6168 01757395 2019-07-12 stsp return err;
6169 01757395 2019-07-12 stsp }
6170 01757395 2019-07-12 stsp
6171 01757395 2019-07-12 stsp void
6172 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6173 01757395 2019-07-12 stsp {
6174 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6175 01757395 2019-07-12 stsp
6176 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6177 01757395 2019-07-12 stsp free((char *)pe->path);
6178 01757395 2019-07-12 stsp
6179 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6180 818c7501 2019-07-11 stsp }
6181 818c7501 2019-07-11 stsp
6182 0ebf8283 2019-07-24 stsp static const struct got_error *
6183 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6184 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6185 818c7501 2019-07-11 stsp {
6186 818c7501 2019-07-11 stsp const struct got_error *err;
6187 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6188 818c7501 2019-07-11 stsp
6189 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6190 818c7501 2019-07-11 stsp if (err) {
6191 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6192 818c7501 2019-07-11 stsp goto done;
6193 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6194 818c7501 2019-07-11 stsp if (err)
6195 818c7501 2019-07-11 stsp goto done;
6196 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6197 818c7501 2019-07-11 stsp if (err)
6198 818c7501 2019-07-11 stsp goto done;
6199 de05890f 2020-03-05 stsp } else if (is_rebase) {
6200 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6201 818c7501 2019-07-11 stsp int cmp;
6202 818c7501 2019-07-11 stsp
6203 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6204 818c7501 2019-07-11 stsp if (err)
6205 818c7501 2019-07-11 stsp goto done;
6206 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6207 818c7501 2019-07-11 stsp free(stored_id);
6208 818c7501 2019-07-11 stsp if (cmp != 0) {
6209 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6210 818c7501 2019-07-11 stsp goto done;
6211 818c7501 2019-07-11 stsp }
6212 818c7501 2019-07-11 stsp }
6213 0ebf8283 2019-07-24 stsp done:
6214 0ebf8283 2019-07-24 stsp if (commit_ref)
6215 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6216 0ebf8283 2019-07-24 stsp return err;
6217 0ebf8283 2019-07-24 stsp }
6218 0ebf8283 2019-07-24 stsp
6219 0ebf8283 2019-07-24 stsp static const struct got_error *
6220 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6221 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6222 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6223 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6224 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6225 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6226 0ebf8283 2019-07-24 stsp {
6227 0ebf8283 2019-07-24 stsp const struct got_error *err;
6228 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6229 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6230 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6231 818c7501 2019-07-11 stsp
6232 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6233 0ebf8283 2019-07-24 stsp
6234 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6235 0ebf8283 2019-07-24 stsp if (err)
6236 0ebf8283 2019-07-24 stsp return err;
6237 0ebf8283 2019-07-24 stsp
6238 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6239 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6240 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6241 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6242 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6243 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6244 818c7501 2019-07-11 stsp if (commit_ref)
6245 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6246 818c7501 2019-07-11 stsp return err;
6247 818c7501 2019-07-11 stsp }
6248 818c7501 2019-07-11 stsp
6249 818c7501 2019-07-11 stsp const struct got_error *
6250 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6251 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6252 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6253 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6254 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6255 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6256 818c7501 2019-07-11 stsp {
6257 0ebf8283 2019-07-24 stsp const struct got_error *err;
6258 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6259 0ebf8283 2019-07-24 stsp
6260 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6261 0ebf8283 2019-07-24 stsp if (err)
6262 0ebf8283 2019-07-24 stsp return err;
6263 0ebf8283 2019-07-24 stsp
6264 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6265 0ebf8283 2019-07-24 stsp if (err)
6266 0ebf8283 2019-07-24 stsp goto done;
6267 0ebf8283 2019-07-24 stsp
6268 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6269 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6270 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6271 0ebf8283 2019-07-24 stsp done:
6272 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6273 0ebf8283 2019-07-24 stsp return err;
6274 0ebf8283 2019-07-24 stsp }
6275 0ebf8283 2019-07-24 stsp
6276 0ebf8283 2019-07-24 stsp const struct got_error *
6277 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6278 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6279 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6280 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6281 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6282 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6283 0ebf8283 2019-07-24 stsp {
6284 0ebf8283 2019-07-24 stsp const struct got_error *err;
6285 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6286 0ebf8283 2019-07-24 stsp
6287 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6288 0ebf8283 2019-07-24 stsp if (err)
6289 0ebf8283 2019-07-24 stsp return err;
6290 0ebf8283 2019-07-24 stsp
6291 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6292 0ebf8283 2019-07-24 stsp if (err)
6293 0ebf8283 2019-07-24 stsp goto done;
6294 0ebf8283 2019-07-24 stsp
6295 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6296 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6297 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6298 0ebf8283 2019-07-24 stsp done:
6299 0ebf8283 2019-07-24 stsp free(commit_ref_name);
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_commit(struct got_object_id **new_commit_id,
6305 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6306 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6307 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6308 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6309 0ebf8283 2019-07-24 stsp {
6310 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6311 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6312 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6313 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6314 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6315 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6316 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6317 818c7501 2019-07-11 stsp
6318 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6319 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6320 a0e95631 2019-07-12 stsp
6321 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6322 818c7501 2019-07-11 stsp
6323 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6324 a0e95631 2019-07-12 stsp if (err)
6325 3e3a69f1 2019-07-25 stsp return err;
6326 a0e95631 2019-07-12 stsp
6327 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6328 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6329 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6330 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6331 01757395 2019-07-12 stsp /*
6332 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6333 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6334 6c13b005 2021-09-02 stsp *
6335 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6336 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6337 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6338 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6339 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6340 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6341 01757395 2019-07-12 stsp */
6342 01757395 2019-07-12 stsp if (merged_paths) {
6343 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6344 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6345 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6346 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6347 f2a9dc41 2019-12-13 tracey 0);
6348 01757395 2019-07-12 stsp if (err)
6349 01757395 2019-07-12 stsp goto done;
6350 01757395 2019-07-12 stsp }
6351 01757395 2019-07-12 stsp } else {
6352 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6353 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6354 01757395 2019-07-12 stsp if (err)
6355 01757395 2019-07-12 stsp goto done;
6356 01757395 2019-07-12 stsp }
6357 a0e95631 2019-07-12 stsp
6358 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6359 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6360 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6361 a0e95631 2019-07-12 stsp if (err)
6362 a0e95631 2019-07-12 stsp goto done;
6363 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6364 a0e95631 2019-07-12 stsp goto done;
6365 ff0d2220 2019-07-11 stsp }
6366 818c7501 2019-07-11 stsp
6367 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6368 edd02c5e 2019-07-11 stsp if (err)
6369 edd02c5e 2019-07-11 stsp goto done;
6370 edd02c5e 2019-07-11 stsp
6371 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6372 818c7501 2019-07-11 stsp if (err)
6373 818c7501 2019-07-11 stsp goto done;
6374 818c7501 2019-07-11 stsp
6375 5943eee2 2019-08-13 stsp if (new_logmsg) {
6376 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6377 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6378 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6379 5943eee2 2019-08-13 stsp goto done;
6380 5943eee2 2019-08-13 stsp }
6381 5943eee2 2019-08-13 stsp } else {
6382 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6383 5943eee2 2019-08-13 stsp if (err)
6384 5943eee2 2019-08-13 stsp goto done;
6385 5943eee2 2019-08-13 stsp }
6386 0ebf8283 2019-07-24 stsp
6387 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6388 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6389 10604dce 2021-09-24 thomas NULL, worktree, got_object_commit_get_author(orig_commit),
6390 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6391 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6392 a0e95631 2019-07-12 stsp if (err)
6393 a0e95631 2019-07-12 stsp goto done;
6394 a0e95631 2019-07-12 stsp
6395 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6396 a0e95631 2019-07-12 stsp if (err)
6397 a0e95631 2019-07-12 stsp goto done;
6398 a0e95631 2019-07-12 stsp
6399 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6400 a0e95631 2019-07-12 stsp if (err)
6401 a0e95631 2019-07-12 stsp goto done;
6402 a0e95631 2019-07-12 stsp
6403 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6404 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6405 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6406 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6407 a0e95631 2019-07-12 stsp err = sync_err;
6408 818c7501 2019-07-11 stsp done:
6409 a0e95631 2019-07-12 stsp free(fileindex_path);
6410 a0e95631 2019-07-12 stsp free(head_commit_id);
6411 a0e95631 2019-07-12 stsp if (head_ref)
6412 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6413 818c7501 2019-07-11 stsp if (err) {
6414 818c7501 2019-07-11 stsp free(*new_commit_id);
6415 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6416 818c7501 2019-07-11 stsp }
6417 818c7501 2019-07-11 stsp return err;
6418 818c7501 2019-07-11 stsp }
6419 818c7501 2019-07-11 stsp
6420 818c7501 2019-07-11 stsp const struct got_error *
6421 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6422 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6423 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6424 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6425 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6426 0ebf8283 2019-07-24 stsp {
6427 0ebf8283 2019-07-24 stsp const struct got_error *err;
6428 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6429 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6430 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6431 0ebf8283 2019-07-24 stsp
6432 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6433 0ebf8283 2019-07-24 stsp if (err)
6434 0ebf8283 2019-07-24 stsp return err;
6435 0ebf8283 2019-07-24 stsp
6436 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6437 0ebf8283 2019-07-24 stsp if (err)
6438 0ebf8283 2019-07-24 stsp goto done;
6439 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6440 0ebf8283 2019-07-24 stsp if (err)
6441 0ebf8283 2019-07-24 stsp goto done;
6442 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6443 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6444 0ebf8283 2019-07-24 stsp goto done;
6445 0ebf8283 2019-07-24 stsp }
6446 0ebf8283 2019-07-24 stsp
6447 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6448 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6449 0ebf8283 2019-07-24 stsp done:
6450 0ebf8283 2019-07-24 stsp if (commit_ref)
6451 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6452 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6453 0ebf8283 2019-07-24 stsp free(commit_id);
6454 0ebf8283 2019-07-24 stsp return err;
6455 0ebf8283 2019-07-24 stsp }
6456 0ebf8283 2019-07-24 stsp
6457 0ebf8283 2019-07-24 stsp const struct got_error *
6458 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6459 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6460 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6461 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6462 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6463 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6464 0ebf8283 2019-07-24 stsp {
6465 0ebf8283 2019-07-24 stsp const struct got_error *err;
6466 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6467 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6468 0ebf8283 2019-07-24 stsp
6469 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6470 0ebf8283 2019-07-24 stsp if (err)
6471 0ebf8283 2019-07-24 stsp return err;
6472 0ebf8283 2019-07-24 stsp
6473 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6474 0ebf8283 2019-07-24 stsp if (err)
6475 0ebf8283 2019-07-24 stsp goto done;
6476 0ebf8283 2019-07-24 stsp
6477 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6478 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6479 0ebf8283 2019-07-24 stsp done:
6480 0ebf8283 2019-07-24 stsp if (commit_ref)
6481 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6482 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6483 0ebf8283 2019-07-24 stsp return err;
6484 0ebf8283 2019-07-24 stsp }
6485 0ebf8283 2019-07-24 stsp
6486 0ebf8283 2019-07-24 stsp const struct got_error *
6487 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6488 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6489 818c7501 2019-07-11 stsp {
6490 3e3a69f1 2019-07-25 stsp if (fileindex)
6491 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6492 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6493 69844fba 2019-07-11 stsp }
6494 69844fba 2019-07-11 stsp
6495 69844fba 2019-07-11 stsp static const struct got_error *
6496 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6497 69844fba 2019-07-11 stsp {
6498 69844fba 2019-07-11 stsp const struct got_error *err;
6499 69844fba 2019-07-11 stsp struct got_reference *ref;
6500 69844fba 2019-07-11 stsp
6501 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6502 69844fba 2019-07-11 stsp if (err) {
6503 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6504 69844fba 2019-07-11 stsp return NULL;
6505 69844fba 2019-07-11 stsp return err;
6506 69844fba 2019-07-11 stsp }
6507 69844fba 2019-07-11 stsp
6508 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6509 69844fba 2019-07-11 stsp got_ref_close(ref);
6510 69844fba 2019-07-11 stsp return err;
6511 818c7501 2019-07-11 stsp }
6512 818c7501 2019-07-11 stsp
6513 69844fba 2019-07-11 stsp static const struct got_error *
6514 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6515 69844fba 2019-07-11 stsp {
6516 69844fba 2019-07-11 stsp const struct got_error *err;
6517 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6518 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6519 69844fba 2019-07-11 stsp
6520 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6521 69844fba 2019-07-11 stsp if (err)
6522 69844fba 2019-07-11 stsp goto done;
6523 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6524 69844fba 2019-07-11 stsp if (err)
6525 69844fba 2019-07-11 stsp goto done;
6526 69844fba 2019-07-11 stsp
6527 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6528 69844fba 2019-07-11 stsp if (err)
6529 69844fba 2019-07-11 stsp goto done;
6530 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6531 69844fba 2019-07-11 stsp if (err)
6532 69844fba 2019-07-11 stsp goto done;
6533 69844fba 2019-07-11 stsp
6534 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6535 69844fba 2019-07-11 stsp if (err)
6536 69844fba 2019-07-11 stsp goto done;
6537 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6538 69844fba 2019-07-11 stsp if (err)
6539 69844fba 2019-07-11 stsp goto done;
6540 69844fba 2019-07-11 stsp
6541 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6542 69844fba 2019-07-11 stsp if (err)
6543 69844fba 2019-07-11 stsp goto done;
6544 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6545 69844fba 2019-07-11 stsp if (err)
6546 69844fba 2019-07-11 stsp goto done;
6547 69844fba 2019-07-11 stsp
6548 69844fba 2019-07-11 stsp done:
6549 69844fba 2019-07-11 stsp free(tmp_branch_name);
6550 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6551 69844fba 2019-07-11 stsp free(branch_ref_name);
6552 69844fba 2019-07-11 stsp free(commit_ref_name);
6553 e600f124 2021-03-21 stsp return err;
6554 e600f124 2021-03-21 stsp }
6555 e600f124 2021-03-21 stsp
6556 e600f124 2021-03-21 stsp const struct got_error *
6557 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6558 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6559 e600f124 2021-03-21 stsp {
6560 e600f124 2021-03-21 stsp const struct got_error *err;
6561 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6562 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6563 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6564 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6565 e600f124 2021-03-21 stsp char *refname = NULL;
6566 e600f124 2021-03-21 stsp
6567 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6568 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6569 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6570 e600f124 2021-03-21 stsp branch_name += 11;
6571 e600f124 2021-03-21 stsp
6572 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6573 e600f124 2021-03-21 stsp if (err)
6574 e600f124 2021-03-21 stsp return err;
6575 e600f124 2021-03-21 stsp
6576 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6577 e600f124 2021-03-21 stsp new_id_str) == -1) {
6578 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6579 e600f124 2021-03-21 stsp goto done;
6580 e600f124 2021-03-21 stsp }
6581 e600f124 2021-03-21 stsp
6582 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6583 e600f124 2021-03-21 stsp if (err)
6584 e600f124 2021-03-21 stsp goto done;
6585 e600f124 2021-03-21 stsp
6586 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6587 e600f124 2021-03-21 stsp if (err)
6588 e600f124 2021-03-21 stsp goto done;
6589 e600f124 2021-03-21 stsp
6590 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6591 e600f124 2021-03-21 stsp done:
6592 e600f124 2021-03-21 stsp free(new_id_str);
6593 e600f124 2021-03-21 stsp free(refname);
6594 e600f124 2021-03-21 stsp free(old_commit_id);
6595 e600f124 2021-03-21 stsp if (ref)
6596 e600f124 2021-03-21 stsp got_ref_close(ref);
6597 69844fba 2019-07-11 stsp return err;
6598 69844fba 2019-07-11 stsp }
6599 69844fba 2019-07-11 stsp
6600 818c7501 2019-07-11 stsp const struct got_error *
6601 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6602 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6603 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6604 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6605 818c7501 2019-07-11 stsp {
6606 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6607 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6608 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6609 818c7501 2019-07-11 stsp
6610 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6611 818c7501 2019-07-11 stsp if (err)
6612 818c7501 2019-07-11 stsp return err;
6613 e600f124 2021-03-21 stsp
6614 e600f124 2021-03-21 stsp if (create_backup) {
6615 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6616 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6617 e600f124 2021-03-21 stsp if (err)
6618 e600f124 2021-03-21 stsp goto done;
6619 e600f124 2021-03-21 stsp }
6620 818c7501 2019-07-11 stsp
6621 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6622 818c7501 2019-07-11 stsp if (err)
6623 818c7501 2019-07-11 stsp goto done;
6624 818c7501 2019-07-11 stsp
6625 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6626 818c7501 2019-07-11 stsp if (err)
6627 818c7501 2019-07-11 stsp goto done;
6628 818c7501 2019-07-11 stsp
6629 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6630 818c7501 2019-07-11 stsp if (err)
6631 818c7501 2019-07-11 stsp goto done;
6632 818c7501 2019-07-11 stsp
6633 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6634 a615e0e7 2020-12-16 stsp if (err)
6635 a615e0e7 2020-12-16 stsp goto done;
6636 a615e0e7 2020-12-16 stsp
6637 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6638 a615e0e7 2020-12-16 stsp if (err)
6639 a615e0e7 2020-12-16 stsp goto done;
6640 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6641 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6642 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6643 a615e0e7 2020-12-16 stsp err = sync_err;
6644 818c7501 2019-07-11 stsp done:
6645 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6646 a615e0e7 2020-12-16 stsp free(fileindex_path);
6647 818c7501 2019-07-11 stsp free(new_head_commit_id);
6648 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6649 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6650 818c7501 2019-07-11 stsp err = unlockerr;
6651 818c7501 2019-07-11 stsp return err;
6652 818c7501 2019-07-11 stsp }
6653 818c7501 2019-07-11 stsp
6654 818c7501 2019-07-11 stsp const struct got_error *
6655 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6656 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6657 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6658 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6659 818c7501 2019-07-11 stsp {
6660 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6661 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6662 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6663 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6664 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6665 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6666 818c7501 2019-07-11 stsp
6667 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6668 818c7501 2019-07-11 stsp if (err)
6669 818c7501 2019-07-11 stsp return err;
6670 818c7501 2019-07-11 stsp
6671 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6672 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6673 818c7501 2019-07-11 stsp if (err)
6674 818c7501 2019-07-11 stsp goto done;
6675 818c7501 2019-07-11 stsp
6676 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6677 818c7501 2019-07-11 stsp if (err)
6678 818c7501 2019-07-11 stsp goto done;
6679 818c7501 2019-07-11 stsp
6680 818c7501 2019-07-11 stsp /*
6681 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6682 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6683 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6684 818c7501 2019-07-11 stsp */
6685 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6686 818c7501 2019-07-11 stsp if (err)
6687 818c7501 2019-07-11 stsp goto done;
6688 818c7501 2019-07-11 stsp
6689 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6690 818c7501 2019-07-11 stsp if (err)
6691 818c7501 2019-07-11 stsp goto done;
6692 818c7501 2019-07-11 stsp
6693 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6694 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6695 ca355955 2019-07-12 stsp if (err)
6696 ca355955 2019-07-12 stsp goto done;
6697 ca355955 2019-07-12 stsp
6698 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6699 ca355955 2019-07-12 stsp if (err)
6700 ca355955 2019-07-12 stsp goto done;
6701 ca355955 2019-07-12 stsp
6702 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6703 818c7501 2019-07-11 stsp if (err)
6704 818c7501 2019-07-11 stsp goto done;
6705 818c7501 2019-07-11 stsp
6706 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6707 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6708 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6709 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6710 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6711 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6712 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6713 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
6714 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6715 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
6716 55bd499d 2019-07-12 stsp if (err)
6717 1f1abb7e 2019-08-08 stsp goto sync;
6718 55bd499d 2019-07-12 stsp
6719 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6720 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6721 ca355955 2019-07-12 stsp sync:
6722 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6723 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6724 ca355955 2019-07-12 stsp err = sync_err;
6725 818c7501 2019-07-11 stsp done:
6726 818c7501 2019-07-11 stsp got_ref_close(resolved);
6727 a3a2faf2 2019-07-12 stsp free(tree_id);
6728 818c7501 2019-07-11 stsp free(commit_id);
6729 0ebf8283 2019-07-24 stsp if (fileindex)
6730 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6731 0ebf8283 2019-07-24 stsp free(fileindex_path);
6732 0ebf8283 2019-07-24 stsp
6733 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6734 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6735 0ebf8283 2019-07-24 stsp err = unlockerr;
6736 0ebf8283 2019-07-24 stsp return err;
6737 0ebf8283 2019-07-24 stsp }
6738 0ebf8283 2019-07-24 stsp
6739 0ebf8283 2019-07-24 stsp const struct got_error *
6740 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6741 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6742 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6743 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6744 0ebf8283 2019-07-24 stsp {
6745 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6746 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6747 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6748 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6749 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6750 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6751 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6752 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6753 0ebf8283 2019-07-24 stsp
6754 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6755 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6756 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6757 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6760 0ebf8283 2019-07-24 stsp if (err)
6761 0ebf8283 2019-07-24 stsp return err;
6762 0ebf8283 2019-07-24 stsp
6763 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6764 0ebf8283 2019-07-24 stsp if (err)
6765 0ebf8283 2019-07-24 stsp goto done;
6766 0ebf8283 2019-07-24 stsp
6767 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6768 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6769 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6770 0ebf8283 2019-07-24 stsp &ok_arg);
6771 0ebf8283 2019-07-24 stsp if (err)
6772 0ebf8283 2019-07-24 stsp goto done;
6773 0ebf8283 2019-07-24 stsp
6774 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6775 0ebf8283 2019-07-24 stsp if (err)
6776 0ebf8283 2019-07-24 stsp goto done;
6777 0ebf8283 2019-07-24 stsp
6778 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6779 0ebf8283 2019-07-24 stsp if (err)
6780 0ebf8283 2019-07-24 stsp goto done;
6781 0ebf8283 2019-07-24 stsp
6782 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6783 0ebf8283 2019-07-24 stsp worktree);
6784 0ebf8283 2019-07-24 stsp if (err)
6785 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp
6787 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6788 0ebf8283 2019-07-24 stsp 0);
6789 0ebf8283 2019-07-24 stsp if (err)
6790 0ebf8283 2019-07-24 stsp goto done;
6791 0ebf8283 2019-07-24 stsp
6792 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6793 0ebf8283 2019-07-24 stsp if (err)
6794 0ebf8283 2019-07-24 stsp goto done;
6795 0ebf8283 2019-07-24 stsp
6796 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6797 0ebf8283 2019-07-24 stsp if (err)
6798 0ebf8283 2019-07-24 stsp goto done;
6799 0ebf8283 2019-07-24 stsp
6800 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6801 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6802 0ebf8283 2019-07-24 stsp if (err)
6803 0ebf8283 2019-07-24 stsp goto done;
6804 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6805 0ebf8283 2019-07-24 stsp if (err)
6806 0ebf8283 2019-07-24 stsp goto done;
6807 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6808 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6809 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6810 0ebf8283 2019-07-24 stsp goto done;
6811 0ebf8283 2019-07-24 stsp }
6812 0ebf8283 2019-07-24 stsp
6813 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6814 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6815 0ebf8283 2019-07-24 stsp if (err)
6816 0ebf8283 2019-07-24 stsp goto done;
6817 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6818 0ebf8283 2019-07-24 stsp if (err)
6819 0ebf8283 2019-07-24 stsp goto done;
6820 0ebf8283 2019-07-24 stsp
6821 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6822 0ebf8283 2019-07-24 stsp if (err)
6823 0ebf8283 2019-07-24 stsp goto done;
6824 0ebf8283 2019-07-24 stsp done:
6825 0ebf8283 2019-07-24 stsp free(fileindex_path);
6826 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6827 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6828 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6829 0ebf8283 2019-07-24 stsp if (wt_branch)
6830 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6831 0ebf8283 2019-07-24 stsp if (err) {
6832 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6833 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6834 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6835 0ebf8283 2019-07-24 stsp }
6836 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6837 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6838 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6839 0ebf8283 2019-07-24 stsp }
6840 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6841 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6842 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6843 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6844 3e3a69f1 2019-07-25 stsp }
6845 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6846 0ebf8283 2019-07-24 stsp }
6847 0ebf8283 2019-07-24 stsp return err;
6848 0ebf8283 2019-07-24 stsp }
6849 0ebf8283 2019-07-24 stsp
6850 0ebf8283 2019-07-24 stsp const struct got_error *
6851 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6852 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6853 0ebf8283 2019-07-24 stsp {
6854 3e3a69f1 2019-07-25 stsp if (fileindex)
6855 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6856 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6857 0ebf8283 2019-07-24 stsp }
6858 0ebf8283 2019-07-24 stsp
6859 0ebf8283 2019-07-24 stsp const struct got_error *
6860 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6861 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6862 0ebf8283 2019-07-24 stsp {
6863 0ebf8283 2019-07-24 stsp const struct got_error *err;
6864 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
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 return err;
6869 0ebf8283 2019-07-24 stsp
6870 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6871 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6872 0ebf8283 2019-07-24 stsp return NULL;
6873 0ebf8283 2019-07-24 stsp }
6874 0ebf8283 2019-07-24 stsp
6875 0ebf8283 2019-07-24 stsp const struct got_error *
6876 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6877 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6878 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6879 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6880 0ebf8283 2019-07-24 stsp {
6881 0ebf8283 2019-07-24 stsp const struct got_error *err;
6882 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6883 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6884 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6885 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6886 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6887 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6888 0ebf8283 2019-07-24 stsp
6889 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6890 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6891 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6892 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6893 0ebf8283 2019-07-24 stsp
6894 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6895 3e3a69f1 2019-07-25 stsp if (err)
6896 3e3a69f1 2019-07-25 stsp return err;
6897 3e3a69f1 2019-07-25 stsp
6898 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6899 3e3a69f1 2019-07-25 stsp if (err)
6900 f032f1f7 2019-08-04 stsp goto done;
6901 f032f1f7 2019-08-04 stsp
6902 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6903 f032f1f7 2019-08-04 stsp &have_staged_files);
6904 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6905 f032f1f7 2019-08-04 stsp goto done;
6906 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6907 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6908 3e3a69f1 2019-07-25 stsp goto done;
6909 f032f1f7 2019-08-04 stsp }
6910 3e3a69f1 2019-07-25 stsp
6911 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6912 0ebf8283 2019-07-24 stsp if (err)
6913 f032f1f7 2019-08-04 stsp goto done;
6914 0ebf8283 2019-07-24 stsp
6915 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6916 0ebf8283 2019-07-24 stsp if (err)
6917 0ebf8283 2019-07-24 stsp goto done;
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6920 0ebf8283 2019-07-24 stsp if (err)
6921 0ebf8283 2019-07-24 stsp goto done;
6922 0ebf8283 2019-07-24 stsp
6923 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6924 0ebf8283 2019-07-24 stsp worktree);
6925 0ebf8283 2019-07-24 stsp if (err)
6926 0ebf8283 2019-07-24 stsp goto done;
6927 0ebf8283 2019-07-24 stsp
6928 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6929 0ebf8283 2019-07-24 stsp if (err)
6930 0ebf8283 2019-07-24 stsp goto done;
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6933 0ebf8283 2019-07-24 stsp if (err)
6934 0ebf8283 2019-07-24 stsp goto done;
6935 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6936 0ebf8283 2019-07-24 stsp if (err)
6937 0ebf8283 2019-07-24 stsp goto done;
6938 0ebf8283 2019-07-24 stsp
6939 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6940 0ebf8283 2019-07-24 stsp if (err)
6941 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6943 0ebf8283 2019-07-24 stsp if (err)
6944 0ebf8283 2019-07-24 stsp goto done;
6945 0ebf8283 2019-07-24 stsp
6946 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6947 0ebf8283 2019-07-24 stsp if (err)
6948 0ebf8283 2019-07-24 stsp goto done;
6949 0ebf8283 2019-07-24 stsp done:
6950 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6951 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6952 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6953 0ebf8283 2019-07-24 stsp if (commit_ref)
6954 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6955 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6956 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6957 0ebf8283 2019-07-24 stsp if (err) {
6958 0ebf8283 2019-07-24 stsp free(*commit_id);
6959 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6960 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6961 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6962 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6963 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6964 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6965 0ebf8283 2019-07-24 stsp }
6966 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6967 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6968 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6969 3e3a69f1 2019-07-25 stsp }
6970 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6971 0ebf8283 2019-07-24 stsp }
6972 0ebf8283 2019-07-24 stsp return err;
6973 0ebf8283 2019-07-24 stsp }
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp static const struct got_error *
6976 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6977 0ebf8283 2019-07-24 stsp {
6978 0ebf8283 2019-07-24 stsp const struct got_error *err;
6979 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6980 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6981 0ebf8283 2019-07-24 stsp
6982 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6983 0ebf8283 2019-07-24 stsp if (err)
6984 0ebf8283 2019-07-24 stsp goto done;
6985 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6986 0ebf8283 2019-07-24 stsp if (err)
6987 0ebf8283 2019-07-24 stsp goto done;
6988 0ebf8283 2019-07-24 stsp
6989 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6990 0ebf8283 2019-07-24 stsp worktree);
6991 0ebf8283 2019-07-24 stsp if (err)
6992 0ebf8283 2019-07-24 stsp goto done;
6993 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6994 0ebf8283 2019-07-24 stsp if (err)
6995 0ebf8283 2019-07-24 stsp goto done;
6996 0ebf8283 2019-07-24 stsp
6997 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6998 0ebf8283 2019-07-24 stsp if (err)
6999 0ebf8283 2019-07-24 stsp goto done;
7000 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7001 0ebf8283 2019-07-24 stsp if (err)
7002 0ebf8283 2019-07-24 stsp goto done;
7003 0ebf8283 2019-07-24 stsp
7004 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7005 0ebf8283 2019-07-24 stsp if (err)
7006 0ebf8283 2019-07-24 stsp goto done;
7007 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7008 0ebf8283 2019-07-24 stsp if (err)
7009 0ebf8283 2019-07-24 stsp goto done;
7010 0ebf8283 2019-07-24 stsp done:
7011 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7012 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7013 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7014 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7015 0ebf8283 2019-07-24 stsp return err;
7016 0ebf8283 2019-07-24 stsp }
7017 0ebf8283 2019-07-24 stsp
7018 0ebf8283 2019-07-24 stsp const struct got_error *
7019 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7020 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7021 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7022 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7023 0ebf8283 2019-07-24 stsp {
7024 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7025 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7026 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7027 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7028 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7031 0ebf8283 2019-07-24 stsp if (err)
7032 0ebf8283 2019-07-24 stsp return err;
7033 0ebf8283 2019-07-24 stsp
7034 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7035 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7036 0ebf8283 2019-07-24 stsp if (err)
7037 0ebf8283 2019-07-24 stsp goto done;
7038 0ebf8283 2019-07-24 stsp
7039 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7040 0ebf8283 2019-07-24 stsp if (err)
7041 0ebf8283 2019-07-24 stsp goto done;
7042 0ebf8283 2019-07-24 stsp
7043 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7044 0ebf8283 2019-07-24 stsp if (err)
7045 0ebf8283 2019-07-24 stsp goto done;
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7048 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7049 0ebf8283 2019-07-24 stsp if (err)
7050 0ebf8283 2019-07-24 stsp goto done;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7053 0ebf8283 2019-07-24 stsp if (err)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp
7056 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7057 0ebf8283 2019-07-24 stsp if (err)
7058 0ebf8283 2019-07-24 stsp goto done;
7059 0ebf8283 2019-07-24 stsp
7060 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7061 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7062 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7063 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7064 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7065 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7066 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7067 10604dce 2021-09-24 thomas rfa.unlink_added_files = 0;
7068 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7069 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7070 0ebf8283 2019-07-24 stsp if (err)
7071 1f1abb7e 2019-08-08 stsp goto sync;
7072 0ebf8283 2019-07-24 stsp
7073 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7074 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7075 0ebf8283 2019-07-24 stsp sync:
7076 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7077 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7078 0ebf8283 2019-07-24 stsp err = sync_err;
7079 0ebf8283 2019-07-24 stsp done:
7080 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7081 0ebf8283 2019-07-24 stsp free(tree_id);
7082 818c7501 2019-07-11 stsp free(fileindex_path);
7083 818c7501 2019-07-11 stsp
7084 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7085 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7086 818c7501 2019-07-11 stsp err = unlockerr;
7087 818c7501 2019-07-11 stsp return err;
7088 818c7501 2019-07-11 stsp }
7089 0ebf8283 2019-07-24 stsp
7090 0ebf8283 2019-07-24 stsp const struct got_error *
7091 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7092 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7093 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7094 0ebf8283 2019-07-24 stsp {
7095 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7096 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7097 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7098 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7099 0ebf8283 2019-07-24 stsp
7100 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7101 0ebf8283 2019-07-24 stsp if (err)
7102 0ebf8283 2019-07-24 stsp return err;
7103 0ebf8283 2019-07-24 stsp
7104 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7105 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7106 e600f124 2021-03-21 stsp if (err)
7107 e600f124 2021-03-21 stsp goto done;
7108 e600f124 2021-03-21 stsp
7109 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7110 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7111 0ebf8283 2019-07-24 stsp if (err)
7112 0ebf8283 2019-07-24 stsp goto done;
7113 0ebf8283 2019-07-24 stsp
7114 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7115 0ebf8283 2019-07-24 stsp if (err)
7116 0ebf8283 2019-07-24 stsp goto done;
7117 0ebf8283 2019-07-24 stsp
7118 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7119 0ebf8283 2019-07-24 stsp if (err)
7120 0ebf8283 2019-07-24 stsp goto done;
7121 0ebf8283 2019-07-24 stsp
7122 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7123 0ebf8283 2019-07-24 stsp if (err)
7124 0ebf8283 2019-07-24 stsp goto done;
7125 0ebf8283 2019-07-24 stsp
7126 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7127 a615e0e7 2020-12-16 stsp if (err)
7128 a615e0e7 2020-12-16 stsp goto done;
7129 a615e0e7 2020-12-16 stsp
7130 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7131 a615e0e7 2020-12-16 stsp if (err)
7132 a615e0e7 2020-12-16 stsp goto done;
7133 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7134 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7135 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7136 a615e0e7 2020-12-16 stsp err = sync_err;
7137 0ebf8283 2019-07-24 stsp done:
7138 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7139 a615e0e7 2020-12-16 stsp free(fileindex_path);
7140 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7141 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7142 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7143 0ebf8283 2019-07-24 stsp err = unlockerr;
7144 0ebf8283 2019-07-24 stsp return err;
7145 0ebf8283 2019-07-24 stsp }
7146 0ebf8283 2019-07-24 stsp
7147 0ebf8283 2019-07-24 stsp const struct got_error *
7148 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7149 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7150 0ebf8283 2019-07-24 stsp {
7151 0ebf8283 2019-07-24 stsp const struct got_error *err;
7152 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7153 0ebf8283 2019-07-24 stsp
7154 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7155 0ebf8283 2019-07-24 stsp if (err)
7156 0ebf8283 2019-07-24 stsp return err;
7157 0ebf8283 2019-07-24 stsp
7158 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7159 0ebf8283 2019-07-24 stsp if (err)
7160 0ebf8283 2019-07-24 stsp goto done;
7161 0ebf8283 2019-07-24 stsp
7162 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7163 0ebf8283 2019-07-24 stsp done:
7164 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7165 2822a352 2019-10-15 stsp return err;
7166 2822a352 2019-10-15 stsp }
7167 2822a352 2019-10-15 stsp
7168 2822a352 2019-10-15 stsp const struct got_error *
7169 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7170 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7171 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7172 2822a352 2019-10-15 stsp struct got_repository *repo)
7173 2822a352 2019-10-15 stsp {
7174 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7175 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7176 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7177 2822a352 2019-10-15 stsp
7178 2822a352 2019-10-15 stsp *fileindex = NULL;
7179 2822a352 2019-10-15 stsp *branch_ref = NULL;
7180 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7181 2822a352 2019-10-15 stsp
7182 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7183 2822a352 2019-10-15 stsp if (err)
7184 2822a352 2019-10-15 stsp return err;
7185 2822a352 2019-10-15 stsp
7186 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7187 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7188 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7189 2822a352 2019-10-15 stsp "update -b or different branch name required");
7190 2822a352 2019-10-15 stsp goto done;
7191 2822a352 2019-10-15 stsp }
7192 2822a352 2019-10-15 stsp
7193 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7194 2822a352 2019-10-15 stsp if (err)
7195 2822a352 2019-10-15 stsp goto done;
7196 2822a352 2019-10-15 stsp
7197 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7198 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7199 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7200 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7201 2822a352 2019-10-15 stsp &ok_arg);
7202 2822a352 2019-10-15 stsp if (err)
7203 2822a352 2019-10-15 stsp goto done;
7204 2822a352 2019-10-15 stsp
7205 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7206 2822a352 2019-10-15 stsp if (err)
7207 2822a352 2019-10-15 stsp goto done;
7208 2822a352 2019-10-15 stsp
7209 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7210 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7211 2822a352 2019-10-15 stsp done:
7212 2822a352 2019-10-15 stsp if (err) {
7213 2822a352 2019-10-15 stsp if (*branch_ref) {
7214 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7215 2822a352 2019-10-15 stsp *branch_ref = NULL;
7216 2822a352 2019-10-15 stsp }
7217 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7218 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7219 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7220 2822a352 2019-10-15 stsp }
7221 2822a352 2019-10-15 stsp if (*fileindex) {
7222 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7223 2822a352 2019-10-15 stsp *fileindex = NULL;
7224 2822a352 2019-10-15 stsp }
7225 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7226 2822a352 2019-10-15 stsp }
7227 0cb83759 2019-08-03 stsp return err;
7228 0cb83759 2019-08-03 stsp }
7229 0cb83759 2019-08-03 stsp
7230 2822a352 2019-10-15 stsp const struct got_error *
7231 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7232 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7233 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7234 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7235 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7236 2822a352 2019-10-15 stsp {
7237 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7238 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7239 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7240 2822a352 2019-10-15 stsp
7241 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7242 2822a352 2019-10-15 stsp if (err)
7243 2822a352 2019-10-15 stsp goto done;
7244 2822a352 2019-10-15 stsp
7245 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7246 2822a352 2019-10-15 stsp if (err)
7247 2822a352 2019-10-15 stsp goto done;
7248 2822a352 2019-10-15 stsp
7249 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7250 2822a352 2019-10-15 stsp worktree->path_prefix);
7251 2822a352 2019-10-15 stsp if (err)
7252 2822a352 2019-10-15 stsp goto done;
7253 2822a352 2019-10-15 stsp
7254 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7255 2822a352 2019-10-15 stsp if (err)
7256 2822a352 2019-10-15 stsp goto done;
7257 2822a352 2019-10-15 stsp
7258 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7259 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7260 2822a352 2019-10-15 stsp if (err)
7261 2822a352 2019-10-15 stsp goto sync;
7262 2822a352 2019-10-15 stsp
7263 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7264 2822a352 2019-10-15 stsp if (err)
7265 2822a352 2019-10-15 stsp goto sync;
7266 2822a352 2019-10-15 stsp
7267 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7268 6e210706 2021-01-22 stsp if (err)
7269 6e210706 2021-01-22 stsp goto sync;
7270 6e210706 2021-01-22 stsp
7271 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7272 2822a352 2019-10-15 stsp sync:
7273 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7274 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7275 2822a352 2019-10-15 stsp err = sync_err;
7276 2822a352 2019-10-15 stsp
7277 2822a352 2019-10-15 stsp done:
7278 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7279 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7280 2822a352 2019-10-15 stsp err = unlockerr;
7281 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7282 2822a352 2019-10-15 stsp
7283 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7284 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7285 2822a352 2019-10-15 stsp err = unlockerr;
7286 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7287 2822a352 2019-10-15 stsp
7288 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7289 2822a352 2019-10-15 stsp free(fileindex_path);
7290 2822a352 2019-10-15 stsp free(tree_id);
7291 2822a352 2019-10-15 stsp
7292 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7293 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7294 2822a352 2019-10-15 stsp err = unlockerr;
7295 2822a352 2019-10-15 stsp return err;
7296 2822a352 2019-10-15 stsp }
7297 2822a352 2019-10-15 stsp
7298 2822a352 2019-10-15 stsp const struct got_error *
7299 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7300 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7301 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7302 2822a352 2019-10-15 stsp {
7303 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7304 8b692cd0 2019-10-21 stsp
7305 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7306 8b692cd0 2019-10-21 stsp
7307 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7308 8b692cd0 2019-10-21 stsp
7309 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7310 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7311 8b692cd0 2019-10-21 stsp err = unlockerr;
7312 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7313 8b692cd0 2019-10-21 stsp
7314 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7315 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7316 8b692cd0 2019-10-21 stsp err = unlockerr;
7317 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7318 10604dce 2021-09-24 thomas
7319 10604dce 2021-09-24 thomas return err;
7320 10604dce 2021-09-24 thomas }
7321 10604dce 2021-09-24 thomas
7322 10604dce 2021-09-24 thomas const struct got_error *
7323 10604dce 2021-09-24 thomas got_worktree_merge_postpone(struct got_worktree *worktree,
7324 10604dce 2021-09-24 thomas struct got_fileindex *fileindex)
7325 10604dce 2021-09-24 thomas {
7326 10604dce 2021-09-24 thomas const struct got_error *err, *sync_err;
7327 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7328 10604dce 2021-09-24 thomas
7329 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7330 10604dce 2021-09-24 thomas if (err)
7331 10604dce 2021-09-24 thomas goto done;
7332 8b692cd0 2019-10-21 stsp
7333 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7334 10604dce 2021-09-24 thomas
7335 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_SH);
7336 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7337 10604dce 2021-09-24 thomas err = sync_err;
7338 10604dce 2021-09-24 thomas done:
7339 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7340 10604dce 2021-09-24 thomas free(fileindex_path);
7341 8b692cd0 2019-10-21 stsp return err;
7342 2822a352 2019-10-15 stsp }
7343 2822a352 2019-10-15 stsp
7344 10604dce 2021-09-24 thomas static const struct got_error *
7345 10604dce 2021-09-24 thomas delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7346 10604dce 2021-09-24 thomas {
7347 10604dce 2021-09-24 thomas const struct got_error *err;
7348 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7349 10604dce 2021-09-24 thomas
7350 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7351 10604dce 2021-09-24 thomas if (err)
7352 10604dce 2021-09-24 thomas goto done;
7353 10604dce 2021-09-24 thomas err = delete_ref(branch_refname, repo);
7354 10604dce 2021-09-24 thomas if (err)
7355 10604dce 2021-09-24 thomas goto done;
7356 10604dce 2021-09-24 thomas
7357 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7358 10604dce 2021-09-24 thomas if (err)
7359 10604dce 2021-09-24 thomas goto done;
7360 10604dce 2021-09-24 thomas err = delete_ref(commit_refname, repo);
7361 10604dce 2021-09-24 thomas if (err)
7362 10604dce 2021-09-24 thomas goto done;
7363 10604dce 2021-09-24 thomas
7364 10604dce 2021-09-24 thomas done:
7365 10604dce 2021-09-24 thomas free(branch_refname);
7366 10604dce 2021-09-24 thomas free(commit_refname);
7367 10604dce 2021-09-24 thomas return err;
7368 10604dce 2021-09-24 thomas }
7369 10604dce 2021-09-24 thomas
7370 10604dce 2021-09-24 thomas struct merge_commit_msg_arg {
7371 10604dce 2021-09-24 thomas struct got_worktree *worktree;
7372 10604dce 2021-09-24 thomas const char *branch_name;
7373 10604dce 2021-09-24 thomas };
7374 10604dce 2021-09-24 thomas
7375 10604dce 2021-09-24 thomas static const struct got_error *
7376 10604dce 2021-09-24 thomas merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7377 10604dce 2021-09-24 thomas void *arg)
7378 10604dce 2021-09-24 thomas {
7379 10604dce 2021-09-24 thomas struct merge_commit_msg_arg *a = arg;
7380 10604dce 2021-09-24 thomas
7381 10604dce 2021-09-24 thomas if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7382 10604dce 2021-09-24 thomas got_worktree_get_head_ref_name(a->worktree)) == -1)
7383 10604dce 2021-09-24 thomas return got_error_from_errno("asprintf");
7384 10604dce 2021-09-24 thomas
7385 10604dce 2021-09-24 thomas return NULL;
7386 10604dce 2021-09-24 thomas }
7387 10604dce 2021-09-24 thomas
7388 10604dce 2021-09-24 thomas
7389 10604dce 2021-09-24 thomas const struct got_error *
7390 10604dce 2021-09-24 thomas got_worktree_merge_branch(struct got_worktree *worktree,
7391 10604dce 2021-09-24 thomas struct got_fileindex *fileindex,
7392 10604dce 2021-09-24 thomas struct got_object_id *yca_commit_id,
7393 10604dce 2021-09-24 thomas struct got_object_id *branch_tip,
7394 10604dce 2021-09-24 thomas struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7395 10604dce 2021-09-24 thomas void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7396 10604dce 2021-09-24 thomas {
7397 10604dce 2021-09-24 thomas const struct got_error *err;
7398 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7399 10604dce 2021-09-24 thomas
7400 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7401 10604dce 2021-09-24 thomas if (err)
7402 10604dce 2021-09-24 thomas goto done;
7403 10604dce 2021-09-24 thomas
7404 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7405 10604dce 2021-09-24 thomas worktree);
7406 10604dce 2021-09-24 thomas if (err)
7407 10604dce 2021-09-24 thomas goto done;
7408 10604dce 2021-09-24 thomas
7409 10604dce 2021-09-24 thomas err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7410 10604dce 2021-09-24 thomas branch_tip, repo, progress_cb, progress_arg,
7411 10604dce 2021-09-24 thomas cancel_cb, cancel_arg);
7412 10604dce 2021-09-24 thomas done:
7413 10604dce 2021-09-24 thomas free(fileindex_path);
7414 10604dce 2021-09-24 thomas return err;
7415 10604dce 2021-09-24 thomas }
7416 10604dce 2021-09-24 thomas
7417 10604dce 2021-09-24 thomas const struct got_error *
7418 10604dce 2021-09-24 thomas got_worktree_merge_commit(struct got_object_id **new_commit_id,
7419 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_fileindex *fileindex,
7420 10604dce 2021-09-24 thomas const char *author, const char *committer, int allow_bad_symlinks,
7421 10604dce 2021-09-24 thomas struct got_object_id *branch_tip, const char *branch_name,
7422 ae1e948a 2021-09-28 thomas struct got_repository *repo,
7423 ae1e948a 2021-09-28 thomas got_worktree_status_cb status_cb, void *status_arg)
7424 ae1e948a 2021-09-28 thomas
7425 10604dce 2021-09-24 thomas {
7426 10604dce 2021-09-24 thomas const struct got_error *err = NULL, *sync_err;
7427 10604dce 2021-09-24 thomas struct got_pathlist_head commitable_paths;
7428 10604dce 2021-09-24 thomas struct collect_commitables_arg cc_arg;
7429 10604dce 2021-09-24 thomas struct got_pathlist_entry *pe;
7430 10604dce 2021-09-24 thomas struct got_reference *head_ref = NULL;
7431 10604dce 2021-09-24 thomas struct got_object_id *head_commit_id = NULL;
7432 10604dce 2021-09-24 thomas int have_staged_files = 0;
7433 10604dce 2021-09-24 thomas struct merge_commit_msg_arg mcm_arg;
7434 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7435 10604dce 2021-09-24 thomas
7436 10604dce 2021-09-24 thomas *new_commit_id = NULL;
7437 10604dce 2021-09-24 thomas
7438 10604dce 2021-09-24 thomas TAILQ_INIT(&commitable_paths);
7439 10604dce 2021-09-24 thomas
7440 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7441 10604dce 2021-09-24 thomas if (err)
7442 10604dce 2021-09-24 thomas goto done;
7443 10604dce 2021-09-24 thomas
7444 10604dce 2021-09-24 thomas err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7445 10604dce 2021-09-24 thomas if (err)
7446 10604dce 2021-09-24 thomas goto done;
7447 10604dce 2021-09-24 thomas
7448 10604dce 2021-09-24 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
7449 10604dce 2021-09-24 thomas if (err)
7450 10604dce 2021-09-24 thomas goto done;
7451 10604dce 2021-09-24 thomas
7452 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7453 10604dce 2021-09-24 thomas &have_staged_files);
7454 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7455 10604dce 2021-09-24 thomas goto done;
7456 10604dce 2021-09-24 thomas if (have_staged_files) {
7457 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7458 10604dce 2021-09-24 thomas goto done;
7459 10604dce 2021-09-24 thomas }
7460 10604dce 2021-09-24 thomas
7461 10604dce 2021-09-24 thomas cc_arg.commitable_paths = &commitable_paths;
7462 10604dce 2021-09-24 thomas cc_arg.worktree = worktree;
7463 10604dce 2021-09-24 thomas cc_arg.fileindex = fileindex;
7464 10604dce 2021-09-24 thomas cc_arg.repo = repo;
7465 10604dce 2021-09-24 thomas cc_arg.have_staged_files = have_staged_files;
7466 10604dce 2021-09-24 thomas cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7467 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7468 6092c299 2021-10-04 thomas collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7469 10604dce 2021-09-24 thomas if (err)
7470 10604dce 2021-09-24 thomas goto done;
7471 10604dce 2021-09-24 thomas
7472 10604dce 2021-09-24 thomas if (TAILQ_EMPTY(&commitable_paths)) {
7473 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7474 10604dce 2021-09-24 thomas "merge of %s cannot proceed", branch_name);
7475 10604dce 2021-09-24 thomas goto done;
7476 10604dce 2021-09-24 thomas }
7477 10604dce 2021-09-24 thomas
7478 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7479 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7480 10604dce 2021-09-24 thomas const char *ct_path = ct->in_repo_path;
7481 10604dce 2021-09-24 thomas
7482 10604dce 2021-09-24 thomas while (ct_path[0] == '/')
7483 10604dce 2021-09-24 thomas ct_path++;
7484 10604dce 2021-09-24 thomas err = check_out_of_date(ct_path, ct->status,
7485 10604dce 2021-09-24 thomas ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7486 10604dce 2021-09-24 thomas head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7487 10604dce 2021-09-24 thomas if (err)
7488 10604dce 2021-09-24 thomas goto done;
7489 10604dce 2021-09-24 thomas
7490 10604dce 2021-09-24 thomas }
7491 10604dce 2021-09-24 thomas
7492 10604dce 2021-09-24 thomas mcm_arg.worktree = worktree;
7493 10604dce 2021-09-24 thomas mcm_arg.branch_name = branch_name;
7494 10604dce 2021-09-24 thomas err = commit_worktree(new_commit_id, &commitable_paths,
7495 10604dce 2021-09-24 thomas head_commit_id, branch_tip, worktree, author, committer,
7496 ae1e948a 2021-09-28 thomas merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7497 10604dce 2021-09-24 thomas if (err)
7498 10604dce 2021-09-24 thomas goto done;
7499 10604dce 2021-09-24 thomas
7500 10604dce 2021-09-24 thomas err = update_fileindex_after_commit(worktree, &commitable_paths,
7501 10604dce 2021-09-24 thomas *new_commit_id, fileindex, have_staged_files);
7502 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7503 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7504 10604dce 2021-09-24 thomas err = sync_err;
7505 10604dce 2021-09-24 thomas done:
7506 10604dce 2021-09-24 thomas TAILQ_FOREACH(pe, &commitable_paths, entry) {
7507 10604dce 2021-09-24 thomas struct got_commitable *ct = pe->data;
7508 10604dce 2021-09-24 thomas free_commitable(ct);
7509 10604dce 2021-09-24 thomas }
7510 10604dce 2021-09-24 thomas got_pathlist_free(&commitable_paths);
7511 10604dce 2021-09-24 thomas free(fileindex_path);
7512 10604dce 2021-09-24 thomas return err;
7513 10604dce 2021-09-24 thomas }
7514 10604dce 2021-09-24 thomas
7515 10604dce 2021-09-24 thomas const struct got_error *
7516 10604dce 2021-09-24 thomas got_worktree_merge_complete(struct got_worktree *worktree,
7517 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo)
7518 10604dce 2021-09-24 thomas {
7519 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7520 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7521 10604dce 2021-09-24 thomas
7522 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7523 10604dce 2021-09-24 thomas if (err)
7524 10604dce 2021-09-24 thomas goto done;
7525 10604dce 2021-09-24 thomas
7526 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7527 10604dce 2021-09-24 thomas if (err)
7528 10604dce 2021-09-24 thomas goto done;
7529 10604dce 2021-09-24 thomas err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7530 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7531 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7532 10604dce 2021-09-24 thomas err = sync_err;
7533 10604dce 2021-09-24 thomas done:
7534 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7535 10604dce 2021-09-24 thomas free(fileindex_path);
7536 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7537 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7538 10604dce 2021-09-24 thomas err = unlockerr;
7539 10604dce 2021-09-24 thomas return err;
7540 10604dce 2021-09-24 thomas }
7541 10604dce 2021-09-24 thomas
7542 10604dce 2021-09-24 thomas const struct got_error *
7543 10604dce 2021-09-24 thomas got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7544 10604dce 2021-09-24 thomas struct got_repository *repo)
7545 10604dce 2021-09-24 thomas {
7546 10604dce 2021-09-24 thomas const struct got_error *err;
7547 10604dce 2021-09-24 thomas char *branch_refname = NULL;
7548 10604dce 2021-09-24 thomas struct got_reference *branch_ref = NULL;
7549 10604dce 2021-09-24 thomas
7550 10604dce 2021-09-24 thomas *in_progress = 0;
7551 10604dce 2021-09-24 thomas
7552 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7553 10604dce 2021-09-24 thomas if (err)
7554 10604dce 2021-09-24 thomas return err;
7555 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7556 dfe86d1f 2021-09-24 thomas free(branch_refname);
7557 10604dce 2021-09-24 thomas if (err) {
7558 10604dce 2021-09-24 thomas if (err->code != GOT_ERR_NOT_REF)
7559 10604dce 2021-09-24 thomas return err;
7560 10604dce 2021-09-24 thomas } else
7561 10604dce 2021-09-24 thomas *in_progress = 1;
7562 10604dce 2021-09-24 thomas
7563 10604dce 2021-09-24 thomas return NULL;
7564 10604dce 2021-09-24 thomas }
7565 10604dce 2021-09-24 thomas
7566 10604dce 2021-09-24 thomas const struct got_error *got_worktree_merge_prepare(
7567 10604dce 2021-09-24 thomas struct got_fileindex **fileindex, struct got_worktree *worktree,
7568 10604dce 2021-09-24 thomas struct got_reference *branch, struct got_repository *repo)
7569 10604dce 2021-09-24 thomas {
7570 10604dce 2021-09-24 thomas const struct got_error *err = NULL;
7571 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7572 10604dce 2021-09-24 thomas char *branch_refname = NULL, *commit_refname = NULL;
7573 10604dce 2021-09-24 thomas struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7574 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL;
7575 10604dce 2021-09-24 thomas struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7576 10604dce 2021-09-24 thomas struct check_rebase_ok_arg ok_arg;
7577 10604dce 2021-09-24 thomas
7578 10604dce 2021-09-24 thomas *fileindex = NULL;
7579 10604dce 2021-09-24 thomas
7580 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7581 10604dce 2021-09-24 thomas if (err)
7582 10604dce 2021-09-24 thomas return err;
7583 10604dce 2021-09-24 thomas
7584 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7585 10604dce 2021-09-24 thomas if (err)
7586 10604dce 2021-09-24 thomas goto done;
7587 10604dce 2021-09-24 thomas
7588 10604dce 2021-09-24 thomas /* Preconditions are the same as for rebase. */
7589 10604dce 2021-09-24 thomas ok_arg.worktree = worktree;
7590 10604dce 2021-09-24 thomas ok_arg.repo = repo;
7591 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7592 10604dce 2021-09-24 thomas &ok_arg);
7593 10604dce 2021-09-24 thomas if (err)
7594 10604dce 2021-09-24 thomas goto done;
7595 10604dce 2021-09-24 thomas
7596 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7597 10604dce 2021-09-24 thomas if (err)
7598 10604dce 2021-09-24 thomas return err;
7599 10604dce 2021-09-24 thomas
7600 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7601 10604dce 2021-09-24 thomas if (err)
7602 10604dce 2021-09-24 thomas return err;
7603 10604dce 2021-09-24 thomas
7604 10604dce 2021-09-24 thomas err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7605 10604dce 2021-09-24 thomas 0);
7606 10604dce 2021-09-24 thomas if (err)
7607 10604dce 2021-09-24 thomas goto done;
7608 10604dce 2021-09-24 thomas
7609 10604dce 2021-09-24 thomas err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7610 10604dce 2021-09-24 thomas if (err)
7611 10604dce 2021-09-24 thomas goto done;
7612 10604dce 2021-09-24 thomas
7613 10604dce 2021-09-24 thomas if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7614 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7615 10604dce 2021-09-24 thomas goto done;
7616 10604dce 2021-09-24 thomas }
7617 10604dce 2021-09-24 thomas
7618 10604dce 2021-09-24 thomas err = got_ref_resolve(&branch_tip, repo, branch);
7619 10604dce 2021-09-24 thomas if (err)
7620 10604dce 2021-09-24 thomas goto done;
7621 10604dce 2021-09-24 thomas
7622 10604dce 2021-09-24 thomas err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7623 10604dce 2021-09-24 thomas if (err)
7624 10604dce 2021-09-24 thomas goto done;
7625 10604dce 2021-09-24 thomas err = got_ref_write(branch_ref, repo);
7626 10604dce 2021-09-24 thomas if (err)
7627 10604dce 2021-09-24 thomas goto done;
7628 10604dce 2021-09-24 thomas
7629 10604dce 2021-09-24 thomas err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7630 10604dce 2021-09-24 thomas if (err)
7631 10604dce 2021-09-24 thomas goto done;
7632 10604dce 2021-09-24 thomas err = got_ref_write(commit_ref, repo);
7633 10604dce 2021-09-24 thomas if (err)
7634 10604dce 2021-09-24 thomas goto done;
7635 10604dce 2021-09-24 thomas
7636 10604dce 2021-09-24 thomas done:
7637 10604dce 2021-09-24 thomas free(branch_refname);
7638 10604dce 2021-09-24 thomas free(commit_refname);
7639 10604dce 2021-09-24 thomas free(fileindex_path);
7640 10604dce 2021-09-24 thomas if (branch_ref)
7641 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7642 10604dce 2021-09-24 thomas if (commit_ref)
7643 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7644 10604dce 2021-09-24 thomas if (wt_branch)
7645 10604dce 2021-09-24 thomas got_ref_close(wt_branch);
7646 10604dce 2021-09-24 thomas free(wt_branch_tip);
7647 10604dce 2021-09-24 thomas if (err) {
7648 10604dce 2021-09-24 thomas if (*fileindex) {
7649 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7650 10604dce 2021-09-24 thomas *fileindex = NULL;
7651 10604dce 2021-09-24 thomas }
7652 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7653 10604dce 2021-09-24 thomas }
7654 10604dce 2021-09-24 thomas return err;
7655 10604dce 2021-09-24 thomas }
7656 10604dce 2021-09-24 thomas
7657 10604dce 2021-09-24 thomas const struct got_error *
7658 10604dce 2021-09-24 thomas got_worktree_merge_continue(char **branch_name,
7659 10604dce 2021-09-24 thomas struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7660 10604dce 2021-09-24 thomas struct got_worktree *worktree, struct got_repository *repo)
7661 10604dce 2021-09-24 thomas {
7662 10604dce 2021-09-24 thomas const struct got_error *err;
7663 10604dce 2021-09-24 thomas char *commit_refname = NULL, *branch_refname = NULL;
7664 10604dce 2021-09-24 thomas struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7665 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7666 10604dce 2021-09-24 thomas int have_staged_files = 0;
7667 10604dce 2021-09-24 thomas
7668 10604dce 2021-09-24 thomas *branch_name = NULL;
7669 10604dce 2021-09-24 thomas *branch_tip = NULL;
7670 10604dce 2021-09-24 thomas *fileindex = NULL;
7671 10604dce 2021-09-24 thomas
7672 10604dce 2021-09-24 thomas err = lock_worktree(worktree, LOCK_EX);
7673 10604dce 2021-09-24 thomas if (err)
7674 10604dce 2021-09-24 thomas return err;
7675 10604dce 2021-09-24 thomas
7676 10604dce 2021-09-24 thomas err = open_fileindex(fileindex, &fileindex_path, worktree);
7677 10604dce 2021-09-24 thomas if (err)
7678 10604dce 2021-09-24 thomas goto done;
7679 10604dce 2021-09-24 thomas
7680 10604dce 2021-09-24 thomas err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7681 10604dce 2021-09-24 thomas &have_staged_files);
7682 10604dce 2021-09-24 thomas if (err && err->code != GOT_ERR_CANCELLED)
7683 10604dce 2021-09-24 thomas goto done;
7684 10604dce 2021-09-24 thomas if (have_staged_files) {
7685 10604dce 2021-09-24 thomas err = got_error(GOT_ERR_STAGED_PATHS);
7686 10604dce 2021-09-24 thomas goto done;
7687 10604dce 2021-09-24 thomas }
7688 10604dce 2021-09-24 thomas
7689 10604dce 2021-09-24 thomas err = get_merge_branch_ref_name(&branch_refname, worktree);
7690 10604dce 2021-09-24 thomas if (err)
7691 10604dce 2021-09-24 thomas goto done;
7692 10604dce 2021-09-24 thomas
7693 10604dce 2021-09-24 thomas err = get_merge_commit_ref_name(&commit_refname, worktree);
7694 10604dce 2021-09-24 thomas if (err)
7695 10604dce 2021-09-24 thomas goto done;
7696 10604dce 2021-09-24 thomas
7697 10604dce 2021-09-24 thomas err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7698 10604dce 2021-09-24 thomas if (err)
7699 10604dce 2021-09-24 thomas goto done;
7700 10604dce 2021-09-24 thomas
7701 10604dce 2021-09-24 thomas if (!got_ref_is_symbolic(branch_ref)) {
7702 10604dce 2021-09-24 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7703 10604dce 2021-09-24 thomas "%s is not a symbolic reference",
7704 10604dce 2021-09-24 thomas got_ref_get_name(branch_ref));
7705 10604dce 2021-09-24 thomas goto done;
7706 10604dce 2021-09-24 thomas }
7707 10604dce 2021-09-24 thomas *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7708 10604dce 2021-09-24 thomas if (*branch_name == NULL) {
7709 10604dce 2021-09-24 thomas err = got_error_from_errno("strdup");
7710 10604dce 2021-09-24 thomas goto done;
7711 10604dce 2021-09-24 thomas }
7712 10604dce 2021-09-24 thomas
7713 10604dce 2021-09-24 thomas err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7714 10604dce 2021-09-24 thomas if (err)
7715 10604dce 2021-09-24 thomas goto done;
7716 10604dce 2021-09-24 thomas
7717 10604dce 2021-09-24 thomas err = got_ref_resolve(branch_tip, repo, commit_ref);
7718 10604dce 2021-09-24 thomas if (err)
7719 10604dce 2021-09-24 thomas goto done;
7720 10604dce 2021-09-24 thomas done:
7721 10604dce 2021-09-24 thomas free(commit_refname);
7722 10604dce 2021-09-24 thomas free(branch_refname);
7723 10604dce 2021-09-24 thomas free(fileindex_path);
7724 10604dce 2021-09-24 thomas if (commit_ref)
7725 10604dce 2021-09-24 thomas got_ref_close(commit_ref);
7726 10604dce 2021-09-24 thomas if (branch_ref)
7727 10604dce 2021-09-24 thomas got_ref_close(branch_ref);
7728 10604dce 2021-09-24 thomas if (err) {
7729 10604dce 2021-09-24 thomas if (*branch_name) {
7730 10604dce 2021-09-24 thomas free(*branch_name);
7731 10604dce 2021-09-24 thomas *branch_name = NULL;
7732 10604dce 2021-09-24 thomas }
7733 10604dce 2021-09-24 thomas free(*branch_tip);
7734 10604dce 2021-09-24 thomas *branch_tip = NULL;
7735 10604dce 2021-09-24 thomas if (*fileindex) {
7736 10604dce 2021-09-24 thomas got_fileindex_free(*fileindex);
7737 10604dce 2021-09-24 thomas *fileindex = NULL;
7738 10604dce 2021-09-24 thomas }
7739 10604dce 2021-09-24 thomas lock_worktree(worktree, LOCK_SH);
7740 10604dce 2021-09-24 thomas }
7741 10604dce 2021-09-24 thomas return err;
7742 10604dce 2021-09-24 thomas }
7743 10604dce 2021-09-24 thomas
7744 10604dce 2021-09-24 thomas const struct got_error *
7745 10604dce 2021-09-24 thomas got_worktree_merge_abort(struct got_worktree *worktree,
7746 10604dce 2021-09-24 thomas struct got_fileindex *fileindex, struct got_repository *repo,
7747 10604dce 2021-09-24 thomas got_worktree_checkout_cb progress_cb, void *progress_arg)
7748 10604dce 2021-09-24 thomas {
7749 10604dce 2021-09-24 thomas const struct got_error *err, *unlockerr, *sync_err;
7750 10604dce 2021-09-24 thomas struct got_object_id *commit_id = NULL;
7751 10604dce 2021-09-24 thomas char *fileindex_path = NULL;
7752 10604dce 2021-09-24 thomas struct revert_file_args rfa;
7753 10604dce 2021-09-24 thomas struct got_object_id *tree_id = NULL;
7754 10604dce 2021-09-24 thomas
7755 10604dce 2021-09-24 thomas err = got_object_id_by_path(&tree_id, repo,
7756 10604dce 2021-09-24 thomas worktree->base_commit_id, worktree->path_prefix);
7757 10604dce 2021-09-24 thomas if (err)
7758 10604dce 2021-09-24 thomas goto done;
7759 10604dce 2021-09-24 thomas
7760 10604dce 2021-09-24 thomas err = delete_merge_refs(worktree, repo);
7761 10604dce 2021-09-24 thomas if (err)
7762 10604dce 2021-09-24 thomas goto done;
7763 10604dce 2021-09-24 thomas
7764 10604dce 2021-09-24 thomas err = get_fileindex_path(&fileindex_path, worktree);
7765 10604dce 2021-09-24 thomas if (err)
7766 10604dce 2021-09-24 thomas goto done;
7767 10604dce 2021-09-24 thomas
7768 10604dce 2021-09-24 thomas rfa.worktree = worktree;
7769 10604dce 2021-09-24 thomas rfa.fileindex = fileindex;
7770 10604dce 2021-09-24 thomas rfa.progress_cb = progress_cb;
7771 10604dce 2021-09-24 thomas rfa.progress_arg = progress_arg;
7772 10604dce 2021-09-24 thomas rfa.patch_cb = NULL;
7773 10604dce 2021-09-24 thomas rfa.patch_arg = NULL;
7774 10604dce 2021-09-24 thomas rfa.repo = repo;
7775 10604dce 2021-09-24 thomas rfa.unlink_added_files = 1;
7776 10604dce 2021-09-24 thomas err = worktree_status(worktree, "", fileindex, repo,
7777 6092c299 2021-10-04 thomas revert_file, &rfa, NULL, NULL, 1, 0);
7778 10604dce 2021-09-24 thomas if (err)
7779 10604dce 2021-09-24 thomas goto sync;
7780 10604dce 2021-09-24 thomas
7781 10604dce 2021-09-24 thomas err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7782 10604dce 2021-09-24 thomas repo, progress_cb, progress_arg, NULL, NULL);
7783 10604dce 2021-09-24 thomas sync:
7784 10604dce 2021-09-24 thomas sync_err = sync_fileindex(fileindex, fileindex_path);
7785 10604dce 2021-09-24 thomas if (sync_err && err == NULL)
7786 10604dce 2021-09-24 thomas err = sync_err;
7787 10604dce 2021-09-24 thomas done:
7788 10604dce 2021-09-24 thomas free(tree_id);
7789 10604dce 2021-09-24 thomas free(commit_id);
7790 10604dce 2021-09-24 thomas if (fileindex)
7791 10604dce 2021-09-24 thomas got_fileindex_free(fileindex);
7792 10604dce 2021-09-24 thomas free(fileindex_path);
7793 10604dce 2021-09-24 thomas
7794 10604dce 2021-09-24 thomas unlockerr = lock_worktree(worktree, LOCK_SH);
7795 10604dce 2021-09-24 thomas if (unlockerr && err == NULL)
7796 10604dce 2021-09-24 thomas err = unlockerr;
7797 10604dce 2021-09-24 thomas return err;
7798 10604dce 2021-09-24 thomas }
7799 10604dce 2021-09-24 thomas
7800 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7801 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7802 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7803 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7804 2db2652d 2019-08-07 stsp struct got_repository *repo;
7805 2db2652d 2019-08-07 stsp int have_changes;
7806 2db2652d 2019-08-07 stsp };
7807 2db2652d 2019-08-07 stsp
7808 2db2652d 2019-08-07 stsp const struct got_error *
7809 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7810 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7811 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7812 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7813 735ef5ac 2019-08-03 stsp {
7814 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7815 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7816 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7817 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7818 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7819 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7820 8b13ce36 2019-08-08 stsp
7821 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7822 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7823 8b13ce36 2019-08-08 stsp return NULL;
7824 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7825 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7826 735ef5ac 2019-08-03 stsp
7827 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7828 735ef5ac 2019-08-03 stsp if (ie == NULL)
7829 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7830 735ef5ac 2019-08-03 stsp
7831 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7832 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7833 735ef5ac 2019-08-03 stsp relpath) == -1)
7834 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7835 735ef5ac 2019-08-03 stsp
7836 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7837 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7838 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7839 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7840 735ef5ac 2019-08-03 stsp }
7841 735ef5ac 2019-08-03 stsp
7842 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7843 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7844 3aa5969e 2019-08-06 stsp goto done;
7845 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7846 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7847 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7848 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7849 735ef5ac 2019-08-03 stsp goto done;
7850 3aa5969e 2019-08-06 stsp }
7851 735ef5ac 2019-08-03 stsp
7852 2db2652d 2019-08-07 stsp a->have_changes = 1;
7853 2db2652d 2019-08-07 stsp
7854 735ef5ac 2019-08-03 stsp p = in_repo_path;
7855 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7856 735ef5ac 2019-08-03 stsp p++;
7857 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7858 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7859 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7860 735ef5ac 2019-08-03 stsp done:
7861 735ef5ac 2019-08-03 stsp free(in_repo_path);
7862 dc424a06 2019-08-07 stsp return err;
7863 dc424a06 2019-08-07 stsp }
7864 dc424a06 2019-08-07 stsp
7865 2db2652d 2019-08-07 stsp struct stage_path_arg {
7866 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7867 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7868 2db2652d 2019-08-07 stsp struct got_repository *repo;
7869 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7870 2db2652d 2019-08-07 stsp void *status_arg;
7871 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7872 2db2652d 2019-08-07 stsp void *patch_arg;
7873 7b5dc508 2019-10-28 stsp int staged_something;
7874 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7875 2db2652d 2019-08-07 stsp };
7876 2db2652d 2019-08-07 stsp
7877 2db2652d 2019-08-07 stsp static const struct got_error *
7878 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7879 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7880 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7881 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7882 0cb83759 2019-08-03 stsp {
7883 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7884 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7885 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7886 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7887 0cb83759 2019-08-03 stsp uint32_t stage;
7888 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7889 0aeb8099 2020-07-23 stsp struct stat sb;
7890 8b13ce36 2019-08-08 stsp
7891 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7892 8b13ce36 2019-08-08 stsp return NULL;
7893 0cb83759 2019-08-03 stsp
7894 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7895 d3e7c587 2019-08-03 stsp if (ie == NULL)
7896 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7897 0cb83759 2019-08-03 stsp
7898 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7899 2db2652d 2019-08-07 stsp relpath)== -1)
7900 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7901 0cb83759 2019-08-03 stsp
7902 0cb83759 2019-08-03 stsp switch (status) {
7903 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7904 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7905 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7906 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7907 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7908 0aeb8099 2020-07-23 stsp break;
7909 0aeb8099 2020-07-23 stsp }
7910 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7911 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7912 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7913 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7914 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7915 dc424a06 2019-08-07 stsp if (err)
7916 dc424a06 2019-08-07 stsp break;
7917 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7918 dc424a06 2019-08-07 stsp break;
7919 dc424a06 2019-08-07 stsp } else {
7920 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7921 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7922 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7923 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7924 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7925 dc424a06 2019-08-07 stsp break;
7926 dc424a06 2019-08-07 stsp }
7927 dc424a06 2019-08-07 stsp }
7928 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7929 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7930 0cb83759 2019-08-03 stsp if (err)
7931 dc424a06 2019-08-07 stsp break;
7932 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7933 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7934 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7935 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7936 0cb83759 2019-08-03 stsp else
7937 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7938 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7939 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7940 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7941 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7942 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7943 35213c7c 2020-07-23 stsp ssize_t target_len;
7944 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7945 35213c7c 2020-07-23 stsp sizeof(target_path));
7946 35213c7c 2020-07-23 stsp if (target_len == -1) {
7947 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7948 35213c7c 2020-07-23 stsp ondisk_path);
7949 35213c7c 2020-07-23 stsp break;
7950 35213c7c 2020-07-23 stsp }
7951 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7952 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7953 35213c7c 2020-07-23 stsp a->worktree->root_path);
7954 35213c7c 2020-07-23 stsp if (err)
7955 35213c7c 2020-07-23 stsp break;
7956 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7957 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7958 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7959 35213c7c 2020-07-23 stsp break;
7960 35213c7c 2020-07-23 stsp }
7961 35213c7c 2020-07-23 stsp }
7962 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7963 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7964 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7965 35213c7c 2020-07-23 stsp else
7966 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7967 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7968 0aeb8099 2020-07-23 stsp } else {
7969 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7970 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7971 0aeb8099 2020-07-23 stsp }
7972 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7973 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7974 dc424a06 2019-08-07 stsp break;
7975 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7976 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7977 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7978 0cb83759 2019-08-03 stsp break;
7979 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7980 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7981 d3e7c587 2019-08-03 stsp break;
7982 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7983 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7984 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7985 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7986 dc424a06 2019-08-07 stsp if (err)
7987 dc424a06 2019-08-07 stsp break;
7988 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7989 88f33a19 2019-08-08 stsp break;
7990 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7991 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7992 dc424a06 2019-08-07 stsp break;
7993 88f33a19 2019-08-08 stsp }
7994 dc424a06 2019-08-07 stsp }
7995 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7996 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7997 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7998 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7999 dc424a06 2019-08-07 stsp break;
8000 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8001 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8002 12463d8b 2019-12-13 stsp de_name);
8003 0cb83759 2019-08-03 stsp break;
8004 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8005 d3e7c587 2019-08-03 stsp break;
8006 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8007 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8008 ebf48fd5 2019-08-03 stsp break;
8009 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8010 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8011 2a06fe5f 2019-08-24 stsp break;
8012 0cb83759 2019-08-03 stsp default:
8013 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8014 537ac44b 2019-08-03 stsp break;
8015 0cb83759 2019-08-03 stsp }
8016 dc424a06 2019-08-07 stsp
8017 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8018 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8019 dc424a06 2019-08-07 stsp free(path_content);
8020 2db2652d 2019-08-07 stsp free(ondisk_path);
8021 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8022 0ebf8283 2019-07-24 stsp return err;
8023 0ebf8283 2019-07-24 stsp }
8024 0cb83759 2019-08-03 stsp
8025 0cb83759 2019-08-03 stsp const struct got_error *
8026 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8027 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8028 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8029 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8030 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8031 0cb83759 2019-08-03 stsp {
8032 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8033 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8034 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8035 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8036 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8037 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8038 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8039 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8040 0cb83759 2019-08-03 stsp
8041 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8042 0cb83759 2019-08-03 stsp if (err)
8043 0cb83759 2019-08-03 stsp return err;
8044 0cb83759 2019-08-03 stsp
8045 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8046 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8047 735ef5ac 2019-08-03 stsp if (err)
8048 735ef5ac 2019-08-03 stsp goto done;
8049 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8050 735ef5ac 2019-08-03 stsp if (err)
8051 735ef5ac 2019-08-03 stsp goto done;
8052 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8053 0cb83759 2019-08-03 stsp if (err)
8054 0cb83759 2019-08-03 stsp goto done;
8055 0cb83759 2019-08-03 stsp
8056 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8057 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8058 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8059 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8060 2db2652d 2019-08-07 stsp oka.repo = repo;
8061 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8062 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8063 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8064 6092c299 2021-10-04 thomas check_stage_ok, &oka, NULL, NULL, 1, 0);
8065 735ef5ac 2019-08-03 stsp if (err)
8066 735ef5ac 2019-08-03 stsp goto done;
8067 735ef5ac 2019-08-03 stsp }
8068 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8069 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8070 2db2652d 2019-08-07 stsp goto done;
8071 2db2652d 2019-08-07 stsp }
8072 735ef5ac 2019-08-03 stsp
8073 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8074 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8075 2db2652d 2019-08-07 stsp spa.repo = repo;
8076 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8077 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8078 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8079 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8080 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8081 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8082 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8083 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8084 6092c299 2021-10-04 thomas stage_path, &spa, NULL, NULL, 1, 0);
8085 42005733 2019-08-03 stsp if (err)
8086 2db2652d 2019-08-07 stsp goto done;
8087 7b5dc508 2019-10-28 stsp }
8088 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8089 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8090 7b5dc508 2019-10-28 stsp goto done;
8091 0cb83759 2019-08-03 stsp }
8092 0cb83759 2019-08-03 stsp
8093 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8094 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8095 0cb83759 2019-08-03 stsp err = sync_err;
8096 0cb83759 2019-08-03 stsp done:
8097 735ef5ac 2019-08-03 stsp if (head_ref)
8098 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8099 735ef5ac 2019-08-03 stsp free(head_commit_id);
8100 0cb83759 2019-08-03 stsp free(fileindex_path);
8101 0cb83759 2019-08-03 stsp if (fileindex)
8102 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8103 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8104 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8105 0cb83759 2019-08-03 stsp err = unlockerr;
8106 0cb83759 2019-08-03 stsp return err;
8107 0cb83759 2019-08-03 stsp }
8108 ad493afc 2019-08-03 stsp
8109 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8110 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8111 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8112 ad493afc 2019-08-03 stsp struct got_repository *repo;
8113 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8114 ad493afc 2019-08-03 stsp void *progress_arg;
8115 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8116 2e1f37b0 2019-08-08 stsp void *patch_arg;
8117 ad493afc 2019-08-03 stsp };
8118 2e1f37b0 2019-08-08 stsp
8119 2e1f37b0 2019-08-08 stsp static const struct got_error *
8120 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8121 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8122 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8123 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8124 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8125 2e1f37b0 2019-08-08 stsp {
8126 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8127 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8128 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8129 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8130 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8131 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8132 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8133 2e1f37b0 2019-08-08 stsp
8134 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8135 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8136 2e1f37b0 2019-08-08 stsp
8137 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8138 2e1f37b0 2019-08-08 stsp if (err)
8139 2e1f37b0 2019-08-08 stsp return err;
8140 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8141 2e1f37b0 2019-08-08 stsp if (err)
8142 2e1f37b0 2019-08-08 stsp goto done;
8143 2e1f37b0 2019-08-08 stsp
8144 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8145 2e1f37b0 2019-08-08 stsp if (err)
8146 2e1f37b0 2019-08-08 stsp goto done;
8147 2e1f37b0 2019-08-08 stsp
8148 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8149 2e1f37b0 2019-08-08 stsp if (err)
8150 2e1f37b0 2019-08-08 stsp goto done;
8151 2e1f37b0 2019-08-08 stsp
8152 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8153 2e1f37b0 2019-08-08 stsp if (err)
8154 2e1f37b0 2019-08-08 stsp goto done;
8155 2e1f37b0 2019-08-08 stsp
8156 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8157 2e1f37b0 2019-08-08 stsp if (err)
8158 2e1f37b0 2019-08-08 stsp goto done;
8159 ad493afc 2019-08-03 stsp
8160 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8161 2e1f37b0 2019-08-08 stsp if (err)
8162 2e1f37b0 2019-08-08 stsp goto done;
8163 2e1f37b0 2019-08-08 stsp
8164 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8165 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8166 2e1f37b0 2019-08-08 stsp if (err)
8167 2e1f37b0 2019-08-08 stsp goto done;
8168 2e1f37b0 2019-08-08 stsp
8169 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8170 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8171 2e1f37b0 2019-08-08 stsp if (err)
8172 2e1f37b0 2019-08-08 stsp goto done;
8173 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8174 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8175 2e1f37b0 2019-08-08 stsp if (err)
8176 2e1f37b0 2019-08-08 stsp goto done;
8177 2e1f37b0 2019-08-08 stsp
8178 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8179 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8180 2e1f37b0 2019-08-08 stsp goto done;
8181 2e1f37b0 2019-08-08 stsp }
8182 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8183 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8184 2e1f37b0 2019-08-08 stsp goto done;
8185 2e1f37b0 2019-08-08 stsp }
8186 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8187 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8188 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8189 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8190 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8191 fe621944 2020-11-10 stsp nchanges++;
8192 fe621944 2020-11-10 stsp }
8193 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8194 2e1f37b0 2019-08-08 stsp int choice;
8195 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8196 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8197 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8198 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8199 2e1f37b0 2019-08-08 stsp if (err)
8200 2e1f37b0 2019-08-08 stsp goto done;
8201 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8202 2e1f37b0 2019-08-08 stsp have_content = 1;
8203 19e4b907 2019-08-08 stsp else
8204 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8205 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8206 2e1f37b0 2019-08-08 stsp break;
8207 2e1f37b0 2019-08-08 stsp }
8208 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8209 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8210 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8211 2e1f37b0 2019-08-08 stsp done:
8212 2e1f37b0 2019-08-08 stsp free(label1);
8213 2e1f37b0 2019-08-08 stsp if (blob)
8214 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8215 2e1f37b0 2019-08-08 stsp if (staged_blob)
8216 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8217 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8218 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8219 fe621944 2020-11-10 stsp err = free_err;
8220 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8221 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8222 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8223 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8224 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8225 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8226 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8227 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8228 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8229 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8230 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8231 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8232 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8233 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8234 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8235 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8236 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8237 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8238 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8239 2e1f37b0 2019-08-08 stsp }
8240 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8241 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8242 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8243 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8244 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8245 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8246 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8247 2e1f37b0 2019-08-08 stsp }
8248 2e1f37b0 2019-08-08 stsp free(path1);
8249 2e1f37b0 2019-08-08 stsp free(path2);
8250 fda8017d 2020-07-23 stsp return err;
8251 fda8017d 2020-07-23 stsp }
8252 fda8017d 2020-07-23 stsp
8253 fda8017d 2020-07-23 stsp static const struct got_error *
8254 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8255 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8256 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8257 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8258 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8259 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8260 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8261 fda8017d 2020-07-23 stsp {
8262 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8263 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8264 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8265 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8266 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8267 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8268 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8269 fda8017d 2020-07-23 stsp struct stat sb;
8270 fda8017d 2020-07-23 stsp
8271 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8272 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8273 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8274 fda8017d 2020-07-23 stsp if (err)
8275 fda8017d 2020-07-23 stsp return err;
8276 fda8017d 2020-07-23 stsp
8277 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8278 fda8017d 2020-07-23 stsp return NULL;
8279 fda8017d 2020-07-23 stsp
8280 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8281 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8282 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8283 fda8017d 2020-07-23 stsp if (err)
8284 fda8017d 2020-07-23 stsp goto done;
8285 fda8017d 2020-07-23 stsp }
8286 fda8017d 2020-07-23 stsp
8287 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
8288 fda8017d 2020-07-23 stsp if (f == NULL) {
8289 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8290 fda8017d 2020-07-23 stsp path_unstaged_content);
8291 fda8017d 2020-07-23 stsp goto done;
8292 fda8017d 2020-07-23 stsp }
8293 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8294 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8295 fda8017d 2020-07-23 stsp goto done;
8296 fda8017d 2020-07-23 stsp }
8297 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8298 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8299 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8300 fda8017d 2020-07-23 stsp size_t r;
8301 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8302 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8303 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8304 fda8017d 2020-07-23 stsp goto done;
8305 fda8017d 2020-07-23 stsp }
8306 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8307 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8308 fda8017d 2020-07-23 stsp goto done;
8309 fda8017d 2020-07-23 stsp }
8310 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8311 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8312 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8313 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8314 fda8017d 2020-07-23 stsp progress_arg);
8315 fda8017d 2020-07-23 stsp } else {
8316 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8317 67a66647 2021-05-31 stsp
8318 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8319 67a66647 2021-05-31 stsp if (err)
8320 67a66647 2021-05-31 stsp return err;
8321 67a66647 2021-05-31 stsp
8322 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8323 67a66647 2021-05-31 stsp parent) == -1) {
8324 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8325 67a66647 2021-05-31 stsp base_path = NULL;
8326 67a66647 2021-05-31 stsp goto done;
8327 67a66647 2021-05-31 stsp }
8328 67a66647 2021-05-31 stsp
8329 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8330 67a66647 2021-05-31 stsp if (err)
8331 67a66647 2021-05-31 stsp goto done;
8332 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8333 67a66647 2021-05-31 stsp blob_base);
8334 67a66647 2021-05-31 stsp if (err)
8335 67a66647 2021-05-31 stsp goto done;
8336 67a66647 2021-05-31 stsp
8337 eec2f5a9 2021-06-03 stsp /*
8338 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8339 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8340 eec2f5a9 2021-06-03 stsp */
8341 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8342 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8343 eec2f5a9 2021-06-03 stsp ondisk_path);
8344 eec2f5a9 2021-06-03 stsp if (err)
8345 eec2f5a9 2021-06-03 stsp goto done;
8346 eec2f5a9 2021-06-03 stsp } else {
8347 eec2f5a9 2021-06-03 stsp int fd;
8348 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8349 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8350 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8351 eec2f5a9 2021-06-03 stsp goto done;
8352 eec2f5a9 2021-06-03 stsp }
8353 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8354 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8355 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8356 eec2f5a9 2021-06-03 stsp close(fd);
8357 eec2f5a9 2021-06-03 stsp goto done;
8358 eec2f5a9 2021-06-03 stsp }
8359 eec2f5a9 2021-06-03 stsp }
8360 eec2f5a9 2021-06-03 stsp
8361 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8362 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8363 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8364 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8365 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8366 fda8017d 2020-07-23 stsp }
8367 fda8017d 2020-07-23 stsp if (err)
8368 fda8017d 2020-07-23 stsp goto done;
8369 fda8017d 2020-07-23 stsp
8370 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8371 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8372 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8373 2ac8aa02 2020-11-09 stsp } else {
8374 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8375 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8376 2ac8aa02 2020-11-09 stsp }
8377 fda8017d 2020-07-23 stsp done:
8378 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8379 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8380 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8381 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8382 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8383 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8384 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8385 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8386 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8387 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8388 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8389 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8390 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8391 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8392 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8393 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8394 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8395 67a66647 2021-05-31 stsp free(blob_base_path);
8396 67a66647 2021-05-31 stsp free(parent);
8397 67a66647 2021-05-31 stsp free(base_path);
8398 2e1f37b0 2019-08-08 stsp return err;
8399 2e1f37b0 2019-08-08 stsp }
8400 2e1f37b0 2019-08-08 stsp
8401 ad493afc 2019-08-03 stsp static const struct got_error *
8402 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8403 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8404 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8405 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8406 ad493afc 2019-08-03 stsp {
8407 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8408 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8409 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8410 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8411 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8412 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8413 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8414 9bc94a15 2019-08-03 stsp struct stat sb;
8415 ad493afc 2019-08-03 stsp
8416 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8417 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8418 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8419 2e1f37b0 2019-08-08 stsp return NULL;
8420 2e1f37b0 2019-08-08 stsp
8421 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8422 ad493afc 2019-08-03 stsp if (ie == NULL)
8423 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8424 9bc94a15 2019-08-03 stsp
8425 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8426 9bc94a15 2019-08-03 stsp == -1)
8427 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8428 ad493afc 2019-08-03 stsp
8429 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8430 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8431 f69721c3 2019-10-21 stsp if (err)
8432 f69721c3 2019-10-21 stsp goto done;
8433 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8434 f69721c3 2019-10-21 stsp id_str) == -1) {
8435 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8436 f69721c3 2019-10-21 stsp goto done;
8437 f69721c3 2019-10-21 stsp }
8438 f69721c3 2019-10-21 stsp
8439 ad493afc 2019-08-03 stsp switch (staged_status) {
8440 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8441 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8442 ad493afc 2019-08-03 stsp blob_id, 8192);
8443 ad493afc 2019-08-03 stsp if (err)
8444 ad493afc 2019-08-03 stsp break;
8445 ad493afc 2019-08-03 stsp /* fall through */
8446 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8447 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8448 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8449 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8450 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8451 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8452 2e1f37b0 2019-08-08 stsp if (err)
8453 2e1f37b0 2019-08-08 stsp break;
8454 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8455 2e1f37b0 2019-08-08 stsp break;
8456 2e1f37b0 2019-08-08 stsp } else {
8457 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8458 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8459 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8460 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8461 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8462 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8463 2e1f37b0 2019-08-08 stsp }
8464 2e1f37b0 2019-08-08 stsp }
8465 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8466 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8467 ad493afc 2019-08-03 stsp if (err)
8468 ad493afc 2019-08-03 stsp break;
8469 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8470 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8471 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8472 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8473 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8474 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8475 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8476 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8477 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8478 ea7786be 2020-07-23 stsp break;
8479 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8480 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8481 36bf999c 2020-07-23 stsp char *staged_target;
8482 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8483 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8484 36bf999c 2020-07-23 stsp if (err)
8485 36bf999c 2020-07-23 stsp goto done;
8486 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8487 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8488 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8489 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8490 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8491 36bf999c 2020-07-23 stsp free(staged_target);
8492 dfe9fba0 2020-07-23 stsp } else {
8493 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8494 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8495 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8496 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8497 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8498 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8499 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8500 dfe9fba0 2020-07-23 stsp }
8501 ea7786be 2020-07-23 stsp break;
8502 ea7786be 2020-07-23 stsp default:
8503 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8504 ea7786be 2020-07-23 stsp break;
8505 ea7786be 2020-07-23 stsp }
8506 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8507 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8508 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8509 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8510 2ac8aa02 2020-11-09 stsp }
8511 ad493afc 2019-08-03 stsp break;
8512 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8513 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8514 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8515 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8516 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8517 2e1f37b0 2019-08-08 stsp if (err)
8518 2e1f37b0 2019-08-08 stsp break;
8519 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8520 2e1f37b0 2019-08-08 stsp break;
8521 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8522 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8523 2e1f37b0 2019-08-08 stsp break;
8524 2e1f37b0 2019-08-08 stsp }
8525 2e1f37b0 2019-08-08 stsp }
8526 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8527 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8528 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8529 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8530 9bc94a15 2019-08-03 stsp if (err)
8531 9bc94a15 2019-08-03 stsp break;
8532 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8533 ad493afc 2019-08-03 stsp break;
8534 ad493afc 2019-08-03 stsp }
8535 f69721c3 2019-10-21 stsp done:
8536 ad493afc 2019-08-03 stsp free(ondisk_path);
8537 ad493afc 2019-08-03 stsp if (blob_base)
8538 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8539 ad493afc 2019-08-03 stsp if (blob_staged)
8540 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8541 f69721c3 2019-10-21 stsp free(id_str);
8542 f69721c3 2019-10-21 stsp free(label_orig);
8543 ad493afc 2019-08-03 stsp return err;
8544 ad493afc 2019-08-03 stsp }
8545 ad493afc 2019-08-03 stsp
8546 ad493afc 2019-08-03 stsp const struct got_error *
8547 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8548 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8549 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8550 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8551 ad493afc 2019-08-03 stsp struct got_repository *repo)
8552 ad493afc 2019-08-03 stsp {
8553 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8554 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8555 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8556 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8557 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8558 ad493afc 2019-08-03 stsp
8559 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8560 ad493afc 2019-08-03 stsp if (err)
8561 ad493afc 2019-08-03 stsp return err;
8562 ad493afc 2019-08-03 stsp
8563 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8564 ad493afc 2019-08-03 stsp if (err)
8565 ad493afc 2019-08-03 stsp goto done;
8566 ad493afc 2019-08-03 stsp
8567 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8568 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8569 ad493afc 2019-08-03 stsp upa.repo = repo;
8570 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8571 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8572 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8573 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8574 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8575 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8576 6092c299 2021-10-04 thomas unstage_path, &upa, NULL, NULL, 1, 0);
8577 ad493afc 2019-08-03 stsp if (err)
8578 ad493afc 2019-08-03 stsp goto done;
8579 ad493afc 2019-08-03 stsp }
8580 ad493afc 2019-08-03 stsp
8581 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8582 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8583 ad493afc 2019-08-03 stsp err = sync_err;
8584 ad493afc 2019-08-03 stsp done:
8585 ad493afc 2019-08-03 stsp free(fileindex_path);
8586 ad493afc 2019-08-03 stsp if (fileindex)
8587 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8588 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8589 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8590 ad493afc 2019-08-03 stsp err = unlockerr;
8591 ad493afc 2019-08-03 stsp return err;
8592 ad493afc 2019-08-03 stsp }
8593 b2118c49 2020-07-28 stsp
8594 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8595 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8596 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8597 b2118c49 2020-07-28 stsp void *info_arg;
8598 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8599 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8600 b2118c49 2020-07-28 stsp void *cancel_arg;
8601 b2118c49 2020-07-28 stsp };
8602 b2118c49 2020-07-28 stsp
8603 b2118c49 2020-07-28 stsp static const struct got_error *
8604 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8605 b2118c49 2020-07-28 stsp {
8606 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8607 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8608 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8609 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8610 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8611 b2118c49 2020-07-28 stsp int stage;
8612 b2118c49 2020-07-28 stsp
8613 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8614 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8615 b2118c49 2020-07-28 stsp
8616 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8617 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8618 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8619 b2118c49 2020-07-28 stsp break;
8620 b2118c49 2020-07-28 stsp }
8621 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8622 b2118c49 2020-07-28 stsp return NULL;
8623 b2118c49 2020-07-28 stsp
8624 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8625 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8626 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8627 b2118c49 2020-07-28 stsp }
8628 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8629 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8630 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8631 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8632 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8633 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8634 b2118c49 2020-07-28 stsp }
8635 b2118c49 2020-07-28 stsp
8636 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8637 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8638 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8639 b2118c49 2020-07-28 stsp }
8640 b2118c49 2020-07-28 stsp
8641 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8642 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8643 b2118c49 2020-07-28 stsp }
8644 b2118c49 2020-07-28 stsp
8645 b2118c49 2020-07-28 stsp const struct got_error *
8646 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8647 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8648 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8649 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8650 b2118c49 2020-07-28 stsp
8651 b2118c49 2020-07-28 stsp {
8652 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8653 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8654 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8655 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8656 b2118c49 2020-07-28 stsp
8657 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8658 b2118c49 2020-07-28 stsp if (err)
8659 b2118c49 2020-07-28 stsp return err;
8660 b2118c49 2020-07-28 stsp
8661 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8662 b2118c49 2020-07-28 stsp if (err)
8663 b2118c49 2020-07-28 stsp goto done;
8664 b2118c49 2020-07-28 stsp
8665 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8666 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8667 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8668 b2118c49 2020-07-28 stsp arg.paths = paths;
8669 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8670 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8671 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8672 b2118c49 2020-07-28 stsp &arg);
8673 b2118c49 2020-07-28 stsp done:
8674 b2118c49 2020-07-28 stsp free(fileindex_path);
8675 b2118c49 2020-07-28 stsp if (fileindex)
8676 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8677 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8678 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8679 b2118c49 2020-07-28 stsp err = unlockerr;
8680 b2118c49 2020-07-28 stsp return err;
8681 b2118c49 2020-07-28 stsp }