Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 09fe317a 2018-03-11 stsp return err;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 024e9686 2019-05-14 stsp static const struct got_error *
121 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
122 024e9686 2019-05-14 stsp {
123 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
124 024e9686 2019-05-14 stsp char *refstr = NULL;
125 024e9686 2019-05-14 stsp
126 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
127 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
128 024e9686 2019-05-14 stsp if (refstr == NULL)
129 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
130 024e9686 2019-05-14 stsp } else {
131 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
132 024e9686 2019-05-14 stsp if (refstr == NULL)
133 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
134 024e9686 2019-05-14 stsp }
135 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 024e9686 2019-05-14 stsp free(refstr);
137 024e9686 2019-05-14 stsp return err;
138 024e9686 2019-05-14 stsp }
139 024e9686 2019-05-14 stsp
140 86c3caaf 2018-03-09 stsp const struct got_error *
141 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
142 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp {
144 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
145 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
146 ec22038e 2019-03-10 stsp uuid_t uuid;
147 ec22038e 2019-03-10 stsp uint32_t uuid_status;
148 65596e15 2018-12-24 stsp int obj_type;
149 7ac97322 2018-03-11 stsp char *path_got = NULL;
150 1451e70d 2018-03-10 stsp char *formatstr = NULL;
151 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
152 65596e15 2018-12-24 stsp char *basestr = NULL;
153 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
154 65596e15 2018-12-24 stsp
155 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
157 0c48fee2 2019-03-11 stsp goto done;
158 0c48fee2 2019-03-11 stsp }
159 0c48fee2 2019-03-11 stsp
160 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
168 86c3caaf 2018-03-09 stsp
169 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
170 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
172 0bb8a95e 2018-03-12 stsp }
173 577ec78f 2018-03-11 stsp
174 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
175 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 86c3caaf 2018-03-09 stsp
180 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
181 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
183 86c3caaf 2018-03-09 stsp goto done;
184 86c3caaf 2018-03-09 stsp }
185 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
187 86c3caaf 2018-03-09 stsp goto done;
188 86c3caaf 2018-03-09 stsp }
189 86c3caaf 2018-03-09 stsp
190 056e7441 2018-03-11 stsp /* Create an empty lock file. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 056e7441 2018-03-11 stsp if (err)
193 056e7441 2018-03-11 stsp goto done;
194 056e7441 2018-03-11 stsp
195 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 99724ed4 2018-03-10 stsp if (err)
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp
200 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
201 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
202 65596e15 2018-12-24 stsp if (err)
203 65596e15 2018-12-24 stsp goto done;
204 65596e15 2018-12-24 stsp
205 65596e15 2018-12-24 stsp /* Record our base commit. */
206 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 99724ed4 2018-03-10 stsp if (err)
211 86c3caaf 2018-03-09 stsp goto done;
212 86c3caaf 2018-03-09 stsp
213 1451e70d 2018-03-10 stsp /* Store path to repository. */
214 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
216 99724ed4 2018-03-10 stsp if (err)
217 86c3caaf 2018-03-09 stsp goto done;
218 86c3caaf 2018-03-09 stsp
219 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
220 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
222 ec22038e 2019-03-10 stsp if (err)
223 ec22038e 2019-03-10 stsp goto done;
224 ec22038e 2019-03-10 stsp
225 ec22038e 2019-03-10 stsp /* Generate UUID. */
226 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
227 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
228 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
229 ec22038e 2019-03-10 stsp goto done;
230 ec22038e 2019-03-10 stsp }
231 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 577ec78f 2018-03-11 stsp if (err)
238 577ec78f 2018-03-11 stsp goto done;
239 577ec78f 2018-03-11 stsp
240 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
241 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
243 1451e70d 2018-03-10 stsp goto done;
244 1451e70d 2018-03-10 stsp }
245 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 99724ed4 2018-03-10 stsp if (err)
247 1451e70d 2018-03-10 stsp goto done;
248 1451e70d 2018-03-10 stsp
249 86c3caaf 2018-03-09 stsp done:
250 65596e15 2018-12-24 stsp free(commit_id);
251 7ac97322 2018-03-11 stsp free(path_got);
252 1451e70d 2018-03-10 stsp free(formatstr);
253 0bb8a95e 2018-03-12 stsp free(absprefix);
254 65596e15 2018-12-24 stsp free(basestr);
255 ec22038e 2019-03-10 stsp free(uuidstr);
256 86c3caaf 2018-03-09 stsp return err;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 247140b2 2019-02-05 stsp const struct got_error *
260 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 e5dc7198 2018-12-29 stsp const char *path_prefix)
262 e5dc7198 2018-12-29 stsp {
263 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
264 e5dc7198 2018-12-29 stsp
265 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
266 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
268 e5dc7198 2018-12-29 stsp }
269 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
270 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
271 e5dc7198 2018-12-29 stsp free(absprefix);
272 e5dc7198 2018-12-29 stsp return NULL;
273 86c3caaf 2018-03-09 stsp }
274 86c3caaf 2018-03-09 stsp
275 bc70eb79 2019-05-09 stsp const char *
276 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
277 86c3caaf 2018-03-09 stsp {
278 36a38700 2019-05-10 stsp return worktree->head_ref_name;
279 024e9686 2019-05-14 stsp }
280 024e9686 2019-05-14 stsp
281 024e9686 2019-05-14 stsp const struct got_error *
282 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
283 024e9686 2019-05-14 stsp struct got_reference *head_ref)
284 024e9686 2019-05-14 stsp {
285 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
286 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
287 024e9686 2019-05-14 stsp
288 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
290 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
291 024e9686 2019-05-14 stsp path_got = NULL;
292 024e9686 2019-05-14 stsp goto done;
293 024e9686 2019-05-14 stsp }
294 024e9686 2019-05-14 stsp
295 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
296 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
297 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
298 024e9686 2019-05-14 stsp goto done;
299 024e9686 2019-05-14 stsp }
300 024e9686 2019-05-14 stsp
301 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
302 024e9686 2019-05-14 stsp if (err)
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp
305 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
306 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
307 024e9686 2019-05-14 stsp done:
308 024e9686 2019-05-14 stsp free(path_got);
309 024e9686 2019-05-14 stsp if (err)
310 024e9686 2019-05-14 stsp free(head_ref_name);
311 024e9686 2019-05-14 stsp return err;
312 507dc3bb 2018-12-29 stsp }
313 507dc3bb 2018-12-29 stsp
314 b72f483a 2019-02-05 stsp struct got_object_id *
315 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
316 507dc3bb 2018-12-29 stsp {
317 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
318 86c3caaf 2018-03-09 stsp }
319 86c3caaf 2018-03-09 stsp
320 507dc3bb 2018-12-29 stsp const struct got_error *
321 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
323 507dc3bb 2018-12-29 stsp {
324 507dc3bb 2018-12-29 stsp const struct got_error *err;
325 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
326 507dc3bb 2018-12-29 stsp char *id_str = NULL;
327 507dc3bb 2018-12-29 stsp char *path_got = NULL;
328 507dc3bb 2018-12-29 stsp
329 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
331 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
332 507dc3bb 2018-12-29 stsp path_got = NULL;
333 507dc3bb 2018-12-29 stsp goto done;
334 507dc3bb 2018-12-29 stsp }
335 507dc3bb 2018-12-29 stsp
336 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
337 507dc3bb 2018-12-29 stsp if (err)
338 507dc3bb 2018-12-29 stsp return err;
339 507dc3bb 2018-12-29 stsp
340 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
342 507dc3bb 2018-12-29 stsp goto done;
343 507dc3bb 2018-12-29 stsp }
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp /* Record our base commit. */
346 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
347 507dc3bb 2018-12-29 stsp if (err)
348 507dc3bb 2018-12-29 stsp goto done;
349 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp
353 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
354 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
355 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
357 507dc3bb 2018-12-29 stsp goto done;
358 507dc3bb 2018-12-29 stsp }
359 507dc3bb 2018-12-29 stsp done:
360 507dc3bb 2018-12-29 stsp if (obj)
361 507dc3bb 2018-12-29 stsp got_object_close(obj);
362 507dc3bb 2018-12-29 stsp free(id_str);
363 507dc3bb 2018-12-29 stsp free(path_got);
364 507dc3bb 2018-12-29 stsp return err;
365 50b0790e 2020-09-11 stsp }
366 50b0790e 2020-09-11 stsp
367 50b0790e 2020-09-11 stsp const struct got_gotconfig *
368 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
369 50b0790e 2020-09-11 stsp {
370 50b0790e 2020-09-11 stsp return worktree->gotconfig;
371 507dc3bb 2018-12-29 stsp }
372 507dc3bb 2018-12-29 stsp
373 9d31a1d8 2018-03-11 stsp static const struct got_error *
374 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
375 86c3caaf 2018-03-09 stsp {
376 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
379 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
380 86c3caaf 2018-03-09 stsp return NULL;
381 21908da4 2019-01-13 stsp }
382 21908da4 2019-01-13 stsp
383 21908da4 2019-01-13 stsp static const struct got_error *
384 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
385 4a1ddfc2 2019-01-12 stsp {
386 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
387 4a1ddfc2 2019-01-12 stsp char *abspath;
388 4a1ddfc2 2019-01-12 stsp
389 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
391 4a1ddfc2 2019-01-12 stsp
392 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
393 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 ddcd8544 2019-03-11 stsp struct stat sb;
395 ddcd8544 2019-03-11 stsp err = NULL;
396 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
398 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
399 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
400 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
401 ddcd8544 2019-03-11 stsp }
402 ddcd8544 2019-03-11 stsp }
403 4a1ddfc2 2019-01-12 stsp free(abspath);
404 68c76935 2019-02-19 stsp return err;
405 68c76935 2019-02-19 stsp }
406 68c76935 2019-02-19 stsp
407 68c76935 2019-02-19 stsp static const struct got_error *
408 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
409 68c76935 2019-02-19 stsp {
410 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
411 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
412 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
413 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
414 68c76935 2019-02-19 stsp
415 68c76935 2019-02-19 stsp *same = 1;
416 68c76935 2019-02-19 stsp
417 230a42bd 2019-05-11 jcs for (;;) {
418 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
421 68c76935 2019-02-19 stsp break;
422 68c76935 2019-02-19 stsp }
423 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp if (flen1 == 0) {
429 68c76935 2019-02-19 stsp if (flen2 != 0)
430 68c76935 2019-02-19 stsp *same = 0;
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
433 68c76935 2019-02-19 stsp if (flen1 != 0)
434 68c76935 2019-02-19 stsp *same = 0;
435 68c76935 2019-02-19 stsp break;
436 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
437 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 68c76935 2019-02-19 stsp *same = 0;
439 68c76935 2019-02-19 stsp break;
440 68c76935 2019-02-19 stsp }
441 68c76935 2019-02-19 stsp } else {
442 68c76935 2019-02-19 stsp *same = 0;
443 68c76935 2019-02-19 stsp break;
444 68c76935 2019-02-19 stsp }
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp
447 68c76935 2019-02-19 stsp return err;
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp static const struct got_error *
451 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
452 68c76935 2019-02-19 stsp {
453 68c76935 2019-02-19 stsp struct stat sb;
454 68c76935 2019-02-19 stsp size_t size1, size2;
455 68c76935 2019-02-19 stsp
456 68c76935 2019-02-19 stsp *same = 1;
457 68c76935 2019-02-19 stsp
458 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
459 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
460 68c76935 2019-02-19 stsp size1 = sb.st_size;
461 68c76935 2019-02-19 stsp
462 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
463 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
464 68c76935 2019-02-19 stsp size2 = sb.st_size;
465 68c76935 2019-02-19 stsp
466 68c76935 2019-02-19 stsp if (size1 != size2) {
467 68c76935 2019-02-19 stsp *same = 0;
468 68c76935 2019-02-19 stsp return NULL;
469 68c76935 2019-02-19 stsp }
470 68c76935 2019-02-19 stsp
471 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
472 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
473 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
477 0e039681 2021-11-15 stsp }
478 0e039681 2021-11-15 stsp
479 0e039681 2021-11-15 stsp static const struct got_error *
480 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
481 0e039681 2021-11-15 stsp {
482 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
483 0e039681 2021-11-15 stsp size_t flen;
484 0e039681 2021-11-15 stsp ssize_t outlen;
485 0e039681 2021-11-15 stsp
486 0e039681 2021-11-15 stsp *outsize = 0;
487 0e039681 2021-11-15 stsp
488 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
489 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
490 0e039681 2021-11-15 stsp
491 0e039681 2021-11-15 stsp for (;;) {
492 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
493 0e039681 2021-11-15 stsp if (flen == 0) {
494 0e039681 2021-11-15 stsp if (ferror(f))
495 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
496 0e039681 2021-11-15 stsp if (feof(f))
497 0e039681 2021-11-15 stsp break;
498 0e039681 2021-11-15 stsp }
499 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
500 0e039681 2021-11-15 stsp if (outlen == -1)
501 0e039681 2021-11-15 stsp return got_error_from_errno("write");
502 0e039681 2021-11-15 stsp if (outlen != flen)
503 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
504 0e039681 2021-11-15 stsp *outsize += outlen;
505 0e039681 2021-11-15 stsp }
506 0e039681 2021-11-15 stsp
507 0e039681 2021-11-15 stsp return NULL;
508 0e039681 2021-11-15 stsp }
509 0e039681 2021-11-15 stsp
510 0e039681 2021-11-15 stsp static const struct got_error *
511 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
512 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 0e039681 2021-11-15 stsp const char *ondisk_path)
515 0e039681 2021-11-15 stsp {
516 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
517 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
518 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
522 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp
524 0e039681 2021-11-15 stsp *overlapcnt = 0;
525 0e039681 2021-11-15 stsp
526 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 0e039681 2021-11-15 stsp if (err)
528 0e039681 2021-11-15 stsp return err;
529 0e039681 2021-11-15 stsp
530 0e039681 2021-11-15 stsp if (same_content)
531 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
532 0e039681 2021-11-15 stsp
533 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
534 0e039681 2021-11-15 stsp if (err)
535 0e039681 2021-11-15 stsp return err;
536 0e039681 2021-11-15 stsp changed_deriv = !same_content;
537 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
538 0e039681 2021-11-15 stsp if (err)
539 0e039681 2021-11-15 stsp return err;
540 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
541 0e039681 2021-11-15 stsp
542 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
543 0e039681 2021-11-15 stsp *overlapcnt = 1;
544 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
546 0e039681 2021-11-15 stsp goto done;
547 0e039681 2021-11-15 stsp }
548 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
550 0e039681 2021-11-15 stsp goto done;
551 0e039681 2021-11-15 stsp }
552 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
554 0e039681 2021-11-15 stsp goto done;
555 0e039681 2021-11-15 stsp }
556 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 0e039681 2021-11-15 stsp base_path_orig);
558 0e039681 2021-11-15 stsp if (err)
559 0e039681 2021-11-15 stsp goto done;
560 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 0e039681 2021-11-15 stsp base_path_deriv);
562 0e039681 2021-11-15 stsp if (err)
563 0e039681 2021-11-15 stsp goto done;
564 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 0e039681 2021-11-15 stsp base_path_deriv2);
566 0e039681 2021-11-15 stsp if (err)
567 0e039681 2021-11-15 stsp goto done;
568 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 0e039681 2021-11-15 stsp if (err)
570 0e039681 2021-11-15 stsp goto done;
571 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 0e039681 2021-11-15 stsp if (err)
573 0e039681 2021-11-15 stsp goto done;
574 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 0e039681 2021-11-15 stsp if (err)
576 0e039681 2021-11-15 stsp goto done;
577 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
578 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
579 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
580 0e039681 2021-11-15 stsp goto done;
581 0e039681 2021-11-15 stsp }
582 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 0e039681 2021-11-15 stsp label_deriv ? " " : "",
585 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
586 0e039681 2021-11-15 stsp path_deriv) < 0) {
587 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
588 0e039681 2021-11-15 stsp goto done;
589 0e039681 2021-11-15 stsp }
590 0e039681 2021-11-15 stsp if (size_orig > 0) {
591 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
593 0e039681 2021-11-15 stsp label_orig ? " " : "",
594 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
595 0e039681 2021-11-15 stsp path_orig) < 0) {
596 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
597 0e039681 2021-11-15 stsp goto done;
598 0e039681 2021-11-15 stsp }
599 0e039681 2021-11-15 stsp }
600 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
602 0e039681 2021-11-15 stsp path_deriv2,
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
604 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
605 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
606 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
607 0e039681 2021-11-15 stsp goto done;
608 0e039681 2021-11-15 stsp }
609 0e039681 2021-11-15 stsp } else if (changed_deriv)
610 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 0e039681 2021-11-15 stsp else if (changed_deriv2)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 0e039681 2021-11-15 stsp done:
614 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 0e039681 2021-11-15 stsp err == NULL)
616 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
617 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
619 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
621 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
623 0e039681 2021-11-15 stsp free(path_orig);
624 0e039681 2021-11-15 stsp free(path_deriv);
625 0e039681 2021-11-15 stsp free(path_deriv2);
626 0e039681 2021-11-15 stsp free(base_path_orig);
627 0e039681 2021-11-15 stsp free(base_path_deriv);
628 0e039681 2021-11-15 stsp free(base_path_deriv2);
629 0e039681 2021-11-15 stsp return err;
630 6353ad76 2019-02-08 stsp }
631 6353ad76 2019-02-08 stsp
632 6353ad76 2019-02-08 stsp /*
633 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
634 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
635 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
636 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
637 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
638 6353ad76 2019-02-08 stsp */
639 6353ad76 2019-02-08 stsp static const struct got_error *
640 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
643 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
646 6353ad76 2019-02-08 stsp {
647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
648 6353ad76 2019-02-08 stsp int merged_fd = -1;
649 db590691 2021-06-02 stsp FILE *f_merged = NULL;
650 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
651 234035bc 2019-06-01 stsp int overlapcnt = 0;
652 3524bbf9 2020-10-20 stsp char *parent = NULL;
653 6353ad76 2019-02-08 stsp
654 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
655 234035bc 2019-06-01 stsp
656 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
657 3524bbf9 2020-10-20 stsp if (err)
658 3524bbf9 2020-10-20 stsp return err;
659 af54ae4a 2019-02-19 stsp
660 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
662 3524bbf9 2020-10-20 stsp goto done;
663 3524bbf9 2020-10-20 stsp }
664 af54ae4a 2019-02-19 stsp
665 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 6353ad76 2019-02-08 stsp if (err)
667 6353ad76 2019-02-08 stsp goto done;
668 3b9f0f87 2020-07-23 stsp
669 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 0e039681 2021-11-15 stsp if (err) {
672 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
673 0e039681 2021-11-15 stsp goto done;
674 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 0e039681 2021-11-15 stsp ondisk_path);
677 0e039681 2021-11-15 stsp if (err)
678 0e039681 2021-11-15 stsp goto done;
679 0e039681 2021-11-15 stsp }
680 6353ad76 2019-02-08 stsp
681 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
682 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 1ee397ad 2019-07-12 stsp if (err)
684 1ee397ad 2019-07-12 stsp goto done;
685 6353ad76 2019-02-08 stsp
686 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
688 816dc654 2019-02-16 stsp goto done;
689 68c76935 2019-02-19 stsp }
690 68c76935 2019-02-19 stsp
691 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
692 db590691 2021-06-02 stsp if (f_merged == NULL) {
693 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
694 db590691 2021-06-02 stsp goto done;
695 db590691 2021-06-02 stsp }
696 db590691 2021-06-02 stsp merged_fd = -1;
697 db590691 2021-06-02 stsp
698 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
699 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
700 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
701 db590691 2021-06-02 stsp f_merged);
702 68c76935 2019-02-19 stsp if (err)
703 68c76935 2019-02-19 stsp goto done;
704 816dc654 2019-02-16 stsp }
705 6353ad76 2019-02-08 stsp
706 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
707 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
708 70a0c8ec 2019-02-20 stsp goto done;
709 70a0c8ec 2019-02-20 stsp }
710 70a0c8ec 2019-02-20 stsp
711 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
713 230a42bd 2019-05-11 jcs ondisk_path);
714 6353ad76 2019-02-08 stsp goto done;
715 6353ad76 2019-02-08 stsp }
716 6353ad76 2019-02-08 stsp done:
717 fdcb7daf 2019-12-15 stsp if (err) {
718 fdcb7daf 2019-12-15 stsp if (merged_path)
719 fdcb7daf 2019-12-15 stsp unlink(merged_path);
720 3b9f0f87 2020-07-23 stsp }
721 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
723 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
725 6353ad76 2019-02-08 stsp free(merged_path);
726 af54ae4a 2019-02-19 stsp free(base_path);
727 3524bbf9 2020-10-20 stsp free(parent);
728 af57b12a 2020-07-23 stsp return err;
729 af57b12a 2020-07-23 stsp }
730 af57b12a 2020-07-23 stsp
731 af57b12a 2020-07-23 stsp static const struct got_error *
732 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
733 af57b12a 2020-07-23 stsp size_t target_len)
734 af57b12a 2020-07-23 stsp {
735 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
736 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
737 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
738 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
740 af57b12a 2020-07-23 stsp ondisk_path);
741 af57b12a 2020-07-23 stsp return NULL;
742 af57b12a 2020-07-23 stsp }
743 af57b12a 2020-07-23 stsp
744 af57b12a 2020-07-23 stsp /*
745 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
747 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
748 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
749 993e2a1b 2020-07-23 stsp *
750 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
751 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
754 993e2a1b 2020-07-23 stsp * has deleted this symlink.
755 11cc08c1 2020-07-23 stsp */
756 11cc08c1 2020-07-23 stsp static const struct got_error *
757 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
758 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
760 11cc08c1 2020-07-23 stsp {
761 11cc08c1 2020-07-23 stsp const struct got_error *err;
762 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 11cc08c1 2020-07-23 stsp FILE *f = NULL;
764 11cc08c1 2020-07-23 stsp
765 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
766 11cc08c1 2020-07-23 stsp if (err)
767 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
768 11cc08c1 2020-07-23 stsp
769 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
770 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
772 11cc08c1 2020-07-23 stsp goto done;
773 11cc08c1 2020-07-23 stsp }
774 11cc08c1 2020-07-23 stsp
775 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 11cc08c1 2020-07-23 stsp if (err)
777 3818e3c4 2020-11-01 naddy goto done;
778 3818e3c4 2020-11-01 naddy
779 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
781 11cc08c1 2020-07-23 stsp goto done;
782 3818e3c4 2020-11-01 naddy }
783 11cc08c1 2020-07-23 stsp
784 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
787 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
788 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
789 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
792 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
794 11cc08c1 2020-07-23 stsp goto done;
795 11cc08c1 2020-07-23 stsp }
796 11cc08c1 2020-07-23 stsp
797 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
802 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
803 11cc08c1 2020-07-23 stsp goto done;
804 11cc08c1 2020-07-23 stsp }
805 11cc08c1 2020-07-23 stsp done:
806 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
808 11cc08c1 2020-07-23 stsp free(path);
809 11cc08c1 2020-07-23 stsp free(id_str);
810 11cc08c1 2020-07-23 stsp free(label_deriv);
811 11cc08c1 2020-07-23 stsp return err;
812 11cc08c1 2020-07-23 stsp }
813 d219f183 2020-07-23 stsp
814 d219f183 2020-07-23 stsp /* forward declaration */
815 d219f183 2020-07-23 stsp static const struct got_error *
816 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
818 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
819 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
820 11cc08c1 2020-07-23 stsp
821 11cc08c1 2020-07-23 stsp /*
822 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
823 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
824 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
825 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
826 af57b12a 2020-07-23 stsp */
827 af57b12a 2020-07-23 stsp static const struct got_error *
828 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
829 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
830 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
831 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
833 af57b12a 2020-07-23 stsp {
834 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
835 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
836 af57b12a 2020-07-23 stsp struct stat sb;
837 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
838 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
839 11cc08c1 2020-07-23 stsp int have_local_change = 0;
840 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
841 af57b12a 2020-07-23 stsp
842 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
843 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
846 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
847 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
848 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
849 af57b12a 2020-07-23 stsp ondisk_path);
850 af57b12a 2020-07-23 stsp goto done;
851 af57b12a 2020-07-23 stsp }
852 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
853 af57b12a 2020-07-23 stsp
854 526a746f 2020-07-23 stsp if (blob_orig) {
855 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 526a746f 2020-07-23 stsp if (err)
857 526a746f 2020-07-23 stsp goto done;
858 526a746f 2020-07-23 stsp }
859 af57b12a 2020-07-23 stsp
860 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
861 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
862 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 11cc08c1 2020-07-23 stsp have_local_change = 1;
864 11cc08c1 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
866 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
867 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
868 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
870 11cc08c1 2020-07-23 stsp
871 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
872 11cc08c1 2020-07-23 stsp if (ancestor_target) {
873 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
874 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 11cc08c1 2020-07-23 stsp path);
876 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
877 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else {
882 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
883 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
884 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
885 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
886 11cc08c1 2020-07-23 stsp if (err)
887 11cc08c1 2020-07-23 stsp goto done;
888 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 11cc08c1 2020-07-23 stsp path);
890 11cc08c1 2020-07-23 stsp }
891 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
892 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
893 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
894 af57b12a 2020-07-23 stsp strlen(deriv_target));
895 af57b12a 2020-07-23 stsp if (err)
896 af57b12a 2020-07-23 stsp goto done;
897 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
899 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
900 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
902 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 ea7786be 2020-07-23 stsp path);
904 ea7786be 2020-07-23 stsp } else {
905 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
906 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
907 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
908 ea7786be 2020-07-23 stsp if (err)
909 ea7786be 2020-07-23 stsp goto done;
910 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 ea7786be 2020-07-23 stsp path);
912 ea7786be 2020-07-23 stsp }
913 6353ad76 2019-02-08 stsp }
914 11cc08c1 2020-07-23 stsp
915 af57b12a 2020-07-23 stsp done:
916 af57b12a 2020-07-23 stsp free(ancestor_target);
917 eec2f5a9 2021-06-03 stsp return err;
918 eec2f5a9 2021-06-03 stsp }
919 eec2f5a9 2021-06-03 stsp
920 eec2f5a9 2021-06-03 stsp static const struct got_error *
921 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
922 eec2f5a9 2021-06-03 stsp {
923 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
924 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
925 eec2f5a9 2021-06-03 stsp ssize_t target_len;
926 eec2f5a9 2021-06-03 stsp size_t n;
927 eec2f5a9 2021-06-03 stsp FILE *f;
928 eec2f5a9 2021-06-03 stsp
929 eec2f5a9 2021-06-03 stsp *outfile = NULL;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp f = got_opentemp();
932 eec2f5a9 2021-06-03 stsp if (f == NULL)
933 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
934 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
936 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
937 eec2f5a9 2021-06-03 stsp goto done;
938 eec2f5a9 2021-06-03 stsp }
939 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
940 eec2f5a9 2021-06-03 stsp if (n != target_len) {
941 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
945 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
946 eec2f5a9 2021-06-03 stsp goto done;
947 eec2f5a9 2021-06-03 stsp }
948 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
949 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
950 eec2f5a9 2021-06-03 stsp goto done;
951 eec2f5a9 2021-06-03 stsp }
952 eec2f5a9 2021-06-03 stsp done:
953 eec2f5a9 2021-06-03 stsp if (err)
954 eec2f5a9 2021-06-03 stsp fclose(f);
955 eec2f5a9 2021-06-03 stsp else
956 eec2f5a9 2021-06-03 stsp *outfile = f;
957 14c901f1 2019-08-08 stsp return err;
958 14c901f1 2019-08-08 stsp }
959 14c901f1 2019-08-08 stsp
960 14c901f1 2019-08-08 stsp /*
961 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
963 14c901f1 2019-08-08 stsp * acts as the second derived version.
964 14c901f1 2019-08-08 stsp */
965 14c901f1 2019-08-08 stsp static const struct got_error *
966 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
968 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
969 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
970 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
972 14c901f1 2019-08-08 stsp {
973 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
974 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
976 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
978 14c901f1 2019-08-08 stsp
979 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
980 14c901f1 2019-08-08 stsp
981 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
982 ed6b5030 2020-10-20 stsp if (err)
983 ed6b5030 2020-10-20 stsp return err;
984 14c901f1 2019-08-08 stsp
985 67a66647 2021-05-31 stsp if (blob_orig) {
986 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 67a66647 2021-05-31 stsp parent) == -1) {
988 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
989 67a66647 2021-05-31 stsp base_path = NULL;
990 67a66647 2021-05-31 stsp goto done;
991 67a66647 2021-05-31 stsp }
992 67a66647 2021-05-31 stsp
993 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 67a66647 2021-05-31 stsp if (err)
995 67a66647 2021-05-31 stsp goto done;
996 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 67a66647 2021-05-31 stsp blob_orig);
998 67a66647 2021-05-31 stsp if (err)
999 67a66647 2021-05-31 stsp goto done;
1000 67a66647 2021-05-31 stsp free(base_path);
1001 db590691 2021-06-02 stsp } else {
1002 db590691 2021-06-02 stsp /*
1003 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1004 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1005 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1006 db590691 2021-06-02 stsp */
1007 db590691 2021-06-02 stsp f_orig = got_opentemp();
1008 db590691 2021-06-02 stsp if (f_orig == NULL) {
1009 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1010 db590691 2021-06-02 stsp goto done;
1011 db590691 2021-06-02 stsp }
1012 67a66647 2021-05-31 stsp }
1013 67a66647 2021-05-31 stsp
1014 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1016 14c901f1 2019-08-08 stsp base_path = NULL;
1017 14c901f1 2019-08-08 stsp goto done;
1018 14c901f1 2019-08-08 stsp }
1019 14c901f1 2019-08-08 stsp
1020 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 14c901f1 2019-08-08 stsp if (err)
1022 14c901f1 2019-08-08 stsp goto done;
1023 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 14c901f1 2019-08-08 stsp blob_deriv);
1025 14c901f1 2019-08-08 stsp if (err)
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp
1028 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 14c901f1 2019-08-08 stsp if (err)
1030 14c901f1 2019-08-08 stsp goto done;
1031 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1032 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1034 14c901f1 2019-08-08 stsp goto done;
1035 eec2f5a9 2021-06-03 stsp }
1036 eec2f5a9 2021-06-03 stsp
1037 eec2f5a9 2021-06-03 stsp /*
1038 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1039 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1040 eec2f5a9 2021-06-03 stsp */
1041 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1042 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 eec2f5a9 2021-06-03 stsp if (err)
1044 eec2f5a9 2021-06-03 stsp goto done;
1045 eec2f5a9 2021-06-03 stsp } else {
1046 eec2f5a9 2021-06-03 stsp int fd;
1047 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1049 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1050 eec2f5a9 2021-06-03 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp }
1052 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1053 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1054 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1055 eec2f5a9 2021-06-03 stsp close(fd);
1056 eec2f5a9 2021-06-03 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp }
1058 14c901f1 2019-08-08 stsp }
1059 14c901f1 2019-08-08 stsp
1060 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 14c901f1 2019-08-08 stsp done:
1064 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1066 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1068 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1070 14c901f1 2019-08-08 stsp free(base_path);
1071 67a66647 2021-05-31 stsp if (blob_orig_path) {
1072 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1073 67a66647 2021-05-31 stsp free(blob_orig_path);
1074 67a66647 2021-05-31 stsp }
1075 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1076 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1077 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1078 14c901f1 2019-08-08 stsp }
1079 6353ad76 2019-02-08 stsp free(id_str);
1080 818c7501 2019-07-11 stsp free(label_deriv);
1081 ed6b5030 2020-10-20 stsp free(parent);
1082 4a1ddfc2 2019-01-12 stsp return err;
1083 4a1ddfc2 2019-01-12 stsp }
1084 4a1ddfc2 2019-01-12 stsp
1085 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1086 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1089 13d9040b 2019-03-27 stsp {
1090 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1091 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1092 13d9040b 2019-03-27 stsp
1093 65b05cec 2020-07-23 stsp *new_iep = NULL;
1094 65b05cec 2020-07-23 stsp
1095 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1096 054041d0 2020-06-24 stsp if (err)
1097 054041d0 2020-06-24 stsp return err;
1098 054041d0 2020-06-24 stsp
1099 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1101 054041d0 2020-06-24 stsp if (err)
1102 054041d0 2020-06-24 stsp goto done;
1103 054041d0 2020-06-24 stsp
1104 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1105 054041d0 2020-06-24 stsp done:
1106 054041d0 2020-06-24 stsp if (err)
1107 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1108 65b05cec 2020-07-23 stsp else
1109 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1110 13d9040b 2019-03-27 stsp return err;
1111 1ebedb77 2019-10-19 stsp }
1112 1ebedb77 2019-10-19 stsp
1113 1ebedb77 2019-10-19 stsp static mode_t
1114 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1115 1ebedb77 2019-10-19 stsp {
1116 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1117 1ebedb77 2019-10-19 stsp
1118 1ebedb77 2019-10-19 stsp if (executable) {
1119 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1120 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1121 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1122 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1123 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1124 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1125 1ebedb77 2019-10-19 stsp }
1126 1ebedb77 2019-10-19 stsp
1127 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1128 13d9040b 2019-03-27 stsp }
1129 13d9040b 2019-03-27 stsp
1130 8ba819a3 2020-07-23 stsp /* forward declaration */
1131 13d9040b 2019-03-27 stsp static const struct got_error *
1132 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1134 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1135 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1136 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1137 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1138 8ba819a3 2020-07-23 stsp
1139 5a1fbc73 2020-07-23 stsp /*
1140 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1141 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1142 5a1fbc73 2020-07-23 stsp */
1143 8ba819a3 2020-07-23 stsp static const struct got_error *
1144 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1146 5a1fbc73 2020-07-23 stsp {
1147 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1148 5a1fbc73 2020-07-23 stsp ssize_t elen;
1149 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1150 5a1fbc73 2020-07-23 stsp int fd;
1151 5a1fbc73 2020-07-23 stsp
1152 c6e8a826 2021-04-05 stsp *did_something = 0;
1153 c6e8a826 2021-04-05 stsp
1154 5a1fbc73 2020-07-23 stsp /*
1155 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1156 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1157 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1158 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1159 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1160 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1161 5a1fbc73 2020-07-23 stsp */
1162 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1164 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1165 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1166 5a1fbc73 2020-07-23 stsp
1167 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1168 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 5a1fbc73 2020-07-23 stsp if (elen == -1)
1170 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1171 5a1fbc73 2020-07-23 stsp
1172 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1173 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1174 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1175 5a1fbc73 2020-07-23 stsp }
1176 5a1fbc73 2020-07-23 stsp
1177 c6e8a826 2021-04-05 stsp *did_something = 1;
1178 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1179 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1180 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1181 5a1fbc73 2020-07-23 stsp return err;
1182 3c1ec782 2020-07-23 stsp }
1183 3c1ec782 2020-07-23 stsp
1184 3c1ec782 2020-07-23 stsp static const struct got_error *
1185 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1187 3c1ec782 2020-07-23 stsp {
1188 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1189 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1190 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1191 3c1ec782 2020-07-23 stsp
1192 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1193 3c1ec782 2020-07-23 stsp
1194 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1195 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1196 3c1ec782 2020-07-23 stsp return NULL;
1197 3c1ec782 2020-07-23 stsp }
1198 3c1ec782 2020-07-23 stsp
1199 3c1ec782 2020-07-23 stsp /*
1200 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1201 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1202 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1203 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1204 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1205 3c1ec782 2020-07-23 stsp */
1206 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1207 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1208 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1209 ce031e9e 2020-10-20 stsp if (err)
1210 ce031e9e 2020-10-20 stsp return err;
1211 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 ce031e9e 2020-10-20 stsp free(parent);
1213 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1214 ce031e9e 2020-10-20 stsp }
1215 ce031e9e 2020-10-20 stsp free(parent);
1216 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1217 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 3c1ec782 2020-07-23 stsp free(abspath);
1219 3c1ec782 2020-07-23 stsp return err;
1220 3c1ec782 2020-07-23 stsp }
1221 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 3c1ec782 2020-07-23 stsp free(abspath);
1223 3c1ec782 2020-07-23 stsp if (err)
1224 3c1ec782 2020-07-23 stsp return err;
1225 3c1ec782 2020-07-23 stsp } else {
1226 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 3c1ec782 2020-07-23 stsp if (err)
1228 3c1ec782 2020-07-23 stsp return err;
1229 3c1ec782 2020-07-23 stsp }
1230 3c1ec782 2020-07-23 stsp
1231 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1232 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1234 3c1ec782 2020-07-23 stsp return NULL;
1235 3c1ec782 2020-07-23 stsp }
1236 3c1ec782 2020-07-23 stsp
1237 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1238 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1240 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1241 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1243 3c1ec782 2020-07-23 stsp
1244 3c1ec782 2020-07-23 stsp free(path_got);
1245 3c1ec782 2020-07-23 stsp return NULL;
1246 5a1fbc73 2020-07-23 stsp }
1247 5a1fbc73 2020-07-23 stsp
1248 5a1fbc73 2020-07-23 stsp static const struct got_error *
1249 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1252 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1253 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1254 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1255 9d31a1d8 2018-03-11 stsp {
1256 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1257 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1258 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1259 906c123b 2020-07-23 stsp char *path_got = NULL;
1260 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1262 8ba819a3 2020-07-23 stsp
1263 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1264 2e1fa222 2020-07-23 stsp
1265 8ba819a3 2020-07-23 stsp /*
1266 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1267 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1268 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1269 8ba819a3 2020-07-23 stsp * in the blob object.
1270 8ba819a3 2020-07-23 stsp */
1271 8ba819a3 2020-07-23 stsp do {
1272 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1273 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1274 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1276 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1277 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1278 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1280 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1281 3b9f0f87 2020-07-23 stsp progress_arg);
1282 8ba819a3 2020-07-23 stsp }
1283 8ba819a3 2020-07-23 stsp if (len > 0) {
1284 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1285 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1286 8ba819a3 2020-07-23 stsp len - hdrlen);
1287 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1288 8ba819a3 2020-07-23 stsp hdrlen = 0;
1289 8ba819a3 2020-07-23 stsp }
1290 8ba819a3 2020-07-23 stsp } while (len != 0);
1291 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1292 8ba819a3 2020-07-23 stsp
1293 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1295 3c1ec782 2020-07-23 stsp if (err)
1296 3c1ec782 2020-07-23 stsp return err;
1297 8ba819a3 2020-07-23 stsp
1298 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1299 906c123b 2020-07-23 stsp /* install as a regular file */
1300 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1301 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1302 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1304 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1305 906c123b 2020-07-23 stsp goto done;
1306 906c123b 2020-07-23 stsp }
1307 906c123b 2020-07-23 stsp
1308 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1309 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1310 c6e8a826 2021-04-05 stsp int symlink_replaced;
1311 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1312 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1313 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1314 c90c8ce3 2020-07-23 stsp goto done;
1315 c90c8ce3 2020-07-23 stsp }
1316 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1317 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1318 5a1fbc73 2020-07-23 stsp if (err)
1319 f35fa46a 2020-07-23 stsp goto done;
1320 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1321 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1322 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1323 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1324 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1325 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1326 c6e8a826 2021-04-05 stsp } else {
1327 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1328 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1329 c6e8a826 2021-04-05 stsp }
1330 f35fa46a 2020-07-23 stsp }
1331 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1332 f35fa46a 2020-07-23 stsp }
1333 f35fa46a 2020-07-23 stsp
1334 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1335 f4994adc 2020-10-20 stsp char *parent;
1336 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1337 f4994adc 2020-10-20 stsp if (err)
1338 f4994adc 2020-10-20 stsp goto done;
1339 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1340 f4994adc 2020-10-20 stsp free(parent);
1341 f35fa46a 2020-07-23 stsp if (err)
1342 f35fa46a 2020-07-23 stsp goto done;
1343 f35fa46a 2020-07-23 stsp /*
1344 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1345 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1346 f35fa46a 2020-07-23 stsp */
1347 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1348 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1349 f35fa46a 2020-07-23 stsp goto done;
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp }
1352 f35fa46a 2020-07-23 stsp
1353 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1354 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1355 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1356 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1357 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1358 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1359 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1361 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1362 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1363 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1364 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1365 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1366 8ba819a3 2020-07-23 stsp } else {
1367 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1368 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1369 8ba819a3 2020-07-23 stsp }
1370 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1371 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 8ba819a3 2020-07-23 stsp done:
1374 906c123b 2020-07-23 stsp free(path_got);
1375 8ba819a3 2020-07-23 stsp return err;
1376 8ba819a3 2020-07-23 stsp }
1377 8ba819a3 2020-07-23 stsp
1378 8ba819a3 2020-07-23 stsp static const struct got_error *
1379 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1381 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1382 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1383 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1384 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1385 8ba819a3 2020-07-23 stsp {
1386 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1387 507dc3bb 2018-12-29 stsp int fd = -1;
1388 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1389 507dc3bb 2018-12-29 stsp int update = 0;
1390 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1391 8ba819a3 2020-07-23 stsp
1392 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 8bd0cdad 2021-12-31 stsp O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1395 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1396 f5375317 2020-10-20 stsp char *parent;
1397 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1398 f5375317 2020-10-20 stsp if (err)
1399 f5375317 2020-10-20 stsp return err;
1400 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1401 f5375317 2020-10-20 stsp free(parent);
1402 21908da4 2019-01-13 stsp if (err)
1403 21908da4 2019-01-13 stsp return err;
1404 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1405 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1407 21908da4 2019-01-13 stsp if (fd == -1)
1408 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1409 230a42bd 2019-05-11 jcs ondisk_path);
1410 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1411 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1412 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1413 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1414 3b9f0f87 2020-07-23 stsp goto done;
1415 3b9f0f87 2020-07-23 stsp }
1416 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1419 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1420 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1421 507dc3bb 2018-12-29 stsp goto done;
1422 d70b8e30 2018-12-27 stsp } else {
1423 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1424 507dc3bb 2018-12-29 stsp ondisk_path);
1425 507dc3bb 2018-12-29 stsp if (err)
1426 507dc3bb 2018-12-29 stsp goto done;
1427 507dc3bb 2018-12-29 stsp update = 1;
1428 9d31a1d8 2018-03-11 stsp }
1429 507dc3bb 2018-12-29 stsp } else
1430 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1431 9d31a1d8 2018-03-11 stsp }
1432 9d31a1d8 2018-03-11 stsp
1433 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1435 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1436 3818e3c4 2020-11-01 naddy goto done;
1437 3818e3c4 2020-11-01 naddy }
1438 3818e3c4 2020-11-01 naddy
1439 bd6aa359 2020-07-23 stsp if (progress_cb) {
1440 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1441 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 bd6aa359 2020-07-23 stsp path);
1443 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1444 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 bd6aa359 2020-07-23 stsp path);
1446 bd6aa359 2020-07-23 stsp else
1447 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1448 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 bd6aa359 2020-07-23 stsp if (err)
1450 bd6aa359 2020-07-23 stsp goto done;
1451 bd6aa359 2020-07-23 stsp }
1452 d7b62c98 2018-12-27 stsp
1453 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1454 9d31a1d8 2018-03-11 stsp do {
1455 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1457 9d31a1d8 2018-03-11 stsp if (err)
1458 9d31a1d8 2018-03-11 stsp break;
1459 9d31a1d8 2018-03-11 stsp if (len > 0) {
1460 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1461 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1463 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1464 b87c6f83 2018-12-24 stsp goto done;
1465 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1466 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1467 b87c6f83 2018-12-24 stsp goto done;
1468 9d31a1d8 2018-03-11 stsp }
1469 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1470 9d31a1d8 2018-03-11 stsp }
1471 9d31a1d8 2018-03-11 stsp } while (len != 0);
1472 9d31a1d8 2018-03-11 stsp
1473 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1474 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1475 816dc654 2019-02-16 stsp goto done;
1476 816dc654 2019-02-16 stsp }
1477 9d31a1d8 2018-03-11 stsp
1478 507dc3bb 2018-12-29 stsp if (update) {
1479 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1481 f6d8c0ac 2020-11-09 stsp goto done;
1482 f6d8c0ac 2020-11-09 stsp }
1483 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1484 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1485 230a42bd 2019-05-11 jcs ondisk_path);
1486 68ed9ba5 2019-02-10 stsp goto done;
1487 68ed9ba5 2019-02-10 stsp }
1488 63df146d 2020-11-09 stsp free(tmppath);
1489 63df146d 2020-11-09 stsp tmppath = NULL;
1490 507dc3bb 2018-12-29 stsp }
1491 507dc3bb 2018-12-29 stsp
1492 9d31a1d8 2018-03-11 stsp done:
1493 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1494 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1495 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1497 507dc3bb 2018-12-29 stsp free(tmppath);
1498 6353ad76 2019-02-08 stsp return err;
1499 6353ad76 2019-02-08 stsp }
1500 6353ad76 2019-02-08 stsp
1501 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 6353ad76 2019-02-08 stsp static const struct got_error *
1503 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1504 7154f6ce 2019-03-27 stsp {
1505 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1506 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1507 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1509 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1510 7154f6ce 2019-03-27 stsp };
1511 7154f6ce 2019-03-27 stsp int i = 0;
1512 9bdd68dd 2021-01-02 naddy char *line = NULL;
1513 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1514 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1515 7154f6ce 2019-03-27 stsp
1516 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1517 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1518 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1519 7154f6ce 2019-03-27 stsp if (feof(f))
1520 7154f6ce 2019-03-27 stsp break;
1521 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1522 7154f6ce 2019-03-27 stsp break;
1523 7154f6ce 2019-03-27 stsp }
1524 7154f6ce 2019-03-27 stsp
1525 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 19332e6d 2019-05-13 stsp == 0)
1528 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1529 7154f6ce 2019-03-27 stsp else
1530 7154f6ce 2019-03-27 stsp i++;
1531 7154f6ce 2019-03-27 stsp }
1532 7154f6ce 2019-03-27 stsp }
1533 9bdd68dd 2021-01-02 naddy free(line);
1534 7154f6ce 2019-03-27 stsp
1535 7154f6ce 2019-03-27 stsp return err;
1536 e2b1e152 2019-07-13 stsp }
1537 e2b1e152 2019-07-13 stsp
1538 e2b1e152 2019-07-13 stsp static int
1539 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1540 1ebedb77 2019-10-19 stsp {
1541 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1543 1ebedb77 2019-10-19 stsp }
1544 1ebedb77 2019-10-19 stsp
1545 1ebedb77 2019-10-19 stsp static int
1546 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1547 e2b1e152 2019-07-13 stsp {
1548 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1553 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1554 c363b2c1 2019-08-03 stsp }
1555 c363b2c1 2019-08-03 stsp
1556 c363b2c1 2019-08-03 stsp static unsigned char
1557 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1558 c363b2c1 2019-08-03 stsp {
1559 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1560 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1561 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1562 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1563 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1564 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1565 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1566 c363b2c1 2019-08-03 stsp default:
1567 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1568 a919d5c4 2020-07-23 stsp }
1569 a919d5c4 2020-07-23 stsp }
1570 a919d5c4 2020-07-23 stsp
1571 a919d5c4 2020-07-23 stsp static const struct got_error *
1572 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1573 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1574 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1575 a919d5c4 2020-07-23 stsp {
1576 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1577 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1578 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1579 a919d5c4 2020-07-23 stsp ssize_t elen;
1580 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1581 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1583 a919d5c4 2020-07-23 stsp
1584 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1585 a919d5c4 2020-07-23 stsp
1586 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1587 a919d5c4 2020-07-23 stsp do {
1588 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1589 a919d5c4 2020-07-23 stsp if (err)
1590 a919d5c4 2020-07-23 stsp return err;
1591 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1592 a919d5c4 2020-07-23 stsp /*
1593 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1594 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1595 a919d5c4 2020-07-23 stsp */
1596 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1597 a919d5c4 2020-07-23 stsp }
1598 a919d5c4 2020-07-23 stsp if (len > 0) {
1599 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1600 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1601 a919d5c4 2020-07-23 stsp len - hdrlen);
1602 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1603 a919d5c4 2020-07-23 stsp hdrlen = 0;
1604 a919d5c4 2020-07-23 stsp }
1605 a919d5c4 2020-07-23 stsp } while (len != 0);
1606 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1607 a919d5c4 2020-07-23 stsp
1608 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1609 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 a919d5c4 2020-07-23 stsp if (elen == -1)
1611 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1612 a919d5c4 2020-07-23 stsp } else {
1613 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1614 a919d5c4 2020-07-23 stsp if (elen == -1)
1615 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1616 c363b2c1 2019-08-03 stsp }
1617 a919d5c4 2020-07-23 stsp
1618 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1620 a919d5c4 2020-07-23 stsp
1621 a919d5c4 2020-07-23 stsp return NULL;
1622 7154f6ce 2019-03-27 stsp }
1623 7154f6ce 2019-03-27 stsp
1624 7154f6ce 2019-03-27 stsp static const struct got_error *
1625 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1626 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1627 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1628 6353ad76 2019-02-08 stsp {
1629 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1630 6353ad76 2019-02-08 stsp struct got_object_id id;
1631 6353ad76 2019-02-08 stsp size_t hdrlen;
1632 1338848f 2019-12-13 stsp int fd = -1;
1633 6353ad76 2019-02-08 stsp FILE *f = NULL;
1634 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1635 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1636 6353ad76 2019-02-08 stsp size_t flen, blen;
1637 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1638 6353ad76 2019-02-08 stsp
1639 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1640 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1641 6353ad76 2019-02-08 stsp
1642 7f91a133 2019-12-13 stsp /*
1643 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1644 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1645 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1646 7f91a133 2019-12-13 stsp */
1647 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1648 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1650 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1651 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1652 882ef1b9 2019-12-13 stsp else
1653 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1654 882ef1b9 2019-12-13 stsp goto done;
1655 882ef1b9 2019-12-13 stsp }
1656 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1657 3d35a492 2019-12-13 stsp goto done;
1658 3d35a492 2019-12-13 stsp }
1659 7f91a133 2019-12-13 stsp } else {
1660 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1662 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1663 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1664 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1666 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1667 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1669 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1670 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1671 3d35a492 2019-12-13 stsp else
1672 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1673 3d35a492 2019-12-13 stsp goto done;
1674 3d35a492 2019-12-13 stsp }
1675 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1676 1338848f 2019-12-13 stsp goto done;
1677 a378724f 2019-02-10 stsp }
1678 a378724f 2019-02-10 stsp }
1679 6353ad76 2019-02-08 stsp
1680 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1682 1338848f 2019-12-13 stsp goto done;
1683 3f148bc6 2019-07-27 stsp }
1684 efdd40df 2019-07-27 stsp
1685 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1687 1338848f 2019-12-13 stsp goto done;
1688 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1689 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1690 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1691 1338848f 2019-12-13 stsp goto done;
1692 d00136be 2019-03-26 stsp }
1693 b8f41171 2019-02-10 stsp
1694 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1695 f179e66d 2020-07-23 stsp goto done;
1696 f179e66d 2020-07-23 stsp
1697 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1698 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1700 1338848f 2019-12-13 stsp goto done;
1701 f179e66d 2020-07-23 stsp }
1702 6353ad76 2019-02-08 stsp
1703 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1704 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1705 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 c363b2c1 2019-08-03 stsp else
1707 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1708 c363b2c1 2019-08-03 stsp
1709 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 6353ad76 2019-02-08 stsp if (err)
1711 1338848f 2019-12-13 stsp goto done;
1712 6353ad76 2019-02-08 stsp
1713 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1714 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1715 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1716 a919d5c4 2020-07-23 stsp goto done;
1717 a919d5c4 2020-07-23 stsp }
1718 a919d5c4 2020-07-23 stsp
1719 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1720 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1721 ab0d4361 2019-12-13 stsp if (fd == -1) {
1722 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1723 ab0d4361 2019-12-13 stsp goto done;
1724 ab0d4361 2019-12-13 stsp }
1725 3d35a492 2019-12-13 stsp }
1726 3d35a492 2019-12-13 stsp
1727 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1728 6353ad76 2019-02-08 stsp if (f == NULL) {
1729 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1730 6353ad76 2019-02-08 stsp goto done;
1731 6353ad76 2019-02-08 stsp }
1732 1338848f 2019-12-13 stsp fd = -1;
1733 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1734 656b1f76 2019-05-11 jcs for (;;) {
1735 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1737 6353ad76 2019-02-08 stsp if (err)
1738 7154f6ce 2019-03-27 stsp goto done;
1739 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1740 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1742 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1743 7154f6ce 2019-03-27 stsp goto done;
1744 80c5c120 2019-02-19 stsp }
1745 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1746 6353ad76 2019-02-08 stsp if (flen != 0)
1747 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1748 6353ad76 2019-02-08 stsp break;
1749 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1750 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1751 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1752 6353ad76 2019-02-08 stsp break;
1753 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1754 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1755 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1757 6353ad76 2019-02-08 stsp break;
1758 6353ad76 2019-02-08 stsp }
1759 6353ad76 2019-02-08 stsp } else {
1760 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1761 6353ad76 2019-02-08 stsp break;
1762 6353ad76 2019-02-08 stsp }
1763 6353ad76 2019-02-08 stsp hdrlen = 0;
1764 6353ad76 2019-02-08 stsp }
1765 7154f6ce 2019-03-27 stsp
1766 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1767 7154f6ce 2019-03-27 stsp rewind(f);
1768 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1769 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1770 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1771 6353ad76 2019-02-08 stsp done:
1772 6353ad76 2019-02-08 stsp if (blob)
1773 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1774 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1775 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1776 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1777 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1778 9d31a1d8 2018-03-11 stsp return err;
1779 e2b1e152 2019-07-13 stsp }
1780 e2b1e152 2019-07-13 stsp
1781 e2b1e152 2019-07-13 stsp /*
1782 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1783 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1784 e2b1e152 2019-07-13 stsp */
1785 e2b1e152 2019-07-13 stsp static const struct got_error *
1786 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1788 e2b1e152 2019-07-13 stsp {
1789 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1791 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1792 e2b1e152 2019-07-13 stsp
1793 e2b1e152 2019-07-13 stsp return NULL;
1794 9d31a1d8 2018-03-11 stsp }
1795 9d31a1d8 2018-03-11 stsp
1796 9d31a1d8 2018-03-11 stsp static const struct got_error *
1797 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1798 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1800 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 0584f854 2019-04-06 stsp void *progress_arg)
1802 9d31a1d8 2018-03-11 stsp {
1803 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1804 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1805 6353ad76 2019-02-08 stsp char *ondisk_path;
1806 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1807 b8f41171 2019-02-10 stsp struct stat sb;
1808 9d31a1d8 2018-03-11 stsp
1809 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1811 6353ad76 2019-02-08 stsp
1812 abb4604f 2019-07-27 stsp if (ie) {
1813 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 a76c42e6 2019-08-03 stsp goto done;
1816 a76c42e6 2019-08-03 stsp }
1817 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 7f91a133 2019-12-13 stsp repo);
1819 abb4604f 2019-07-27 stsp if (err)
1820 abb4604f 2019-07-27 stsp goto done;
1821 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1823 c90c8ce3 2020-07-23 stsp } else {
1824 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1825 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1826 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1827 f6764181 2021-09-24 stsp ondisk_path);
1828 f6764181 2021-09-24 stsp goto done;
1829 f6764181 2021-09-24 stsp }
1830 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1832 f6764181 2021-09-24 stsp } else {
1833 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1835 f6764181 2021-09-24 stsp else
1836 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1837 f6764181 2021-09-24 stsp }
1838 c90c8ce3 2020-07-23 stsp }
1839 6353ad76 2019-02-08 stsp
1840 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1841 2c41dce7 2021-06-27 stsp if (ie)
1842 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1843 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1844 b8f41171 2019-02-10 stsp goto done;
1845 b8f41171 2019-02-10 stsp }
1846 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1847 a769b60b 2021-06-27 stsp if (ie)
1848 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1849 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1850 5036ab18 2020-04-18 stsp path);
1851 5036ab18 2020-04-18 stsp goto done;
1852 5036ab18 2020-04-18 stsp }
1853 b8f41171 2019-02-10 stsp
1854 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1856 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1857 c6e8a826 2021-04-05 stsp /*
1858 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1859 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1860 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1861 c6e8a826 2021-04-05 stsp * updating contents of this file.
1862 c6e8a826 2021-04-05 stsp */
1863 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1864 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1866 c6e8a826 2021-04-05 stsp /* Same commit. */
1867 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1868 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1869 e2b1e152 2019-07-13 stsp if (err)
1870 e2b1e152 2019-07-13 stsp goto done;
1871 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 b8f41171 2019-02-10 stsp path);
1873 6353ad76 2019-02-08 stsp goto done;
1874 a378724f 2019-02-10 stsp }
1875 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1876 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1877 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1878 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1879 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1880 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1881 0f58026f 2021-04-05 stsp if (err)
1882 0f58026f 2021-04-05 stsp goto done;
1883 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1884 0f58026f 2021-04-05 stsp path);
1885 b8f41171 2019-02-10 stsp goto done;
1886 e2b1e152 2019-07-13 stsp }
1887 9d31a1d8 2018-03-11 stsp }
1888 9d31a1d8 2018-03-11 stsp
1889 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 8da9e5f4 2019-01-12 stsp if (err)
1891 6353ad76 2019-02-08 stsp goto done;
1892 512f0d0e 2019-01-02 stsp
1893 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 234035bc 2019-06-01 stsp int update_timestamps;
1895 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1896 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1897 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1898 234035bc 2019-06-01 stsp struct got_object_id id2;
1899 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 234035bc 2019-06-01 stsp if (err)
1902 f69721c3 2019-10-21 stsp goto done;
1903 f69721c3 2019-10-21 stsp }
1904 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1905 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1908 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1909 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1910 f69721c3 2019-10-21 stsp goto done;
1911 f69721c3 2019-10-21 stsp }
1912 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1913 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1915 234035bc 2019-06-01 stsp goto done;
1916 f69721c3 2019-10-21 stsp }
1917 234035bc 2019-06-01 stsp }
1918 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 36bf999c 2020-07-23 stsp char *link_target;
1920 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1921 36bf999c 2020-07-23 stsp if (err)
1922 36bf999c 2020-07-23 stsp goto done;
1923 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1925 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1926 36bf999c 2020-07-23 stsp free(link_target);
1927 993e2a1b 2020-07-23 stsp } else {
1928 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1929 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1930 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1931 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1932 993e2a1b 2020-07-23 stsp }
1933 f69721c3 2019-10-21 stsp free(label_orig);
1934 234035bc 2019-06-01 stsp if (blob2)
1935 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1936 909d120e 2019-09-22 stsp if (err)
1937 909d120e 2019-09-22 stsp goto done;
1938 234035bc 2019-06-01 stsp /*
1939 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1940 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1941 234035bc 2019-06-01 stsp * unmodified files again.
1942 234035bc 2019-06-01 stsp */
1943 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1945 234035bc 2019-06-01 stsp update_timestamps);
1946 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1950 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 1ee397ad 2019-07-12 stsp if (err)
1952 1ee397ad 2019-07-12 stsp goto done;
1953 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 13d9040b 2019-03-27 stsp if (err)
1956 13d9040b 2019-03-27 stsp goto done;
1957 13d9040b 2019-03-27 stsp } else {
1958 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1959 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1960 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1961 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1962 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1963 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
1964 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
1965 2e1fa222 2020-07-23 stsp } else {
1966 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1967 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1968 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1969 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1970 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1971 2e1fa222 2020-07-23 stsp }
1972 13d9040b 2019-03-27 stsp if (err)
1973 13d9040b 2019-03-27 stsp goto done;
1974 65b05cec 2020-07-23 stsp
1975 054041d0 2020-06-24 stsp if (ie) {
1976 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
1977 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
1978 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
1979 054041d0 2020-06-24 stsp } else {
1980 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1981 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
1982 054041d0 2020-06-24 stsp &blob->id);
1983 054041d0 2020-06-24 stsp }
1984 13d9040b 2019-03-27 stsp if (err)
1985 13d9040b 2019-03-27 stsp goto done;
1986 65b05cec 2020-07-23 stsp
1987 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1988 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1989 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1990 2e1fa222 2020-07-23 stsp }
1991 13d9040b 2019-03-27 stsp }
1992 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1993 6353ad76 2019-02-08 stsp done:
1994 6353ad76 2019-02-08 stsp free(ondisk_path);
1995 8da9e5f4 2019-01-12 stsp return err;
1996 90285c3b 2019-01-08 stsp }
1997 90285c3b 2019-01-08 stsp
1998 90285c3b 2019-01-08 stsp static const struct got_error *
1999 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2000 90285c3b 2019-01-08 stsp {
2001 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2002 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2003 90285c3b 2019-01-08 stsp
2004 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2006 90285c3b 2019-01-08 stsp
2007 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2008 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2009 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2010 c97665ae 2019-01-13 stsp } else {
2011 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2012 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2013 1c4cdd89 2021-06-20 stsp if (err)
2014 1c4cdd89 2021-06-20 stsp goto done;
2015 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2016 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2017 fddefe3b 2020-10-20 stsp free(ondisk_path);
2018 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2019 1c4cdd89 2021-06-20 stsp parent = NULL;
2020 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2021 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2022 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2023 fddefe3b 2020-10-20 stsp ondisk_path);
2024 90285c3b 2019-01-08 stsp break;
2025 90285c3b 2019-01-08 stsp }
2026 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2027 1c4cdd89 2021-06-20 stsp if (err)
2028 1c4cdd89 2021-06-20 stsp break;
2029 1c4cdd89 2021-06-20 stsp }
2030 90285c3b 2019-01-08 stsp }
2031 1c4cdd89 2021-06-20 stsp done:
2032 90285c3b 2019-01-08 stsp free(ondisk_path);
2033 1c4cdd89 2021-06-20 stsp free(parent);
2034 90285c3b 2019-01-08 stsp return err;
2035 512f0d0e 2019-01-02 stsp }
2036 512f0d0e 2019-01-02 stsp
2037 708d8e67 2019-03-27 stsp static const struct got_error *
2038 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2040 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2041 708d8e67 2019-03-27 stsp {
2042 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2043 708d8e67 2019-03-27 stsp unsigned char status;
2044 708d8e67 2019-03-27 stsp struct stat sb;
2045 708d8e67 2019-03-27 stsp char *ondisk_path;
2046 708d8e67 2019-03-27 stsp
2047 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2049 a76c42e6 2019-08-03 stsp
2050 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 708d8e67 2019-03-27 stsp == -1)
2052 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2053 708d8e67 2019-03-27 stsp
2054 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 708d8e67 2019-03-27 stsp if (err)
2056 5a58a424 2020-06-23 stsp goto done;
2057 708d8e67 2019-03-27 stsp
2058 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2060 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2062 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2063 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2064 993e2a1b 2020-07-23 stsp goto done;
2065 993e2a1b 2020-07-23 stsp }
2066 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2067 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2069 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2070 993e2a1b 2020-07-23 stsp if (err)
2071 993e2a1b 2020-07-23 stsp goto done;
2072 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 993e2a1b 2020-07-23 stsp ie->path);
2074 993e2a1b 2020-07-23 stsp goto done;
2075 993e2a1b 2020-07-23 stsp }
2076 993e2a1b 2020-07-23 stsp
2077 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2079 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 1ee397ad 2019-07-12 stsp if (err)
2081 5a58a424 2020-06-23 stsp goto done;
2082 fc6346c4 2019-03-27 stsp /*
2083 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2084 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2085 fc6346c4 2019-03-27 stsp */
2086 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2088 fc6346c4 2019-03-27 stsp } else {
2089 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 1ee397ad 2019-07-12 stsp if (err)
2091 5a58a424 2020-06-23 stsp goto done;
2092 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2093 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2094 fc6346c4 2019-03-27 stsp if (err)
2095 5a58a424 2020-06-23 stsp goto done;
2096 fc6346c4 2019-03-27 stsp }
2097 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2098 708d8e67 2019-03-27 stsp }
2099 5a58a424 2020-06-23 stsp done:
2100 5a58a424 2020-06-23 stsp free(ondisk_path);
2101 fc6346c4 2019-03-27 stsp return err;
2102 708d8e67 2019-03-27 stsp }
2103 708d8e67 2019-03-27 stsp
2104 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2105 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2106 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2107 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2108 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2109 8da9e5f4 2019-01-12 stsp void *progress_arg;
2110 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2111 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2112 8da9e5f4 2019-01-12 stsp };
2113 8da9e5f4 2019-01-12 stsp
2114 512f0d0e 2019-01-02 stsp static const struct got_error *
2115 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2117 512f0d0e 2019-01-02 stsp {
2118 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2119 0584f854 2019-04-06 stsp
2120 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2122 512f0d0e 2019-01-02 stsp
2123 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2124 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2125 8da9e5f4 2019-01-12 stsp }
2126 512f0d0e 2019-01-02 stsp
2127 8da9e5f4 2019-01-12 stsp static const struct got_error *
2128 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2129 8da9e5f4 2019-01-12 stsp {
2130 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2131 7a9df742 2019-01-08 stsp
2132 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2134 0584f854 2019-04-06 stsp
2135 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2136 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2137 9d31a1d8 2018-03-11 stsp }
2138 9d31a1d8 2018-03-11 stsp
2139 9d31a1d8 2018-03-11 stsp static const struct got_error *
2140 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2141 9d31a1d8 2018-03-11 stsp {
2142 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2143 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2144 8da9e5f4 2019-01-12 stsp char *path;
2145 9d31a1d8 2018-03-11 stsp
2146 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2148 0584f854 2019-04-06 stsp
2149 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2150 63c5ca5d 2019-08-24 stsp return NULL;
2151 63c5ca5d 2019-08-24 stsp
2152 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2153 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2154 8da9e5f4 2019-01-12 stsp == -1)
2155 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2156 9d31a1d8 2018-03-11 stsp
2157 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2158 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2159 8da9e5f4 2019-01-12 stsp else
2160 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2162 9d31a1d8 2018-03-11 stsp
2163 8da9e5f4 2019-01-12 stsp free(path);
2164 0cd1c46a 2019-03-11 stsp return err;
2165 0cd1c46a 2019-03-11 stsp }
2166 0cd1c46a 2019-03-11 stsp
2167 b2118c49 2020-07-28 stsp const struct got_error *
2168 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2169 b2118c49 2020-07-28 stsp {
2170 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2171 b2118c49 2020-07-28 stsp
2172 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2174 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2175 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2176 b2118c49 2020-07-28 stsp }
2177 b2118c49 2020-07-28 stsp
2178 b2118c49 2020-07-28 stsp return NULL;
2179 b2118c49 2020-07-28 stsp }
2180 b2118c49 2020-07-28 stsp
2181 818c7501 2019-07-11 stsp static const struct got_error *
2182 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2183 0cd1c46a 2019-03-11 stsp {
2184 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2185 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2186 0cd1c46a 2019-03-11 stsp
2187 517bab73 2019-03-11 stsp *refname = NULL;
2188 517bab73 2019-03-11 stsp
2189 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2190 b2118c49 2020-07-28 stsp if (err)
2191 b2118c49 2020-07-28 stsp return err;
2192 0cd1c46a 2019-03-11 stsp
2193 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2195 0647c563 2019-03-11 stsp *refname = NULL;
2196 0cd1c46a 2019-03-11 stsp }
2197 517bab73 2019-03-11 stsp free(uuidstr);
2198 517bab73 2019-03-11 stsp return err;
2199 517bab73 2019-03-11 stsp }
2200 517bab73 2019-03-11 stsp
2201 818c7501 2019-07-11 stsp const struct got_error *
2202 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2203 818c7501 2019-07-11 stsp {
2204 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2205 818c7501 2019-07-11 stsp }
2206 818c7501 2019-07-11 stsp
2207 818c7501 2019-07-11 stsp static const struct got_error *
2208 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 818c7501 2019-07-11 stsp {
2210 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2211 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2212 818c7501 2019-07-11 stsp }
2213 818c7501 2019-07-11 stsp
2214 818c7501 2019-07-11 stsp static const struct got_error *
2215 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2216 818c7501 2019-07-11 stsp {
2217 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2218 818c7501 2019-07-11 stsp }
2219 818c7501 2019-07-11 stsp
2220 818c7501 2019-07-11 stsp static const struct got_error *
2221 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2222 818c7501 2019-07-11 stsp {
2223 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2224 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2225 818c7501 2019-07-11 stsp }
2226 818c7501 2019-07-11 stsp
2227 818c7501 2019-07-11 stsp static const struct got_error *
2228 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 818c7501 2019-07-11 stsp {
2230 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2231 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2232 0ebf8283 2019-07-24 stsp }
2233 0ebf8283 2019-07-24 stsp
2234 0ebf8283 2019-07-24 stsp static const struct got_error *
2235 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 0ebf8283 2019-07-24 stsp {
2237 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2238 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2239 0ebf8283 2019-07-24 stsp }
2240 0ebf8283 2019-07-24 stsp
2241 0ebf8283 2019-07-24 stsp static const struct got_error *
2242 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 0ebf8283 2019-07-24 stsp {
2244 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2245 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2246 0ebf8283 2019-07-24 stsp }
2247 0ebf8283 2019-07-24 stsp
2248 0ebf8283 2019-07-24 stsp static const struct got_error *
2249 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 0ebf8283 2019-07-24 stsp {
2251 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2252 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2253 818c7501 2019-07-11 stsp }
2254 818c7501 2019-07-11 stsp
2255 0ebf8283 2019-07-24 stsp static const struct got_error *
2256 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 0ebf8283 2019-07-24 stsp {
2258 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2259 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2260 0ebf8283 2019-07-24 stsp }
2261 818c7501 2019-07-11 stsp
2262 0ebf8283 2019-07-24 stsp const struct got_error *
2263 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2264 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2265 0ebf8283 2019-07-24 stsp {
2266 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 0ebf8283 2019-07-24 stsp *path = NULL;
2269 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2270 0ebf8283 2019-07-24 stsp }
2271 0ebf8283 2019-07-24 stsp return NULL;
2272 f259c4c1 2021-09-24 stsp }
2273 f259c4c1 2021-09-24 stsp
2274 f259c4c1 2021-09-24 stsp static const struct got_error *
2275 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2276 f259c4c1 2021-09-24 stsp {
2277 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2278 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2279 0ebf8283 2019-07-24 stsp }
2280 0ebf8283 2019-07-24 stsp
2281 f259c4c1 2021-09-24 stsp static const struct got_error *
2282 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 f259c4c1 2021-09-24 stsp {
2284 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2285 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 f259c4c1 2021-09-24 stsp }
2287 f259c4c1 2021-09-24 stsp
2288 517bab73 2019-03-11 stsp /*
2289 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2290 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2291 517bab73 2019-03-11 stsp */
2292 517bab73 2019-03-11 stsp static const struct got_error *
2293 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2294 517bab73 2019-03-11 stsp {
2295 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2296 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2297 517bab73 2019-03-11 stsp char *refname;
2298 517bab73 2019-03-11 stsp
2299 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2300 517bab73 2019-03-11 stsp if (err)
2301 517bab73 2019-03-11 stsp return err;
2302 0cd1c46a 2019-03-11 stsp
2303 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 0cd1c46a 2019-03-11 stsp if (err)
2305 0cd1c46a 2019-03-11 stsp goto done;
2306 0cd1c46a 2019-03-11 stsp
2307 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2308 0cd1c46a 2019-03-11 stsp done:
2309 0cd1c46a 2019-03-11 stsp free(refname);
2310 0cd1c46a 2019-03-11 stsp if (ref)
2311 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2312 3e3a69f1 2019-07-25 stsp return err;
2313 3e3a69f1 2019-07-25 stsp }
2314 3e3a69f1 2019-07-25 stsp
2315 3e3a69f1 2019-07-25 stsp static const struct got_error *
2316 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2317 3e3a69f1 2019-07-25 stsp {
2318 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2319 3e3a69f1 2019-07-25 stsp
2320 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2323 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2324 3e3a69f1 2019-07-25 stsp }
2325 9d31a1d8 2018-03-11 stsp return err;
2326 9d31a1d8 2018-03-11 stsp }
2327 9d31a1d8 2018-03-11 stsp
2328 3e3a69f1 2019-07-25 stsp
2329 ebf99748 2019-05-09 stsp static const struct got_error *
2330 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2332 ebf99748 2019-05-09 stsp {
2333 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2334 ebf99748 2019-05-09 stsp FILE *index = NULL;
2335 0cd1c46a 2019-03-11 stsp
2336 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2337 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2338 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2339 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2340 ebf99748 2019-05-09 stsp
2341 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2342 3e3a69f1 2019-07-25 stsp if (err)
2343 ebf99748 2019-05-09 stsp goto done;
2344 ebf99748 2019-05-09 stsp
2345 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2346 ebf99748 2019-05-09 stsp if (index == NULL) {
2347 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2348 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2349 ebf99748 2019-05-09 stsp } else {
2350 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2351 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2353 ebf99748 2019-05-09 stsp }
2354 ebf99748 2019-05-09 stsp done:
2355 ebf99748 2019-05-09 stsp if (err) {
2356 ebf99748 2019-05-09 stsp free(*fileindex_path);
2357 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2358 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2359 ebf99748 2019-05-09 stsp *fileindex = NULL;
2360 ebf99748 2019-05-09 stsp }
2361 ebf99748 2019-05-09 stsp return err;
2362 ebf99748 2019-05-09 stsp }
2363 c932eeeb 2019-05-22 stsp
2364 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2365 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2366 c932eeeb 2019-05-22 stsp const char *path;
2367 c932eeeb 2019-05-22 stsp size_t path_len;
2368 c932eeeb 2019-05-22 stsp const char *entry_name;
2369 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2370 a484d721 2019-06-10 stsp void *progress_arg;
2371 c932eeeb 2019-05-22 stsp };
2372 ebf99748 2019-05-09 stsp
2373 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2374 c932eeeb 2019-05-22 stsp static const struct got_error *
2375 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2376 c932eeeb 2019-05-22 stsp {
2377 1ee397ad 2019-07-12 stsp const struct got_error *err;
2378 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2379 c932eeeb 2019-05-22 stsp
2380 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2381 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2382 c932eeeb 2019-05-22 stsp return NULL;
2383 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 102ff934 2019-06-11 stsp return NULL;
2385 102ff934 2019-06-11 stsp
2386 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2387 a769b60b 2021-06-27 stsp return NULL;
2388 a769b60b 2021-06-27 stsp
2389 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2391 c932eeeb 2019-05-22 stsp return NULL;
2392 c932eeeb 2019-05-22 stsp
2393 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2394 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 edd02c5e 2019-07-11 stsp ie->path);
2396 1ee397ad 2019-07-12 stsp if (err)
2397 1ee397ad 2019-07-12 stsp return err;
2398 1ee397ad 2019-07-12 stsp }
2399 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 c932eeeb 2019-05-22 stsp return NULL;
2401 a615e0e7 2020-12-16 stsp }
2402 a615e0e7 2020-12-16 stsp
2403 a615e0e7 2020-12-16 stsp static const struct got_error *
2404 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2406 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2407 a615e0e7 2020-12-16 stsp {
2408 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2409 a615e0e7 2020-12-16 stsp
2410 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2411 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2412 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2413 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2414 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2415 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2416 a615e0e7 2020-12-16 stsp
2417 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2418 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2419 9c6338c4 2019-06-02 stsp }
2420 9c6338c4 2019-06-02 stsp
2421 9c6338c4 2019-06-02 stsp static const struct got_error *
2422 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2423 9c6338c4 2019-06-02 stsp {
2424 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2425 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2426 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2427 867630bb 2020-01-17 stsp struct timespec timeout;
2428 9c6338c4 2019-06-02 stsp
2429 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 9c6338c4 2019-06-02 stsp fileindex_path);
2431 9c6338c4 2019-06-02 stsp if (err)
2432 9c6338c4 2019-06-02 stsp goto done;
2433 9c6338c4 2019-06-02 stsp
2434 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2435 9c6338c4 2019-06-02 stsp if (err)
2436 9c6338c4 2019-06-02 stsp goto done;
2437 9c6338c4 2019-06-02 stsp
2438 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2440 9c6338c4 2019-06-02 stsp fileindex_path);
2441 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2442 9c6338c4 2019-06-02 stsp }
2443 867630bb 2020-01-17 stsp
2444 867630bb 2020-01-17 stsp /*
2445 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2446 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2447 867630bb 2020-01-17 stsp * was recorded in the file index.
2448 867630bb 2020-01-17 stsp */
2449 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2450 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2451 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2452 9c6338c4 2019-06-02 stsp done:
2453 9c6338c4 2019-06-02 stsp if (new_index)
2454 9c6338c4 2019-06-02 stsp fclose(new_index);
2455 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2456 9c6338c4 2019-06-02 stsp return err;
2457 c932eeeb 2019-05-22 stsp }
2458 6ced4176 2019-07-12 stsp
2459 6ced4176 2019-07-12 stsp static const struct got_error *
2460 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2462 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2463 6ced4176 2019-07-12 stsp {
2464 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2465 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2466 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2467 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2468 6ced4176 2019-07-12 stsp
2469 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2470 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2471 6ced4176 2019-07-12 stsp *tree_id = NULL;
2472 6ced4176 2019-07-12 stsp
2473 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2474 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2475 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2476 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2477 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2478 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2479 6ced4176 2019-07-12 stsp goto done;
2480 6ced4176 2019-07-12 stsp }
2481 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2482 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2483 6ced4176 2019-07-12 stsp if (err)
2484 6ced4176 2019-07-12 stsp goto done;
2485 6ced4176 2019-07-12 stsp return NULL;
2486 6ced4176 2019-07-12 stsp }
2487 6ced4176 2019-07-12 stsp
2488 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2489 6ced4176 2019-07-12 stsp
2490 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2493 6ced4176 2019-07-12 stsp goto done;
2494 6ced4176 2019-07-12 stsp }
2495 6ced4176 2019-07-12 stsp
2496 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 6ced4176 2019-07-12 stsp in_repo_path);
2498 6ced4176 2019-07-12 stsp if (err)
2499 6ced4176 2019-07-12 stsp goto done;
2500 6ced4176 2019-07-12 stsp
2501 6ced4176 2019-07-12 stsp free(in_repo_path);
2502 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2503 6ced4176 2019-07-12 stsp
2504 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2505 6ced4176 2019-07-12 stsp if (err)
2506 6ced4176 2019-07-12 stsp goto done;
2507 c932eeeb 2019-05-22 stsp
2508 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 6ced4176 2019-07-12 stsp /* Check out a single file. */
2510 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2511 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2512 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2513 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2514 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2515 6ced4176 2019-07-12 stsp goto done;
2516 6ced4176 2019-07-12 stsp }
2517 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2518 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2520 6ced4176 2019-07-12 stsp goto done;
2521 6ced4176 2019-07-12 stsp }
2522 6ced4176 2019-07-12 stsp } else {
2523 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2524 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2525 6ced4176 2019-07-12 stsp if (err)
2526 6ced4176 2019-07-12 stsp return err;
2527 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2528 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2529 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2531 6ced4176 2019-07-12 stsp goto done;
2532 6ced4176 2019-07-12 stsp }
2533 6ced4176 2019-07-12 stsp }
2534 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2535 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2536 6ced4176 2019-07-12 stsp } else {
2537 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2538 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2539 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2540 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2541 6ced4176 2019-07-12 stsp goto done;
2542 6ced4176 2019-07-12 stsp }
2543 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2544 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2546 6ced4176 2019-07-12 stsp goto done;
2547 6ced4176 2019-07-12 stsp }
2548 6ced4176 2019-07-12 stsp }
2549 6ced4176 2019-07-12 stsp done:
2550 6ced4176 2019-07-12 stsp free(id);
2551 6ced4176 2019-07-12 stsp free(in_repo_path);
2552 6ced4176 2019-07-12 stsp if (err) {
2553 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2554 6ced4176 2019-07-12 stsp free(*tree_relpath);
2555 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2556 6ced4176 2019-07-12 stsp free(*tree_id);
2557 6ced4176 2019-07-12 stsp *tree_id = NULL;
2558 6ced4176 2019-07-12 stsp }
2559 07ed472d 2019-07-12 stsp return err;
2560 07ed472d 2019-07-12 stsp }
2561 07ed472d 2019-07-12 stsp
2562 07ed472d 2019-07-12 stsp static const struct got_error *
2563 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2567 07ed472d 2019-07-12 stsp {
2568 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2569 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2570 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2571 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2572 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2573 07ed472d 2019-07-12 stsp
2574 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2575 7f47418f 2019-12-20 stsp if (err) {
2576 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2577 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2578 7f47418f 2019-12-20 stsp goto done;
2579 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2580 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 7f47418f 2019-12-20 stsp if (err)
2582 7f47418f 2019-12-20 stsp return err;
2583 7f47418f 2019-12-20 stsp }
2584 07ed472d 2019-07-12 stsp
2585 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2586 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2587 07ed472d 2019-07-12 stsp if (err)
2588 07ed472d 2019-07-12 stsp goto done;
2589 07ed472d 2019-07-12 stsp
2590 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2591 07ed472d 2019-07-12 stsp if (err)
2592 07ed472d 2019-07-12 stsp goto done;
2593 07ed472d 2019-07-12 stsp
2594 07ed472d 2019-07-12 stsp if (entry_name &&
2595 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 07ed472d 2019-07-12 stsp goto done;
2598 07ed472d 2019-07-12 stsp }
2599 07ed472d 2019-07-12 stsp
2600 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2601 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2602 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2603 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2604 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2605 07ed472d 2019-07-12 stsp arg.repo = repo;
2606 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2607 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2608 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2609 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2610 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2612 07ed472d 2019-07-12 stsp done:
2613 07ed472d 2019-07-12 stsp if (tree)
2614 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2615 07ed472d 2019-07-12 stsp if (commit)
2616 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2617 6ced4176 2019-07-12 stsp return err;
2618 6ced4176 2019-07-12 stsp }
2619 6ced4176 2019-07-12 stsp
2620 9d31a1d8 2018-03-11 stsp const struct got_error *
2621 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2622 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2623 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2625 9d31a1d8 2018-03-11 stsp {
2626 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2628 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2629 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2630 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2631 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2632 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2633 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2634 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2635 f2ea84fa 2019-07-27 stsp int entry_type;
2636 f2ea84fa 2019-07-27 stsp char *relpath;
2637 f2ea84fa 2019-07-27 stsp char *entry_name;
2638 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2639 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2640 9d31a1d8 2018-03-11 stsp
2641 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2642 f2ea84fa 2019-07-27 stsp
2643 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2644 9d31a1d8 2018-03-11 stsp if (err)
2645 9d31a1d8 2018-03-11 stsp return err;
2646 93a30277 2018-12-24 stsp
2647 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2648 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2649 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2650 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2651 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2652 f2ea84fa 2019-07-27 stsp goto done;
2653 f2ea84fa 2019-07-27 stsp }
2654 6ced4176 2019-07-12 stsp
2655 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 f2ea84fa 2019-07-27 stsp if (err) {
2658 f2ea84fa 2019-07-27 stsp free(tpd);
2659 6ced4176 2019-07-12 stsp goto done;
2660 6ced4176 2019-07-12 stsp }
2661 f2ea84fa 2019-07-27 stsp
2662 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2664 f2ea84fa 2019-07-27 stsp if (err) {
2665 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2666 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2667 f2ea84fa 2019-07-27 stsp free(tpd);
2668 f2ea84fa 2019-07-27 stsp goto done;
2669 f2ea84fa 2019-07-27 stsp }
2670 f2ea84fa 2019-07-27 stsp } else
2671 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2672 f2ea84fa 2019-07-27 stsp
2673 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2674 6ced4176 2019-07-12 stsp }
2675 6ced4176 2019-07-12 stsp
2676 51514078 2018-12-25 stsp /*
2677 51514078 2018-12-25 stsp * Read the file index.
2678 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2679 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2680 51514078 2018-12-25 stsp */
2681 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2682 8da9e5f4 2019-01-12 stsp if (err)
2683 c4cdcb68 2019-04-03 stsp goto done;
2684 c4cdcb68 2019-04-03 stsp
2685 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2686 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2687 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2688 f2ea84fa 2019-07-27 stsp
2689 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2690 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2691 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 f2ea84fa 2019-07-27 stsp if (err)
2693 f2ea84fa 2019-07-27 stsp break;
2694 f2ea84fa 2019-07-27 stsp
2695 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2696 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2697 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2698 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2699 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2700 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2701 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2702 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2703 f2ea84fa 2019-07-27 stsp if (err)
2704 f2ea84fa 2019-07-27 stsp break;
2705 f2ea84fa 2019-07-27 stsp
2706 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2707 711d9cd9 2019-07-12 stsp }
2708 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2709 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2710 af12c6b9 2019-06-04 stsp err = sync_err;
2711 9d31a1d8 2018-03-11 stsp done:
2712 9c6338c4 2019-06-02 stsp free(fileindex_path);
2713 edfa7d7f 2018-09-11 stsp if (tree)
2714 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2715 9d31a1d8 2018-03-11 stsp if (commit)
2716 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2717 5ade8233 2019-07-12 stsp if (fileindex)
2718 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2719 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2720 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2721 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2722 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2723 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2724 f2ea84fa 2019-07-27 stsp free(tpd);
2725 f2ea84fa 2019-07-27 stsp }
2726 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2727 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2728 9d31a1d8 2018-03-11 stsp err = unlockerr;
2729 f8d1f275 2019-02-04 stsp return err;
2730 f8d1f275 2019-02-04 stsp }
2731 f8d1f275 2019-02-04 stsp
2732 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2733 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2734 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2735 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2736 234035bc 2019-06-01 stsp void *progress_arg;
2737 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2738 234035bc 2019-06-01 stsp void *cancel_arg;
2739 f69721c3 2019-10-21 stsp const char *label_orig;
2740 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2741 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2742 234035bc 2019-06-01 stsp };
2743 234035bc 2019-06-01 stsp
2744 234035bc 2019-06-01 stsp static const struct got_error *
2745 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2746 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2747 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2748 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2749 234035bc 2019-06-01 stsp {
2750 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2751 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2752 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2753 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2754 234035bc 2019-06-01 stsp struct stat sb;
2755 234035bc 2019-06-01 stsp unsigned char status;
2756 234035bc 2019-06-01 stsp int local_changes_subsumed;
2757 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2758 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2759 234035bc 2019-06-01 stsp
2760 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2761 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2762 d6c87207 2019-08-02 stsp strlen(path2));
2763 1ee397ad 2019-07-12 stsp if (ie == NULL)
2764 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2765 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2766 234035bc 2019-06-01 stsp
2767 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2768 234035bc 2019-06-01 stsp path2) == -1)
2769 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2770 234035bc 2019-06-01 stsp
2771 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2772 7f91a133 2019-12-13 stsp repo);
2773 234035bc 2019-06-01 stsp if (err)
2774 234035bc 2019-06-01 stsp goto done;
2775 234035bc 2019-06-01 stsp
2776 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2777 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2778 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2779 234035bc 2019-06-01 stsp goto done;
2780 234035bc 2019-06-01 stsp }
2781 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2782 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2783 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2784 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2785 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 234035bc 2019-06-01 stsp goto done;
2787 234035bc 2019-06-01 stsp }
2788 234035bc 2019-06-01 stsp
2789 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2790 36bf999c 2020-07-23 stsp char *link_target2;
2791 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2792 36bf999c 2020-07-23 stsp if (err)
2793 36bf999c 2020-07-23 stsp goto done;
2794 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2796 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2797 36bf999c 2020-07-23 stsp free(link_target2);
2798 af57b12a 2020-07-23 stsp } else {
2799 1af628f4 2021-06-03 stsp int fd;
2800 1af628f4 2021-06-03 stsp
2801 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2802 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2803 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2804 1af628f4 2021-06-03 stsp goto done;
2805 1af628f4 2021-06-03 stsp }
2806 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2807 1af628f4 2021-06-03 stsp f_orig, blob1);
2808 1af628f4 2021-06-03 stsp if (err)
2809 1af628f4 2021-06-03 stsp goto done;
2810 1af628f4 2021-06-03 stsp
2811 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2812 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2813 1af628f4 2021-06-03 stsp goto done;
2814 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2815 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2816 1af628f4 2021-06-03 stsp if (err)
2817 1af628f4 2021-06-03 stsp goto done;
2818 1af628f4 2021-06-03 stsp
2819 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2820 1af628f4 2021-06-03 stsp if (fd == -1) {
2821 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2822 1af628f4 2021-06-03 stsp ondisk_path);
2823 1af628f4 2021-06-03 stsp goto done;
2824 1af628f4 2021-06-03 stsp }
2825 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2826 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2827 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2828 1af628f4 2021-06-03 stsp ondisk_path);
2829 1af628f4 2021-06-03 stsp close(fd);
2830 1af628f4 2021-06-03 stsp goto done;
2831 1af628f4 2021-06-03 stsp }
2832 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2833 1af628f4 2021-06-03 stsp if (err)
2834 1af628f4 2021-06-03 stsp goto done;
2835 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2836 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2837 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2838 1af628f4 2021-06-03 stsp goto done;
2839 1af628f4 2021-06-03 stsp }
2840 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2841 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2842 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2843 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2844 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2845 af57b12a 2020-07-23 stsp }
2846 234035bc 2019-06-01 stsp } else if (blob1) {
2847 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2848 d6c87207 2019-08-02 stsp strlen(path1));
2849 1ee397ad 2019-07-12 stsp if (ie == NULL)
2850 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2851 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2852 2b92fad7 2019-06-02 stsp
2853 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2854 2b92fad7 2019-06-02 stsp path1) == -1)
2855 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2856 2b92fad7 2019-06-02 stsp
2857 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2858 7f91a133 2019-12-13 stsp repo);
2859 2b92fad7 2019-06-02 stsp if (err)
2860 2b92fad7 2019-06-02 stsp goto done;
2861 2b92fad7 2019-06-02 stsp
2862 2b92fad7 2019-06-02 stsp switch (status) {
2863 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2864 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2865 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2866 1ee397ad 2019-07-12 stsp if (err)
2867 1ee397ad 2019-07-12 stsp goto done;
2868 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2869 2b92fad7 2019-06-02 stsp if (err)
2870 2b92fad7 2019-06-02 stsp goto done;
2871 2b92fad7 2019-06-02 stsp if (ie)
2872 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2873 2b92fad7 2019-06-02 stsp break;
2874 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2875 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2876 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2877 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2878 1ee397ad 2019-07-12 stsp if (err)
2879 1ee397ad 2019-07-12 stsp goto done;
2880 2b92fad7 2019-06-02 stsp if (ie)
2881 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2882 2b92fad7 2019-06-02 stsp break;
2883 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2884 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2885 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2886 0a22ca1a 2020-09-23 stsp /*
2887 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2888 0a22ca1a 2020-09-23 stsp * exists in the repository.
2889 0a22ca1a 2020-09-23 stsp */
2890 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2891 0a22ca1a 2020-09-23 stsp if (err)
2892 0a22ca1a 2020-09-23 stsp goto done;
2893 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2894 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2895 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2896 0a22ca1a 2020-09-23 stsp if (err)
2897 0a22ca1a 2020-09-23 stsp goto done;
2898 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2899 0a22ca1a 2020-09-23 stsp path1);
2900 0a22ca1a 2020-09-23 stsp if (err)
2901 0a22ca1a 2020-09-23 stsp goto done;
2902 0a22ca1a 2020-09-23 stsp if (ie)
2903 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2904 0a22ca1a 2020-09-23 stsp ie);
2905 0a22ca1a 2020-09-23 stsp } else {
2906 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2907 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2908 0a22ca1a 2020-09-23 stsp }
2909 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2910 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2911 0a22ca1a 2020-09-23 stsp free(id);
2912 0a22ca1a 2020-09-23 stsp if (err)
2913 0a22ca1a 2020-09-23 stsp goto done;
2914 0a22ca1a 2020-09-23 stsp break;
2915 0a22ca1a 2020-09-23 stsp }
2916 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2917 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2918 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2919 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2920 1ee397ad 2019-07-12 stsp if (err)
2921 1ee397ad 2019-07-12 stsp goto done;
2922 2b92fad7 2019-06-02 stsp break;
2923 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2924 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2925 1ee397ad 2019-07-12 stsp if (err)
2926 1ee397ad 2019-07-12 stsp goto done;
2927 2b92fad7 2019-06-02 stsp break;
2928 2b92fad7 2019-06-02 stsp default:
2929 2b92fad7 2019-06-02 stsp break;
2930 2b92fad7 2019-06-02 stsp }
2931 234035bc 2019-06-01 stsp } else if (blob2) {
2932 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2933 234035bc 2019-06-01 stsp path2) == -1)
2934 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2935 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2936 d6c87207 2019-08-02 stsp strlen(path2));
2937 234035bc 2019-06-01 stsp if (ie) {
2938 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2939 7f91a133 2019-12-13 stsp -1, NULL, repo);
2940 234035bc 2019-06-01 stsp if (err)
2941 234035bc 2019-06-01 stsp goto done;
2942 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2943 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2944 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2945 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2946 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2947 1ee397ad 2019-07-12 stsp status, path2);
2948 234035bc 2019-06-01 stsp goto done;
2949 234035bc 2019-06-01 stsp }
2950 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2951 36bf999c 2020-07-23 stsp char *link_target2;
2952 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2953 36bf999c 2020-07-23 stsp blob2);
2954 36bf999c 2020-07-23 stsp if (err)
2955 36bf999c 2020-07-23 stsp goto done;
2956 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2957 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2958 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2959 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2960 36bf999c 2020-07-23 stsp free(link_target2);
2961 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2962 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2963 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2964 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2965 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2966 526a746f 2020-07-23 stsp a->progress_arg);
2967 dfe9fba0 2020-07-23 stsp } else {
2968 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2969 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2970 526a746f 2020-07-23 stsp }
2971 dfe9fba0 2020-07-23 stsp if (err)
2972 dfe9fba0 2020-07-23 stsp goto done;
2973 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2974 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2975 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2976 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2977 234035bc 2019-06-01 stsp if (err)
2978 234035bc 2019-06-01 stsp goto done;
2979 234035bc 2019-06-01 stsp }
2980 234035bc 2019-06-01 stsp } else {
2981 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2982 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2983 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2984 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2985 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2986 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
2987 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
2988 2e1fa222 2020-07-23 stsp } else {
2989 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2990 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2991 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2992 2e1fa222 2020-07-23 stsp }
2993 234035bc 2019-06-01 stsp if (err)
2994 234035bc 2019-06-01 stsp goto done;
2995 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2996 234035bc 2019-06-01 stsp if (err)
2997 234035bc 2019-06-01 stsp goto done;
2998 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2999 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3000 3969253a 2020-03-07 stsp if (err) {
3001 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3002 3969253a 2020-03-07 stsp goto done;
3003 3969253a 2020-03-07 stsp }
3004 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3005 2b92fad7 2019-06-02 stsp if (err) {
3006 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3007 2b92fad7 2019-06-02 stsp goto done;
3008 2b92fad7 2019-06-02 stsp }
3009 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3010 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3011 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3012 2e1fa222 2020-07-23 stsp }
3013 234035bc 2019-06-01 stsp }
3014 234035bc 2019-06-01 stsp }
3015 234035bc 2019-06-01 stsp done:
3016 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3017 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3018 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3019 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3020 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3021 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3022 1af628f4 2021-06-03 stsp free(id_str);
3023 54d5be07 2021-06-03 stsp free(label_deriv2);
3024 234035bc 2019-06-01 stsp free(ondisk_path);
3025 234035bc 2019-06-01 stsp return err;
3026 234035bc 2019-06-01 stsp }
3027 234035bc 2019-06-01 stsp
3028 69de9dd4 2021-09-03 stsp static const struct got_error *
3029 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3030 69de9dd4 2021-09-03 stsp {
3031 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3032 69de9dd4 2021-09-03 stsp
3033 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3034 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3035 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3036 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3037 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3038 69de9dd4 2021-09-03 stsp
3039 69de9dd4 2021-09-03 stsp return NULL;
3040 69de9dd4 2021-09-03 stsp }
3041 69de9dd4 2021-09-03 stsp
3042 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3043 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3044 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3045 234035bc 2019-06-01 stsp struct got_repository *repo;
3046 234035bc 2019-06-01 stsp };
3047 234035bc 2019-06-01 stsp
3048 234035bc 2019-06-01 stsp static const struct got_error *
3049 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3050 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3051 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3052 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3053 234035bc 2019-06-01 stsp {
3054 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3055 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3056 234035bc 2019-06-01 stsp unsigned char status;
3057 234035bc 2019-06-01 stsp struct stat sb;
3058 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3059 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3060 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3061 234035bc 2019-06-01 stsp char *ondisk_path;
3062 234035bc 2019-06-01 stsp
3063 69de9dd4 2021-09-03 stsp if (id == NULL)
3064 69de9dd4 2021-09-03 stsp return NULL;
3065 234035bc 2019-06-01 stsp
3066 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3067 69de9dd4 2021-09-03 stsp if (ie == NULL)
3068 69de9dd4 2021-09-03 stsp return NULL;
3069 69de9dd4 2021-09-03 stsp
3070 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3071 234035bc 2019-06-01 stsp == -1)
3072 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3073 234035bc 2019-06-01 stsp
3074 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3075 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3076 5546d466 2021-09-02 stsp free(ondisk_path);
3077 234035bc 2019-06-01 stsp if (err)
3078 234035bc 2019-06-01 stsp return err;
3079 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3080 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3081 234035bc 2019-06-01 stsp
3082 234035bc 2019-06-01 stsp return NULL;
3083 234035bc 2019-06-01 stsp }
3084 234035bc 2019-06-01 stsp
3085 818c7501 2019-07-11 stsp static const struct got_error *
3086 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3087 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3088 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3089 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3090 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3091 234035bc 2019-06-01 stsp {
3092 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3093 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3094 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3095 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3096 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3097 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3098 234035bc 2019-06-01 stsp
3099 03415a1a 2019-06-02 stsp if (commit_id1) {
3100 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3101 03415a1a 2019-06-02 stsp worktree->path_prefix);
3102 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3103 03415a1a 2019-06-02 stsp goto done;
3104 69d57f3d 2020-07-31 stsp }
3105 69d57f3d 2020-07-31 stsp if (tree_id1) {
3106 69d57f3d 2020-07-31 stsp char *id_str;
3107 03415a1a 2019-06-02 stsp
3108 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3109 03415a1a 2019-06-02 stsp if (err)
3110 03415a1a 2019-06-02 stsp goto done;
3111 f69721c3 2019-10-21 stsp
3112 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3113 f69721c3 2019-10-21 stsp if (err)
3114 f69721c3 2019-10-21 stsp goto done;
3115 f69721c3 2019-10-21 stsp
3116 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3117 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3118 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3119 f69721c3 2019-10-21 stsp free(id_str);
3120 f69721c3 2019-10-21 stsp goto done;
3121 f69721c3 2019-10-21 stsp }
3122 f69721c3 2019-10-21 stsp free(id_str);
3123 03415a1a 2019-06-02 stsp }
3124 234035bc 2019-06-01 stsp
3125 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3126 234035bc 2019-06-01 stsp worktree->path_prefix);
3127 234035bc 2019-06-01 stsp if (err)
3128 234035bc 2019-06-01 stsp goto done;
3129 234035bc 2019-06-01 stsp
3130 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3131 234035bc 2019-06-01 stsp if (err)
3132 234035bc 2019-06-01 stsp goto done;
3133 234035bc 2019-06-01 stsp
3134 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3135 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3136 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3137 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3138 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3139 69de9dd4 2021-09-03 stsp if (err)
3140 69de9dd4 2021-09-03 stsp goto done;
3141 69de9dd4 2021-09-03 stsp
3142 234035bc 2019-06-01 stsp arg.worktree = worktree;
3143 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3144 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3145 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3146 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3147 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3148 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3149 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3150 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3151 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3152 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3153 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3154 af12c6b9 2019-06-04 stsp err = sync_err;
3155 234035bc 2019-06-01 stsp done:
3156 234035bc 2019-06-01 stsp if (tree1)
3157 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3158 234035bc 2019-06-01 stsp if (tree2)
3159 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3160 f69721c3 2019-10-21 stsp free(label_orig);
3161 818c7501 2019-07-11 stsp return err;
3162 818c7501 2019-07-11 stsp }
3163 234035bc 2019-06-01 stsp
3164 818c7501 2019-07-11 stsp const struct got_error *
3165 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3166 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3167 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3168 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3169 818c7501 2019-07-11 stsp {
3170 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3171 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3172 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3173 818c7501 2019-07-11 stsp
3174 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3175 818c7501 2019-07-11 stsp if (err)
3176 818c7501 2019-07-11 stsp return err;
3177 818c7501 2019-07-11 stsp
3178 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3179 818c7501 2019-07-11 stsp if (err)
3180 818c7501 2019-07-11 stsp goto done;
3181 818c7501 2019-07-11 stsp
3182 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3183 69de9dd4 2021-09-03 stsp worktree);
3184 818c7501 2019-07-11 stsp if (err)
3185 818c7501 2019-07-11 stsp goto done;
3186 818c7501 2019-07-11 stsp
3187 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3189 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3190 818c7501 2019-07-11 stsp done:
3191 818c7501 2019-07-11 stsp if (fileindex)
3192 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3193 818c7501 2019-07-11 stsp free(fileindex_path);
3194 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3195 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3196 234035bc 2019-06-01 stsp err = unlockerr;
3197 234035bc 2019-06-01 stsp return err;
3198 234035bc 2019-06-01 stsp }
3199 234035bc 2019-06-01 stsp
3200 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3201 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3202 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3203 927df6b7 2019-02-10 stsp const char *status_path;
3204 927df6b7 2019-02-10 stsp size_t status_path_len;
3205 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3206 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3207 f8d1f275 2019-02-04 stsp void *status_arg;
3208 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3209 f8d1f275 2019-02-04 stsp void *cancel_arg;
3210 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3211 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3212 f2a9dc41 2019-12-13 tracey int report_unchanged;
3213 3143d852 2020-06-25 stsp int no_ignores;
3214 f8d1f275 2019-02-04 stsp };
3215 88d0e355 2019-08-03 stsp
3216 f8d1f275 2019-02-04 stsp static const struct got_error *
3217 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3218 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3219 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3220 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3221 927df6b7 2019-02-10 stsp {
3222 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3223 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3224 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3225 927df6b7 2019-02-10 stsp struct stat sb;
3226 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3227 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3228 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3229 927df6b7 2019-02-10 stsp
3230 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3231 98eaaa12 2019-08-03 stsp if (err)
3232 98eaaa12 2019-08-03 stsp return err;
3233 98eaaa12 2019-08-03 stsp
3234 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3235 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3236 98eaaa12 2019-08-03 stsp return NULL;
3237 98eaaa12 2019-08-03 stsp
3238 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3239 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3240 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3241 98eaaa12 2019-08-03 stsp }
3242 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3243 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3244 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3245 927df6b7 2019-02-10 stsp }
3246 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3247 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3248 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3249 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3250 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3251 98eaaa12 2019-08-03 stsp }
3252 98eaaa12 2019-08-03 stsp
3253 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3254 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3255 927df6b7 2019-02-10 stsp }
3256 927df6b7 2019-02-10 stsp
3257 927df6b7 2019-02-10 stsp static const struct got_error *
3258 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3259 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3260 f8d1f275 2019-02-04 stsp {
3261 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3262 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3263 f8d1f275 2019-02-04 stsp char *abspath;
3264 f8d1f275 2019-02-04 stsp
3265 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3266 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3267 0584f854 2019-04-06 stsp
3268 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3269 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3270 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3271 927df6b7 2019-02-10 stsp return NULL;
3272 927df6b7 2019-02-10 stsp
3273 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3274 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3275 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3276 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3277 f8d1f275 2019-02-04 stsp } else {
3278 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3279 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3280 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3281 f8d1f275 2019-02-04 stsp }
3282 f8d1f275 2019-02-04 stsp
3283 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3284 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3285 f8d1f275 2019-02-04 stsp free(abspath);
3286 9d31a1d8 2018-03-11 stsp return err;
3287 9d31a1d8 2018-03-11 stsp }
3288 f8d1f275 2019-02-04 stsp
3289 f8d1f275 2019-02-04 stsp static const struct got_error *
3290 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3291 f8d1f275 2019-02-04 stsp {
3292 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3293 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3294 2ec1f75b 2019-03-26 stsp unsigned char status;
3295 927df6b7 2019-02-10 stsp
3296 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3297 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3298 0584f854 2019-04-06 stsp
3299 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3300 927df6b7 2019-02-10 stsp return NULL;
3301 927df6b7 2019-02-10 stsp
3302 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3303 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3304 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3305 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3306 2ec1f75b 2019-03-26 stsp else
3307 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3308 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3309 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3310 6841da00 2019-08-08 stsp }
3311 6841da00 2019-08-08 stsp
3312 6841da00 2019-08-08 stsp void
3313 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3314 6841da00 2019-08-08 stsp {
3315 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3316 6841da00 2019-08-08 stsp
3317 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3318 6841da00 2019-08-08 stsp free((char *)pe->path);
3319 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3320 6841da00 2019-08-08 stsp }
3321 6841da00 2019-08-08 stsp
3322 6841da00 2019-08-08 stsp void
3323 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3324 6841da00 2019-08-08 stsp {
3325 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3326 6841da00 2019-08-08 stsp
3327 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3328 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3329 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3330 6841da00 2019-08-08 stsp free((char *)pe->path);
3331 6841da00 2019-08-08 stsp }
3332 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3333 6841da00 2019-08-08 stsp }
3334 6841da00 2019-08-08 stsp
3335 6841da00 2019-08-08 stsp static const struct got_error *
3336 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3337 6841da00 2019-08-08 stsp {
3338 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3339 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3340 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3341 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3342 6841da00 2019-08-08 stsp size_t linesize = 0;
3343 6841da00 2019-08-08 stsp ssize_t linelen;
3344 6841da00 2019-08-08 stsp
3345 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3346 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3347 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3348 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3349 6841da00 2019-08-08 stsp
3350 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3351 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3352 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3353 bd8de430 2019-10-04 stsp
3354 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3355 bd8de430 2019-10-04 stsp if (line[0] == '#')
3356 bd8de430 2019-10-04 stsp continue;
3357 bd8de430 2019-10-04 stsp
3358 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3359 bd8de430 2019-10-04 stsp if (line[0] == '!')
3360 bd8de430 2019-10-04 stsp continue;
3361 bd8de430 2019-10-04 stsp
3362 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3363 6841da00 2019-08-08 stsp line) == -1) {
3364 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3365 6841da00 2019-08-08 stsp goto done;
3366 6841da00 2019-08-08 stsp }
3367 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3368 6841da00 2019-08-08 stsp if (err)
3369 6841da00 2019-08-08 stsp goto done;
3370 6841da00 2019-08-08 stsp }
3371 6841da00 2019-08-08 stsp if (ferror(f)) {
3372 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3373 6841da00 2019-08-08 stsp goto done;
3374 6841da00 2019-08-08 stsp }
3375 6841da00 2019-08-08 stsp
3376 6841da00 2019-08-08 stsp dirpath = strdup(path);
3377 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3378 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3379 6841da00 2019-08-08 stsp goto done;
3380 6841da00 2019-08-08 stsp }
3381 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3382 6841da00 2019-08-08 stsp done:
3383 6841da00 2019-08-08 stsp free(line);
3384 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3385 6841da00 2019-08-08 stsp free(dirpath);
3386 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3387 6841da00 2019-08-08 stsp }
3388 6841da00 2019-08-08 stsp return err;
3389 6841da00 2019-08-08 stsp }
3390 6841da00 2019-08-08 stsp
3391 6841da00 2019-08-08 stsp int
3392 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3393 6841da00 2019-08-08 stsp {
3394 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3395 bd8de430 2019-10-04 stsp
3396 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3397 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3398 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3399 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3400 bd8de430 2019-10-04 stsp
3401 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3402 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3403 6841da00 2019-08-08 stsp
3404 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3405 bd8de430 2019-10-04 stsp continue;
3406 bd8de430 2019-10-04 stsp pattern += 3;
3407 bd8de430 2019-10-04 stsp p = path;
3408 bd8de430 2019-10-04 stsp while (*p) {
3409 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3410 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3411 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3412 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3413 bd8de430 2019-10-04 stsp p++;
3414 bd8de430 2019-10-04 stsp while (*p == '/')
3415 bd8de430 2019-10-04 stsp p++;
3416 bd8de430 2019-10-04 stsp continue;
3417 bd8de430 2019-10-04 stsp }
3418 bd8de430 2019-10-04 stsp return 1;
3419 bd8de430 2019-10-04 stsp }
3420 bd8de430 2019-10-04 stsp }
3421 bd8de430 2019-10-04 stsp }
3422 bd8de430 2019-10-04 stsp
3423 6841da00 2019-08-08 stsp /*
3424 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3425 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3426 6841da00 2019-08-08 stsp * ignores backwards.
3427 6841da00 2019-08-08 stsp */
3428 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3429 6841da00 2019-08-08 stsp while (pe) {
3430 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3431 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3432 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3433 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3434 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3435 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3436 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3437 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3438 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3439 6841da00 2019-08-08 stsp continue;
3440 6841da00 2019-08-08 stsp return 1;
3441 6841da00 2019-08-08 stsp }
3442 6841da00 2019-08-08 stsp }
3443 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3444 6841da00 2019-08-08 stsp }
3445 6841da00 2019-08-08 stsp
3446 6841da00 2019-08-08 stsp return 0;
3447 6841da00 2019-08-08 stsp }
3448 6841da00 2019-08-08 stsp
3449 6841da00 2019-08-08 stsp static const struct got_error *
3450 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3451 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3452 6841da00 2019-08-08 stsp {
3453 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3454 6841da00 2019-08-08 stsp char *ignorespath;
3455 886cec17 2019-12-15 stsp int fd = -1;
3456 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3457 6841da00 2019-08-08 stsp
3458 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3459 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3460 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3461 6841da00 2019-08-08 stsp
3462 886cec17 2019-12-15 stsp if (dirfd != -1) {
3463 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3464 886cec17 2019-12-15 stsp if (fd == -1) {
3465 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3466 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3467 886cec17 2019-12-15 stsp ignorespath);
3468 886cec17 2019-12-15 stsp } else {
3469 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3470 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3471 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3472 886cec17 2019-12-15 stsp ignorespath);
3473 886cec17 2019-12-15 stsp else {
3474 886cec17 2019-12-15 stsp fd = -1;
3475 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3476 886cec17 2019-12-15 stsp }
3477 886cec17 2019-12-15 stsp }
3478 886cec17 2019-12-15 stsp } else {
3479 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3480 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3481 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3482 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3483 886cec17 2019-12-15 stsp ignorespath);
3484 886cec17 2019-12-15 stsp } else
3485 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3486 886cec17 2019-12-15 stsp }
3487 6841da00 2019-08-08 stsp
3488 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3489 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3490 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3491 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3492 6841da00 2019-08-08 stsp free(ignorespath);
3493 6841da00 2019-08-08 stsp return err;
3494 f8d1f275 2019-02-04 stsp }
3495 f8d1f275 2019-02-04 stsp
3496 f8d1f275 2019-02-04 stsp static const struct got_error *
3497 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3498 62da3196 2021-10-01 stsp int dirfd)
3499 f8d1f275 2019-02-04 stsp {
3500 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3501 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3502 f8d1f275 2019-02-04 stsp char *path = NULL;
3503 62da3196 2021-10-01 stsp
3504 62da3196 2021-10-01 stsp if (ignore != NULL)
3505 62da3196 2021-10-01 stsp *ignore = 0;
3506 f8d1f275 2019-02-04 stsp
3507 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3509 0584f854 2019-04-06 stsp
3510 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3511 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3512 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3513 f8d1f275 2019-02-04 stsp } else {
3514 f8d1f275 2019-02-04 stsp path = de->d_name;
3515 f8d1f275 2019-02-04 stsp }
3516 f8d1f275 2019-02-04 stsp
3517 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3518 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3519 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3520 62da3196 2021-10-01 stsp *ignore = 1;
3521 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3522 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3523 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3524 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3525 f8d1f275 2019-02-04 stsp if (parent_path[0])
3526 f8d1f275 2019-02-04 stsp free(path);
3527 b72f483a 2019-02-05 stsp return err;
3528 f8d1f275 2019-02-04 stsp }
3529 f8d1f275 2019-02-04 stsp
3530 347d1d3e 2019-07-12 stsp static const struct got_error *
3531 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3532 3143d852 2020-06-25 stsp {
3533 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3534 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3535 3143d852 2020-06-25 stsp
3536 3143d852 2020-06-25 stsp if (a->no_ignores)
3537 3143d852 2020-06-25 stsp return NULL;
3538 3143d852 2020-06-25 stsp
3539 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3540 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3541 3143d852 2020-06-25 stsp if (err)
3542 3143d852 2020-06-25 stsp return err;
3543 3143d852 2020-06-25 stsp
3544 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3545 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3546 3143d852 2020-06-25 stsp
3547 3143d852 2020-06-25 stsp return err;
3548 3143d852 2020-06-25 stsp }
3549 3143d852 2020-06-25 stsp
3550 3143d852 2020-06-25 stsp static const struct got_error *
3551 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3552 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3553 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3554 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3555 abb4604f 2019-07-27 stsp {
3556 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3557 abb4604f 2019-07-27 stsp struct stat sb;
3558 abb4604f 2019-07-27 stsp
3559 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3560 ff56836b 2021-07-08 stsp return NULL;
3561 ff56836b 2021-07-08 stsp
3562 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3563 abb4604f 2019-07-27 stsp if (ie)
3564 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3565 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3566 abb4604f 2019-07-27 stsp
3567 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3568 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3569 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3570 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3571 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 abb4604f 2019-07-27 stsp return NULL;
3573 abb4604f 2019-07-27 stsp }
3574 abb4604f 2019-07-27 stsp
3575 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3578 abb4604f 2019-07-27 stsp
3579 abb4604f 2019-07-27 stsp return NULL;
3580 3143d852 2020-06-25 stsp }
3581 3143d852 2020-06-25 stsp
3582 3143d852 2020-06-25 stsp static const struct got_error *
3583 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3585 3143d852 2020-06-25 stsp {
3586 3143d852 2020-06-25 stsp const struct got_error *err;
3587 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3588 3143d852 2020-06-25 stsp
3589 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3590 3143d852 2020-06-25 stsp ".cvsignore");
3591 3143d852 2020-06-25 stsp if (err)
3592 3143d852 2020-06-25 stsp return err;
3593 3143d852 2020-06-25 stsp
3594 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3595 3143d852 2020-06-25 stsp ".gitignore");
3596 3143d852 2020-06-25 stsp if (err)
3597 3143d852 2020-06-25 stsp return err;
3598 3143d852 2020-06-25 stsp
3599 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3600 3143d852 2020-06-25 stsp if (err) {
3601 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3602 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3603 3143d852 2020-06-25 stsp return err;
3604 3143d852 2020-06-25 stsp }
3605 3143d852 2020-06-25 stsp for (;;) {
3606 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3607 3143d852 2020-06-25 stsp ".cvsignore");
3608 3143d852 2020-06-25 stsp if (err)
3609 3143d852 2020-06-25 stsp break;
3610 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3611 3143d852 2020-06-25 stsp ".gitignore");
3612 3143d852 2020-06-25 stsp if (err)
3613 3143d852 2020-06-25 stsp break;
3614 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3615 3143d852 2020-06-25 stsp if (err) {
3616 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3617 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3618 3143d852 2020-06-25 stsp break;
3619 3143d852 2020-06-25 stsp }
3620 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3621 ff56836b 2021-07-08 stsp break;
3622 b737c85a 2020-06-26 stsp free(parent_path);
3623 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3624 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3625 3143d852 2020-06-25 stsp }
3626 3143d852 2020-06-25 stsp
3627 b737c85a 2020-06-26 stsp free(parent_path);
3628 b737c85a 2020-06-26 stsp free(next_parent_path);
3629 3143d852 2020-06-25 stsp return err;
3630 abb4604f 2019-07-27 stsp }
3631 abb4604f 2019-07-27 stsp
3632 abb4604f 2019-07-27 stsp static const struct got_error *
3633 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3634 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3635 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3636 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 f2a9dc41 2019-12-13 tracey int report_unchanged)
3638 f8d1f275 2019-02-04 stsp {
3639 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3640 6fc93f37 2019-12-13 stsp int fd = -1;
3641 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3642 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3643 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3644 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3645 3143d852 2020-06-25 stsp
3646 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3647 f8d1f275 2019-02-04 stsp
3648 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3649 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3650 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3651 8dc303cc 2019-07-27 stsp
3652 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3653 6fc93f37 2019-12-13 stsp if (fd == -1) {
3654 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3655 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3656 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3657 ff56836b 2021-07-08 stsp else {
3658 ff56836b 2021-07-08 stsp if (!no_ignores) {
3659 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3660 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3661 ff56836b 2021-07-08 stsp if (err)
3662 ff56836b 2021-07-08 stsp goto done;
3663 ff56836b 2021-07-08 stsp }
3664 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3665 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3666 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3667 ff56836b 2021-07-08 stsp }
3668 abb4604f 2019-07-27 stsp } else {
3669 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3670 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3671 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3672 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3673 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3674 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3675 abb4604f 2019-07-27 stsp arg.status_path = path;
3676 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3677 abb4604f 2019-07-27 stsp arg.repo = repo;
3678 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3679 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3680 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3681 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3682 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3683 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3684 022fae89 2019-12-06 tracey if (!no_ignores) {
3685 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3686 3143d852 2020-06-25 stsp worktree->root_path, path);
3687 3143d852 2020-06-25 stsp if (err)
3688 3143d852 2020-06-25 stsp goto done;
3689 022fae89 2019-12-06 tracey }
3690 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3691 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3692 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3693 927df6b7 2019-02-10 stsp }
3694 3143d852 2020-06-25 stsp done:
3695 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3696 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3697 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3698 927df6b7 2019-02-10 stsp free(ondisk_path);
3699 347d1d3e 2019-07-12 stsp return err;
3700 347d1d3e 2019-07-12 stsp }
3701 347d1d3e 2019-07-12 stsp
3702 347d1d3e 2019-07-12 stsp const struct got_error *
3703 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3704 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3705 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3706 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3707 347d1d3e 2019-07-12 stsp {
3708 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3709 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3710 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3711 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3712 347d1d3e 2019-07-12 stsp
3713 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3714 347d1d3e 2019-07-12 stsp if (err)
3715 347d1d3e 2019-07-12 stsp return err;
3716 347d1d3e 2019-07-12 stsp
3717 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3718 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3719 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3720 f6343036 2021-06-22 stsp no_ignores, 0);
3721 72ea6654 2019-07-27 stsp if (err)
3722 72ea6654 2019-07-27 stsp break;
3723 72ea6654 2019-07-27 stsp }
3724 f8d1f275 2019-02-04 stsp free(fileindex_path);
3725 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3726 f8d1f275 2019-02-04 stsp return err;
3727 f8d1f275 2019-02-04 stsp }
3728 6c7ab921 2019-03-18 stsp
3729 6c7ab921 2019-03-18 stsp const struct got_error *
3730 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3731 6c7ab921 2019-03-18 stsp const char *arg)
3732 6c7ab921 2019-03-18 stsp {
3733 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3734 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3735 6c7ab921 2019-03-18 stsp size_t len;
3736 00bb5ea0 2020-07-23 stsp struct stat sb;
3737 01740607 2020-11-04 stsp char *abspath = NULL;
3738 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3739 6c7ab921 2019-03-18 stsp
3740 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3741 6c7ab921 2019-03-18 stsp
3742 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3743 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3744 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3745 00bb5ea0 2020-07-23 stsp
3746 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3747 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3748 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3749 00bb5ea0 2020-07-23 stsp goto done;
3750 00bb5ea0 2020-07-23 stsp }
3751 727173c3 2020-11-06 stsp sb.st_mode = 0;
3752 00bb5ea0 2020-07-23 stsp }
3753 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3754 00bb5ea0 2020-07-23 stsp /*
3755 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3756 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3757 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3758 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3759 00bb5ea0 2020-07-23 stsp */
3760 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3761 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3762 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3763 00bb5ea0 2020-07-23 stsp goto done;
3764 00bb5ea0 2020-07-23 stsp }
3765 00bb5ea0 2020-07-23 stsp
3766 00bb5ea0 2020-07-23 stsp }
3767 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3768 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3769 00bb5ea0 2020-07-23 stsp if (err)
3770 d0710d08 2019-07-22 stsp goto done;
3771 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3772 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3773 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3774 00bb5ea0 2020-07-23 stsp goto done;
3775 00bb5ea0 2020-07-23 stsp }
3776 00bb5ea0 2020-07-23 stsp } else {
3777 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3778 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3779 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3780 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3781 00bb5ea0 2020-07-23 stsp goto done;
3782 00bb5ea0 2020-07-23 stsp }
3783 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3785 01740607 2020-11-04 stsp goto done;
3786 01740607 2020-11-04 stsp }
3787 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3788 01740607 2020-11-04 stsp sizeof(canonpath));
3789 01740607 2020-11-04 stsp if (err)
3790 00bb5ea0 2020-07-23 stsp goto done;
3791 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3792 01740607 2020-11-04 stsp if (resolved == NULL) {
3793 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3794 01740607 2020-11-04 stsp goto done;
3795 00bb5ea0 2020-07-23 stsp }
3796 d0710d08 2019-07-22 stsp }
3797 d0710d08 2019-07-22 stsp }
3798 6c7ab921 2019-03-18 stsp
3799 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3800 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3801 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3802 6c7ab921 2019-03-18 stsp goto done;
3803 6c7ab921 2019-03-18 stsp }
3804 6c7ab921 2019-03-18 stsp
3805 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3806 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3807 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3808 f6d88e1a 2019-05-29 stsp if (err)
3809 f6d88e1a 2019-05-29 stsp goto done;
3810 f6d88e1a 2019-05-29 stsp } else {
3811 f6d88e1a 2019-05-29 stsp path = strdup("");
3812 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3813 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3814 f6d88e1a 2019-05-29 stsp goto done;
3815 f6d88e1a 2019-05-29 stsp }
3816 6c7ab921 2019-03-18 stsp }
3817 6c7ab921 2019-03-18 stsp
3818 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3819 6c7ab921 2019-03-18 stsp len = strlen(path);
3820 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3821 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3822 6c7ab921 2019-03-18 stsp len--;
3823 6c7ab921 2019-03-18 stsp }
3824 6c7ab921 2019-03-18 stsp done:
3825 01740607 2020-11-04 stsp free(abspath);
3826 6c7ab921 2019-03-18 stsp free(resolved);
3827 d0710d08 2019-07-22 stsp free(cwd);
3828 6c7ab921 2019-03-18 stsp if (err == NULL)
3829 6c7ab921 2019-03-18 stsp *wt_path = path;
3830 6c7ab921 2019-03-18 stsp else
3831 6c7ab921 2019-03-18 stsp free(path);
3832 d00136be 2019-03-26 stsp return err;
3833 d00136be 2019-03-26 stsp }
3834 d00136be 2019-03-26 stsp
3835 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3836 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3837 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3838 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3839 4e68cba3 2019-11-23 stsp void *progress_arg;
3840 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3841 4e68cba3 2019-11-23 stsp };
3842 4e68cba3 2019-11-23 stsp
3843 4e68cba3 2019-11-23 stsp static const struct got_error *
3844 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3845 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3846 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3847 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3848 07f5b47a 2019-06-02 stsp {
3849 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3850 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3851 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3852 6d022e97 2019-08-04 stsp struct stat sb;
3853 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3854 07f5b47a 2019-06-02 stsp
3855 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3856 dbb83fbd 2019-12-12 stsp relpath) == -1)
3857 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3858 dbb83fbd 2019-12-12 stsp
3859 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3860 6d022e97 2019-08-04 stsp if (ie) {
3861 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3862 12463d8b 2019-12-13 stsp de_name, a->repo);
3863 6d022e97 2019-08-04 stsp if (err)
3864 dbb83fbd 2019-12-12 stsp goto done;
3865 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3866 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3867 dbb83fbd 2019-12-12 stsp goto done;
3868 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3869 dbb83fbd 2019-12-12 stsp if (err)
3870 dbb83fbd 2019-12-12 stsp goto done;
3871 6d022e97 2019-08-04 stsp }
3872 07f5b47a 2019-06-02 stsp
3873 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3874 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3875 dbb83fbd 2019-12-12 stsp goto done;
3876 4e68cba3 2019-11-23 stsp }
3877 07f5b47a 2019-06-02 stsp
3878 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3879 4e68cba3 2019-11-23 stsp if (err)
3880 4e68cba3 2019-11-23 stsp goto done;
3881 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3882 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3883 3969253a 2020-03-07 stsp if (err) {
3884 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3885 3969253a 2020-03-07 stsp goto done;
3886 3969253a 2020-03-07 stsp }
3887 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3888 07f5b47a 2019-06-02 stsp if (err) {
3889 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3890 4e68cba3 2019-11-23 stsp goto done;
3891 07f5b47a 2019-06-02 stsp }
3892 4e68cba3 2019-11-23 stsp done:
3893 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3894 dbb83fbd 2019-12-12 stsp if (err)
3895 dbb83fbd 2019-12-12 stsp return err;
3896 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3897 dbb83fbd 2019-12-12 stsp return NULL;
3898 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3899 07f5b47a 2019-06-02 stsp }
3900 07f5b47a 2019-06-02 stsp
3901 d00136be 2019-03-26 stsp const struct got_error *
3902 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3903 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3904 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3905 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3906 d00136be 2019-03-26 stsp {
3907 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3908 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3909 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3910 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3911 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3912 d00136be 2019-03-26 stsp
3913 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3914 d00136be 2019-03-26 stsp if (err)
3915 d00136be 2019-03-26 stsp return err;
3916 d00136be 2019-03-26 stsp
3917 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3918 d00136be 2019-03-26 stsp if (err)
3919 d00136be 2019-03-26 stsp goto done;
3920 d00136be 2019-03-26 stsp
3921 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3922 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3923 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3924 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3925 4e68cba3 2019-11-23 stsp saa.repo = repo;
3926 4e68cba3 2019-11-23 stsp
3927 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3928 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3929 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3930 1dd54920 2019-05-11 stsp if (err)
3931 af12c6b9 2019-06-04 stsp break;
3932 1dd54920 2019-05-11 stsp }
3933 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3934 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3935 af12c6b9 2019-06-04 stsp err = sync_err;
3936 d00136be 2019-03-26 stsp done:
3937 fb399478 2019-07-12 stsp free(fileindex_path);
3938 d00136be 2019-03-26 stsp if (fileindex)
3939 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3940 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3941 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3942 d00136be 2019-03-26 stsp err = unlockerr;
3943 6c7ab921 2019-03-18 stsp return err;
3944 6c7ab921 2019-03-18 stsp }
3945 17ed4618 2019-06-02 stsp
3946 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3947 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3948 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3949 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3950 f2a9dc41 2019-12-13 tracey void *progress_arg;
3951 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3952 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3953 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3954 766841c2 2020-08-13 stsp const char *status_codes;
3955 f2a9dc41 2019-12-13 tracey };
3956 f2a9dc41 2019-12-13 tracey
3957 f2a9dc41 2019-12-13 tracey static const struct got_error *
3958 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3959 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3960 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3961 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3962 17ed4618 2019-06-02 stsp {
3963 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3964 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3965 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3966 17ed4618 2019-06-02 stsp struct stat sb;
3967 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3968 17ed4618 2019-06-02 stsp
3969 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3970 17ed4618 2019-06-02 stsp if (ie == NULL)
3971 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3972 2ec1f75b 2019-03-26 stsp
3973 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3974 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3975 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3976 9acbc4fa 2019-08-03 stsp return NULL;
3977 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3978 9acbc4fa 2019-08-03 stsp }
3979 9acbc4fa 2019-08-03 stsp
3980 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3981 f2a9dc41 2019-12-13 tracey relpath) == -1)
3982 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3983 f2a9dc41 2019-12-13 tracey
3984 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3985 12463d8b 2019-12-13 stsp a->repo);
3986 17ed4618 2019-06-02 stsp if (err)
3987 f2a9dc41 2019-12-13 tracey goto done;
3988 17ed4618 2019-06-02 stsp
3989 766841c2 2020-08-13 stsp if (a->status_codes) {
3990 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3991 766841c2 2020-08-13 stsp int i;
3992 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3993 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3994 766841c2 2020-08-13 stsp break;
3995 766841c2 2020-08-13 stsp }
3996 766841c2 2020-08-13 stsp if (i == ncodes) {
3997 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3998 766841c2 2020-08-13 stsp free(ondisk_path);
3999 766841c2 2020-08-13 stsp return NULL;
4000 766841c2 2020-08-13 stsp }
4001 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4002 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4003 766841c2 2020-08-13 stsp static char msg[64];
4004 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4005 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4006 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4007 766841c2 2020-08-13 stsp goto done;
4008 766841c2 2020-08-13 stsp }
4009 766841c2 2020-08-13 stsp }
4010 766841c2 2020-08-13 stsp
4011 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4012 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4013 f2a9dc41 2019-12-13 tracey goto done;
4014 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4015 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4016 f2a9dc41 2019-12-13 tracey goto done;
4017 f2a9dc41 2019-12-13 tracey }
4018 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4019 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4020 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4021 f2a9dc41 2019-12-13 tracey goto done;
4022 f2a9dc41 2019-12-13 tracey }
4023 17ed4618 2019-06-02 stsp }
4024 17ed4618 2019-06-02 stsp
4025 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4026 20a2ad1c 2020-10-20 stsp size_t root_len;
4027 20a2ad1c 2020-10-20 stsp
4028 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4029 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4030 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4031 12463d8b 2019-12-13 stsp ondisk_path);
4032 12463d8b 2019-12-13 stsp goto done;
4033 12463d8b 2019-12-13 stsp }
4034 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4035 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4036 12463d8b 2019-12-13 stsp goto done;
4037 15341bfd 2020-03-05 tracey }
4038 15341bfd 2020-03-05 tracey
4039 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4040 20a2ad1c 2020-10-20 stsp do {
4041 20a2ad1c 2020-10-20 stsp char *parent;
4042 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4043 20a2ad1c 2020-10-20 stsp if (err)
4044 2513f20a 2020-10-20 stsp goto done;
4045 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4046 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4047 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4048 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4049 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4050 20a2ad1c 2020-10-20 stsp ondisk_path);
4051 15341bfd 2020-03-05 tracey break;
4052 15341bfd 2020-03-05 tracey }
4053 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4054 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4055 f2a9dc41 2019-12-13 tracey }
4056 17ed4618 2019-06-02 stsp
4057 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4058 f2a9dc41 2019-12-13 tracey done:
4059 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4060 f2a9dc41 2019-12-13 tracey if (err)
4061 f2a9dc41 2019-12-13 tracey return err;
4062 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4063 f2a9dc41 2019-12-13 tracey return NULL;
4064 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4065 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4066 17ed4618 2019-06-02 stsp }
4067 17ed4618 2019-06-02 stsp
4068 2ec1f75b 2019-03-26 stsp const struct got_error *
4069 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4070 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4071 766841c2 2020-08-13 stsp const char *status_codes,
4072 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4073 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4074 2ec1f75b 2019-03-26 stsp {
4075 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4076 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4077 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4078 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4079 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4080 2ec1f75b 2019-03-26 stsp
4081 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4082 2ec1f75b 2019-03-26 stsp if (err)
4083 2ec1f75b 2019-03-26 stsp return err;
4084 2ec1f75b 2019-03-26 stsp
4085 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4086 2ec1f75b 2019-03-26 stsp if (err)
4087 2ec1f75b 2019-03-26 stsp goto done;
4088 f2a9dc41 2019-12-13 tracey
4089 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4090 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4091 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4092 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4093 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4094 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4095 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4096 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4097 2ec1f75b 2019-03-26 stsp
4098 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4099 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4100 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4101 17ed4618 2019-06-02 stsp if (err)
4102 af12c6b9 2019-06-04 stsp break;
4103 2ec1f75b 2019-03-26 stsp }
4104 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4105 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4106 af12c6b9 2019-06-04 stsp err = sync_err;
4107 2ec1f75b 2019-03-26 stsp done:
4108 fb399478 2019-07-12 stsp free(fileindex_path);
4109 2ec1f75b 2019-03-26 stsp if (fileindex)
4110 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4111 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4112 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4113 2ec1f75b 2019-03-26 stsp err = unlockerr;
4114 33aa809d 2019-08-08 stsp return err;
4115 33aa809d 2019-08-08 stsp }
4116 33aa809d 2019-08-08 stsp
4117 33aa809d 2019-08-08 stsp static const struct got_error *
4118 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4119 33aa809d 2019-08-08 stsp {
4120 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4121 33aa809d 2019-08-08 stsp char *line = NULL;
4122 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4123 33aa809d 2019-08-08 stsp ssize_t linelen;
4124 33aa809d 2019-08-08 stsp
4125 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4126 33aa809d 2019-08-08 stsp if (linelen == -1) {
4127 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4128 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4129 33aa809d 2019-08-08 stsp goto done;
4130 33aa809d 2019-08-08 stsp }
4131 33aa809d 2019-08-08 stsp return NULL;
4132 33aa809d 2019-08-08 stsp }
4133 33aa809d 2019-08-08 stsp if (outfile) {
4134 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4135 33aa809d 2019-08-08 stsp if (n != linelen) {
4136 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4137 33aa809d 2019-08-08 stsp goto done;
4138 33aa809d 2019-08-08 stsp }
4139 33aa809d 2019-08-08 stsp }
4140 33aa809d 2019-08-08 stsp if (rejectfile) {
4141 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4142 33aa809d 2019-08-08 stsp if (n != linelen)
4143 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4144 33aa809d 2019-08-08 stsp }
4145 33aa809d 2019-08-08 stsp done:
4146 33aa809d 2019-08-08 stsp free(line);
4147 2ec1f75b 2019-03-26 stsp return err;
4148 2ec1f75b 2019-03-26 stsp }
4149 1f1abb7e 2019-08-08 stsp
4150 33aa809d 2019-08-08 stsp static const struct got_error *
4151 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4152 33aa809d 2019-08-08 stsp {
4153 33aa809d 2019-08-08 stsp char *line = NULL;
4154 33aa809d 2019-08-08 stsp size_t linesize = 0;
4155 33aa809d 2019-08-08 stsp ssize_t linelen;
4156 33aa809d 2019-08-08 stsp
4157 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4158 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4159 500467ff 2019-09-25 hiltjo if (ferror(f))
4160 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4161 500467ff 2019-09-25 hiltjo return NULL;
4162 500467ff 2019-09-25 hiltjo }
4163 33aa809d 2019-08-08 stsp free(line);
4164 33aa809d 2019-08-08 stsp return NULL;
4165 33aa809d 2019-08-08 stsp }
4166 33aa809d 2019-08-08 stsp
4167 33aa809d 2019-08-08 stsp static const struct got_error *
4168 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4169 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4170 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4171 abc59930 2021-09-05 naddy {
4172 33aa809d 2019-08-08 stsp const struct got_error *err;
4173 33aa809d 2019-08-08 stsp
4174 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4175 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4176 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4177 33aa809d 2019-08-08 stsp if (err)
4178 33aa809d 2019-08-08 stsp return err;
4179 33aa809d 2019-08-08 stsp (*line_cur1)++;
4180 33aa809d 2019-08-08 stsp }
4181 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4182 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4183 33aa809d 2019-08-08 stsp if (rejectfile)
4184 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4185 33aa809d 2019-08-08 stsp else
4186 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4187 33aa809d 2019-08-08 stsp if (err)
4188 33aa809d 2019-08-08 stsp return err;
4189 33aa809d 2019-08-08 stsp (*line_cur2)++;
4190 33aa809d 2019-08-08 stsp }
4191 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4192 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4193 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4194 33aa809d 2019-08-08 stsp if (err)
4195 33aa809d 2019-08-08 stsp return err;
4196 33aa809d 2019-08-08 stsp (*line_cur2)++;
4197 33aa809d 2019-08-08 stsp }
4198 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4199 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4200 33aa809d 2019-08-08 stsp if (rejectfile)
4201 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4202 33aa809d 2019-08-08 stsp else
4203 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4204 33aa809d 2019-08-08 stsp if (err)
4205 33aa809d 2019-08-08 stsp return err;
4206 33aa809d 2019-08-08 stsp (*line_cur1)++;
4207 33aa809d 2019-08-08 stsp }
4208 f1e81a05 2019-08-10 stsp
4209 f1e81a05 2019-08-10 stsp return NULL;
4210 f1e81a05 2019-08-10 stsp }
4211 f1e81a05 2019-08-10 stsp
4212 f1e81a05 2019-08-10 stsp static const struct got_error *
4213 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4214 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4215 f1e81a05 2019-08-10 stsp {
4216 f1e81a05 2019-08-10 stsp const struct got_error *err;
4217 f1e81a05 2019-08-10 stsp
4218 f1e81a05 2019-08-10 stsp if (outfile) {
4219 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4220 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4221 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4222 f1e81a05 2019-08-10 stsp if (err)
4223 f1e81a05 2019-08-10 stsp return err;
4224 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4225 f1e81a05 2019-08-10 stsp }
4226 f1e81a05 2019-08-10 stsp }
4227 f1e81a05 2019-08-10 stsp if (rejectfile) {
4228 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4229 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4230 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4231 f1e81a05 2019-08-10 stsp if (err)
4232 f1e81a05 2019-08-10 stsp return err;
4233 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4234 f1e81a05 2019-08-10 stsp }
4235 33aa809d 2019-08-08 stsp }
4236 33aa809d 2019-08-08 stsp
4237 33aa809d 2019-08-08 stsp return NULL;
4238 33aa809d 2019-08-08 stsp }
4239 33aa809d 2019-08-08 stsp
4240 33aa809d 2019-08-08 stsp static const struct got_error *
4241 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4242 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4243 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4244 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4245 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4246 33aa809d 2019-08-08 stsp {
4247 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4248 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4249 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4250 33aa809d 2019-08-08 stsp FILE *hunkfile;
4251 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4252 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4253 fe621944 2020-11-10 stsp int rc;
4254 33aa809d 2019-08-08 stsp
4255 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4256 33aa809d 2019-08-08 stsp
4257 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4258 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4259 fe621944 2020-11-10 stsp start_old = cc.left.start;
4260 fe621944 2020-11-10 stsp end_old = cc.left.end;
4261 fe621944 2020-11-10 stsp start_new = cc.right.start;
4262 fe621944 2020-11-10 stsp end_new = cc.right.end;
4263 33aa809d 2019-08-08 stsp
4264 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4265 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4266 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4267 33aa809d 2019-08-08 stsp
4268 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4269 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4270 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4271 33aa809d 2019-08-08 stsp
4272 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4273 fe621944 2020-11-10 stsp if (diff_state == NULL)
4274 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4275 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4276 fe621944 2020-11-10 stsp
4277 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4278 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4279 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4280 33aa809d 2019-08-08 stsp goto done;
4281 33aa809d 2019-08-08 stsp }
4282 fe621944 2020-11-10 stsp
4283 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4284 fe621944 2020-11-10 stsp diff_result, &cc);
4285 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4286 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4287 33aa809d 2019-08-08 stsp goto done;
4288 33aa809d 2019-08-08 stsp }
4289 fe621944 2020-11-10 stsp
4290 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4291 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4292 33aa809d 2019-08-08 stsp goto done;
4293 33aa809d 2019-08-08 stsp }
4294 33aa809d 2019-08-08 stsp
4295 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4296 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4297 33aa809d 2019-08-08 stsp if (err)
4298 33aa809d 2019-08-08 stsp goto done;
4299 33aa809d 2019-08-08 stsp
4300 33aa809d 2019-08-08 stsp switch (*choice) {
4301 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4302 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4303 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4304 33aa809d 2019-08-08 stsp break;
4305 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4306 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4307 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4308 33aa809d 2019-08-08 stsp break;
4309 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4310 33aa809d 2019-08-08 stsp break;
4311 33aa809d 2019-08-08 stsp default:
4312 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4313 33aa809d 2019-08-08 stsp break;
4314 33aa809d 2019-08-08 stsp }
4315 33aa809d 2019-08-08 stsp done:
4316 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4317 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4318 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4319 33aa809d 2019-08-08 stsp return err;
4320 33aa809d 2019-08-08 stsp }
4321 33aa809d 2019-08-08 stsp
4322 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4323 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4324 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4325 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4326 1f1abb7e 2019-08-08 stsp void *progress_arg;
4327 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4328 33aa809d 2019-08-08 stsp void *patch_arg;
4329 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4330 f259c4c1 2021-09-24 stsp int unlink_added_files;
4331 1f1abb7e 2019-08-08 stsp };
4332 a129376b 2019-03-28 stsp
4333 e20a8b6f 2019-06-04 stsp static const struct got_error *
4334 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4335 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4336 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4337 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4338 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4339 33aa809d 2019-08-08 stsp {
4340 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4341 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4342 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4343 1ebedb77 2019-10-19 stsp int fd2 = -1;
4344 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4345 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4346 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4347 fe621944 2020-11-10 stsp struct stat sb2;
4348 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4349 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4350 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4351 33aa809d 2019-08-08 stsp
4352 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4353 33aa809d 2019-08-08 stsp
4354 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4355 33aa809d 2019-08-08 stsp if (err)
4356 33aa809d 2019-08-08 stsp return err;
4357 33aa809d 2019-08-08 stsp
4358 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4359 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4360 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4361 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4362 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4363 fa3cef63 2020-07-23 stsp goto done;
4364 fa3cef63 2020-07-23 stsp }
4365 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4366 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4367 fa3cef63 2020-07-23 stsp if (link_len == -1)
4368 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4369 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4370 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4371 12463d8b 2019-12-13 stsp }
4372 12463d8b 2019-12-13 stsp } else {
4373 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4374 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4375 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4376 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4377 fa3cef63 2020-07-23 stsp goto done;
4378 fa3cef63 2020-07-23 stsp }
4379 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4380 fa3cef63 2020-07-23 stsp sizeof(link_target));
4381 fa3cef63 2020-07-23 stsp if (link_len == -1)
4382 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4383 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4384 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4385 12463d8b 2019-12-13 stsp }
4386 1ebedb77 2019-10-19 stsp }
4387 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4388 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4389 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4390 fa3cef63 2020-07-23 stsp goto done;
4391 fa3cef63 2020-07-23 stsp }
4392 1ebedb77 2019-10-19 stsp
4393 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4394 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4395 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4396 fa3cef63 2020-07-23 stsp goto done;
4397 fa3cef63 2020-07-23 stsp }
4398 fa3cef63 2020-07-23 stsp fd2 = -1;
4399 fa3cef63 2020-07-23 stsp } else {
4400 fa3cef63 2020-07-23 stsp size_t n;
4401 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4402 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4403 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4404 fa3cef63 2020-07-23 stsp goto done;
4405 fa3cef63 2020-07-23 stsp }
4406 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4407 fa3cef63 2020-07-23 stsp if (n != link_len) {
4408 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4409 fa3cef63 2020-07-23 stsp goto done;
4410 fa3cef63 2020-07-23 stsp }
4411 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4412 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4413 fa3cef63 2020-07-23 stsp goto done;
4414 fa3cef63 2020-07-23 stsp }
4415 fa3cef63 2020-07-23 stsp rewind(f2);
4416 33aa809d 2019-08-08 stsp }
4417 33aa809d 2019-08-08 stsp
4418 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4419 33aa809d 2019-08-08 stsp if (err)
4420 33aa809d 2019-08-08 stsp goto done;
4421 33aa809d 2019-08-08 stsp
4422 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4423 33aa809d 2019-08-08 stsp if (err)
4424 33aa809d 2019-08-08 stsp goto done;
4425 33aa809d 2019-08-08 stsp
4426 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4427 33aa809d 2019-08-08 stsp if (err)
4428 33aa809d 2019-08-08 stsp goto done;
4429 33aa809d 2019-08-08 stsp
4430 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4431 fe621944 2020-11-10 stsp NULL);
4432 33aa809d 2019-08-08 stsp if (err)
4433 33aa809d 2019-08-08 stsp goto done;
4434 33aa809d 2019-08-08 stsp
4435 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4436 33aa809d 2019-08-08 stsp if (err)
4437 33aa809d 2019-08-08 stsp goto done;
4438 33aa809d 2019-08-08 stsp
4439 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4440 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4441 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4442 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4443 fe621944 2020-11-10 stsp
4444 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4445 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4446 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4447 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4448 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4449 fe621944 2020-11-10 stsp nchanges++;
4450 fe621944 2020-11-10 stsp }
4451 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4452 33aa809d 2019-08-08 stsp int choice;
4453 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4454 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4455 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4456 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4457 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4458 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4459 33aa809d 2019-08-08 stsp if (err)
4460 33aa809d 2019-08-08 stsp goto done;
4461 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4462 33aa809d 2019-08-08 stsp have_content = 1;
4463 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4464 33aa809d 2019-08-08 stsp break;
4465 33aa809d 2019-08-08 stsp }
4466 1ebedb77 2019-10-19 stsp if (have_content) {
4467 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4468 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4469 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4470 1ebedb77 2019-10-19 stsp if (err)
4471 1ebedb77 2019-10-19 stsp goto done;
4472 1ebedb77 2019-10-19 stsp
4473 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4474 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4475 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4476 fa3cef63 2020-07-23 stsp goto done;
4477 fa3cef63 2020-07-23 stsp }
4478 1ebedb77 2019-10-19 stsp }
4479 1ebedb77 2019-10-19 stsp }
4480 33aa809d 2019-08-08 stsp done:
4481 33aa809d 2019-08-08 stsp free(id_str);
4482 33aa809d 2019-08-08 stsp if (blob)
4483 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4484 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4485 fe621944 2020-11-10 stsp if (err == NULL)
4486 fe621944 2020-11-10 stsp err = free_err;
4487 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4488 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4489 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4490 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4491 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4492 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4493 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4494 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4495 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4496 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4497 33aa809d 2019-08-08 stsp if (err || !have_content) {
4498 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4499 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4500 33aa809d 2019-08-08 stsp free(*path_outfile);
4501 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4502 33aa809d 2019-08-08 stsp }
4503 33aa809d 2019-08-08 stsp free(path1);
4504 33aa809d 2019-08-08 stsp return err;
4505 33aa809d 2019-08-08 stsp }
4506 33aa809d 2019-08-08 stsp
4507 33aa809d 2019-08-08 stsp static const struct got_error *
4508 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4509 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4510 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4511 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4512 a129376b 2019-03-28 stsp {
4513 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4514 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4515 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4516 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4517 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4518 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4519 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4520 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4521 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4522 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4523 a129376b 2019-03-28 stsp
4524 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4525 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4526 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4527 d3bcc3d1 2019-08-08 stsp return NULL;
4528 3d69ad8d 2019-08-17 semarie
4529 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4530 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4531 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4532 d3bcc3d1 2019-08-08 stsp
4533 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4534 65084dad 2019-08-08 stsp if (ie == NULL)
4535 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4536 a129376b 2019-03-28 stsp
4537 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4538 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4539 a129376b 2019-03-28 stsp if (err) {
4540 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4541 a129376b 2019-03-28 stsp goto done;
4542 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4543 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4544 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4545 e20a8b6f 2019-06-04 stsp goto done;
4546 e20a8b6f 2019-06-04 stsp }
4547 a129376b 2019-03-28 stsp }
4548 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4549 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4550 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4551 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4552 a129376b 2019-03-28 stsp goto done;
4553 a129376b 2019-03-28 stsp }
4554 a129376b 2019-03-28 stsp } else {
4555 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4556 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4557 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4558 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4559 a129376b 2019-03-28 stsp goto done;
4560 a129376b 2019-03-28 stsp }
4561 a129376b 2019-03-28 stsp } else {
4562 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4563 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4564 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4565 a129376b 2019-03-28 stsp goto done;
4566 a129376b 2019-03-28 stsp }
4567 a129376b 2019-03-28 stsp }
4568 a129376b 2019-03-28 stsp }
4569 a129376b 2019-03-28 stsp
4570 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4571 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4572 a9fa2909 2019-07-27 stsp if (err) {
4573 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4574 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4575 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4576 a9fa2909 2019-07-27 stsp goto done;
4577 a9fa2909 2019-07-27 stsp } else {
4578 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4579 a9fa2909 2019-07-27 stsp if (err)
4580 a9fa2909 2019-07-27 stsp goto done;
4581 a9fa2909 2019-07-27 stsp
4582 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4583 1233e6b6 2020-10-19 stsp if (err)
4584 a9fa2909 2019-07-27 stsp goto done;
4585 a9fa2909 2019-07-27 stsp
4586 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4587 1233e6b6 2020-10-19 stsp free(te_name);
4588 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4589 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4590 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4591 a9fa2909 2019-07-27 stsp goto done;
4592 a9fa2909 2019-07-27 stsp }
4593 a129376b 2019-03-28 stsp }
4594 a129376b 2019-03-28 stsp
4595 a129376b 2019-03-28 stsp switch (status) {
4596 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4597 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4598 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4599 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4600 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4601 33aa809d 2019-08-08 stsp if (err)
4602 33aa809d 2019-08-08 stsp goto done;
4603 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4604 33aa809d 2019-08-08 stsp break;
4605 33aa809d 2019-08-08 stsp }
4606 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4607 1f1abb7e 2019-08-08 stsp ie->path);
4608 1ee397ad 2019-07-12 stsp if (err)
4609 1ee397ad 2019-07-12 stsp goto done;
4610 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4611 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4612 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4613 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4614 f259c4c1 2021-09-24 stsp relpath) == -1) {
4615 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4616 f259c4c1 2021-09-24 stsp goto done;
4617 f259c4c1 2021-09-24 stsp }
4618 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4619 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4620 f259c4c1 2021-09-24 stsp ondisk_path);
4621 f259c4c1 2021-09-24 stsp break;
4622 f259c4c1 2021-09-24 stsp }
4623 f259c4c1 2021-09-24 stsp }
4624 a129376b 2019-03-28 stsp break;
4625 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4626 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4627 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4628 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4629 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4630 33aa809d 2019-08-08 stsp if (err)
4631 33aa809d 2019-08-08 stsp goto done;
4632 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4633 33aa809d 2019-08-08 stsp break;
4634 33aa809d 2019-08-08 stsp }
4635 33aa809d 2019-08-08 stsp /* fall through */
4636 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4637 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4638 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4639 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4640 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4641 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4642 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4643 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4644 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4645 24278f30 2019-08-03 stsp } else
4646 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4647 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4648 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4649 a129376b 2019-03-28 stsp if (err)
4650 65084dad 2019-08-08 stsp goto done;
4651 65084dad 2019-08-08 stsp
4652 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4653 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4654 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4655 a129376b 2019-03-28 stsp goto done;
4656 65084dad 2019-08-08 stsp }
4657 65084dad 2019-08-08 stsp
4658 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4659 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4660 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4661 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4662 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4663 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4664 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4665 33aa809d 2019-08-08 stsp break;
4666 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4667 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4668 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4669 369fd7e5 2020-07-23 stsp path_content);
4670 369fd7e5 2020-07-23 stsp break;
4671 369fd7e5 2020-07-23 stsp }
4672 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4673 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4674 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4675 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4676 369fd7e5 2020-07-23 stsp } else {
4677 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4678 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4679 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4680 369fd7e5 2020-07-23 stsp goto done;
4681 369fd7e5 2020-07-23 stsp }
4682 33aa809d 2019-08-08 stsp }
4683 33aa809d 2019-08-08 stsp } else {
4684 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4685 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4686 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4687 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4688 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4689 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4690 2e1fa222 2020-07-23 stsp } else {
4691 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4692 2e1fa222 2020-07-23 stsp ie->path,
4693 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4694 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4695 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4696 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4697 2e1fa222 2020-07-23 stsp }
4698 a129376b 2019-03-28 stsp if (err)
4699 a129376b 2019-03-28 stsp goto done;
4700 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4701 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4702 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4703 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4704 437adc9d 2020-12-10 yzhong blob->id.sha1,
4705 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4706 2e1fa222 2020-07-23 stsp if (err)
4707 2e1fa222 2020-07-23 stsp goto done;
4708 2e1fa222 2020-07-23 stsp }
4709 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4710 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4711 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4712 33aa809d 2019-08-08 stsp }
4713 a129376b 2019-03-28 stsp }
4714 a129376b 2019-03-28 stsp break;
4715 e20a8b6f 2019-06-04 stsp }
4716 a129376b 2019-03-28 stsp default:
4717 1f1abb7e 2019-08-08 stsp break;
4718 a129376b 2019-03-28 stsp }
4719 a129376b 2019-03-28 stsp done:
4720 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4721 33aa809d 2019-08-08 stsp free(path_content);
4722 e20a8b6f 2019-06-04 stsp free(parent_path);
4723 a129376b 2019-03-28 stsp free(tree_path);
4724 a129376b 2019-03-28 stsp if (blob)
4725 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4726 a129376b 2019-03-28 stsp if (tree)
4727 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4728 a129376b 2019-03-28 stsp free(tree_id);
4729 e20a8b6f 2019-06-04 stsp return err;
4730 e20a8b6f 2019-06-04 stsp }
4731 e20a8b6f 2019-06-04 stsp
4732 e20a8b6f 2019-06-04 stsp const struct got_error *
4733 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4734 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4735 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4736 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4737 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4738 e20a8b6f 2019-06-04 stsp {
4739 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4740 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4741 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4742 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4743 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4744 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4745 e20a8b6f 2019-06-04 stsp
4746 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4747 e20a8b6f 2019-06-04 stsp if (err)
4748 e20a8b6f 2019-06-04 stsp return err;
4749 e20a8b6f 2019-06-04 stsp
4750 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4751 e20a8b6f 2019-06-04 stsp if (err)
4752 e20a8b6f 2019-06-04 stsp goto done;
4753 e20a8b6f 2019-06-04 stsp
4754 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4755 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4756 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4757 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4758 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4759 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4760 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4761 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4762 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4763 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4764 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4765 e20a8b6f 2019-06-04 stsp if (err)
4766 af12c6b9 2019-06-04 stsp break;
4767 e20a8b6f 2019-06-04 stsp }
4768 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4769 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4770 e20a8b6f 2019-06-04 stsp err = sync_err;
4771 af12c6b9 2019-06-04 stsp done:
4772 fb399478 2019-07-12 stsp free(fileindex_path);
4773 a129376b 2019-03-28 stsp if (fileindex)
4774 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4775 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4776 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4777 a129376b 2019-03-28 stsp err = unlockerr;
4778 c4296144 2019-05-09 stsp return err;
4779 c4296144 2019-05-09 stsp }
4780 c4296144 2019-05-09 stsp
4781 cf066bf8 2019-05-09 stsp static void
4782 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4783 cf066bf8 2019-05-09 stsp {
4784 24519714 2019-05-09 stsp free(ct->path);
4785 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4786 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4787 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4788 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4789 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4790 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4791 cf066bf8 2019-05-09 stsp free(ct);
4792 cf066bf8 2019-05-09 stsp }
4793 24519714 2019-05-09 stsp
4794 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4795 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4796 24519714 2019-05-09 stsp struct got_repository *repo;
4797 24519714 2019-05-09 stsp struct got_worktree *worktree;
4798 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4799 5f8a88c6 2019-08-03 stsp int have_staged_files;
4800 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4801 24519714 2019-05-09 stsp };
4802 cf066bf8 2019-05-09 stsp
4803 c4296144 2019-05-09 stsp static const struct got_error *
4804 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4805 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4806 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4807 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4808 c4296144 2019-05-09 stsp {
4809 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4810 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4811 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4812 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4813 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4814 768aea60 2019-05-09 stsp struct stat sb;
4815 c4296144 2019-05-09 stsp
4816 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4817 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4818 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4819 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4820 5f8a88c6 2019-08-03 stsp return NULL;
4821 5f8a88c6 2019-08-03 stsp } else {
4822 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4823 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4824 c4296144 2019-05-09 stsp
4825 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4826 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4827 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4828 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4829 5f8a88c6 2019-08-03 stsp return NULL;
4830 5f8a88c6 2019-08-03 stsp }
4831 0b5cc0d6 2019-05-09 stsp
4832 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4833 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4834 036813ee 2019-05-09 stsp goto done;
4835 036813ee 2019-05-09 stsp }
4836 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4837 036813ee 2019-05-09 stsp parent_path = strdup("");
4838 69960a46 2019-05-09 stsp if (parent_path == NULL)
4839 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4840 69960a46 2019-05-09 stsp } else {
4841 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4842 69960a46 2019-05-09 stsp if (err)
4843 69960a46 2019-05-09 stsp return err;
4844 69960a46 2019-05-09 stsp }
4845 c4296144 2019-05-09 stsp
4846 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4847 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4848 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4849 c4296144 2019-05-09 stsp goto done;
4850 768aea60 2019-05-09 stsp }
4851 768aea60 2019-05-09 stsp
4852 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4853 768aea60 2019-05-09 stsp relpath) == -1) {
4854 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4855 768aea60 2019-05-09 stsp goto done;
4856 768aea60 2019-05-09 stsp }
4857 0aeb8099 2020-07-23 stsp
4858 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4859 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4860 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4861 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4862 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4863 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4864 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4865 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4866 0aeb8099 2020-07-23 stsp break;
4867 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4868 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4869 0aeb8099 2020-07-23 stsp break;
4870 0aeb8099 2020-07-23 stsp default:
4871 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4872 0aeb8099 2020-07-23 stsp goto done;
4873 0aeb8099 2020-07-23 stsp }
4874 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4875 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4876 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4877 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4878 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4879 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4880 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4881 12463d8b 2019-12-13 stsp ct->ondisk_path);
4882 12463d8b 2019-12-13 stsp goto done;
4883 12463d8b 2019-12-13 stsp }
4884 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4885 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4886 768aea60 2019-05-09 stsp goto done;
4887 768aea60 2019-05-09 stsp }
4888 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4889 c4296144 2019-05-09 stsp }
4890 c4296144 2019-05-09 stsp
4891 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4892 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4893 44d03001 2019-05-09 stsp relpath) == -1) {
4894 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4895 44d03001 2019-05-09 stsp goto done;
4896 44d03001 2019-05-09 stsp }
4897 44d03001 2019-05-09 stsp
4898 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4899 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4900 35213c7c 2020-07-23 stsp int is_bad_symlink;
4901 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4902 35213c7c 2020-07-23 stsp ssize_t target_len;
4903 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4904 35213c7c 2020-07-23 stsp sizeof(target_path));
4905 35213c7c 2020-07-23 stsp if (target_len == -1) {
4906 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4907 35213c7c 2020-07-23 stsp ct->ondisk_path);
4908 35213c7c 2020-07-23 stsp goto done;
4909 35213c7c 2020-07-23 stsp }
4910 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4911 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4912 35213c7c 2020-07-23 stsp if (err)
4913 35213c7c 2020-07-23 stsp goto done;
4914 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4915 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4916 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4917 35213c7c 2020-07-23 stsp goto done;
4918 35213c7c 2020-07-23 stsp }
4919 35213c7c 2020-07-23 stsp }
4920 35213c7c 2020-07-23 stsp
4921 35213c7c 2020-07-23 stsp
4922 cf066bf8 2019-05-09 stsp ct->status = status;
4923 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4924 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4925 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4926 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4927 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4928 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4929 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4930 b416585c 2019-05-13 stsp goto done;
4931 b416585c 2019-05-13 stsp }
4932 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4933 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4934 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4935 f0b75401 2019-08-03 stsp goto done;
4936 f0b75401 2019-08-03 stsp }
4937 f0b75401 2019-08-03 stsp }
4938 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4939 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4940 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4941 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4942 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4943 036813ee 2019-05-09 stsp goto done;
4944 036813ee 2019-05-09 stsp }
4945 ca2503ea 2019-05-09 stsp }
4946 24519714 2019-05-09 stsp ct->path = strdup(path);
4947 24519714 2019-05-09 stsp if (ct->path == NULL) {
4948 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4949 24519714 2019-05-09 stsp goto done;
4950 24519714 2019-05-09 stsp }
4951 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4952 c4296144 2019-05-09 stsp done:
4953 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4954 ed175427 2019-05-09 stsp free_commitable(ct);
4955 24519714 2019-05-09 stsp free(parent_path);
4956 036813ee 2019-05-09 stsp free(path);
4957 a129376b 2019-03-28 stsp return err;
4958 a129376b 2019-03-28 stsp }
4959 c4296144 2019-05-09 stsp
4960 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4961 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4962 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4963 036813ee 2019-05-09 stsp struct got_repository *);
4964 ed175427 2019-05-09 stsp
4965 ed175427 2019-05-09 stsp static const struct got_error *
4966 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4967 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4968 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4969 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4970 afa376bf 2019-05-09 stsp struct got_repository *repo)
4971 ed175427 2019-05-09 stsp {
4972 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4973 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4974 036813ee 2019-05-09 stsp char *subpath;
4975 ed175427 2019-05-09 stsp
4976 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4977 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4978 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4979 ed175427 2019-05-09 stsp
4980 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4981 ed175427 2019-05-09 stsp if (err)
4982 ed175427 2019-05-09 stsp return err;
4983 ed175427 2019-05-09 stsp
4984 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4985 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4986 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4987 036813ee 2019-05-09 stsp free(subpath);
4988 036813ee 2019-05-09 stsp return err;
4989 036813ee 2019-05-09 stsp }
4990 ed175427 2019-05-09 stsp
4991 036813ee 2019-05-09 stsp static const struct got_error *
4992 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4993 036813ee 2019-05-09 stsp {
4994 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4995 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4996 ed175427 2019-05-09 stsp
4997 036813ee 2019-05-09 stsp *match = 0;
4998 ed175427 2019-05-09 stsp
4999 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5000 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5001 0f63689d 2019-05-10 stsp return NULL;
5002 036813ee 2019-05-09 stsp }
5003 0b5cc0d6 2019-05-09 stsp
5004 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5005 0f63689d 2019-05-10 stsp if (err)
5006 0f63689d 2019-05-10 stsp return err;
5007 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5008 036813ee 2019-05-09 stsp free(ct_parent_path);
5009 036813ee 2019-05-09 stsp return err;
5010 036813ee 2019-05-09 stsp }
5011 0b5cc0d6 2019-05-09 stsp
5012 768aea60 2019-05-09 stsp static mode_t
5013 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5014 768aea60 2019-05-09 stsp {
5015 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5016 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5017 3d9a4ec4 2020-07-23 stsp
5018 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5019 768aea60 2019-05-09 stsp }
5020 768aea60 2019-05-09 stsp
5021 036813ee 2019-05-09 stsp static const struct got_error *
5022 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5023 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5024 036813ee 2019-05-09 stsp {
5025 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5026 ca2503ea 2019-05-09 stsp
5027 036813ee 2019-05-09 stsp *new_te = NULL;
5028 0b5cc0d6 2019-05-09 stsp
5029 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5030 036813ee 2019-05-09 stsp if (err)
5031 036813ee 2019-05-09 stsp goto done;
5032 0b5cc0d6 2019-05-09 stsp
5033 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5034 036813ee 2019-05-09 stsp
5035 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5036 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5037 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5038 5f8a88c6 2019-08-03 stsp else
5039 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5040 036813ee 2019-05-09 stsp done:
5041 036813ee 2019-05-09 stsp if (err && *new_te) {
5042 56e0773d 2019-11-28 stsp free(*new_te);
5043 036813ee 2019-05-09 stsp *new_te = NULL;
5044 036813ee 2019-05-09 stsp }
5045 036813ee 2019-05-09 stsp return err;
5046 036813ee 2019-05-09 stsp }
5047 036813ee 2019-05-09 stsp
5048 036813ee 2019-05-09 stsp static const struct got_error *
5049 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5050 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5051 036813ee 2019-05-09 stsp {
5052 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5053 102b254e 2020-10-19 stsp char *ct_name = NULL;
5054 036813ee 2019-05-09 stsp
5055 036813ee 2019-05-09 stsp *new_te = NULL;
5056 036813ee 2019-05-09 stsp
5057 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5058 036813ee 2019-05-09 stsp if (*new_te == NULL)
5059 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5060 036813ee 2019-05-09 stsp
5061 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5062 102b254e 2020-10-19 stsp if (err)
5063 036813ee 2019-05-09 stsp goto done;
5064 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5065 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5066 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5067 036813ee 2019-05-09 stsp goto done;
5068 036813ee 2019-05-09 stsp }
5069 036813ee 2019-05-09 stsp
5070 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5071 036813ee 2019-05-09 stsp
5072 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5073 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5074 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5075 5f8a88c6 2019-08-03 stsp else
5076 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5077 036813ee 2019-05-09 stsp done:
5078 102b254e 2020-10-19 stsp free(ct_name);
5079 036813ee 2019-05-09 stsp if (err && *new_te) {
5080 56e0773d 2019-11-28 stsp free(*new_te);
5081 036813ee 2019-05-09 stsp *new_te = NULL;
5082 036813ee 2019-05-09 stsp }
5083 036813ee 2019-05-09 stsp return err;
5084 036813ee 2019-05-09 stsp }
5085 036813ee 2019-05-09 stsp
5086 036813ee 2019-05-09 stsp static const struct got_error *
5087 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5088 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5089 036813ee 2019-05-09 stsp {
5090 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5091 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5092 036813ee 2019-05-09 stsp
5093 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5094 036813ee 2019-05-09 stsp if (err)
5095 036813ee 2019-05-09 stsp return err;
5096 036813ee 2019-05-09 stsp if (new_pe == NULL)
5097 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5098 036813ee 2019-05-09 stsp return NULL;
5099 afa376bf 2019-05-09 stsp }
5100 afa376bf 2019-05-09 stsp
5101 afa376bf 2019-05-09 stsp static const struct got_error *
5102 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5103 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5104 afa376bf 2019-05-09 stsp {
5105 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5106 5f8a88c6 2019-08-03 stsp unsigned char status;
5107 0ff8d236 2021-09-28 stsp
5108 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5109 0ff8d236 2021-09-28 stsp return NULL;
5110 5f8a88c6 2019-08-03 stsp
5111 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5112 afa376bf 2019-05-09 stsp ct_path++;
5113 5f8a88c6 2019-08-03 stsp
5114 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5115 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5116 5f8a88c6 2019-08-03 stsp else
5117 5f8a88c6 2019-08-03 stsp status = ct->status;
5118 5f8a88c6 2019-08-03 stsp
5119 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5120 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5121 036813ee 2019-05-09 stsp }
5122 036813ee 2019-05-09 stsp
5123 036813ee 2019-05-09 stsp static const struct got_error *
5124 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5125 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5126 44d03001 2019-05-09 stsp {
5127 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5128 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5129 44d03001 2019-05-09 stsp char *te_path;
5130 44d03001 2019-05-09 stsp
5131 44d03001 2019-05-09 stsp *modified = 0;
5132 44d03001 2019-05-09 stsp
5133 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5134 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5135 44d03001 2019-05-09 stsp te->name) == -1)
5136 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5137 44d03001 2019-05-09 stsp
5138 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5139 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5140 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5141 44d03001 2019-05-09 stsp strlen(te_path));
5142 62d463ca 2020-10-20 naddy if (*modified)
5143 44d03001 2019-05-09 stsp break;
5144 44d03001 2019-05-09 stsp }
5145 44d03001 2019-05-09 stsp
5146 44d03001 2019-05-09 stsp free(te_path);
5147 44d03001 2019-05-09 stsp return err;
5148 44d03001 2019-05-09 stsp }
5149 44d03001 2019-05-09 stsp
5150 44d03001 2019-05-09 stsp static const struct got_error *
5151 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5152 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5153 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5154 036813ee 2019-05-09 stsp {
5155 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5156 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5157 036813ee 2019-05-09 stsp
5158 036813ee 2019-05-09 stsp *ctp = NULL;
5159 036813ee 2019-05-09 stsp
5160 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5161 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5162 036813ee 2019-05-09 stsp char *ct_name = NULL;
5163 036813ee 2019-05-09 stsp int path_matches;
5164 036813ee 2019-05-09 stsp
5165 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5166 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5167 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5168 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5169 5f8a88c6 2019-08-03 stsp continue;
5170 5f8a88c6 2019-08-03 stsp } else {
5171 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5172 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5173 5f8a88c6 2019-08-03 stsp continue;
5174 5f8a88c6 2019-08-03 stsp }
5175 036813ee 2019-05-09 stsp
5176 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5177 036813ee 2019-05-09 stsp continue;
5178 036813ee 2019-05-09 stsp
5179 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5180 62d463ca 2020-10-20 naddy if (err)
5181 036813ee 2019-05-09 stsp return err;
5182 036813ee 2019-05-09 stsp if (!path_matches)
5183 036813ee 2019-05-09 stsp continue;
5184 036813ee 2019-05-09 stsp
5185 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5186 d34b633e 2020-10-19 stsp if (err)
5187 d34b633e 2020-10-19 stsp return err;
5188 d34b633e 2020-10-19 stsp
5189 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5190 d34b633e 2020-10-19 stsp free(ct_name);
5191 036813ee 2019-05-09 stsp continue;
5192 d34b633e 2020-10-19 stsp }
5193 d34b633e 2020-10-19 stsp free(ct_name);
5194 036813ee 2019-05-09 stsp
5195 036813ee 2019-05-09 stsp *ctp = ct;
5196 036813ee 2019-05-09 stsp break;
5197 036813ee 2019-05-09 stsp }
5198 2b496619 2019-07-10 stsp
5199 2b496619 2019-07-10 stsp return err;
5200 2b496619 2019-07-10 stsp }
5201 2b496619 2019-07-10 stsp
5202 2b496619 2019-07-10 stsp static const struct got_error *
5203 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5204 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5205 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5206 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5207 2b496619 2019-07-10 stsp struct got_repository *repo)
5208 2b496619 2019-07-10 stsp {
5209 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5210 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5211 2b496619 2019-07-10 stsp char *subtree_path;
5212 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5213 ba580f68 2020-03-22 stsp int nentries;
5214 2b496619 2019-07-10 stsp
5215 2b496619 2019-07-10 stsp *new_tep = NULL;
5216 2b496619 2019-07-10 stsp
5217 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5218 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5219 2b496619 2019-07-10 stsp child_path) == -1)
5220 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5221 036813ee 2019-05-09 stsp
5222 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5223 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5224 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5225 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5226 56e0773d 2019-11-28 stsp
5227 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5228 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5229 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5230 2b496619 2019-07-10 stsp goto done;
5231 2b496619 2019-07-10 stsp }
5232 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5233 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5234 2b496619 2019-07-10 stsp if (err) {
5235 56e0773d 2019-11-28 stsp free(new_te);
5236 2b496619 2019-07-10 stsp goto done;
5237 2b496619 2019-07-10 stsp }
5238 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5239 2b496619 2019-07-10 stsp done:
5240 56e0773d 2019-11-28 stsp free(id);
5241 2b496619 2019-07-10 stsp free(subtree_path);
5242 2b496619 2019-07-10 stsp if (err == NULL)
5243 2b496619 2019-07-10 stsp *new_tep = new_te;
5244 036813ee 2019-05-09 stsp return err;
5245 036813ee 2019-05-09 stsp }
5246 036813ee 2019-05-09 stsp
5247 036813ee 2019-05-09 stsp static const struct got_error *
5248 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5249 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5250 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5251 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5252 036813ee 2019-05-09 stsp struct got_repository *repo)
5253 036813ee 2019-05-09 stsp {
5254 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5255 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5256 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5257 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5258 036813ee 2019-05-09 stsp
5259 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5260 ba580f68 2020-03-22 stsp *nentries = 0;
5261 036813ee 2019-05-09 stsp
5262 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5263 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5264 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5265 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5266 036813ee 2019-05-09 stsp
5267 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5268 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5269 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5270 036813ee 2019-05-09 stsp continue;
5271 036813ee 2019-05-09 stsp
5272 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5273 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5274 036813ee 2019-05-09 stsp continue;
5275 036813ee 2019-05-09 stsp
5276 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5277 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5278 036813ee 2019-05-09 stsp if (err)
5279 036813ee 2019-05-09 stsp goto done;
5280 036813ee 2019-05-09 stsp
5281 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5282 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5283 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5284 036813ee 2019-05-09 stsp if (err)
5285 036813ee 2019-05-09 stsp goto done;
5286 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5287 afa376bf 2019-05-09 stsp if (err)
5288 afa376bf 2019-05-09 stsp goto done;
5289 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5290 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5291 2b496619 2019-07-10 stsp if (err)
5292 2f51b5b3 2019-05-09 stsp goto done;
5293 ba580f68 2020-03-22 stsp (*nentries)++;
5294 2b496619 2019-07-10 stsp } else {
5295 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5296 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5297 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5298 2b496619 2019-07-10 stsp == NULL) {
5299 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5300 2b496619 2019-07-10 stsp child_path, path_base_tree,
5301 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5302 2b496619 2019-07-10 stsp repo);
5303 2b496619 2019-07-10 stsp if (err)
5304 2b496619 2019-07-10 stsp goto done;
5305 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5306 2b496619 2019-07-10 stsp if (err)
5307 2b496619 2019-07-10 stsp goto done;
5308 ba580f68 2020-03-22 stsp (*nentries)++;
5309 9ba0479c 2019-05-10 stsp }
5310 ed175427 2019-05-09 stsp }
5311 2f51b5b3 2019-05-09 stsp }
5312 2f51b5b3 2019-05-09 stsp
5313 2f51b5b3 2019-05-09 stsp if (base_tree) {
5314 56e0773d 2019-11-28 stsp int i, nbase_entries;
5315 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5316 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5317 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5318 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5319 63c5ca5d 2019-08-24 stsp
5320 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5321 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5322 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5323 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5324 63c5ca5d 2019-08-24 stsp if (err)
5325 63c5ca5d 2019-08-24 stsp goto done;
5326 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5327 63c5ca5d 2019-08-24 stsp if (err)
5328 63c5ca5d 2019-08-24 stsp goto done;
5329 ba580f68 2020-03-22 stsp (*nentries)++;
5330 63c5ca5d 2019-08-24 stsp continue;
5331 63c5ca5d 2019-08-24 stsp }
5332 2f51b5b3 2019-05-09 stsp
5333 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5334 44d03001 2019-05-09 stsp int modified;
5335 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5336 036813ee 2019-05-09 stsp if (err)
5337 036813ee 2019-05-09 stsp goto done;
5338 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5339 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5340 2f51b5b3 2019-05-09 stsp if (err)
5341 2f51b5b3 2019-05-09 stsp goto done;
5342 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5343 44d03001 2019-05-09 stsp if (modified) {
5344 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5345 ba580f68 2020-03-22 stsp int nsubentries;
5346 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5347 ba580f68 2020-03-22 stsp &nsubentries, te,
5348 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5349 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5350 44d03001 2019-05-09 stsp if (err)
5351 44d03001 2019-05-09 stsp goto done;
5352 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5353 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5354 ba580f68 2020-03-22 stsp free(new_id);
5355 ba580f68 2020-03-22 stsp continue;
5356 ba580f68 2020-03-22 stsp }
5357 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5358 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5359 56e0773d 2019-11-28 stsp free(new_id);
5360 44d03001 2019-05-09 stsp }
5361 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5362 036813ee 2019-05-09 stsp if (err)
5363 036813ee 2019-05-09 stsp goto done;
5364 ba580f68 2020-03-22 stsp (*nentries)++;
5365 2f51b5b3 2019-05-09 stsp continue;
5366 036813ee 2019-05-09 stsp }
5367 2f51b5b3 2019-05-09 stsp
5368 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5369 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5370 f66c734c 2019-09-22 stsp if (err)
5371 f66c734c 2019-09-22 stsp goto done;
5372 2f51b5b3 2019-05-09 stsp if (ct) {
5373 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5374 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5375 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5376 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5377 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5378 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5379 2f51b5b3 2019-05-09 stsp if (err)
5380 2f51b5b3 2019-05-09 stsp goto done;
5381 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5382 2f51b5b3 2019-05-09 stsp if (err)
5383 2f51b5b3 2019-05-09 stsp goto done;
5384 ba580f68 2020-03-22 stsp (*nentries)++;
5385 2f51b5b3 2019-05-09 stsp }
5386 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5387 b416585c 2019-05-13 stsp status_arg);
5388 afa376bf 2019-05-09 stsp if (err)
5389 afa376bf 2019-05-09 stsp goto done;
5390 2f51b5b3 2019-05-09 stsp } else {
5391 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5392 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5393 2f51b5b3 2019-05-09 stsp if (err)
5394 2f51b5b3 2019-05-09 stsp goto done;
5395 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5396 2f51b5b3 2019-05-09 stsp if (err)
5397 2f51b5b3 2019-05-09 stsp goto done;
5398 ba580f68 2020-03-22 stsp (*nentries)++;
5399 2f51b5b3 2019-05-09 stsp }
5400 ca2503ea 2019-05-09 stsp }
5401 ed175427 2019-05-09 stsp }
5402 0b5cc0d6 2019-05-09 stsp
5403 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5404 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5405 ed175427 2019-05-09 stsp done:
5406 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5407 2e1fa222 2020-07-23 stsp return err;
5408 2e1fa222 2020-07-23 stsp }
5409 2e1fa222 2020-07-23 stsp
5410 2e1fa222 2020-07-23 stsp static const struct got_error *
5411 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5412 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5413 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5414 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5415 ebf99748 2019-05-09 stsp {
5416 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5417 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5418 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5419 ebf99748 2019-05-09 stsp
5420 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5421 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5422 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5423 ebf99748 2019-05-09 stsp
5424 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5425 437adc9d 2020-12-10 yzhong
5426 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5427 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5428 437adc9d 2020-12-10 yzhong if (err)
5429 437adc9d 2020-12-10 yzhong goto done;
5430 437adc9d 2020-12-10 yzhong
5431 ebf99748 2019-05-09 stsp if (ie) {
5432 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5433 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5434 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5435 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5436 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5437 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5438 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5439 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5440 437adc9d 2020-12-10 yzhong
5441 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5442 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5443 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5444 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5445 72fd46fa 2019-09-06 stsp !have_staged_files);
5446 ebf99748 2019-05-09 stsp } else
5447 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5448 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5449 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5450 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5451 72fd46fa 2019-09-06 stsp !have_staged_files);
5452 ebf99748 2019-05-09 stsp } else {
5453 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5454 ebf99748 2019-05-09 stsp if (err)
5455 437adc9d 2020-12-10 yzhong goto done;
5456 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5457 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5458 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5459 3969253a 2020-03-07 stsp if (err) {
5460 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5461 437adc9d 2020-12-10 yzhong goto done;
5462 3969253a 2020-03-07 stsp }
5463 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5464 3969253a 2020-03-07 stsp if (err) {
5465 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5466 437adc9d 2020-12-10 yzhong goto done;
5467 3969253a 2020-03-07 stsp }
5468 ebf99748 2019-05-09 stsp }
5469 437adc9d 2020-12-10 yzhong free(relpath);
5470 437adc9d 2020-12-10 yzhong relpath = NULL;
5471 ebf99748 2019-05-09 stsp }
5472 437adc9d 2020-12-10 yzhong done:
5473 437adc9d 2020-12-10 yzhong free(relpath);
5474 d56d26ce 2019-05-10 stsp return err;
5475 d56d26ce 2019-05-10 stsp }
5476 735ef5ac 2019-08-03 stsp
5477 d56d26ce 2019-05-10 stsp
5478 d56d26ce 2019-05-10 stsp static const struct got_error *
5479 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5480 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5481 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5482 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5483 735ef5ac 2019-08-03 stsp int ood_errcode)
5484 d56d26ce 2019-05-10 stsp {
5485 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5486 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5487 1a36436d 2019-06-10 stsp
5488 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5489 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5490 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5491 1a36436d 2019-06-10 stsp return NULL;
5492 9bead371 2019-07-28 stsp /*
5493 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5494 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5495 9bead371 2019-07-28 stsp */
5496 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5497 735ef5ac 2019-08-03 stsp in_repo_path);
5498 9bead371 2019-07-28 stsp if (err) {
5499 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5500 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5501 1a36436d 2019-06-10 stsp goto done;
5502 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5503 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5504 1a36436d 2019-06-10 stsp } else {
5505 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5506 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5507 735ef5ac 2019-08-03 stsp in_repo_path);
5508 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5509 1a36436d 2019-06-10 stsp goto done;
5510 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5511 1a36436d 2019-06-10 stsp }
5512 1a36436d 2019-06-10 stsp done:
5513 1a36436d 2019-06-10 stsp free(id);
5514 1a36436d 2019-06-10 stsp return err;
5515 ebf99748 2019-05-09 stsp }
5516 ebf99748 2019-05-09 stsp
5517 c4296144 2019-05-09 stsp const struct got_error *
5518 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5519 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5520 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5521 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5522 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5523 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5524 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5525 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5526 afa376bf 2019-05-09 stsp struct got_repository *repo)
5527 c4296144 2019-05-09 stsp {
5528 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5529 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5530 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5531 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5532 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5533 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5534 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5535 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5536 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5537 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5538 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5539 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5540 c4296144 2019-05-09 stsp
5541 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5542 c4296144 2019-05-09 stsp
5543 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5544 675c7539 2019-05-09 stsp
5545 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5546 588edf97 2019-05-10 stsp if (err)
5547 588edf97 2019-05-10 stsp goto done;
5548 de18fc63 2019-05-09 stsp
5549 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5550 588edf97 2019-05-10 stsp if (err)
5551 588edf97 2019-05-10 stsp goto done;
5552 588edf97 2019-05-10 stsp
5553 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5554 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5555 33ad4cbe 2019-05-12 jcs if (err)
5556 33ad4cbe 2019-05-12 jcs goto done;
5557 33ad4cbe 2019-05-12 jcs }
5558 c4296144 2019-05-09 stsp
5559 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5560 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5561 33ad4cbe 2019-05-12 jcs goto done;
5562 33ad4cbe 2019-05-12 jcs }
5563 33ad4cbe 2019-05-12 jcs
5564 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5565 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5566 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5567 cf066bf8 2019-05-09 stsp char *ondisk_path;
5568 cf066bf8 2019-05-09 stsp
5569 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5570 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5571 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5572 5f8a88c6 2019-08-03 stsp continue;
5573 5f8a88c6 2019-08-03 stsp
5574 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5575 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5576 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5577 cf066bf8 2019-05-09 stsp continue;
5578 cf066bf8 2019-05-09 stsp
5579 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5580 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5581 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5582 cf066bf8 2019-05-09 stsp goto done;
5583 cf066bf8 2019-05-09 stsp }
5584 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5585 2e1fa222 2020-07-23 stsp free(ondisk_path);
5586 2e1fa222 2020-07-23 stsp if (err)
5587 cf066bf8 2019-05-09 stsp goto done;
5588 cf066bf8 2019-05-09 stsp }
5589 036813ee 2019-05-09 stsp
5590 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5591 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5592 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5593 036813ee 2019-05-09 stsp if (err)
5594 036813ee 2019-05-09 stsp goto done;
5595 036813ee 2019-05-09 stsp
5596 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5597 de18fc63 2019-05-09 stsp if (err)
5598 de18fc63 2019-05-09 stsp goto done;
5599 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5600 f259c4c1 2021-09-24 stsp nparents++;
5601 f259c4c1 2021-09-24 stsp if (parent_id2) {
5602 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5603 f259c4c1 2021-09-24 stsp if (err)
5604 f259c4c1 2021-09-24 stsp goto done;
5605 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5606 f259c4c1 2021-09-24 stsp nparents++;
5607 f259c4c1 2021-09-24 stsp }
5608 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5609 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5610 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5611 33ad4cbe 2019-05-12 jcs free(logmsg);
5612 9d40349a 2019-05-09 stsp if (err)
5613 9d40349a 2019-05-09 stsp goto done;
5614 9d40349a 2019-05-09 stsp
5615 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5616 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5617 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5618 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5619 09f5bd90 2019-05-09 stsp goto done;
5620 09f5bd90 2019-05-09 stsp }
5621 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5622 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5623 09f5bd90 2019-05-09 stsp if (err)
5624 09f5bd90 2019-05-09 stsp goto done;
5625 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5626 ebf99748 2019-05-09 stsp if (err)
5627 ebf99748 2019-05-09 stsp goto done;
5628 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5629 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5630 09f5bd90 2019-05-09 stsp goto done;
5631 09f5bd90 2019-05-09 stsp }
5632 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5633 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5634 f2c16586 2019-05-09 stsp if (err)
5635 f2c16586 2019-05-09 stsp goto done;
5636 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5637 f2c16586 2019-05-09 stsp if (err)
5638 f2c16586 2019-05-09 stsp goto done;
5639 f2c16586 2019-05-09 stsp
5640 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5641 09f5bd90 2019-05-09 stsp if (err)
5642 09f5bd90 2019-05-09 stsp goto done;
5643 09f5bd90 2019-05-09 stsp
5644 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5645 ebf99748 2019-05-09 stsp if (err)
5646 ebf99748 2019-05-09 stsp goto done;
5647 c4296144 2019-05-09 stsp done:
5648 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5649 588edf97 2019-05-10 stsp if (head_tree)
5650 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5651 588edf97 2019-05-10 stsp if (head_commit)
5652 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5653 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5654 2f17228e 2019-05-12 stsp if (head_ref2) {
5655 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5656 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5657 2f17228e 2019-05-12 stsp err = unlockerr;
5658 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5659 39cd0ff6 2019-07-12 stsp }
5660 39cd0ff6 2019-07-12 stsp return err;
5661 39cd0ff6 2019-07-12 stsp }
5662 5c1e53bc 2019-07-28 stsp
5663 5c1e53bc 2019-07-28 stsp static const struct got_error *
5664 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5665 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5666 5c1e53bc 2019-07-28 stsp {
5667 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5668 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5669 39cd0ff6 2019-07-12 stsp
5670 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5671 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5672 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5673 5c1e53bc 2019-07-28 stsp
5674 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5675 5c1e53bc 2019-07-28 stsp ct_path++;
5676 5c1e53bc 2019-07-28 stsp
5677 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5678 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5679 5c1e53bc 2019-07-28 stsp break;
5680 5c1e53bc 2019-07-28 stsp }
5681 5c1e53bc 2019-07-28 stsp
5682 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5683 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5684 5c1e53bc 2019-07-28 stsp
5685 5c1e53bc 2019-07-28 stsp return NULL;
5686 5c1e53bc 2019-07-28 stsp }
5687 5c1e53bc 2019-07-28 stsp
5688 f0b75401 2019-08-03 stsp static const struct got_error *
5689 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5690 f0b75401 2019-08-03 stsp {
5691 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5692 f0b75401 2019-08-03 stsp
5693 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5694 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5695 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5696 f0b75401 2019-08-03 stsp }
5697 f0b75401 2019-08-03 stsp
5698 f0b75401 2019-08-03 stsp return NULL;
5699 f0b75401 2019-08-03 stsp }
5700 f0b75401 2019-08-03 stsp
5701 5f8a88c6 2019-08-03 stsp static const struct got_error *
5702 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5703 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5704 5f8a88c6 2019-08-03 stsp {
5705 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5706 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5707 5f8a88c6 2019-08-03 stsp
5708 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5709 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5710 5f8a88c6 2019-08-03 stsp continue;
5711 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5712 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5713 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5714 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5715 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5716 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5717 5f8a88c6 2019-08-03 stsp }
5718 5f8a88c6 2019-08-03 stsp
5719 5f8a88c6 2019-08-03 stsp return NULL;
5720 5f8a88c6 2019-08-03 stsp }
5721 5f8a88c6 2019-08-03 stsp
5722 39cd0ff6 2019-07-12 stsp const struct got_error *
5723 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5724 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5725 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5726 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5727 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5728 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5729 39cd0ff6 2019-07-12 stsp {
5730 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5731 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5732 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5733 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5734 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5735 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5736 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5737 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5738 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5739 39cd0ff6 2019-07-12 stsp
5740 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5741 39cd0ff6 2019-07-12 stsp
5742 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5743 39cd0ff6 2019-07-12 stsp
5744 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5745 39cd0ff6 2019-07-12 stsp if (err)
5746 39cd0ff6 2019-07-12 stsp goto done;
5747 39cd0ff6 2019-07-12 stsp
5748 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5749 39cd0ff6 2019-07-12 stsp if (err)
5750 39cd0ff6 2019-07-12 stsp goto done;
5751 39cd0ff6 2019-07-12 stsp
5752 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5753 39cd0ff6 2019-07-12 stsp if (err)
5754 39cd0ff6 2019-07-12 stsp goto done;
5755 39cd0ff6 2019-07-12 stsp
5756 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5757 39cd0ff6 2019-07-12 stsp if (err)
5758 39cd0ff6 2019-07-12 stsp goto done;
5759 39cd0ff6 2019-07-12 stsp
5760 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5761 5f8a88c6 2019-08-03 stsp &have_staged_files);
5762 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5763 5f8a88c6 2019-08-03 stsp goto done;
5764 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5765 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5766 5f8a88c6 2019-08-03 stsp if (err)
5767 5f8a88c6 2019-08-03 stsp goto done;
5768 5f8a88c6 2019-08-03 stsp }
5769 5f8a88c6 2019-08-03 stsp
5770 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5771 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5772 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5773 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5774 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5775 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5776 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5777 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5778 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5779 5c1e53bc 2019-07-28 stsp if (err)
5780 5c1e53bc 2019-07-28 stsp goto done;
5781 5c1e53bc 2019-07-28 stsp }
5782 39cd0ff6 2019-07-12 stsp
5783 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5784 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5785 39cd0ff6 2019-07-12 stsp goto done;
5786 5c1e53bc 2019-07-28 stsp }
5787 5c1e53bc 2019-07-28 stsp
5788 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5789 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5790 5c1e53bc 2019-07-28 stsp if (err)
5791 5c1e53bc 2019-07-28 stsp goto done;
5792 39cd0ff6 2019-07-12 stsp }
5793 f0b75401 2019-08-03 stsp
5794 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5795 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5796 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5797 f0b75401 2019-08-03 stsp
5798 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5799 f0b75401 2019-08-03 stsp ct_path++;
5800 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5801 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5802 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5803 b50cabdf 2019-07-12 stsp if (err)
5804 b50cabdf 2019-07-12 stsp goto done;
5805 f0b75401 2019-08-03 stsp
5806 b50cabdf 2019-07-12 stsp }
5807 b50cabdf 2019-07-12 stsp
5808 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5809 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5810 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5811 39cd0ff6 2019-07-12 stsp if (err)
5812 39cd0ff6 2019-07-12 stsp goto done;
5813 39cd0ff6 2019-07-12 stsp
5814 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5815 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5816 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5817 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5818 39cd0ff6 2019-07-12 stsp err = sync_err;
5819 39cd0ff6 2019-07-12 stsp done:
5820 39cd0ff6 2019-07-12 stsp if (fileindex)
5821 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5822 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5823 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5824 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5825 39cd0ff6 2019-07-12 stsp err = unlockerr;
5826 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5827 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5828 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5829 39cd0ff6 2019-07-12 stsp }
5830 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5831 c4296144 2019-05-09 stsp return err;
5832 8656d6c4 2019-05-20 stsp }
5833 8656d6c4 2019-05-20 stsp
5834 8656d6c4 2019-05-20 stsp const char *
5835 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5836 8656d6c4 2019-05-20 stsp {
5837 8656d6c4 2019-05-20 stsp return ct->path;
5838 8656d6c4 2019-05-20 stsp }
5839 8656d6c4 2019-05-20 stsp
5840 8656d6c4 2019-05-20 stsp unsigned int
5841 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5842 8656d6c4 2019-05-20 stsp {
5843 8656d6c4 2019-05-20 stsp return ct->status;
5844 818c7501 2019-07-11 stsp }
5845 818c7501 2019-07-11 stsp
5846 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5847 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5848 818c7501 2019-07-11 stsp struct got_repository *repo;
5849 818c7501 2019-07-11 stsp };
5850 818c7501 2019-07-11 stsp
5851 818c7501 2019-07-11 stsp static const struct got_error *
5852 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5853 818c7501 2019-07-11 stsp {
5854 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5855 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5856 818c7501 2019-07-11 stsp unsigned char status;
5857 818c7501 2019-07-11 stsp struct stat sb;
5858 818c7501 2019-07-11 stsp char *ondisk_path;
5859 818c7501 2019-07-11 stsp
5860 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5861 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5862 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5863 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5864 818c7501 2019-07-11 stsp
5865 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5866 818c7501 2019-07-11 stsp == -1)
5867 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5868 818c7501 2019-07-11 stsp
5869 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5870 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5871 818c7501 2019-07-11 stsp free(ondisk_path);
5872 818c7501 2019-07-11 stsp if (err)
5873 818c7501 2019-07-11 stsp return err;
5874 818c7501 2019-07-11 stsp
5875 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5876 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5877 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5878 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5879 818c7501 2019-07-11 stsp
5880 818c7501 2019-07-11 stsp return NULL;
5881 818c7501 2019-07-11 stsp }
5882 818c7501 2019-07-11 stsp
5883 818c7501 2019-07-11 stsp const struct got_error *
5884 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5885 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5886 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5887 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5888 818c7501 2019-07-11 stsp {
5889 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5890 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5891 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5892 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5893 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5894 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5895 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5896 818c7501 2019-07-11 stsp
5897 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5898 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5899 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5900 818c7501 2019-07-11 stsp
5901 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5902 818c7501 2019-07-11 stsp if (err)
5903 818c7501 2019-07-11 stsp return err;
5904 818c7501 2019-07-11 stsp
5905 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5906 818c7501 2019-07-11 stsp if (err)
5907 818c7501 2019-07-11 stsp goto done;
5908 818c7501 2019-07-11 stsp
5909 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5910 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5911 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5912 818c7501 2019-07-11 stsp &ok_arg);
5913 818c7501 2019-07-11 stsp if (err)
5914 818c7501 2019-07-11 stsp goto done;
5915 818c7501 2019-07-11 stsp
5916 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5917 818c7501 2019-07-11 stsp if (err)
5918 818c7501 2019-07-11 stsp goto done;
5919 818c7501 2019-07-11 stsp
5920 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5921 818c7501 2019-07-11 stsp if (err)
5922 818c7501 2019-07-11 stsp goto done;
5923 818c7501 2019-07-11 stsp
5924 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5925 818c7501 2019-07-11 stsp if (err)
5926 818c7501 2019-07-11 stsp goto done;
5927 818c7501 2019-07-11 stsp
5928 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5929 818c7501 2019-07-11 stsp 0);
5930 e51d7b55 2020-01-04 stsp if (err)
5931 e51d7b55 2020-01-04 stsp goto done;
5932 e51d7b55 2020-01-04 stsp
5933 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5934 818c7501 2019-07-11 stsp if (err)
5935 818c7501 2019-07-11 stsp goto done;
5936 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5937 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5938 e51d7b55 2020-01-04 stsp goto done;
5939 e51d7b55 2020-01-04 stsp }
5940 818c7501 2019-07-11 stsp
5941 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5942 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5943 818c7501 2019-07-11 stsp if (err)
5944 818c7501 2019-07-11 stsp goto done;
5945 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5946 818c7501 2019-07-11 stsp if (err)
5947 818c7501 2019-07-11 stsp goto done;
5948 818c7501 2019-07-11 stsp
5949 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5950 818c7501 2019-07-11 stsp
5951 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5952 818c7501 2019-07-11 stsp if (err)
5953 818c7501 2019-07-11 stsp goto done;
5954 818c7501 2019-07-11 stsp
5955 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5956 818c7501 2019-07-11 stsp if (err)
5957 818c7501 2019-07-11 stsp goto done;
5958 818c7501 2019-07-11 stsp
5959 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5960 818c7501 2019-07-11 stsp worktree->base_commit_id);
5961 818c7501 2019-07-11 stsp if (err)
5962 818c7501 2019-07-11 stsp goto done;
5963 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5964 818c7501 2019-07-11 stsp if (err)
5965 818c7501 2019-07-11 stsp goto done;
5966 818c7501 2019-07-11 stsp
5967 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5968 818c7501 2019-07-11 stsp if (err)
5969 818c7501 2019-07-11 stsp goto done;
5970 818c7501 2019-07-11 stsp done:
5971 818c7501 2019-07-11 stsp free(fileindex_path);
5972 818c7501 2019-07-11 stsp free(tmp_branch_name);
5973 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5974 818c7501 2019-07-11 stsp free(branch_ref_name);
5975 818c7501 2019-07-11 stsp if (branch_ref)
5976 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5977 818c7501 2019-07-11 stsp if (wt_branch)
5978 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5979 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5980 818c7501 2019-07-11 stsp if (err) {
5981 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5982 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5983 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5984 818c7501 2019-07-11 stsp }
5985 818c7501 2019-07-11 stsp if (*tmp_branch) {
5986 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5987 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5988 818c7501 2019-07-11 stsp }
5989 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5990 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5991 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5992 3e3a69f1 2019-07-25 stsp }
5993 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5994 818c7501 2019-07-11 stsp }
5995 818c7501 2019-07-11 stsp return err;
5996 818c7501 2019-07-11 stsp }
5997 818c7501 2019-07-11 stsp
5998 818c7501 2019-07-11 stsp const struct got_error *
5999 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6000 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6001 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6002 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6003 818c7501 2019-07-11 stsp {
6004 818c7501 2019-07-11 stsp const struct got_error *err;
6005 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6006 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6007 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6008 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6009 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6010 818c7501 2019-07-11 stsp
6011 818c7501 2019-07-11 stsp *commit_id = NULL;
6012 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6013 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6014 3e3a69f1 2019-07-25 stsp *branch = NULL;
6015 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6016 3e3a69f1 2019-07-25 stsp
6017 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6018 3e3a69f1 2019-07-25 stsp if (err)
6019 3e3a69f1 2019-07-25 stsp return err;
6020 818c7501 2019-07-11 stsp
6021 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6022 3e3a69f1 2019-07-25 stsp if (err)
6023 f032f1f7 2019-08-04 stsp goto done;
6024 f032f1f7 2019-08-04 stsp
6025 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6026 f032f1f7 2019-08-04 stsp &have_staged_files);
6027 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6028 f032f1f7 2019-08-04 stsp goto done;
6029 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6030 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6031 3e3a69f1 2019-07-25 stsp goto done;
6032 f032f1f7 2019-08-04 stsp }
6033 3e3a69f1 2019-07-25 stsp
6034 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6035 818c7501 2019-07-11 stsp if (err)
6036 f032f1f7 2019-08-04 stsp goto done;
6037 818c7501 2019-07-11 stsp
6038 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6039 818c7501 2019-07-11 stsp if (err)
6040 818c7501 2019-07-11 stsp goto done;
6041 818c7501 2019-07-11 stsp
6042 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6043 818c7501 2019-07-11 stsp if (err)
6044 818c7501 2019-07-11 stsp goto done;
6045 818c7501 2019-07-11 stsp
6046 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6047 818c7501 2019-07-11 stsp if (err)
6048 818c7501 2019-07-11 stsp goto done;
6049 818c7501 2019-07-11 stsp
6050 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6051 818c7501 2019-07-11 stsp if (err)
6052 818c7501 2019-07-11 stsp goto done;
6053 818c7501 2019-07-11 stsp
6054 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6055 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6056 818c7501 2019-07-11 stsp if (err)
6057 818c7501 2019-07-11 stsp goto done;
6058 818c7501 2019-07-11 stsp
6059 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6060 818c7501 2019-07-11 stsp if (err)
6061 818c7501 2019-07-11 stsp goto done;
6062 818c7501 2019-07-11 stsp
6063 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6064 818c7501 2019-07-11 stsp if (err)
6065 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp
6067 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6068 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6069 818c7501 2019-07-11 stsp if (err)
6070 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6073 818c7501 2019-07-11 stsp if (err)
6074 818c7501 2019-07-11 stsp goto done;
6075 818c7501 2019-07-11 stsp done:
6076 818c7501 2019-07-11 stsp free(commit_ref_name);
6077 818c7501 2019-07-11 stsp free(branch_ref_name);
6078 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6079 818c7501 2019-07-11 stsp if (commit_ref)
6080 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6081 818c7501 2019-07-11 stsp if (branch_ref)
6082 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6083 818c7501 2019-07-11 stsp if (err) {
6084 818c7501 2019-07-11 stsp free(*commit_id);
6085 818c7501 2019-07-11 stsp *commit_id = NULL;
6086 818c7501 2019-07-11 stsp if (*tmp_branch) {
6087 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6088 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6089 818c7501 2019-07-11 stsp }
6090 818c7501 2019-07-11 stsp if (*new_base_branch) {
6091 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6092 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6093 818c7501 2019-07-11 stsp }
6094 818c7501 2019-07-11 stsp if (*branch) {
6095 818c7501 2019-07-11 stsp got_ref_close(*branch);
6096 818c7501 2019-07-11 stsp *branch = NULL;
6097 818c7501 2019-07-11 stsp }
6098 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6099 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6100 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6101 3e3a69f1 2019-07-25 stsp }
6102 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6103 818c7501 2019-07-11 stsp }
6104 818c7501 2019-07-11 stsp return err;
6105 c4296144 2019-05-09 stsp }
6106 818c7501 2019-07-11 stsp
6107 818c7501 2019-07-11 stsp const struct got_error *
6108 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6109 818c7501 2019-07-11 stsp {
6110 818c7501 2019-07-11 stsp const struct got_error *err;
6111 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6112 818c7501 2019-07-11 stsp
6113 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6114 818c7501 2019-07-11 stsp if (err)
6115 818c7501 2019-07-11 stsp return err;
6116 818c7501 2019-07-11 stsp
6117 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6118 818c7501 2019-07-11 stsp free(tmp_branch_name);
6119 818c7501 2019-07-11 stsp return NULL;
6120 818c7501 2019-07-11 stsp }
6121 818c7501 2019-07-11 stsp
6122 818c7501 2019-07-11 stsp static const struct got_error *
6123 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6124 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6125 818c7501 2019-07-11 stsp {
6126 0ebf8283 2019-07-24 stsp *logmsg = arg;
6127 818c7501 2019-07-11 stsp return NULL;
6128 818c7501 2019-07-11 stsp }
6129 818c7501 2019-07-11 stsp
6130 818c7501 2019-07-11 stsp static const struct got_error *
6131 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6132 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6133 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6134 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6135 818c7501 2019-07-11 stsp {
6136 818c7501 2019-07-11 stsp return NULL;
6137 01757395 2019-07-12 stsp }
6138 01757395 2019-07-12 stsp
6139 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6140 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6141 01757395 2019-07-12 stsp void *progress_arg;
6142 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6143 01757395 2019-07-12 stsp };
6144 01757395 2019-07-12 stsp
6145 01757395 2019-07-12 stsp static const struct got_error *
6146 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6147 01757395 2019-07-12 stsp {
6148 01757395 2019-07-12 stsp const struct got_error *err;
6149 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6150 01757395 2019-07-12 stsp char *p;
6151 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6152 01757395 2019-07-12 stsp
6153 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6154 01757395 2019-07-12 stsp if (err)
6155 01757395 2019-07-12 stsp return err;
6156 01757395 2019-07-12 stsp
6157 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6158 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6159 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6160 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6161 01757395 2019-07-12 stsp return NULL;
6162 01757395 2019-07-12 stsp
6163 01757395 2019-07-12 stsp p = strdup(path);
6164 01757395 2019-07-12 stsp if (p == NULL)
6165 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6166 01757395 2019-07-12 stsp
6167 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6168 01757395 2019-07-12 stsp if (err || new == NULL)
6169 01757395 2019-07-12 stsp free(p);
6170 01757395 2019-07-12 stsp return err;
6171 01757395 2019-07-12 stsp }
6172 01757395 2019-07-12 stsp
6173 01757395 2019-07-12 stsp void
6174 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6175 01757395 2019-07-12 stsp {
6176 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6177 01757395 2019-07-12 stsp
6178 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6179 01757395 2019-07-12 stsp free((char *)pe->path);
6180 01757395 2019-07-12 stsp
6181 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6182 818c7501 2019-07-11 stsp }
6183 818c7501 2019-07-11 stsp
6184 0ebf8283 2019-07-24 stsp static const struct got_error *
6185 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6186 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6187 818c7501 2019-07-11 stsp {
6188 818c7501 2019-07-11 stsp const struct got_error *err;
6189 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6190 818c7501 2019-07-11 stsp
6191 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6192 818c7501 2019-07-11 stsp if (err) {
6193 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6194 818c7501 2019-07-11 stsp goto done;
6195 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6196 818c7501 2019-07-11 stsp if (err)
6197 818c7501 2019-07-11 stsp goto done;
6198 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6199 818c7501 2019-07-11 stsp if (err)
6200 818c7501 2019-07-11 stsp goto done;
6201 de05890f 2020-03-05 stsp } else if (is_rebase) {
6202 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6203 818c7501 2019-07-11 stsp int cmp;
6204 818c7501 2019-07-11 stsp
6205 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6206 818c7501 2019-07-11 stsp if (err)
6207 818c7501 2019-07-11 stsp goto done;
6208 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6209 818c7501 2019-07-11 stsp free(stored_id);
6210 818c7501 2019-07-11 stsp if (cmp != 0) {
6211 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6212 818c7501 2019-07-11 stsp goto done;
6213 818c7501 2019-07-11 stsp }
6214 818c7501 2019-07-11 stsp }
6215 0ebf8283 2019-07-24 stsp done:
6216 0ebf8283 2019-07-24 stsp if (commit_ref)
6217 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6218 0ebf8283 2019-07-24 stsp return err;
6219 0ebf8283 2019-07-24 stsp }
6220 0ebf8283 2019-07-24 stsp
6221 0ebf8283 2019-07-24 stsp static const struct got_error *
6222 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6223 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6224 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6225 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6226 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6227 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6228 0ebf8283 2019-07-24 stsp {
6229 0ebf8283 2019-07-24 stsp const struct got_error *err;
6230 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6231 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6232 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6233 818c7501 2019-07-11 stsp
6234 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6235 0ebf8283 2019-07-24 stsp
6236 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6237 0ebf8283 2019-07-24 stsp if (err)
6238 0ebf8283 2019-07-24 stsp return err;
6239 0ebf8283 2019-07-24 stsp
6240 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6241 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6242 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6243 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6244 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6245 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6246 818c7501 2019-07-11 stsp if (commit_ref)
6247 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6248 818c7501 2019-07-11 stsp return err;
6249 818c7501 2019-07-11 stsp }
6250 818c7501 2019-07-11 stsp
6251 818c7501 2019-07-11 stsp const struct got_error *
6252 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6253 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6254 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6255 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6256 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6257 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6258 818c7501 2019-07-11 stsp {
6259 0ebf8283 2019-07-24 stsp const struct got_error *err;
6260 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6261 0ebf8283 2019-07-24 stsp
6262 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6263 0ebf8283 2019-07-24 stsp if (err)
6264 0ebf8283 2019-07-24 stsp return err;
6265 0ebf8283 2019-07-24 stsp
6266 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6267 0ebf8283 2019-07-24 stsp if (err)
6268 0ebf8283 2019-07-24 stsp goto done;
6269 0ebf8283 2019-07-24 stsp
6270 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6271 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6272 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6273 0ebf8283 2019-07-24 stsp done:
6274 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6275 0ebf8283 2019-07-24 stsp return err;
6276 0ebf8283 2019-07-24 stsp }
6277 0ebf8283 2019-07-24 stsp
6278 0ebf8283 2019-07-24 stsp const struct got_error *
6279 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6280 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6281 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6282 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6283 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6284 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6285 0ebf8283 2019-07-24 stsp {
6286 0ebf8283 2019-07-24 stsp const struct got_error *err;
6287 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6288 0ebf8283 2019-07-24 stsp
6289 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6290 0ebf8283 2019-07-24 stsp if (err)
6291 0ebf8283 2019-07-24 stsp return err;
6292 0ebf8283 2019-07-24 stsp
6293 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6294 0ebf8283 2019-07-24 stsp if (err)
6295 0ebf8283 2019-07-24 stsp goto done;
6296 0ebf8283 2019-07-24 stsp
6297 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6298 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6299 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6300 0ebf8283 2019-07-24 stsp done:
6301 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6302 0ebf8283 2019-07-24 stsp return err;
6303 0ebf8283 2019-07-24 stsp }
6304 0ebf8283 2019-07-24 stsp
6305 0ebf8283 2019-07-24 stsp static const struct got_error *
6306 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6307 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6308 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6309 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6310 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6311 0ebf8283 2019-07-24 stsp {
6312 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6313 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6314 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6315 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6316 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6317 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6318 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6319 818c7501 2019-07-11 stsp
6320 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6321 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6322 a0e95631 2019-07-12 stsp
6323 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6324 818c7501 2019-07-11 stsp
6325 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6326 a0e95631 2019-07-12 stsp if (err)
6327 3e3a69f1 2019-07-25 stsp return err;
6328 a0e95631 2019-07-12 stsp
6329 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6330 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6331 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6332 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6333 01757395 2019-07-12 stsp /*
6334 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6335 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6336 6c13b005 2021-09-02 stsp *
6337 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6338 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6339 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6340 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6341 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6342 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6343 01757395 2019-07-12 stsp */
6344 01757395 2019-07-12 stsp if (merged_paths) {
6345 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6346 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6347 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6348 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6349 f2a9dc41 2019-12-13 tracey 0);
6350 01757395 2019-07-12 stsp if (err)
6351 01757395 2019-07-12 stsp goto done;
6352 01757395 2019-07-12 stsp }
6353 01757395 2019-07-12 stsp } else {
6354 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6355 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6356 01757395 2019-07-12 stsp if (err)
6357 01757395 2019-07-12 stsp goto done;
6358 01757395 2019-07-12 stsp }
6359 a0e95631 2019-07-12 stsp
6360 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6361 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6362 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6363 a0e95631 2019-07-12 stsp if (err)
6364 a0e95631 2019-07-12 stsp goto done;
6365 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6366 a0e95631 2019-07-12 stsp goto done;
6367 ff0d2220 2019-07-11 stsp }
6368 818c7501 2019-07-11 stsp
6369 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6370 edd02c5e 2019-07-11 stsp if (err)
6371 edd02c5e 2019-07-11 stsp goto done;
6372 edd02c5e 2019-07-11 stsp
6373 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6374 818c7501 2019-07-11 stsp if (err)
6375 818c7501 2019-07-11 stsp goto done;
6376 818c7501 2019-07-11 stsp
6377 5943eee2 2019-08-13 stsp if (new_logmsg) {
6378 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6379 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6380 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6381 5943eee2 2019-08-13 stsp goto done;
6382 5943eee2 2019-08-13 stsp }
6383 5943eee2 2019-08-13 stsp } else {
6384 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6385 5943eee2 2019-08-13 stsp if (err)
6386 5943eee2 2019-08-13 stsp goto done;
6387 5943eee2 2019-08-13 stsp }
6388 0ebf8283 2019-07-24 stsp
6389 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6390 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6391 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6392 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6393 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6394 a0e95631 2019-07-12 stsp if (err)
6395 a0e95631 2019-07-12 stsp goto done;
6396 a0e95631 2019-07-12 stsp
6397 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6398 a0e95631 2019-07-12 stsp if (err)
6399 a0e95631 2019-07-12 stsp goto done;
6400 a0e95631 2019-07-12 stsp
6401 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6402 a0e95631 2019-07-12 stsp if (err)
6403 a0e95631 2019-07-12 stsp goto done;
6404 a0e95631 2019-07-12 stsp
6405 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6406 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6407 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6408 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6409 a0e95631 2019-07-12 stsp err = sync_err;
6410 818c7501 2019-07-11 stsp done:
6411 a0e95631 2019-07-12 stsp free(fileindex_path);
6412 a0e95631 2019-07-12 stsp free(head_commit_id);
6413 a0e95631 2019-07-12 stsp if (head_ref)
6414 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6415 818c7501 2019-07-11 stsp if (err) {
6416 818c7501 2019-07-11 stsp free(*new_commit_id);
6417 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6418 818c7501 2019-07-11 stsp }
6419 818c7501 2019-07-11 stsp return err;
6420 818c7501 2019-07-11 stsp }
6421 818c7501 2019-07-11 stsp
6422 818c7501 2019-07-11 stsp const struct got_error *
6423 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6424 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6425 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6426 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6427 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6428 0ebf8283 2019-07-24 stsp {
6429 0ebf8283 2019-07-24 stsp const struct got_error *err;
6430 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6431 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6432 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6433 0ebf8283 2019-07-24 stsp
6434 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6435 0ebf8283 2019-07-24 stsp if (err)
6436 0ebf8283 2019-07-24 stsp return err;
6437 0ebf8283 2019-07-24 stsp
6438 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6439 0ebf8283 2019-07-24 stsp if (err)
6440 0ebf8283 2019-07-24 stsp goto done;
6441 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6442 0ebf8283 2019-07-24 stsp if (err)
6443 0ebf8283 2019-07-24 stsp goto done;
6444 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6445 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6446 0ebf8283 2019-07-24 stsp goto done;
6447 0ebf8283 2019-07-24 stsp }
6448 0ebf8283 2019-07-24 stsp
6449 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6450 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6451 0ebf8283 2019-07-24 stsp done:
6452 0ebf8283 2019-07-24 stsp if (commit_ref)
6453 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6454 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6455 0ebf8283 2019-07-24 stsp free(commit_id);
6456 0ebf8283 2019-07-24 stsp return err;
6457 0ebf8283 2019-07-24 stsp }
6458 0ebf8283 2019-07-24 stsp
6459 0ebf8283 2019-07-24 stsp const struct got_error *
6460 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6461 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6462 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6463 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6464 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6465 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6466 0ebf8283 2019-07-24 stsp {
6467 0ebf8283 2019-07-24 stsp const struct got_error *err;
6468 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6469 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6470 0ebf8283 2019-07-24 stsp
6471 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6472 0ebf8283 2019-07-24 stsp if (err)
6473 0ebf8283 2019-07-24 stsp return err;
6474 0ebf8283 2019-07-24 stsp
6475 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6476 0ebf8283 2019-07-24 stsp if (err)
6477 0ebf8283 2019-07-24 stsp goto done;
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6480 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6481 0ebf8283 2019-07-24 stsp done:
6482 0ebf8283 2019-07-24 stsp if (commit_ref)
6483 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6484 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6485 0ebf8283 2019-07-24 stsp return err;
6486 0ebf8283 2019-07-24 stsp }
6487 0ebf8283 2019-07-24 stsp
6488 0ebf8283 2019-07-24 stsp const struct got_error *
6489 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6490 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6491 818c7501 2019-07-11 stsp {
6492 3e3a69f1 2019-07-25 stsp if (fileindex)
6493 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6494 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6495 69844fba 2019-07-11 stsp }
6496 69844fba 2019-07-11 stsp
6497 69844fba 2019-07-11 stsp static const struct got_error *
6498 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6499 69844fba 2019-07-11 stsp {
6500 69844fba 2019-07-11 stsp const struct got_error *err;
6501 69844fba 2019-07-11 stsp struct got_reference *ref;
6502 69844fba 2019-07-11 stsp
6503 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6504 69844fba 2019-07-11 stsp if (err) {
6505 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6506 69844fba 2019-07-11 stsp return NULL;
6507 69844fba 2019-07-11 stsp return err;
6508 69844fba 2019-07-11 stsp }
6509 69844fba 2019-07-11 stsp
6510 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6511 69844fba 2019-07-11 stsp got_ref_close(ref);
6512 69844fba 2019-07-11 stsp return err;
6513 818c7501 2019-07-11 stsp }
6514 818c7501 2019-07-11 stsp
6515 69844fba 2019-07-11 stsp static const struct got_error *
6516 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6517 69844fba 2019-07-11 stsp {
6518 69844fba 2019-07-11 stsp const struct got_error *err;
6519 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6520 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6521 69844fba 2019-07-11 stsp
6522 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6523 69844fba 2019-07-11 stsp if (err)
6524 69844fba 2019-07-11 stsp goto done;
6525 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6526 69844fba 2019-07-11 stsp if (err)
6527 69844fba 2019-07-11 stsp goto done;
6528 69844fba 2019-07-11 stsp
6529 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6530 69844fba 2019-07-11 stsp if (err)
6531 69844fba 2019-07-11 stsp goto done;
6532 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6533 69844fba 2019-07-11 stsp if (err)
6534 69844fba 2019-07-11 stsp goto done;
6535 69844fba 2019-07-11 stsp
6536 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6537 69844fba 2019-07-11 stsp if (err)
6538 69844fba 2019-07-11 stsp goto done;
6539 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6540 69844fba 2019-07-11 stsp if (err)
6541 69844fba 2019-07-11 stsp goto done;
6542 69844fba 2019-07-11 stsp
6543 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6544 69844fba 2019-07-11 stsp if (err)
6545 69844fba 2019-07-11 stsp goto done;
6546 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6547 69844fba 2019-07-11 stsp if (err)
6548 69844fba 2019-07-11 stsp goto done;
6549 69844fba 2019-07-11 stsp
6550 69844fba 2019-07-11 stsp done:
6551 69844fba 2019-07-11 stsp free(tmp_branch_name);
6552 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6553 69844fba 2019-07-11 stsp free(branch_ref_name);
6554 69844fba 2019-07-11 stsp free(commit_ref_name);
6555 e600f124 2021-03-21 stsp return err;
6556 e600f124 2021-03-21 stsp }
6557 e600f124 2021-03-21 stsp
6558 e600f124 2021-03-21 stsp const struct got_error *
6559 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6560 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6561 e600f124 2021-03-21 stsp {
6562 e600f124 2021-03-21 stsp const struct got_error *err;
6563 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6564 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6565 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6566 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6567 e600f124 2021-03-21 stsp char *refname = NULL;
6568 e600f124 2021-03-21 stsp
6569 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6570 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6571 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6572 e600f124 2021-03-21 stsp branch_name += 11;
6573 e600f124 2021-03-21 stsp
6574 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6575 e600f124 2021-03-21 stsp if (err)
6576 e600f124 2021-03-21 stsp return err;
6577 e600f124 2021-03-21 stsp
6578 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6579 e600f124 2021-03-21 stsp new_id_str) == -1) {
6580 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6581 e600f124 2021-03-21 stsp goto done;
6582 e600f124 2021-03-21 stsp }
6583 e600f124 2021-03-21 stsp
6584 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6585 e600f124 2021-03-21 stsp if (err)
6586 e600f124 2021-03-21 stsp goto done;
6587 e600f124 2021-03-21 stsp
6588 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6589 e600f124 2021-03-21 stsp if (err)
6590 e600f124 2021-03-21 stsp goto done;
6591 e600f124 2021-03-21 stsp
6592 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6593 e600f124 2021-03-21 stsp done:
6594 e600f124 2021-03-21 stsp free(new_id_str);
6595 e600f124 2021-03-21 stsp free(refname);
6596 e600f124 2021-03-21 stsp free(old_commit_id);
6597 e600f124 2021-03-21 stsp if (ref)
6598 e600f124 2021-03-21 stsp got_ref_close(ref);
6599 69844fba 2019-07-11 stsp return err;
6600 69844fba 2019-07-11 stsp }
6601 69844fba 2019-07-11 stsp
6602 818c7501 2019-07-11 stsp const struct got_error *
6603 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6604 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6605 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6606 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6607 818c7501 2019-07-11 stsp {
6608 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6609 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6610 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6611 818c7501 2019-07-11 stsp
6612 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6613 818c7501 2019-07-11 stsp if (err)
6614 818c7501 2019-07-11 stsp return err;
6615 e600f124 2021-03-21 stsp
6616 e600f124 2021-03-21 stsp if (create_backup) {
6617 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6618 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6619 e600f124 2021-03-21 stsp if (err)
6620 e600f124 2021-03-21 stsp goto done;
6621 e600f124 2021-03-21 stsp }
6622 818c7501 2019-07-11 stsp
6623 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6624 818c7501 2019-07-11 stsp if (err)
6625 818c7501 2019-07-11 stsp goto done;
6626 818c7501 2019-07-11 stsp
6627 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6628 818c7501 2019-07-11 stsp if (err)
6629 818c7501 2019-07-11 stsp goto done;
6630 818c7501 2019-07-11 stsp
6631 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6632 818c7501 2019-07-11 stsp if (err)
6633 818c7501 2019-07-11 stsp goto done;
6634 818c7501 2019-07-11 stsp
6635 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6636 a615e0e7 2020-12-16 stsp if (err)
6637 a615e0e7 2020-12-16 stsp goto done;
6638 a615e0e7 2020-12-16 stsp
6639 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6640 a615e0e7 2020-12-16 stsp if (err)
6641 a615e0e7 2020-12-16 stsp goto done;
6642 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6643 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6644 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6645 a615e0e7 2020-12-16 stsp err = sync_err;
6646 818c7501 2019-07-11 stsp done:
6647 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6648 a615e0e7 2020-12-16 stsp free(fileindex_path);
6649 818c7501 2019-07-11 stsp free(new_head_commit_id);
6650 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6651 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6652 818c7501 2019-07-11 stsp err = unlockerr;
6653 818c7501 2019-07-11 stsp return err;
6654 818c7501 2019-07-11 stsp }
6655 818c7501 2019-07-11 stsp
6656 818c7501 2019-07-11 stsp const struct got_error *
6657 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6658 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6659 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6660 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6661 818c7501 2019-07-11 stsp {
6662 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6663 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6664 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6665 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6666 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6667 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6668 818c7501 2019-07-11 stsp
6669 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6670 818c7501 2019-07-11 stsp if (err)
6671 818c7501 2019-07-11 stsp return err;
6672 818c7501 2019-07-11 stsp
6673 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6674 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6675 818c7501 2019-07-11 stsp if (err)
6676 818c7501 2019-07-11 stsp goto done;
6677 818c7501 2019-07-11 stsp
6678 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6679 818c7501 2019-07-11 stsp if (err)
6680 818c7501 2019-07-11 stsp goto done;
6681 818c7501 2019-07-11 stsp
6682 818c7501 2019-07-11 stsp /*
6683 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6684 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6685 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6686 818c7501 2019-07-11 stsp */
6687 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6688 818c7501 2019-07-11 stsp if (err)
6689 818c7501 2019-07-11 stsp goto done;
6690 818c7501 2019-07-11 stsp
6691 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6692 818c7501 2019-07-11 stsp if (err)
6693 818c7501 2019-07-11 stsp goto done;
6694 818c7501 2019-07-11 stsp
6695 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6696 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6697 ca355955 2019-07-12 stsp if (err)
6698 ca355955 2019-07-12 stsp goto done;
6699 ca355955 2019-07-12 stsp
6700 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6701 ca355955 2019-07-12 stsp if (err)
6702 ca355955 2019-07-12 stsp goto done;
6703 ca355955 2019-07-12 stsp
6704 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6705 818c7501 2019-07-11 stsp if (err)
6706 818c7501 2019-07-11 stsp goto done;
6707 818c7501 2019-07-11 stsp
6708 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6709 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6710 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6711 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6712 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6713 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6714 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6715 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6716 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6717 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6718 55bd499d 2019-07-12 stsp if (err)
6719 1f1abb7e 2019-08-08 stsp goto sync;
6720 55bd499d 2019-07-12 stsp
6721 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6722 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6723 ca355955 2019-07-12 stsp sync:
6724 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6725 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6726 ca355955 2019-07-12 stsp err = sync_err;
6727 818c7501 2019-07-11 stsp done:
6728 818c7501 2019-07-11 stsp got_ref_close(resolved);
6729 a3a2faf2 2019-07-12 stsp free(tree_id);
6730 818c7501 2019-07-11 stsp free(commit_id);
6731 0ebf8283 2019-07-24 stsp if (fileindex)
6732 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6733 0ebf8283 2019-07-24 stsp free(fileindex_path);
6734 0ebf8283 2019-07-24 stsp
6735 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6736 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6737 0ebf8283 2019-07-24 stsp err = unlockerr;
6738 0ebf8283 2019-07-24 stsp return err;
6739 0ebf8283 2019-07-24 stsp }
6740 0ebf8283 2019-07-24 stsp
6741 0ebf8283 2019-07-24 stsp const struct got_error *
6742 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6743 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6744 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6745 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6746 0ebf8283 2019-07-24 stsp {
6747 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6748 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6749 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6750 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6751 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6752 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6753 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6754 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6755 0ebf8283 2019-07-24 stsp
6756 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6757 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6758 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6759 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6760 0ebf8283 2019-07-24 stsp
6761 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6762 0ebf8283 2019-07-24 stsp if (err)
6763 0ebf8283 2019-07-24 stsp return err;
6764 0ebf8283 2019-07-24 stsp
6765 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6766 0ebf8283 2019-07-24 stsp if (err)
6767 0ebf8283 2019-07-24 stsp goto done;
6768 0ebf8283 2019-07-24 stsp
6769 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6770 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6771 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6772 0ebf8283 2019-07-24 stsp &ok_arg);
6773 0ebf8283 2019-07-24 stsp if (err)
6774 0ebf8283 2019-07-24 stsp goto done;
6775 0ebf8283 2019-07-24 stsp
6776 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6777 0ebf8283 2019-07-24 stsp if (err)
6778 0ebf8283 2019-07-24 stsp goto done;
6779 0ebf8283 2019-07-24 stsp
6780 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6781 0ebf8283 2019-07-24 stsp if (err)
6782 0ebf8283 2019-07-24 stsp goto done;
6783 0ebf8283 2019-07-24 stsp
6784 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6785 0ebf8283 2019-07-24 stsp worktree);
6786 0ebf8283 2019-07-24 stsp if (err)
6787 0ebf8283 2019-07-24 stsp goto done;
6788 0ebf8283 2019-07-24 stsp
6789 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6790 0ebf8283 2019-07-24 stsp 0);
6791 0ebf8283 2019-07-24 stsp if (err)
6792 0ebf8283 2019-07-24 stsp goto done;
6793 0ebf8283 2019-07-24 stsp
6794 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6795 0ebf8283 2019-07-24 stsp if (err)
6796 0ebf8283 2019-07-24 stsp goto done;
6797 0ebf8283 2019-07-24 stsp
6798 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6799 0ebf8283 2019-07-24 stsp if (err)
6800 0ebf8283 2019-07-24 stsp goto done;
6801 0ebf8283 2019-07-24 stsp
6802 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6803 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6804 0ebf8283 2019-07-24 stsp if (err)
6805 0ebf8283 2019-07-24 stsp goto done;
6806 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6807 0ebf8283 2019-07-24 stsp if (err)
6808 0ebf8283 2019-07-24 stsp goto done;
6809 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6810 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6811 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6812 0ebf8283 2019-07-24 stsp goto done;
6813 0ebf8283 2019-07-24 stsp }
6814 0ebf8283 2019-07-24 stsp
6815 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6816 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6817 0ebf8283 2019-07-24 stsp if (err)
6818 0ebf8283 2019-07-24 stsp goto done;
6819 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6820 0ebf8283 2019-07-24 stsp if (err)
6821 0ebf8283 2019-07-24 stsp goto done;
6822 0ebf8283 2019-07-24 stsp
6823 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6824 0ebf8283 2019-07-24 stsp if (err)
6825 0ebf8283 2019-07-24 stsp goto done;
6826 0ebf8283 2019-07-24 stsp done:
6827 0ebf8283 2019-07-24 stsp free(fileindex_path);
6828 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6829 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6830 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6831 0ebf8283 2019-07-24 stsp if (wt_branch)
6832 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6833 0ebf8283 2019-07-24 stsp if (err) {
6834 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6835 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6836 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6837 0ebf8283 2019-07-24 stsp }
6838 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6839 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6840 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6841 0ebf8283 2019-07-24 stsp }
6842 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6843 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6844 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6845 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6846 3e3a69f1 2019-07-25 stsp }
6847 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6848 0ebf8283 2019-07-24 stsp }
6849 0ebf8283 2019-07-24 stsp return err;
6850 0ebf8283 2019-07-24 stsp }
6851 0ebf8283 2019-07-24 stsp
6852 0ebf8283 2019-07-24 stsp const struct got_error *
6853 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6854 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6855 0ebf8283 2019-07-24 stsp {
6856 3e3a69f1 2019-07-25 stsp if (fileindex)
6857 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6858 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6859 0ebf8283 2019-07-24 stsp }
6860 0ebf8283 2019-07-24 stsp
6861 0ebf8283 2019-07-24 stsp const struct got_error *
6862 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6863 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6864 0ebf8283 2019-07-24 stsp {
6865 0ebf8283 2019-07-24 stsp const struct got_error *err;
6866 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6867 0ebf8283 2019-07-24 stsp
6868 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6869 0ebf8283 2019-07-24 stsp if (err)
6870 0ebf8283 2019-07-24 stsp return err;
6871 0ebf8283 2019-07-24 stsp
6872 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6873 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6874 0ebf8283 2019-07-24 stsp return NULL;
6875 0ebf8283 2019-07-24 stsp }
6876 0ebf8283 2019-07-24 stsp
6877 0ebf8283 2019-07-24 stsp const struct got_error *
6878 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6879 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6880 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6881 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6882 0ebf8283 2019-07-24 stsp {
6883 0ebf8283 2019-07-24 stsp const struct got_error *err;
6884 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6885 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6886 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6887 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6888 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6889 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6890 0ebf8283 2019-07-24 stsp
6891 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6892 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6893 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6894 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6895 0ebf8283 2019-07-24 stsp
6896 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6897 3e3a69f1 2019-07-25 stsp if (err)
6898 3e3a69f1 2019-07-25 stsp return err;
6899 3e3a69f1 2019-07-25 stsp
6900 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6901 3e3a69f1 2019-07-25 stsp if (err)
6902 f032f1f7 2019-08-04 stsp goto done;
6903 f032f1f7 2019-08-04 stsp
6904 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6905 f032f1f7 2019-08-04 stsp &have_staged_files);
6906 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6907 f032f1f7 2019-08-04 stsp goto done;
6908 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6909 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6910 3e3a69f1 2019-07-25 stsp goto done;
6911 f032f1f7 2019-08-04 stsp }
6912 3e3a69f1 2019-07-25 stsp
6913 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6914 0ebf8283 2019-07-24 stsp if (err)
6915 f032f1f7 2019-08-04 stsp goto done;
6916 0ebf8283 2019-07-24 stsp
6917 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6918 0ebf8283 2019-07-24 stsp if (err)
6919 0ebf8283 2019-07-24 stsp goto done;
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp goto done;
6924 0ebf8283 2019-07-24 stsp
6925 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6926 0ebf8283 2019-07-24 stsp worktree);
6927 0ebf8283 2019-07-24 stsp if (err)
6928 0ebf8283 2019-07-24 stsp goto done;
6929 0ebf8283 2019-07-24 stsp
6930 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6931 0ebf8283 2019-07-24 stsp if (err)
6932 0ebf8283 2019-07-24 stsp goto done;
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6935 0ebf8283 2019-07-24 stsp if (err)
6936 0ebf8283 2019-07-24 stsp goto done;
6937 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6938 0ebf8283 2019-07-24 stsp if (err)
6939 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp
6941 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6942 0ebf8283 2019-07-24 stsp if (err)
6943 0ebf8283 2019-07-24 stsp goto done;
6944 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6945 0ebf8283 2019-07-24 stsp if (err)
6946 0ebf8283 2019-07-24 stsp goto done;
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6949 0ebf8283 2019-07-24 stsp if (err)
6950 0ebf8283 2019-07-24 stsp goto done;
6951 0ebf8283 2019-07-24 stsp done:
6952 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6953 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6954 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6955 0ebf8283 2019-07-24 stsp if (commit_ref)
6956 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6957 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6958 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6959 0ebf8283 2019-07-24 stsp if (err) {
6960 0ebf8283 2019-07-24 stsp free(*commit_id);
6961 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6962 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6963 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6964 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6965 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6966 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6967 0ebf8283 2019-07-24 stsp }
6968 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6969 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6970 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6971 3e3a69f1 2019-07-25 stsp }
6972 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6973 0ebf8283 2019-07-24 stsp }
6974 0ebf8283 2019-07-24 stsp return err;
6975 0ebf8283 2019-07-24 stsp }
6976 0ebf8283 2019-07-24 stsp
6977 0ebf8283 2019-07-24 stsp static const struct got_error *
6978 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6979 0ebf8283 2019-07-24 stsp {
6980 0ebf8283 2019-07-24 stsp const struct got_error *err;
6981 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6982 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6983 0ebf8283 2019-07-24 stsp
6984 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6985 0ebf8283 2019-07-24 stsp if (err)
6986 0ebf8283 2019-07-24 stsp goto done;
6987 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6988 0ebf8283 2019-07-24 stsp if (err)
6989 0ebf8283 2019-07-24 stsp goto done;
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6992 0ebf8283 2019-07-24 stsp worktree);
6993 0ebf8283 2019-07-24 stsp if (err)
6994 0ebf8283 2019-07-24 stsp goto done;
6995 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6996 0ebf8283 2019-07-24 stsp if (err)
6997 0ebf8283 2019-07-24 stsp goto done;
6998 0ebf8283 2019-07-24 stsp
6999 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7000 0ebf8283 2019-07-24 stsp if (err)
7001 0ebf8283 2019-07-24 stsp goto done;
7002 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7003 0ebf8283 2019-07-24 stsp if (err)
7004 0ebf8283 2019-07-24 stsp goto done;
7005 0ebf8283 2019-07-24 stsp
7006 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7007 0ebf8283 2019-07-24 stsp if (err)
7008 0ebf8283 2019-07-24 stsp goto done;
7009 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7010 0ebf8283 2019-07-24 stsp if (err)
7011 0ebf8283 2019-07-24 stsp goto done;
7012 0ebf8283 2019-07-24 stsp done:
7013 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7014 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7015 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7016 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7017 0ebf8283 2019-07-24 stsp return err;
7018 0ebf8283 2019-07-24 stsp }
7019 0ebf8283 2019-07-24 stsp
7020 0ebf8283 2019-07-24 stsp const struct got_error *
7021 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7022 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7023 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7024 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7025 0ebf8283 2019-07-24 stsp {
7026 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7027 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7028 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7029 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7030 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7031 0ebf8283 2019-07-24 stsp
7032 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7033 0ebf8283 2019-07-24 stsp if (err)
7034 0ebf8283 2019-07-24 stsp return err;
7035 0ebf8283 2019-07-24 stsp
7036 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7037 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7038 0ebf8283 2019-07-24 stsp if (err)
7039 0ebf8283 2019-07-24 stsp goto done;
7040 0ebf8283 2019-07-24 stsp
7041 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7042 0ebf8283 2019-07-24 stsp if (err)
7043 0ebf8283 2019-07-24 stsp goto done;
7044 0ebf8283 2019-07-24 stsp
7045 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7046 0ebf8283 2019-07-24 stsp if (err)
7047 0ebf8283 2019-07-24 stsp goto done;
7048 0ebf8283 2019-07-24 stsp
7049 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7050 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7051 0ebf8283 2019-07-24 stsp if (err)
7052 0ebf8283 2019-07-24 stsp goto done;
7053 0ebf8283 2019-07-24 stsp
7054 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7055 0ebf8283 2019-07-24 stsp if (err)
7056 0ebf8283 2019-07-24 stsp goto done;
7057 0ebf8283 2019-07-24 stsp
7058 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7059 0ebf8283 2019-07-24 stsp if (err)
7060 0ebf8283 2019-07-24 stsp goto done;
7061 0ebf8283 2019-07-24 stsp
7062 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7063 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7064 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7065 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7066 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7067 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7068 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7069 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7070 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7071 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7072 0ebf8283 2019-07-24 stsp if (err)
7073 1f1abb7e 2019-08-08 stsp goto sync;
7074 0ebf8283 2019-07-24 stsp
7075 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7076 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7077 0ebf8283 2019-07-24 stsp sync:
7078 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7079 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7080 0ebf8283 2019-07-24 stsp err = sync_err;
7081 0ebf8283 2019-07-24 stsp done:
7082 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7083 0ebf8283 2019-07-24 stsp free(tree_id);
7084 818c7501 2019-07-11 stsp free(fileindex_path);
7085 818c7501 2019-07-11 stsp
7086 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7087 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7088 818c7501 2019-07-11 stsp err = unlockerr;
7089 818c7501 2019-07-11 stsp return err;
7090 818c7501 2019-07-11 stsp }
7091 0ebf8283 2019-07-24 stsp
7092 0ebf8283 2019-07-24 stsp const struct got_error *
7093 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7094 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7095 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7096 0ebf8283 2019-07-24 stsp {
7097 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7098 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7099 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7100 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7101 0ebf8283 2019-07-24 stsp
7102 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7103 0ebf8283 2019-07-24 stsp if (err)
7104 0ebf8283 2019-07-24 stsp return err;
7105 0ebf8283 2019-07-24 stsp
7106 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7107 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7108 e600f124 2021-03-21 stsp if (err)
7109 e600f124 2021-03-21 stsp goto done;
7110 e600f124 2021-03-21 stsp
7111 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7112 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7113 0ebf8283 2019-07-24 stsp if (err)
7114 0ebf8283 2019-07-24 stsp goto done;
7115 0ebf8283 2019-07-24 stsp
7116 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7117 0ebf8283 2019-07-24 stsp if (err)
7118 0ebf8283 2019-07-24 stsp goto done;
7119 0ebf8283 2019-07-24 stsp
7120 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7121 0ebf8283 2019-07-24 stsp if (err)
7122 0ebf8283 2019-07-24 stsp goto done;
7123 0ebf8283 2019-07-24 stsp
7124 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7125 0ebf8283 2019-07-24 stsp if (err)
7126 0ebf8283 2019-07-24 stsp goto done;
7127 0ebf8283 2019-07-24 stsp
7128 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7129 a615e0e7 2020-12-16 stsp if (err)
7130 a615e0e7 2020-12-16 stsp goto done;
7131 a615e0e7 2020-12-16 stsp
7132 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7133 a615e0e7 2020-12-16 stsp if (err)
7134 a615e0e7 2020-12-16 stsp goto done;
7135 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7136 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7137 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7138 a615e0e7 2020-12-16 stsp err = sync_err;
7139 0ebf8283 2019-07-24 stsp done:
7140 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7141 a615e0e7 2020-12-16 stsp free(fileindex_path);
7142 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7143 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7144 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7145 0ebf8283 2019-07-24 stsp err = unlockerr;
7146 0ebf8283 2019-07-24 stsp return err;
7147 0ebf8283 2019-07-24 stsp }
7148 0ebf8283 2019-07-24 stsp
7149 0ebf8283 2019-07-24 stsp const struct got_error *
7150 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7151 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7152 0ebf8283 2019-07-24 stsp {
7153 0ebf8283 2019-07-24 stsp const struct got_error *err;
7154 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7155 0ebf8283 2019-07-24 stsp
7156 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7157 0ebf8283 2019-07-24 stsp if (err)
7158 0ebf8283 2019-07-24 stsp return err;
7159 0ebf8283 2019-07-24 stsp
7160 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp
7164 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7165 0ebf8283 2019-07-24 stsp done:
7166 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7167 2822a352 2019-10-15 stsp return err;
7168 2822a352 2019-10-15 stsp }
7169 2822a352 2019-10-15 stsp
7170 2822a352 2019-10-15 stsp const struct got_error *
7171 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7172 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7173 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7174 2822a352 2019-10-15 stsp struct got_repository *repo)
7175 2822a352 2019-10-15 stsp {
7176 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7177 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7178 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7179 2822a352 2019-10-15 stsp
7180 2822a352 2019-10-15 stsp *fileindex = NULL;
7181 2822a352 2019-10-15 stsp *branch_ref = NULL;
7182 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7183 2822a352 2019-10-15 stsp
7184 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7185 2822a352 2019-10-15 stsp if (err)
7186 2822a352 2019-10-15 stsp return err;
7187 2822a352 2019-10-15 stsp
7188 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7189 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7190 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7191 2822a352 2019-10-15 stsp "update -b or different branch name required");
7192 2822a352 2019-10-15 stsp goto done;
7193 2822a352 2019-10-15 stsp }
7194 2822a352 2019-10-15 stsp
7195 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7196 2822a352 2019-10-15 stsp if (err)
7197 2822a352 2019-10-15 stsp goto done;
7198 2822a352 2019-10-15 stsp
7199 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7200 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7201 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7202 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7203 2822a352 2019-10-15 stsp &ok_arg);
7204 2822a352 2019-10-15 stsp if (err)
7205 2822a352 2019-10-15 stsp goto done;
7206 2822a352 2019-10-15 stsp
7207 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7208 2822a352 2019-10-15 stsp if (err)
7209 2822a352 2019-10-15 stsp goto done;
7210 2822a352 2019-10-15 stsp
7211 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7212 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7213 2822a352 2019-10-15 stsp done:
7214 2822a352 2019-10-15 stsp if (err) {
7215 2822a352 2019-10-15 stsp if (*branch_ref) {
7216 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7217 2822a352 2019-10-15 stsp *branch_ref = NULL;
7218 2822a352 2019-10-15 stsp }
7219 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7220 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7221 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7222 2822a352 2019-10-15 stsp }
7223 2822a352 2019-10-15 stsp if (*fileindex) {
7224 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7225 2822a352 2019-10-15 stsp *fileindex = NULL;
7226 2822a352 2019-10-15 stsp }
7227 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7228 2822a352 2019-10-15 stsp }
7229 0cb83759 2019-08-03 stsp return err;
7230 0cb83759 2019-08-03 stsp }
7231 0cb83759 2019-08-03 stsp
7232 2822a352 2019-10-15 stsp const struct got_error *
7233 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7234 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7235 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7236 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7237 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7238 2822a352 2019-10-15 stsp {
7239 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7240 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7241 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7242 2822a352 2019-10-15 stsp
7243 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7244 2822a352 2019-10-15 stsp if (err)
7245 2822a352 2019-10-15 stsp goto done;
7246 2822a352 2019-10-15 stsp
7247 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7248 2822a352 2019-10-15 stsp if (err)
7249 2822a352 2019-10-15 stsp goto done;
7250 2822a352 2019-10-15 stsp
7251 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7252 2822a352 2019-10-15 stsp worktree->path_prefix);
7253 2822a352 2019-10-15 stsp if (err)
7254 2822a352 2019-10-15 stsp goto done;
7255 2822a352 2019-10-15 stsp
7256 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7257 2822a352 2019-10-15 stsp if (err)
7258 2822a352 2019-10-15 stsp goto done;
7259 2822a352 2019-10-15 stsp
7260 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7261 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7262 2822a352 2019-10-15 stsp if (err)
7263 2822a352 2019-10-15 stsp goto sync;
7264 2822a352 2019-10-15 stsp
7265 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7266 2822a352 2019-10-15 stsp if (err)
7267 2822a352 2019-10-15 stsp goto sync;
7268 2822a352 2019-10-15 stsp
7269 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7270 6e210706 2021-01-22 stsp if (err)
7271 6e210706 2021-01-22 stsp goto sync;
7272 6e210706 2021-01-22 stsp
7273 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7274 2822a352 2019-10-15 stsp sync:
7275 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7276 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7277 2822a352 2019-10-15 stsp err = sync_err;
7278 2822a352 2019-10-15 stsp
7279 2822a352 2019-10-15 stsp done:
7280 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7281 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7282 2822a352 2019-10-15 stsp err = unlockerr;
7283 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7284 2822a352 2019-10-15 stsp
7285 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7286 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7287 2822a352 2019-10-15 stsp err = unlockerr;
7288 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7289 2822a352 2019-10-15 stsp
7290 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7291 2822a352 2019-10-15 stsp free(fileindex_path);
7292 2822a352 2019-10-15 stsp free(tree_id);
7293 2822a352 2019-10-15 stsp
7294 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7295 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7296 2822a352 2019-10-15 stsp err = unlockerr;
7297 2822a352 2019-10-15 stsp return err;
7298 2822a352 2019-10-15 stsp }
7299 2822a352 2019-10-15 stsp
7300 2822a352 2019-10-15 stsp const struct got_error *
7301 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7302 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7303 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7304 2822a352 2019-10-15 stsp {
7305 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7306 8b692cd0 2019-10-21 stsp
7307 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7308 8b692cd0 2019-10-21 stsp
7309 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7310 8b692cd0 2019-10-21 stsp
7311 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7312 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7313 8b692cd0 2019-10-21 stsp err = unlockerr;
7314 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7315 8b692cd0 2019-10-21 stsp
7316 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7317 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7318 8b692cd0 2019-10-21 stsp err = unlockerr;
7319 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7320 f259c4c1 2021-09-24 stsp
7321 f259c4c1 2021-09-24 stsp return err;
7322 f259c4c1 2021-09-24 stsp }
7323 f259c4c1 2021-09-24 stsp
7324 f259c4c1 2021-09-24 stsp const struct got_error *
7325 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7326 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7327 f259c4c1 2021-09-24 stsp {
7328 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7329 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7330 f259c4c1 2021-09-24 stsp
7331 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7332 f259c4c1 2021-09-24 stsp if (err)
7333 f259c4c1 2021-09-24 stsp goto done;
7334 8b692cd0 2019-10-21 stsp
7335 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7336 f259c4c1 2021-09-24 stsp
7337 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7338 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7339 f259c4c1 2021-09-24 stsp err = sync_err;
7340 f259c4c1 2021-09-24 stsp done:
7341 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7342 f259c4c1 2021-09-24 stsp free(fileindex_path);
7343 8b692cd0 2019-10-21 stsp return err;
7344 2822a352 2019-10-15 stsp }
7345 2822a352 2019-10-15 stsp
7346 f259c4c1 2021-09-24 stsp static const struct got_error *
7347 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7348 f259c4c1 2021-09-24 stsp {
7349 f259c4c1 2021-09-24 stsp const struct got_error *err;
7350 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7351 f259c4c1 2021-09-24 stsp
7352 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7353 f259c4c1 2021-09-24 stsp if (err)
7354 f259c4c1 2021-09-24 stsp goto done;
7355 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7356 f259c4c1 2021-09-24 stsp if (err)
7357 f259c4c1 2021-09-24 stsp goto done;
7358 f259c4c1 2021-09-24 stsp
7359 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7360 f259c4c1 2021-09-24 stsp if (err)
7361 f259c4c1 2021-09-24 stsp goto done;
7362 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7363 f259c4c1 2021-09-24 stsp if (err)
7364 f259c4c1 2021-09-24 stsp goto done;
7365 f259c4c1 2021-09-24 stsp
7366 f259c4c1 2021-09-24 stsp done:
7367 f259c4c1 2021-09-24 stsp free(branch_refname);
7368 f259c4c1 2021-09-24 stsp free(commit_refname);
7369 f259c4c1 2021-09-24 stsp return err;
7370 f259c4c1 2021-09-24 stsp }
7371 f259c4c1 2021-09-24 stsp
7372 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7373 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7374 f259c4c1 2021-09-24 stsp const char *branch_name;
7375 f259c4c1 2021-09-24 stsp };
7376 f259c4c1 2021-09-24 stsp
7377 f259c4c1 2021-09-24 stsp static const struct got_error *
7378 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7379 f259c4c1 2021-09-24 stsp void *arg)
7380 f259c4c1 2021-09-24 stsp {
7381 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7382 f259c4c1 2021-09-24 stsp
7383 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7384 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7385 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7386 f259c4c1 2021-09-24 stsp
7387 f259c4c1 2021-09-24 stsp return NULL;
7388 f259c4c1 2021-09-24 stsp }
7389 f259c4c1 2021-09-24 stsp
7390 f259c4c1 2021-09-24 stsp
7391 f259c4c1 2021-09-24 stsp const struct got_error *
7392 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7393 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7394 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7395 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7396 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7397 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7398 f259c4c1 2021-09-24 stsp {
7399 f259c4c1 2021-09-24 stsp const struct got_error *err;
7400 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7401 f259c4c1 2021-09-24 stsp
7402 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7403 f259c4c1 2021-09-24 stsp if (err)
7404 f259c4c1 2021-09-24 stsp goto done;
7405 f259c4c1 2021-09-24 stsp
7406 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7407 f259c4c1 2021-09-24 stsp worktree);
7408 f259c4c1 2021-09-24 stsp if (err)
7409 f259c4c1 2021-09-24 stsp goto done;
7410 f259c4c1 2021-09-24 stsp
7411 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7412 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7413 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7414 f259c4c1 2021-09-24 stsp done:
7415 f259c4c1 2021-09-24 stsp free(fileindex_path);
7416 f259c4c1 2021-09-24 stsp return err;
7417 f259c4c1 2021-09-24 stsp }
7418 f259c4c1 2021-09-24 stsp
7419 f259c4c1 2021-09-24 stsp const struct got_error *
7420 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7421 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7422 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7423 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7424 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7425 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7426 0ff8d236 2021-09-28 stsp
7427 f259c4c1 2021-09-24 stsp {
7428 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7429 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7430 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7431 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7432 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7433 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7434 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7435 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7436 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7437 f259c4c1 2021-09-24 stsp
7438 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7439 f259c4c1 2021-09-24 stsp
7440 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7441 f259c4c1 2021-09-24 stsp
7442 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7443 f259c4c1 2021-09-24 stsp if (err)
7444 f259c4c1 2021-09-24 stsp goto done;
7445 f259c4c1 2021-09-24 stsp
7446 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7447 f259c4c1 2021-09-24 stsp if (err)
7448 f259c4c1 2021-09-24 stsp goto done;
7449 f259c4c1 2021-09-24 stsp
7450 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7451 f259c4c1 2021-09-24 stsp if (err)
7452 f259c4c1 2021-09-24 stsp goto done;
7453 f259c4c1 2021-09-24 stsp
7454 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7455 f259c4c1 2021-09-24 stsp &have_staged_files);
7456 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7457 f259c4c1 2021-09-24 stsp goto done;
7458 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7459 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7460 f259c4c1 2021-09-24 stsp goto done;
7461 f259c4c1 2021-09-24 stsp }
7462 f259c4c1 2021-09-24 stsp
7463 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7464 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7465 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7466 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7467 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7468 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7469 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7470 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7471 f259c4c1 2021-09-24 stsp if (err)
7472 f259c4c1 2021-09-24 stsp goto done;
7473 f259c4c1 2021-09-24 stsp
7474 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7475 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7476 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7477 f259c4c1 2021-09-24 stsp goto done;
7478 f259c4c1 2021-09-24 stsp }
7479 f259c4c1 2021-09-24 stsp
7480 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7481 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7482 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7483 f259c4c1 2021-09-24 stsp
7484 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7485 f259c4c1 2021-09-24 stsp ct_path++;
7486 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7487 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7488 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7489 f259c4c1 2021-09-24 stsp if (err)
7490 f259c4c1 2021-09-24 stsp goto done;
7491 f259c4c1 2021-09-24 stsp
7492 f259c4c1 2021-09-24 stsp }
7493 f259c4c1 2021-09-24 stsp
7494 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7495 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7496 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7497 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7498 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7499 f259c4c1 2021-09-24 stsp if (err)
7500 f259c4c1 2021-09-24 stsp goto done;
7501 f259c4c1 2021-09-24 stsp
7502 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7503 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7504 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7505 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7506 f259c4c1 2021-09-24 stsp err = sync_err;
7507 f259c4c1 2021-09-24 stsp done:
7508 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7509 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7510 f259c4c1 2021-09-24 stsp free_commitable(ct);
7511 f259c4c1 2021-09-24 stsp }
7512 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7513 f259c4c1 2021-09-24 stsp free(fileindex_path);
7514 f259c4c1 2021-09-24 stsp return err;
7515 f259c4c1 2021-09-24 stsp }
7516 f259c4c1 2021-09-24 stsp
7517 f259c4c1 2021-09-24 stsp const struct got_error *
7518 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7519 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7520 f259c4c1 2021-09-24 stsp {
7521 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7522 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7523 f259c4c1 2021-09-24 stsp
7524 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7525 f259c4c1 2021-09-24 stsp if (err)
7526 f259c4c1 2021-09-24 stsp goto done;
7527 f259c4c1 2021-09-24 stsp
7528 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7529 f259c4c1 2021-09-24 stsp if (err)
7530 f259c4c1 2021-09-24 stsp goto done;
7531 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7532 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7533 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7534 f259c4c1 2021-09-24 stsp err = sync_err;
7535 f259c4c1 2021-09-24 stsp done:
7536 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7537 f259c4c1 2021-09-24 stsp free(fileindex_path);
7538 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7539 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7540 f259c4c1 2021-09-24 stsp err = unlockerr;
7541 f259c4c1 2021-09-24 stsp return err;
7542 f259c4c1 2021-09-24 stsp }
7543 f259c4c1 2021-09-24 stsp
7544 f259c4c1 2021-09-24 stsp const struct got_error *
7545 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7546 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7547 f259c4c1 2021-09-24 stsp {
7548 f259c4c1 2021-09-24 stsp const struct got_error *err;
7549 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7550 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7551 f259c4c1 2021-09-24 stsp
7552 f259c4c1 2021-09-24 stsp *in_progress = 0;
7553 f259c4c1 2021-09-24 stsp
7554 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7555 f259c4c1 2021-09-24 stsp if (err)
7556 f259c4c1 2021-09-24 stsp return err;
7557 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7558 793fcac3 2021-09-24 stsp free(branch_refname);
7559 f259c4c1 2021-09-24 stsp if (err) {
7560 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7561 f259c4c1 2021-09-24 stsp return err;
7562 f259c4c1 2021-09-24 stsp } else
7563 f259c4c1 2021-09-24 stsp *in_progress = 1;
7564 f259c4c1 2021-09-24 stsp
7565 f259c4c1 2021-09-24 stsp return NULL;
7566 f259c4c1 2021-09-24 stsp }
7567 f259c4c1 2021-09-24 stsp
7568 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7569 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7570 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7571 f259c4c1 2021-09-24 stsp {
7572 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7573 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7574 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7575 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7576 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7577 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7578 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7579 f259c4c1 2021-09-24 stsp
7580 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7581 f259c4c1 2021-09-24 stsp
7582 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7583 f259c4c1 2021-09-24 stsp if (err)
7584 f259c4c1 2021-09-24 stsp return err;
7585 f259c4c1 2021-09-24 stsp
7586 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7587 f259c4c1 2021-09-24 stsp if (err)
7588 f259c4c1 2021-09-24 stsp goto done;
7589 f259c4c1 2021-09-24 stsp
7590 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7591 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7592 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7593 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7594 f259c4c1 2021-09-24 stsp &ok_arg);
7595 f259c4c1 2021-09-24 stsp if (err)
7596 f259c4c1 2021-09-24 stsp goto done;
7597 f259c4c1 2021-09-24 stsp
7598 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7599 f259c4c1 2021-09-24 stsp if (err)
7600 f259c4c1 2021-09-24 stsp return err;
7601 f259c4c1 2021-09-24 stsp
7602 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7603 f259c4c1 2021-09-24 stsp if (err)
7604 f259c4c1 2021-09-24 stsp return err;
7605 f259c4c1 2021-09-24 stsp
7606 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7607 f259c4c1 2021-09-24 stsp 0);
7608 f259c4c1 2021-09-24 stsp if (err)
7609 f259c4c1 2021-09-24 stsp goto done;
7610 f259c4c1 2021-09-24 stsp
7611 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7612 f259c4c1 2021-09-24 stsp if (err)
7613 f259c4c1 2021-09-24 stsp goto done;
7614 f259c4c1 2021-09-24 stsp
7615 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7616 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7617 f259c4c1 2021-09-24 stsp goto done;
7618 f259c4c1 2021-09-24 stsp }
7619 f259c4c1 2021-09-24 stsp
7620 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7621 f259c4c1 2021-09-24 stsp if (err)
7622 f259c4c1 2021-09-24 stsp goto done;
7623 f259c4c1 2021-09-24 stsp
7624 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7625 f259c4c1 2021-09-24 stsp if (err)
7626 f259c4c1 2021-09-24 stsp goto done;
7627 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7628 f259c4c1 2021-09-24 stsp if (err)
7629 f259c4c1 2021-09-24 stsp goto done;
7630 f259c4c1 2021-09-24 stsp
7631 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7632 f259c4c1 2021-09-24 stsp if (err)
7633 f259c4c1 2021-09-24 stsp goto done;
7634 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7635 f259c4c1 2021-09-24 stsp if (err)
7636 f259c4c1 2021-09-24 stsp goto done;
7637 f259c4c1 2021-09-24 stsp
7638 f259c4c1 2021-09-24 stsp done:
7639 f259c4c1 2021-09-24 stsp free(branch_refname);
7640 f259c4c1 2021-09-24 stsp free(commit_refname);
7641 f259c4c1 2021-09-24 stsp free(fileindex_path);
7642 f259c4c1 2021-09-24 stsp if (branch_ref)
7643 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7644 f259c4c1 2021-09-24 stsp if (commit_ref)
7645 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7646 f259c4c1 2021-09-24 stsp if (wt_branch)
7647 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7648 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7649 f259c4c1 2021-09-24 stsp if (err) {
7650 f259c4c1 2021-09-24 stsp if (*fileindex) {
7651 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7652 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7653 f259c4c1 2021-09-24 stsp }
7654 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7655 f259c4c1 2021-09-24 stsp }
7656 f259c4c1 2021-09-24 stsp return err;
7657 f259c4c1 2021-09-24 stsp }
7658 f259c4c1 2021-09-24 stsp
7659 f259c4c1 2021-09-24 stsp const struct got_error *
7660 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7661 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7662 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7663 f259c4c1 2021-09-24 stsp {
7664 f259c4c1 2021-09-24 stsp const struct got_error *err;
7665 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7666 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7667 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7668 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7669 f259c4c1 2021-09-24 stsp
7670 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7671 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7672 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7673 f259c4c1 2021-09-24 stsp
7674 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7675 f259c4c1 2021-09-24 stsp if (err)
7676 f259c4c1 2021-09-24 stsp return err;
7677 f259c4c1 2021-09-24 stsp
7678 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7679 f259c4c1 2021-09-24 stsp if (err)
7680 f259c4c1 2021-09-24 stsp goto done;
7681 f259c4c1 2021-09-24 stsp
7682 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7683 f259c4c1 2021-09-24 stsp &have_staged_files);
7684 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7685 f259c4c1 2021-09-24 stsp goto done;
7686 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7687 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7688 f259c4c1 2021-09-24 stsp goto done;
7689 f259c4c1 2021-09-24 stsp }
7690 f259c4c1 2021-09-24 stsp
7691 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7692 f259c4c1 2021-09-24 stsp if (err)
7693 f259c4c1 2021-09-24 stsp goto done;
7694 f259c4c1 2021-09-24 stsp
7695 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7696 f259c4c1 2021-09-24 stsp if (err)
7697 f259c4c1 2021-09-24 stsp goto done;
7698 f259c4c1 2021-09-24 stsp
7699 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7700 f259c4c1 2021-09-24 stsp if (err)
7701 f259c4c1 2021-09-24 stsp goto done;
7702 f259c4c1 2021-09-24 stsp
7703 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7704 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7705 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7706 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7707 f259c4c1 2021-09-24 stsp goto done;
7708 f259c4c1 2021-09-24 stsp }
7709 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7710 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7711 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7712 f259c4c1 2021-09-24 stsp goto done;
7713 f259c4c1 2021-09-24 stsp }
7714 f259c4c1 2021-09-24 stsp
7715 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7716 f259c4c1 2021-09-24 stsp if (err)
7717 f259c4c1 2021-09-24 stsp goto done;
7718 f259c4c1 2021-09-24 stsp
7719 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7720 f259c4c1 2021-09-24 stsp if (err)
7721 f259c4c1 2021-09-24 stsp goto done;
7722 f259c4c1 2021-09-24 stsp done:
7723 f259c4c1 2021-09-24 stsp free(commit_refname);
7724 f259c4c1 2021-09-24 stsp free(branch_refname);
7725 f259c4c1 2021-09-24 stsp free(fileindex_path);
7726 f259c4c1 2021-09-24 stsp if (commit_ref)
7727 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7728 f259c4c1 2021-09-24 stsp if (branch_ref)
7729 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7730 f259c4c1 2021-09-24 stsp if (err) {
7731 f259c4c1 2021-09-24 stsp if (*branch_name) {
7732 f259c4c1 2021-09-24 stsp free(*branch_name);
7733 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7734 f259c4c1 2021-09-24 stsp }
7735 f259c4c1 2021-09-24 stsp free(*branch_tip);
7736 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7737 f259c4c1 2021-09-24 stsp if (*fileindex) {
7738 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7739 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7740 f259c4c1 2021-09-24 stsp }
7741 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7742 f259c4c1 2021-09-24 stsp }
7743 f259c4c1 2021-09-24 stsp return err;
7744 f259c4c1 2021-09-24 stsp }
7745 f259c4c1 2021-09-24 stsp
7746 f259c4c1 2021-09-24 stsp const struct got_error *
7747 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7748 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7749 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7750 f259c4c1 2021-09-24 stsp {
7751 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7752 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7753 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7754 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7755 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7756 f259c4c1 2021-09-24 stsp
7757 f259c4c1 2021-09-24 stsp err = got_object_id_by_path(&tree_id, repo,
7758 f259c4c1 2021-09-24 stsp worktree->base_commit_id, worktree->path_prefix);
7759 f259c4c1 2021-09-24 stsp if (err)
7760 f259c4c1 2021-09-24 stsp goto done;
7761 f259c4c1 2021-09-24 stsp
7762 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7763 f259c4c1 2021-09-24 stsp if (err)
7764 f259c4c1 2021-09-24 stsp goto done;
7765 f259c4c1 2021-09-24 stsp
7766 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7767 f259c4c1 2021-09-24 stsp if (err)
7768 f259c4c1 2021-09-24 stsp goto done;
7769 f259c4c1 2021-09-24 stsp
7770 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7771 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7772 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7773 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7774 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7775 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7776 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7777 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7778 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7779 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7780 f259c4c1 2021-09-24 stsp if (err)
7781 f259c4c1 2021-09-24 stsp goto sync;
7782 f259c4c1 2021-09-24 stsp
7783 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7784 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7785 f259c4c1 2021-09-24 stsp sync:
7786 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7787 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7788 f259c4c1 2021-09-24 stsp err = sync_err;
7789 f259c4c1 2021-09-24 stsp done:
7790 f259c4c1 2021-09-24 stsp free(tree_id);
7791 f259c4c1 2021-09-24 stsp free(commit_id);
7792 f259c4c1 2021-09-24 stsp if (fileindex)
7793 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7794 f259c4c1 2021-09-24 stsp free(fileindex_path);
7795 f259c4c1 2021-09-24 stsp
7796 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7797 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7798 f259c4c1 2021-09-24 stsp err = unlockerr;
7799 f259c4c1 2021-09-24 stsp return err;
7800 f259c4c1 2021-09-24 stsp }
7801 f259c4c1 2021-09-24 stsp
7802 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7803 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7804 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7805 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7806 2db2652d 2019-08-07 stsp struct got_repository *repo;
7807 2db2652d 2019-08-07 stsp int have_changes;
7808 2db2652d 2019-08-07 stsp };
7809 2db2652d 2019-08-07 stsp
7810 2db2652d 2019-08-07 stsp const struct got_error *
7811 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7812 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7813 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7814 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7815 735ef5ac 2019-08-03 stsp {
7816 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7817 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7818 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7819 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7820 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7821 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7822 8b13ce36 2019-08-08 stsp
7823 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7824 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7825 8b13ce36 2019-08-08 stsp return NULL;
7826 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7827 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7828 735ef5ac 2019-08-03 stsp
7829 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7830 735ef5ac 2019-08-03 stsp if (ie == NULL)
7831 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7832 735ef5ac 2019-08-03 stsp
7833 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7834 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7835 735ef5ac 2019-08-03 stsp relpath) == -1)
7836 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7837 735ef5ac 2019-08-03 stsp
7838 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7839 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7840 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7841 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7842 735ef5ac 2019-08-03 stsp }
7843 735ef5ac 2019-08-03 stsp
7844 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7845 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7846 3aa5969e 2019-08-06 stsp goto done;
7847 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7848 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7849 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7850 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7851 735ef5ac 2019-08-03 stsp goto done;
7852 3aa5969e 2019-08-06 stsp }
7853 735ef5ac 2019-08-03 stsp
7854 2db2652d 2019-08-07 stsp a->have_changes = 1;
7855 2db2652d 2019-08-07 stsp
7856 735ef5ac 2019-08-03 stsp p = in_repo_path;
7857 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7858 735ef5ac 2019-08-03 stsp p++;
7859 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7860 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7861 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7862 735ef5ac 2019-08-03 stsp done:
7863 735ef5ac 2019-08-03 stsp free(in_repo_path);
7864 dc424a06 2019-08-07 stsp return err;
7865 dc424a06 2019-08-07 stsp }
7866 dc424a06 2019-08-07 stsp
7867 2db2652d 2019-08-07 stsp struct stage_path_arg {
7868 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7869 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7870 2db2652d 2019-08-07 stsp struct got_repository *repo;
7871 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7872 2db2652d 2019-08-07 stsp void *status_arg;
7873 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7874 2db2652d 2019-08-07 stsp void *patch_arg;
7875 7b5dc508 2019-10-28 stsp int staged_something;
7876 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7877 2db2652d 2019-08-07 stsp };
7878 2db2652d 2019-08-07 stsp
7879 2db2652d 2019-08-07 stsp static const struct got_error *
7880 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7881 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7882 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7883 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7884 0cb83759 2019-08-03 stsp {
7885 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7886 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7887 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7888 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7889 0cb83759 2019-08-03 stsp uint32_t stage;
7890 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7891 0aeb8099 2020-07-23 stsp struct stat sb;
7892 8b13ce36 2019-08-08 stsp
7893 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7894 8b13ce36 2019-08-08 stsp return NULL;
7895 0cb83759 2019-08-03 stsp
7896 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7897 d3e7c587 2019-08-03 stsp if (ie == NULL)
7898 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7899 0cb83759 2019-08-03 stsp
7900 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7901 2db2652d 2019-08-07 stsp relpath)== -1)
7902 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7903 0cb83759 2019-08-03 stsp
7904 0cb83759 2019-08-03 stsp switch (status) {
7905 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7906 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7907 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7908 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7909 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7910 0aeb8099 2020-07-23 stsp break;
7911 0aeb8099 2020-07-23 stsp }
7912 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7913 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7914 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7915 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7916 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7917 dc424a06 2019-08-07 stsp if (err)
7918 dc424a06 2019-08-07 stsp break;
7919 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7920 dc424a06 2019-08-07 stsp break;
7921 dc424a06 2019-08-07 stsp } else {
7922 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7923 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7924 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7925 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7926 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7927 dc424a06 2019-08-07 stsp break;
7928 dc424a06 2019-08-07 stsp }
7929 dc424a06 2019-08-07 stsp }
7930 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7931 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7932 0cb83759 2019-08-03 stsp if (err)
7933 dc424a06 2019-08-07 stsp break;
7934 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7935 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7936 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7937 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7938 0cb83759 2019-08-03 stsp else
7939 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7940 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7941 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7942 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7943 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7944 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7945 35213c7c 2020-07-23 stsp ssize_t target_len;
7946 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7947 35213c7c 2020-07-23 stsp sizeof(target_path));
7948 35213c7c 2020-07-23 stsp if (target_len == -1) {
7949 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7950 35213c7c 2020-07-23 stsp ondisk_path);
7951 35213c7c 2020-07-23 stsp break;
7952 35213c7c 2020-07-23 stsp }
7953 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7954 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7955 35213c7c 2020-07-23 stsp a->worktree->root_path);
7956 35213c7c 2020-07-23 stsp if (err)
7957 35213c7c 2020-07-23 stsp break;
7958 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7959 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7960 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7961 35213c7c 2020-07-23 stsp break;
7962 35213c7c 2020-07-23 stsp }
7963 35213c7c 2020-07-23 stsp }
7964 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7965 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7966 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7967 35213c7c 2020-07-23 stsp else
7968 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7969 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7970 0aeb8099 2020-07-23 stsp } else {
7971 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7972 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7973 0aeb8099 2020-07-23 stsp }
7974 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7975 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7976 dc424a06 2019-08-07 stsp break;
7977 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7978 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7979 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7980 0cb83759 2019-08-03 stsp break;
7981 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7982 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7983 d3e7c587 2019-08-03 stsp break;
7984 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7985 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7986 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7987 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7988 dc424a06 2019-08-07 stsp if (err)
7989 dc424a06 2019-08-07 stsp break;
7990 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7991 88f33a19 2019-08-08 stsp break;
7992 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7993 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7994 dc424a06 2019-08-07 stsp break;
7995 88f33a19 2019-08-08 stsp }
7996 dc424a06 2019-08-07 stsp }
7997 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7998 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7999 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8000 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8001 dc424a06 2019-08-07 stsp break;
8002 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8003 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8004 12463d8b 2019-12-13 stsp de_name);
8005 0cb83759 2019-08-03 stsp break;
8006 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8007 d3e7c587 2019-08-03 stsp break;
8008 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8009 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8010 ebf48fd5 2019-08-03 stsp break;
8011 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8012 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8013 2a06fe5f 2019-08-24 stsp break;
8014 0cb83759 2019-08-03 stsp default:
8015 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8016 537ac44b 2019-08-03 stsp break;
8017 0cb83759 2019-08-03 stsp }
8018 dc424a06 2019-08-07 stsp
8019 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8020 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8021 dc424a06 2019-08-07 stsp free(path_content);
8022 2db2652d 2019-08-07 stsp free(ondisk_path);
8023 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8024 0ebf8283 2019-07-24 stsp return err;
8025 0ebf8283 2019-07-24 stsp }
8026 0cb83759 2019-08-03 stsp
8027 0cb83759 2019-08-03 stsp const struct got_error *
8028 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8029 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8030 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8031 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8032 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8033 0cb83759 2019-08-03 stsp {
8034 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8035 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8036 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8037 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8038 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8039 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8040 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8041 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8042 0cb83759 2019-08-03 stsp
8043 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8044 0cb83759 2019-08-03 stsp if (err)
8045 0cb83759 2019-08-03 stsp return err;
8046 0cb83759 2019-08-03 stsp
8047 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8048 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8049 735ef5ac 2019-08-03 stsp if (err)
8050 735ef5ac 2019-08-03 stsp goto done;
8051 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8052 735ef5ac 2019-08-03 stsp if (err)
8053 735ef5ac 2019-08-03 stsp goto done;
8054 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8055 0cb83759 2019-08-03 stsp if (err)
8056 0cb83759 2019-08-03 stsp goto done;
8057 0cb83759 2019-08-03 stsp
8058 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8059 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8060 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8061 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8062 2db2652d 2019-08-07 stsp oka.repo = repo;
8063 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8064 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8065 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8066 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8067 735ef5ac 2019-08-03 stsp if (err)
8068 735ef5ac 2019-08-03 stsp goto done;
8069 735ef5ac 2019-08-03 stsp }
8070 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8071 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8072 2db2652d 2019-08-07 stsp goto done;
8073 2db2652d 2019-08-07 stsp }
8074 735ef5ac 2019-08-03 stsp
8075 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8076 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8077 2db2652d 2019-08-07 stsp spa.repo = repo;
8078 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8079 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8080 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8081 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8082 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8083 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8084 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8085 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8086 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8087 42005733 2019-08-03 stsp if (err)
8088 2db2652d 2019-08-07 stsp goto done;
8089 7b5dc508 2019-10-28 stsp }
8090 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8091 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8092 7b5dc508 2019-10-28 stsp goto done;
8093 0cb83759 2019-08-03 stsp }
8094 0cb83759 2019-08-03 stsp
8095 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8096 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8097 0cb83759 2019-08-03 stsp err = sync_err;
8098 0cb83759 2019-08-03 stsp done:
8099 735ef5ac 2019-08-03 stsp if (head_ref)
8100 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8101 735ef5ac 2019-08-03 stsp free(head_commit_id);
8102 0cb83759 2019-08-03 stsp free(fileindex_path);
8103 0cb83759 2019-08-03 stsp if (fileindex)
8104 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8105 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8106 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8107 0cb83759 2019-08-03 stsp err = unlockerr;
8108 0cb83759 2019-08-03 stsp return err;
8109 0cb83759 2019-08-03 stsp }
8110 ad493afc 2019-08-03 stsp
8111 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8112 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8113 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8114 ad493afc 2019-08-03 stsp struct got_repository *repo;
8115 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8116 ad493afc 2019-08-03 stsp void *progress_arg;
8117 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8118 2e1f37b0 2019-08-08 stsp void *patch_arg;
8119 ad493afc 2019-08-03 stsp };
8120 2e1f37b0 2019-08-08 stsp
8121 2e1f37b0 2019-08-08 stsp static const struct got_error *
8122 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8123 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8124 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8125 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8126 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8127 2e1f37b0 2019-08-08 stsp {
8128 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8129 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8130 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8131 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8132 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8133 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8134 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8135 2e1f37b0 2019-08-08 stsp
8136 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8137 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8138 2e1f37b0 2019-08-08 stsp
8139 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8140 2e1f37b0 2019-08-08 stsp if (err)
8141 2e1f37b0 2019-08-08 stsp return err;
8142 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8143 2e1f37b0 2019-08-08 stsp if (err)
8144 2e1f37b0 2019-08-08 stsp goto done;
8145 2e1f37b0 2019-08-08 stsp
8146 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8147 2e1f37b0 2019-08-08 stsp if (err)
8148 2e1f37b0 2019-08-08 stsp goto done;
8149 2e1f37b0 2019-08-08 stsp
8150 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8151 2e1f37b0 2019-08-08 stsp if (err)
8152 2e1f37b0 2019-08-08 stsp goto done;
8153 2e1f37b0 2019-08-08 stsp
8154 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8155 2e1f37b0 2019-08-08 stsp if (err)
8156 2e1f37b0 2019-08-08 stsp goto done;
8157 2e1f37b0 2019-08-08 stsp
8158 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8159 2e1f37b0 2019-08-08 stsp if (err)
8160 2e1f37b0 2019-08-08 stsp goto done;
8161 ad493afc 2019-08-03 stsp
8162 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8163 2e1f37b0 2019-08-08 stsp if (err)
8164 2e1f37b0 2019-08-08 stsp goto done;
8165 2e1f37b0 2019-08-08 stsp
8166 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8167 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8168 2e1f37b0 2019-08-08 stsp if (err)
8169 2e1f37b0 2019-08-08 stsp goto done;
8170 2e1f37b0 2019-08-08 stsp
8171 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8172 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8173 2e1f37b0 2019-08-08 stsp if (err)
8174 2e1f37b0 2019-08-08 stsp goto done;
8175 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8176 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8177 2e1f37b0 2019-08-08 stsp if (err)
8178 2e1f37b0 2019-08-08 stsp goto done;
8179 2e1f37b0 2019-08-08 stsp
8180 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8181 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8182 2e1f37b0 2019-08-08 stsp goto done;
8183 2e1f37b0 2019-08-08 stsp }
8184 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8185 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8186 2e1f37b0 2019-08-08 stsp goto done;
8187 2e1f37b0 2019-08-08 stsp }
8188 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8189 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8190 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8191 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8192 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8193 fe621944 2020-11-10 stsp nchanges++;
8194 fe621944 2020-11-10 stsp }
8195 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8196 2e1f37b0 2019-08-08 stsp int choice;
8197 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8198 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8199 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8200 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8201 2e1f37b0 2019-08-08 stsp if (err)
8202 2e1f37b0 2019-08-08 stsp goto done;
8203 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8204 2e1f37b0 2019-08-08 stsp have_content = 1;
8205 19e4b907 2019-08-08 stsp else
8206 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8207 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8208 2e1f37b0 2019-08-08 stsp break;
8209 2e1f37b0 2019-08-08 stsp }
8210 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8211 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8212 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8213 2e1f37b0 2019-08-08 stsp done:
8214 2e1f37b0 2019-08-08 stsp free(label1);
8215 2e1f37b0 2019-08-08 stsp if (blob)
8216 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8217 2e1f37b0 2019-08-08 stsp if (staged_blob)
8218 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8219 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8220 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8221 fe621944 2020-11-10 stsp err = free_err;
8222 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8223 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8224 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8225 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8226 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8227 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8228 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8229 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8230 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8231 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8232 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8233 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8234 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8235 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8236 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8237 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8238 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8239 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8240 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8241 2e1f37b0 2019-08-08 stsp }
8242 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8243 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8244 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8245 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8246 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8247 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8248 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8249 2e1f37b0 2019-08-08 stsp }
8250 2e1f37b0 2019-08-08 stsp free(path1);
8251 2e1f37b0 2019-08-08 stsp free(path2);
8252 fda8017d 2020-07-23 stsp return err;
8253 fda8017d 2020-07-23 stsp }
8254 fda8017d 2020-07-23 stsp
8255 fda8017d 2020-07-23 stsp static const struct got_error *
8256 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8257 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8258 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8259 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8260 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8261 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8262 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8263 fda8017d 2020-07-23 stsp {
8264 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8265 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8266 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8267 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8268 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8269 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8270 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8271 fda8017d 2020-07-23 stsp struct stat sb;
8272 fda8017d 2020-07-23 stsp
8273 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8274 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8275 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8276 fda8017d 2020-07-23 stsp if (err)
8277 fda8017d 2020-07-23 stsp return err;
8278 fda8017d 2020-07-23 stsp
8279 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8280 fda8017d 2020-07-23 stsp return NULL;
8281 fda8017d 2020-07-23 stsp
8282 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8283 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8284 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8285 fda8017d 2020-07-23 stsp if (err)
8286 fda8017d 2020-07-23 stsp goto done;
8287 fda8017d 2020-07-23 stsp }
8288 fda8017d 2020-07-23 stsp
8289 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8290 fda8017d 2020-07-23 stsp if (f == NULL) {
8291 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8292 fda8017d 2020-07-23 stsp path_unstaged_content);
8293 fda8017d 2020-07-23 stsp goto done;
8294 fda8017d 2020-07-23 stsp }
8295 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8296 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8297 fda8017d 2020-07-23 stsp goto done;
8298 fda8017d 2020-07-23 stsp }
8299 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8300 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8301 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8302 fda8017d 2020-07-23 stsp size_t r;
8303 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8304 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8305 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8306 fda8017d 2020-07-23 stsp goto done;
8307 fda8017d 2020-07-23 stsp }
8308 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8309 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8310 fda8017d 2020-07-23 stsp goto done;
8311 fda8017d 2020-07-23 stsp }
8312 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8313 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8314 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8315 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8316 fda8017d 2020-07-23 stsp progress_arg);
8317 fda8017d 2020-07-23 stsp } else {
8318 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8319 67a66647 2021-05-31 stsp
8320 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8321 67a66647 2021-05-31 stsp if (err)
8322 67a66647 2021-05-31 stsp return err;
8323 67a66647 2021-05-31 stsp
8324 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8325 67a66647 2021-05-31 stsp parent) == -1) {
8326 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8327 67a66647 2021-05-31 stsp base_path = NULL;
8328 67a66647 2021-05-31 stsp goto done;
8329 67a66647 2021-05-31 stsp }
8330 67a66647 2021-05-31 stsp
8331 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8332 67a66647 2021-05-31 stsp if (err)
8333 67a66647 2021-05-31 stsp goto done;
8334 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8335 67a66647 2021-05-31 stsp blob_base);
8336 67a66647 2021-05-31 stsp if (err)
8337 67a66647 2021-05-31 stsp goto done;
8338 67a66647 2021-05-31 stsp
8339 eec2f5a9 2021-06-03 stsp /*
8340 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8341 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8342 eec2f5a9 2021-06-03 stsp */
8343 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8344 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8345 eec2f5a9 2021-06-03 stsp ondisk_path);
8346 eec2f5a9 2021-06-03 stsp if (err)
8347 eec2f5a9 2021-06-03 stsp goto done;
8348 eec2f5a9 2021-06-03 stsp } else {
8349 eec2f5a9 2021-06-03 stsp int fd;
8350 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8351 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8352 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8353 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8354 eec2f5a9 2021-06-03 stsp goto done;
8355 eec2f5a9 2021-06-03 stsp }
8356 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8357 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8358 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8359 eec2f5a9 2021-06-03 stsp close(fd);
8360 eec2f5a9 2021-06-03 stsp goto done;
8361 eec2f5a9 2021-06-03 stsp }
8362 eec2f5a9 2021-06-03 stsp }
8363 eec2f5a9 2021-06-03 stsp
8364 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8365 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8366 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8367 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8368 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8369 fda8017d 2020-07-23 stsp }
8370 fda8017d 2020-07-23 stsp if (err)
8371 fda8017d 2020-07-23 stsp goto done;
8372 fda8017d 2020-07-23 stsp
8373 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8374 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8375 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8376 2ac8aa02 2020-11-09 stsp } else {
8377 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8378 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8379 2ac8aa02 2020-11-09 stsp }
8380 fda8017d 2020-07-23 stsp done:
8381 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8382 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8383 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8384 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8385 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8386 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8387 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8388 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8389 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8390 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8391 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8392 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8393 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8394 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8395 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8396 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8397 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8398 67a66647 2021-05-31 stsp free(blob_base_path);
8399 67a66647 2021-05-31 stsp free(parent);
8400 67a66647 2021-05-31 stsp free(base_path);
8401 2e1f37b0 2019-08-08 stsp return err;
8402 2e1f37b0 2019-08-08 stsp }
8403 2e1f37b0 2019-08-08 stsp
8404 ad493afc 2019-08-03 stsp static const struct got_error *
8405 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8406 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8407 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8408 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8409 ad493afc 2019-08-03 stsp {
8410 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8411 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8412 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8413 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8414 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8415 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8416 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8417 9bc94a15 2019-08-03 stsp struct stat sb;
8418 ad493afc 2019-08-03 stsp
8419 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8420 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8421 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8422 2e1f37b0 2019-08-08 stsp return NULL;
8423 2e1f37b0 2019-08-08 stsp
8424 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8425 ad493afc 2019-08-03 stsp if (ie == NULL)
8426 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8427 9bc94a15 2019-08-03 stsp
8428 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8429 9bc94a15 2019-08-03 stsp == -1)
8430 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8431 ad493afc 2019-08-03 stsp
8432 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8433 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8434 f69721c3 2019-10-21 stsp if (err)
8435 f69721c3 2019-10-21 stsp goto done;
8436 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8437 f69721c3 2019-10-21 stsp id_str) == -1) {
8438 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8439 f69721c3 2019-10-21 stsp goto done;
8440 f69721c3 2019-10-21 stsp }
8441 f69721c3 2019-10-21 stsp
8442 ad493afc 2019-08-03 stsp switch (staged_status) {
8443 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8444 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8445 ad493afc 2019-08-03 stsp blob_id, 8192);
8446 ad493afc 2019-08-03 stsp if (err)
8447 ad493afc 2019-08-03 stsp break;
8448 ad493afc 2019-08-03 stsp /* fall through */
8449 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8450 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8451 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8452 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8453 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8454 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8455 2e1f37b0 2019-08-08 stsp if (err)
8456 2e1f37b0 2019-08-08 stsp break;
8457 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8458 2e1f37b0 2019-08-08 stsp break;
8459 2e1f37b0 2019-08-08 stsp } else {
8460 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8461 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8462 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8463 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8464 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8465 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8466 2e1f37b0 2019-08-08 stsp }
8467 2e1f37b0 2019-08-08 stsp }
8468 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8469 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8470 ad493afc 2019-08-03 stsp if (err)
8471 ad493afc 2019-08-03 stsp break;
8472 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8473 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8474 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8475 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8476 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8477 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8478 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8479 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8480 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8481 ea7786be 2020-07-23 stsp break;
8482 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8483 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8484 36bf999c 2020-07-23 stsp char *staged_target;
8485 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8486 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8487 36bf999c 2020-07-23 stsp if (err)
8488 36bf999c 2020-07-23 stsp goto done;
8489 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8490 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8491 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8492 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8493 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8494 36bf999c 2020-07-23 stsp free(staged_target);
8495 dfe9fba0 2020-07-23 stsp } else {
8496 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8497 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8498 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8499 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8500 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8501 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8502 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8503 dfe9fba0 2020-07-23 stsp }
8504 ea7786be 2020-07-23 stsp break;
8505 ea7786be 2020-07-23 stsp default:
8506 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8507 ea7786be 2020-07-23 stsp break;
8508 ea7786be 2020-07-23 stsp }
8509 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8510 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8511 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8512 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8513 2ac8aa02 2020-11-09 stsp }
8514 ad493afc 2019-08-03 stsp break;
8515 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8516 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8517 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8518 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8519 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8520 2e1f37b0 2019-08-08 stsp if (err)
8521 2e1f37b0 2019-08-08 stsp break;
8522 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8523 2e1f37b0 2019-08-08 stsp break;
8524 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8525 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8526 2e1f37b0 2019-08-08 stsp break;
8527 2e1f37b0 2019-08-08 stsp }
8528 2e1f37b0 2019-08-08 stsp }
8529 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8530 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8531 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8532 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8533 9bc94a15 2019-08-03 stsp if (err)
8534 9bc94a15 2019-08-03 stsp break;
8535 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8536 ad493afc 2019-08-03 stsp break;
8537 ad493afc 2019-08-03 stsp }
8538 f69721c3 2019-10-21 stsp done:
8539 ad493afc 2019-08-03 stsp free(ondisk_path);
8540 ad493afc 2019-08-03 stsp if (blob_base)
8541 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8542 ad493afc 2019-08-03 stsp if (blob_staged)
8543 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8544 f69721c3 2019-10-21 stsp free(id_str);
8545 f69721c3 2019-10-21 stsp free(label_orig);
8546 ad493afc 2019-08-03 stsp return err;
8547 ad493afc 2019-08-03 stsp }
8548 ad493afc 2019-08-03 stsp
8549 ad493afc 2019-08-03 stsp const struct got_error *
8550 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8551 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8552 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8553 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8554 ad493afc 2019-08-03 stsp struct got_repository *repo)
8555 ad493afc 2019-08-03 stsp {
8556 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8557 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8558 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8559 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8560 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8561 ad493afc 2019-08-03 stsp
8562 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8563 ad493afc 2019-08-03 stsp if (err)
8564 ad493afc 2019-08-03 stsp return err;
8565 ad493afc 2019-08-03 stsp
8566 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8567 ad493afc 2019-08-03 stsp if (err)
8568 ad493afc 2019-08-03 stsp goto done;
8569 ad493afc 2019-08-03 stsp
8570 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8571 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8572 ad493afc 2019-08-03 stsp upa.repo = repo;
8573 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8574 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8575 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8576 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8577 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8578 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8579 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8580 ad493afc 2019-08-03 stsp if (err)
8581 ad493afc 2019-08-03 stsp goto done;
8582 ad493afc 2019-08-03 stsp }
8583 ad493afc 2019-08-03 stsp
8584 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8585 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8586 ad493afc 2019-08-03 stsp err = sync_err;
8587 ad493afc 2019-08-03 stsp done:
8588 ad493afc 2019-08-03 stsp free(fileindex_path);
8589 ad493afc 2019-08-03 stsp if (fileindex)
8590 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8591 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8592 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8593 ad493afc 2019-08-03 stsp err = unlockerr;
8594 ad493afc 2019-08-03 stsp return err;
8595 ad493afc 2019-08-03 stsp }
8596 b2118c49 2020-07-28 stsp
8597 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8598 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8599 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8600 b2118c49 2020-07-28 stsp void *info_arg;
8601 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8602 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8603 b2118c49 2020-07-28 stsp void *cancel_arg;
8604 b2118c49 2020-07-28 stsp };
8605 b2118c49 2020-07-28 stsp
8606 b2118c49 2020-07-28 stsp static const struct got_error *
8607 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8608 b2118c49 2020-07-28 stsp {
8609 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8610 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8611 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8612 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8613 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8614 b2118c49 2020-07-28 stsp int stage;
8615 b2118c49 2020-07-28 stsp
8616 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8617 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8618 b2118c49 2020-07-28 stsp
8619 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8620 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8621 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8622 b2118c49 2020-07-28 stsp break;
8623 b2118c49 2020-07-28 stsp }
8624 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8625 b2118c49 2020-07-28 stsp return NULL;
8626 b2118c49 2020-07-28 stsp
8627 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8628 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8629 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8630 b2118c49 2020-07-28 stsp }
8631 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8632 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8633 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8634 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8635 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8636 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8637 b2118c49 2020-07-28 stsp }
8638 b2118c49 2020-07-28 stsp
8639 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8640 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8641 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8642 b2118c49 2020-07-28 stsp }
8643 b2118c49 2020-07-28 stsp
8644 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8645 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8646 b2118c49 2020-07-28 stsp }
8647 b2118c49 2020-07-28 stsp
8648 b2118c49 2020-07-28 stsp const struct got_error *
8649 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8650 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8651 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8652 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8653 b2118c49 2020-07-28 stsp
8654 b2118c49 2020-07-28 stsp {
8655 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8656 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8657 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8658 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8659 b2118c49 2020-07-28 stsp
8660 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8661 b2118c49 2020-07-28 stsp if (err)
8662 b2118c49 2020-07-28 stsp return err;
8663 b2118c49 2020-07-28 stsp
8664 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8665 b2118c49 2020-07-28 stsp if (err)
8666 b2118c49 2020-07-28 stsp goto done;
8667 b2118c49 2020-07-28 stsp
8668 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8669 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8670 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8671 b2118c49 2020-07-28 stsp arg.paths = paths;
8672 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8673 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8674 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8675 b2118c49 2020-07-28 stsp &arg);
8676 b2118c49 2020-07-28 stsp done:
8677 b2118c49 2020-07-28 stsp free(fileindex_path);
8678 b2118c49 2020-07-28 stsp if (fileindex)
8679 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8680 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8681 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8682 b2118c49 2020-07-28 stsp err = unlockerr;
8683 b2118c49 2020-07-28 stsp return err;
8684 b2118c49 2020-07-28 stsp }