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 5822e79e 2023-02-23 op #include <sha2.h>
33 9d31a1d8 2018-03-11 stsp #include <zlib.h>
34 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
35 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 ec22038e 2019-03-10 stsp #include <uuid.h>
37 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp
39 86c3caaf 2018-03-09 stsp #include "got_error.h"
40 86c3caaf 2018-03-09 stsp #include "got_repository.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 9d31a1d8 2018-03-11 stsp #include "got_object.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 234035bc 2019-06-01 stsp #include "got_diff.h"
48 86c3caaf 2018-03-09 stsp
49 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
50 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
54 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
55 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
56 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
57 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
58 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
60 9d31a1d8 2018-03-11 stsp
61 9d31a1d8 2018-03-11 stsp #ifndef MIN
62 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 9d31a1d8 2018-03-11 stsp #endif
64 f69721c3 2019-10-21 stsp
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
66 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 b2b3fce1 2022-10-29 op
68 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
69 86c3caaf 2018-03-09 stsp
70 99724ed4 2018-03-10 stsp static const struct got_error *
71 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
72 99724ed4 2018-03-10 stsp {
73 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
74 99724ed4 2018-03-10 stsp char *path;
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
78 99724ed4 2018-03-10 stsp
79 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
80 99724ed4 2018-03-10 stsp free(path);
81 507dc3bb 2018-12-29 stsp return err;
82 507dc3bb 2018-12-29 stsp }
83 507dc3bb 2018-12-29 stsp
84 507dc3bb 2018-12-29 stsp static const struct got_error *
85 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
86 507dc3bb 2018-12-29 stsp {
87 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
88 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
89 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
90 507dc3bb 2018-12-29 stsp char *path = NULL;
91 507dc3bb 2018-12-29 stsp
92 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
94 507dc3bb 2018-12-29 stsp path = NULL;
95 507dc3bb 2018-12-29 stsp goto done;
96 507dc3bb 2018-12-29 stsp }
97 507dc3bb 2018-12-29 stsp
98 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 507dc3bb 2018-12-29 stsp if (err)
100 507dc3bb 2018-12-29 stsp goto done;
101 507dc3bb 2018-12-29 stsp
102 507dc3bb 2018-12-29 stsp if (content) {
103 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
104 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
105 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
106 507dc3bb 2018-12-29 stsp goto done;
107 507dc3bb 2018-12-29 stsp }
108 507dc3bb 2018-12-29 stsp }
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
111 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
112 2a57020b 2019-02-20 stsp unlink(tmppath);
113 507dc3bb 2018-12-29 stsp goto done;
114 507dc3bb 2018-12-29 stsp }
115 507dc3bb 2018-12-29 stsp
116 507dc3bb 2018-12-29 stsp done:
117 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
118 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
119 230a42bd 2019-05-11 jcs free(tmppath);
120 09fe317a 2018-03-11 stsp return err;
121 09fe317a 2018-03-11 stsp }
122 09fe317a 2018-03-11 stsp
123 024e9686 2019-05-14 stsp static const struct got_error *
124 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
125 024e9686 2019-05-14 stsp {
126 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
127 024e9686 2019-05-14 stsp char *refstr = NULL;
128 024e9686 2019-05-14 stsp
129 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
130 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
131 024e9686 2019-05-14 stsp if (refstr == NULL)
132 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
133 024e9686 2019-05-14 stsp } else {
134 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
135 024e9686 2019-05-14 stsp if (refstr == NULL)
136 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
137 024e9686 2019-05-14 stsp }
138 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 024e9686 2019-05-14 stsp free(refstr);
140 024e9686 2019-05-14 stsp return err;
141 024e9686 2019-05-14 stsp }
142 024e9686 2019-05-14 stsp
143 86c3caaf 2018-03-09 stsp const struct got_error *
144 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
145 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
146 86c3caaf 2018-03-09 stsp {
147 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
148 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
149 ec22038e 2019-03-10 stsp uuid_t uuid;
150 ec22038e 2019-03-10 stsp uint32_t uuid_status;
151 65596e15 2018-12-24 stsp int obj_type;
152 7ac97322 2018-03-11 stsp char *path_got = NULL;
153 1451e70d 2018-03-10 stsp char *formatstr = NULL;
154 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
155 65596e15 2018-12-24 stsp char *basestr = NULL;
156 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
157 65596e15 2018-12-24 stsp
158 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
160 0c48fee2 2019-03-11 stsp goto done;
161 0c48fee2 2019-03-11 stsp }
162 0c48fee2 2019-03-11 stsp
163 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
167 65596e15 2018-12-24 stsp if (err)
168 65596e15 2018-12-24 stsp return err;
169 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
171 86c3caaf 2018-03-09 stsp
172 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
173 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
174 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
175 0bb8a95e 2018-03-12 stsp }
176 577ec78f 2018-03-11 stsp
177 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
178 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
180 86c3caaf 2018-03-09 stsp goto done;
181 86c3caaf 2018-03-09 stsp }
182 86c3caaf 2018-03-09 stsp
183 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
184 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
186 86c3caaf 2018-03-09 stsp goto done;
187 86c3caaf 2018-03-09 stsp }
188 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
190 86c3caaf 2018-03-09 stsp goto done;
191 86c3caaf 2018-03-09 stsp }
192 86c3caaf 2018-03-09 stsp
193 056e7441 2018-03-11 stsp /* Create an empty lock file. */
194 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 056e7441 2018-03-11 stsp if (err)
196 056e7441 2018-03-11 stsp goto done;
197 056e7441 2018-03-11 stsp
198 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 99724ed4 2018-03-10 stsp if (err)
201 86c3caaf 2018-03-09 stsp goto done;
202 86c3caaf 2018-03-09 stsp
203 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
204 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
205 65596e15 2018-12-24 stsp if (err)
206 65596e15 2018-12-24 stsp goto done;
207 65596e15 2018-12-24 stsp
208 65596e15 2018-12-24 stsp /* Record our base commit. */
209 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
210 65596e15 2018-12-24 stsp if (err)
211 65596e15 2018-12-24 stsp goto done;
212 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 99724ed4 2018-03-10 stsp if (err)
214 86c3caaf 2018-03-09 stsp goto done;
215 86c3caaf 2018-03-09 stsp
216 1451e70d 2018-03-10 stsp /* Store path to repository. */
217 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
219 99724ed4 2018-03-10 stsp if (err)
220 86c3caaf 2018-03-09 stsp goto done;
221 86c3caaf 2018-03-09 stsp
222 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
223 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
225 ec22038e 2019-03-10 stsp if (err)
226 ec22038e 2019-03-10 stsp goto done;
227 ec22038e 2019-03-10 stsp
228 ec22038e 2019-03-10 stsp /* Generate UUID. */
229 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
230 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
231 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
232 ec22038e 2019-03-10 stsp goto done;
233 ec22038e 2019-03-10 stsp }
234 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
237 ec22038e 2019-03-10 stsp goto done;
238 ec22038e 2019-03-10 stsp }
239 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 577ec78f 2018-03-11 stsp if (err)
241 577ec78f 2018-03-11 stsp goto done;
242 577ec78f 2018-03-11 stsp
243 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
244 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 1451e70d 2018-03-10 stsp goto done;
247 1451e70d 2018-03-10 stsp }
248 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
249 99724ed4 2018-03-10 stsp if (err)
250 1451e70d 2018-03-10 stsp goto done;
251 1451e70d 2018-03-10 stsp
252 86c3caaf 2018-03-09 stsp done:
253 65596e15 2018-12-24 stsp free(commit_id);
254 7ac97322 2018-03-11 stsp free(path_got);
255 1451e70d 2018-03-10 stsp free(formatstr);
256 0bb8a95e 2018-03-12 stsp free(absprefix);
257 65596e15 2018-12-24 stsp free(basestr);
258 ec22038e 2019-03-10 stsp free(uuidstr);
259 86c3caaf 2018-03-09 stsp return err;
260 86c3caaf 2018-03-09 stsp }
261 86c3caaf 2018-03-09 stsp
262 247140b2 2019-02-05 stsp const struct got_error *
263 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 e5dc7198 2018-12-29 stsp const char *path_prefix)
265 e5dc7198 2018-12-29 stsp {
266 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
267 e5dc7198 2018-12-29 stsp
268 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
269 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
271 e5dc7198 2018-12-29 stsp }
272 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
273 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
274 e5dc7198 2018-12-29 stsp free(absprefix);
275 e5dc7198 2018-12-29 stsp return NULL;
276 86c3caaf 2018-03-09 stsp }
277 86c3caaf 2018-03-09 stsp
278 bc70eb79 2019-05-09 stsp const char *
279 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 86c3caaf 2018-03-09 stsp {
281 36a38700 2019-05-10 stsp return worktree->head_ref_name;
282 024e9686 2019-05-14 stsp }
283 024e9686 2019-05-14 stsp
284 024e9686 2019-05-14 stsp const struct got_error *
285 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
286 024e9686 2019-05-14 stsp struct got_reference *head_ref)
287 024e9686 2019-05-14 stsp {
288 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
289 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
290 024e9686 2019-05-14 stsp
291 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
293 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
294 024e9686 2019-05-14 stsp path_got = NULL;
295 024e9686 2019-05-14 stsp goto done;
296 024e9686 2019-05-14 stsp }
297 024e9686 2019-05-14 stsp
298 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
299 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
300 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
301 024e9686 2019-05-14 stsp goto done;
302 024e9686 2019-05-14 stsp }
303 024e9686 2019-05-14 stsp
304 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
305 024e9686 2019-05-14 stsp if (err)
306 024e9686 2019-05-14 stsp goto done;
307 024e9686 2019-05-14 stsp
308 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
309 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
310 024e9686 2019-05-14 stsp done:
311 024e9686 2019-05-14 stsp free(path_got);
312 024e9686 2019-05-14 stsp if (err)
313 024e9686 2019-05-14 stsp free(head_ref_name);
314 024e9686 2019-05-14 stsp return err;
315 507dc3bb 2018-12-29 stsp }
316 507dc3bb 2018-12-29 stsp
317 b72f483a 2019-02-05 stsp struct got_object_id *
318 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 507dc3bb 2018-12-29 stsp {
320 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 507dc3bb 2018-12-29 stsp const struct got_error *
324 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
326 507dc3bb 2018-12-29 stsp {
327 507dc3bb 2018-12-29 stsp const struct got_error *err;
328 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
329 507dc3bb 2018-12-29 stsp char *id_str = NULL;
330 507dc3bb 2018-12-29 stsp char *path_got = NULL;
331 507dc3bb 2018-12-29 stsp
332 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 507dc3bb 2018-12-29 stsp path_got = NULL;
336 507dc3bb 2018-12-29 stsp goto done;
337 507dc3bb 2018-12-29 stsp }
338 507dc3bb 2018-12-29 stsp
339 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
340 507dc3bb 2018-12-29 stsp if (err)
341 507dc3bb 2018-12-29 stsp return err;
342 507dc3bb 2018-12-29 stsp
343 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
345 507dc3bb 2018-12-29 stsp goto done;
346 507dc3bb 2018-12-29 stsp }
347 507dc3bb 2018-12-29 stsp
348 507dc3bb 2018-12-29 stsp /* Record our base commit. */
349 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 507dc3bb 2018-12-29 stsp if (err)
354 507dc3bb 2018-12-29 stsp goto done;
355 507dc3bb 2018-12-29 stsp
356 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
357 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
358 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
359 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
360 507dc3bb 2018-12-29 stsp goto done;
361 507dc3bb 2018-12-29 stsp }
362 507dc3bb 2018-12-29 stsp done:
363 507dc3bb 2018-12-29 stsp if (obj)
364 507dc3bb 2018-12-29 stsp got_object_close(obj);
365 507dc3bb 2018-12-29 stsp free(id_str);
366 507dc3bb 2018-12-29 stsp free(path_got);
367 507dc3bb 2018-12-29 stsp return err;
368 50b0790e 2020-09-11 stsp }
369 50b0790e 2020-09-11 stsp
370 50b0790e 2020-09-11 stsp const struct got_gotconfig *
371 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
372 50b0790e 2020-09-11 stsp {
373 50b0790e 2020-09-11 stsp return worktree->gotconfig;
374 507dc3bb 2018-12-29 stsp }
375 507dc3bb 2018-12-29 stsp
376 9d31a1d8 2018-03-11 stsp static const struct got_error *
377 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
378 86c3caaf 2018-03-09 stsp {
379 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
382 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
383 86c3caaf 2018-03-09 stsp return NULL;
384 21908da4 2019-01-13 stsp }
385 21908da4 2019-01-13 stsp
386 21908da4 2019-01-13 stsp static const struct got_error *
387 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 4a1ddfc2 2019-01-12 stsp {
389 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
390 4a1ddfc2 2019-01-12 stsp char *abspath;
391 4a1ddfc2 2019-01-12 stsp
392 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
394 4a1ddfc2 2019-01-12 stsp
395 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
396 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 ddcd8544 2019-03-11 stsp struct stat sb;
398 ddcd8544 2019-03-11 stsp err = NULL;
399 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
400 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
401 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
402 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
403 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 ddcd8544 2019-03-11 stsp }
405 ddcd8544 2019-03-11 stsp }
406 4a1ddfc2 2019-01-12 stsp free(abspath);
407 68c76935 2019-02-19 stsp return err;
408 68c76935 2019-02-19 stsp }
409 68c76935 2019-02-19 stsp
410 68c76935 2019-02-19 stsp static const struct got_error *
411 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 68c76935 2019-02-19 stsp {
413 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
414 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
415 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
416 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
417 68c76935 2019-02-19 stsp
418 68c76935 2019-02-19 stsp *same = 1;
419 68c76935 2019-02-19 stsp
420 230a42bd 2019-05-11 jcs for (;;) {
421 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
423 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
424 68c76935 2019-02-19 stsp break;
425 68c76935 2019-02-19 stsp }
426 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
429 68c76935 2019-02-19 stsp break;
430 68c76935 2019-02-19 stsp }
431 68c76935 2019-02-19 stsp if (flen1 == 0) {
432 68c76935 2019-02-19 stsp if (flen2 != 0)
433 68c76935 2019-02-19 stsp *same = 0;
434 68c76935 2019-02-19 stsp break;
435 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
436 68c76935 2019-02-19 stsp if (flen1 != 0)
437 68c76935 2019-02-19 stsp *same = 0;
438 68c76935 2019-02-19 stsp break;
439 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
440 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
441 68c76935 2019-02-19 stsp *same = 0;
442 68c76935 2019-02-19 stsp break;
443 68c76935 2019-02-19 stsp }
444 68c76935 2019-02-19 stsp } else {
445 68c76935 2019-02-19 stsp *same = 0;
446 68c76935 2019-02-19 stsp break;
447 68c76935 2019-02-19 stsp }
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp return err;
451 68c76935 2019-02-19 stsp }
452 68c76935 2019-02-19 stsp
453 68c76935 2019-02-19 stsp static const struct got_error *
454 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
455 68c76935 2019-02-19 stsp {
456 68c76935 2019-02-19 stsp struct stat sb;
457 68c76935 2019-02-19 stsp size_t size1, size2;
458 68c76935 2019-02-19 stsp
459 68c76935 2019-02-19 stsp *same = 1;
460 68c76935 2019-02-19 stsp
461 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
462 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
463 68c76935 2019-02-19 stsp size1 = sb.st_size;
464 68c76935 2019-02-19 stsp
465 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
466 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
467 68c76935 2019-02-19 stsp size2 = sb.st_size;
468 68c76935 2019-02-19 stsp
469 68c76935 2019-02-19 stsp if (size1 != size2) {
470 68c76935 2019-02-19 stsp *same = 0;
471 68c76935 2019-02-19 stsp return NULL;
472 68c76935 2019-02-19 stsp }
473 68c76935 2019-02-19 stsp
474 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
475 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
476 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
477 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
478 68c76935 2019-02-19 stsp
479 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
480 0e039681 2021-11-15 stsp }
481 0e039681 2021-11-15 stsp
482 0e039681 2021-11-15 stsp static const struct got_error *
483 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 0e039681 2021-11-15 stsp {
485 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
486 0e039681 2021-11-15 stsp size_t flen;
487 0e039681 2021-11-15 stsp ssize_t outlen;
488 0e039681 2021-11-15 stsp
489 0e039681 2021-11-15 stsp *outsize = 0;
490 0e039681 2021-11-15 stsp
491 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
492 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
493 0e039681 2021-11-15 stsp
494 0e039681 2021-11-15 stsp for (;;) {
495 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
496 0e039681 2021-11-15 stsp if (flen == 0) {
497 0e039681 2021-11-15 stsp if (ferror(f))
498 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
499 0e039681 2021-11-15 stsp if (feof(f))
500 0e039681 2021-11-15 stsp break;
501 0e039681 2021-11-15 stsp }
502 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
503 0e039681 2021-11-15 stsp if (outlen == -1)
504 0e039681 2021-11-15 stsp return got_error_from_errno("write");
505 0e039681 2021-11-15 stsp if (outlen != flen)
506 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
507 0e039681 2021-11-15 stsp *outsize += outlen;
508 0e039681 2021-11-15 stsp }
509 0e039681 2021-11-15 stsp
510 0e039681 2021-11-15 stsp return NULL;
511 0e039681 2021-11-15 stsp }
512 0e039681 2021-11-15 stsp
513 0e039681 2021-11-15 stsp static const struct got_error *
514 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
515 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 0e039681 2021-11-15 stsp const char *ondisk_path)
518 0e039681 2021-11-15 stsp {
519 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
520 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
521 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
525 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
526 0e039681 2021-11-15 stsp
527 0e039681 2021-11-15 stsp *overlapcnt = 0;
528 0e039681 2021-11-15 stsp
529 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 0e039681 2021-11-15 stsp if (err)
531 0e039681 2021-11-15 stsp return err;
532 0e039681 2021-11-15 stsp
533 0e039681 2021-11-15 stsp if (same_content)
534 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 0e039681 2021-11-15 stsp
536 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
537 0e039681 2021-11-15 stsp if (err)
538 0e039681 2021-11-15 stsp return err;
539 0e039681 2021-11-15 stsp changed_deriv = !same_content;
540 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
541 0e039681 2021-11-15 stsp if (err)
542 0e039681 2021-11-15 stsp return err;
543 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
544 0e039681 2021-11-15 stsp
545 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
546 0e039681 2021-11-15 stsp *overlapcnt = 1;
547 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
549 0e039681 2021-11-15 stsp goto done;
550 0e039681 2021-11-15 stsp }
551 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
553 0e039681 2021-11-15 stsp goto done;
554 0e039681 2021-11-15 stsp }
555 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
557 0e039681 2021-11-15 stsp goto done;
558 0e039681 2021-11-15 stsp }
559 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 b90054ed 2022-11-01 stsp base_path_orig, "");
561 0e039681 2021-11-15 stsp if (err)
562 0e039681 2021-11-15 stsp goto done;
563 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 b90054ed 2022-11-01 stsp base_path_deriv, "");
565 0e039681 2021-11-15 stsp if (err)
566 0e039681 2021-11-15 stsp goto done;
567 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 b90054ed 2022-11-01 stsp base_path_deriv2, "");
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_orig, f_orig, fd_orig);
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_deriv, f_deriv, fd_deriv);
575 0e039681 2021-11-15 stsp if (err)
576 0e039681 2021-11-15 stsp goto done;
577 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 0e039681 2021-11-15 stsp if (err)
579 0e039681 2021-11-15 stsp goto done;
580 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
581 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
582 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
583 0e039681 2021-11-15 stsp goto done;
584 0e039681 2021-11-15 stsp }
585 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 0e039681 2021-11-15 stsp label_deriv ? " " : "",
588 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
589 0e039681 2021-11-15 stsp path_deriv) < 0) {
590 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
591 0e039681 2021-11-15 stsp goto done;
592 0e039681 2021-11-15 stsp }
593 0e039681 2021-11-15 stsp if (size_orig > 0) {
594 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
596 0e039681 2021-11-15 stsp label_orig ? " " : "",
597 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
598 0e039681 2021-11-15 stsp path_orig) < 0) {
599 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
600 0e039681 2021-11-15 stsp goto done;
601 0e039681 2021-11-15 stsp }
602 0e039681 2021-11-15 stsp }
603 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
605 0e039681 2021-11-15 stsp path_deriv2,
606 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
607 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
608 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
609 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
610 0e039681 2021-11-15 stsp goto done;
611 0e039681 2021-11-15 stsp }
612 0e039681 2021-11-15 stsp } else if (changed_deriv)
613 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 0e039681 2021-11-15 stsp else if (changed_deriv2)
615 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 0e039681 2021-11-15 stsp done:
617 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 0e039681 2021-11-15 stsp err == NULL)
619 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
620 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
622 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
624 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
626 0e039681 2021-11-15 stsp free(path_orig);
627 0e039681 2021-11-15 stsp free(path_deriv);
628 0e039681 2021-11-15 stsp free(path_deriv2);
629 0e039681 2021-11-15 stsp free(base_path_orig);
630 0e039681 2021-11-15 stsp free(base_path_deriv);
631 0e039681 2021-11-15 stsp free(base_path_deriv2);
632 0e039681 2021-11-15 stsp return err;
633 6353ad76 2019-02-08 stsp }
634 6353ad76 2019-02-08 stsp
635 6353ad76 2019-02-08 stsp /*
636 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
637 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
638 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
639 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
640 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
641 6353ad76 2019-02-08 stsp */
642 6353ad76 2019-02-08 stsp static const struct got_error *
643 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
646 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
649 6353ad76 2019-02-08 stsp {
650 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
651 6353ad76 2019-02-08 stsp int merged_fd = -1;
652 db590691 2021-06-02 stsp FILE *f_merged = NULL;
653 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
654 234035bc 2019-06-01 stsp int overlapcnt = 0;
655 3524bbf9 2020-10-20 stsp char *parent = NULL;
656 6353ad76 2019-02-08 stsp
657 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
658 234035bc 2019-06-01 stsp
659 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
660 3524bbf9 2020-10-20 stsp if (err)
661 3524bbf9 2020-10-20 stsp return err;
662 af54ae4a 2019-02-19 stsp
663 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
665 3524bbf9 2020-10-20 stsp goto done;
666 3524bbf9 2020-10-20 stsp }
667 af54ae4a 2019-02-19 stsp
668 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 6353ad76 2019-02-08 stsp if (err)
670 6353ad76 2019-02-08 stsp goto done;
671 3b9f0f87 2020-07-23 stsp
672 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 0e039681 2021-11-15 stsp if (err) {
675 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
676 0e039681 2021-11-15 stsp goto done;
677 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 0e039681 2021-11-15 stsp ondisk_path);
680 0e039681 2021-11-15 stsp if (err)
681 0e039681 2021-11-15 stsp goto done;
682 0e039681 2021-11-15 stsp }
683 6353ad76 2019-02-08 stsp
684 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
685 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 1ee397ad 2019-07-12 stsp if (err)
687 1ee397ad 2019-07-12 stsp goto done;
688 6353ad76 2019-02-08 stsp
689 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
691 816dc654 2019-02-16 stsp goto done;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp
694 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
695 db590691 2021-06-02 stsp if (f_merged == NULL) {
696 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
697 db590691 2021-06-02 stsp goto done;
698 db590691 2021-06-02 stsp }
699 db590691 2021-06-02 stsp merged_fd = -1;
700 db590691 2021-06-02 stsp
701 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
702 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
703 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
704 db590691 2021-06-02 stsp f_merged);
705 68c76935 2019-02-19 stsp if (err)
706 68c76935 2019-02-19 stsp goto done;
707 816dc654 2019-02-16 stsp }
708 6353ad76 2019-02-08 stsp
709 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
711 70a0c8ec 2019-02-20 stsp goto done;
712 70a0c8ec 2019-02-20 stsp }
713 70a0c8ec 2019-02-20 stsp
714 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
715 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
716 230a42bd 2019-05-11 jcs ondisk_path);
717 6353ad76 2019-02-08 stsp goto done;
718 6353ad76 2019-02-08 stsp }
719 6353ad76 2019-02-08 stsp done:
720 fdcb7daf 2019-12-15 stsp if (err) {
721 fdcb7daf 2019-12-15 stsp if (merged_path)
722 fdcb7daf 2019-12-15 stsp unlink(merged_path);
723 3b9f0f87 2020-07-23 stsp }
724 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
726 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
728 6353ad76 2019-02-08 stsp free(merged_path);
729 af54ae4a 2019-02-19 stsp free(base_path);
730 3524bbf9 2020-10-20 stsp free(parent);
731 af57b12a 2020-07-23 stsp return err;
732 af57b12a 2020-07-23 stsp }
733 af57b12a 2020-07-23 stsp
734 af57b12a 2020-07-23 stsp static const struct got_error *
735 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
736 af57b12a 2020-07-23 stsp size_t target_len)
737 af57b12a 2020-07-23 stsp {
738 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
739 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
740 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
741 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
742 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
743 af57b12a 2020-07-23 stsp ondisk_path);
744 af57b12a 2020-07-23 stsp return NULL;
745 af57b12a 2020-07-23 stsp }
746 af57b12a 2020-07-23 stsp
747 af57b12a 2020-07-23 stsp /*
748 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
750 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
751 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
754 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
755 993e2a1b 2020-07-23 stsp *
756 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
757 993e2a1b 2020-07-23 stsp * has deleted this symlink.
758 11cc08c1 2020-07-23 stsp */
759 11cc08c1 2020-07-23 stsp static const struct got_error *
760 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
761 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
763 11cc08c1 2020-07-23 stsp {
764 11cc08c1 2020-07-23 stsp const struct got_error *err;
765 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 11cc08c1 2020-07-23 stsp FILE *f = NULL;
767 11cc08c1 2020-07-23 stsp
768 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
769 11cc08c1 2020-07-23 stsp if (err)
770 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
771 11cc08c1 2020-07-23 stsp
772 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
773 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
775 11cc08c1 2020-07-23 stsp goto done;
776 11cc08c1 2020-07-23 stsp }
777 11cc08c1 2020-07-23 stsp
778 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 11cc08c1 2020-07-23 stsp if (err)
780 3818e3c4 2020-11-01 naddy goto done;
781 3818e3c4 2020-11-01 naddy
782 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
784 11cc08c1 2020-07-23 stsp goto done;
785 3818e3c4 2020-11-01 naddy }
786 11cc08c1 2020-07-23 stsp
787 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
790 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
791 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
792 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
793 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
794 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
795 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
797 11cc08c1 2020-07-23 stsp goto done;
798 11cc08c1 2020-07-23 stsp }
799 11cc08c1 2020-07-23 stsp
800 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
801 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
802 11cc08c1 2020-07-23 stsp goto done;
803 11cc08c1 2020-07-23 stsp }
804 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
805 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
806 11cc08c1 2020-07-23 stsp goto done;
807 11cc08c1 2020-07-23 stsp }
808 11cc08c1 2020-07-23 stsp done:
809 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
810 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
811 11cc08c1 2020-07-23 stsp free(path);
812 11cc08c1 2020-07-23 stsp free(id_str);
813 11cc08c1 2020-07-23 stsp free(label_deriv);
814 11cc08c1 2020-07-23 stsp return err;
815 11cc08c1 2020-07-23 stsp }
816 d219f183 2020-07-23 stsp
817 d219f183 2020-07-23 stsp /* forward declaration */
818 d219f183 2020-07-23 stsp static const struct got_error *
819 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
821 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
822 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
823 11cc08c1 2020-07-23 stsp
824 11cc08c1 2020-07-23 stsp /*
825 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
826 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
827 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
828 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
829 af57b12a 2020-07-23 stsp */
830 af57b12a 2020-07-23 stsp static const struct got_error *
831 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
832 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
833 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
834 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
836 af57b12a 2020-07-23 stsp {
837 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
838 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
839 af57b12a 2020-07-23 stsp struct stat sb;
840 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
841 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
842 11cc08c1 2020-07-23 stsp int have_local_change = 0;
843 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
846 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
847 af57b12a 2020-07-23 stsp
848 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
849 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
850 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
851 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
852 af57b12a 2020-07-23 stsp ondisk_path);
853 af57b12a 2020-07-23 stsp goto done;
854 af57b12a 2020-07-23 stsp }
855 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
856 af57b12a 2020-07-23 stsp
857 526a746f 2020-07-23 stsp if (blob_orig) {
858 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
859 526a746f 2020-07-23 stsp if (err)
860 526a746f 2020-07-23 stsp goto done;
861 526a746f 2020-07-23 stsp }
862 af57b12a 2020-07-23 stsp
863 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
864 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
865 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 11cc08c1 2020-07-23 stsp have_local_change = 1;
867 11cc08c1 2020-07-23 stsp
868 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
869 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
870 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
871 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
873 11cc08c1 2020-07-23 stsp
874 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
875 11cc08c1 2020-07-23 stsp if (ancestor_target) {
876 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
877 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 11cc08c1 2020-07-23 stsp path);
879 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
880 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
882 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 11cc08c1 2020-07-23 stsp path);
884 11cc08c1 2020-07-23 stsp } else {
885 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
886 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
887 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
888 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
889 11cc08c1 2020-07-23 stsp if (err)
890 11cc08c1 2020-07-23 stsp goto done;
891 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 11cc08c1 2020-07-23 stsp path);
893 11cc08c1 2020-07-23 stsp }
894 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
895 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
896 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
897 af57b12a 2020-07-23 stsp strlen(deriv_target));
898 af57b12a 2020-07-23 stsp if (err)
899 af57b12a 2020-07-23 stsp goto done;
900 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
902 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
903 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
905 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 ea7786be 2020-07-23 stsp path);
907 ea7786be 2020-07-23 stsp } else {
908 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
909 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
910 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
911 ea7786be 2020-07-23 stsp if (err)
912 ea7786be 2020-07-23 stsp goto done;
913 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 ea7786be 2020-07-23 stsp path);
915 ea7786be 2020-07-23 stsp }
916 6353ad76 2019-02-08 stsp }
917 11cc08c1 2020-07-23 stsp
918 af57b12a 2020-07-23 stsp done:
919 af57b12a 2020-07-23 stsp free(ancestor_target);
920 eec2f5a9 2021-06-03 stsp return err;
921 eec2f5a9 2021-06-03 stsp }
922 eec2f5a9 2021-06-03 stsp
923 eec2f5a9 2021-06-03 stsp static const struct got_error *
924 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 eec2f5a9 2021-06-03 stsp {
926 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
927 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
928 eec2f5a9 2021-06-03 stsp ssize_t target_len;
929 eec2f5a9 2021-06-03 stsp size_t n;
930 eec2f5a9 2021-06-03 stsp FILE *f;
931 eec2f5a9 2021-06-03 stsp
932 eec2f5a9 2021-06-03 stsp *outfile = NULL;
933 eec2f5a9 2021-06-03 stsp
934 eec2f5a9 2021-06-03 stsp f = got_opentemp();
935 eec2f5a9 2021-06-03 stsp if (f == NULL)
936 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
937 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
939 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
940 eec2f5a9 2021-06-03 stsp goto done;
941 eec2f5a9 2021-06-03 stsp }
942 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
943 eec2f5a9 2021-06-03 stsp if (n != target_len) {
944 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
945 eec2f5a9 2021-06-03 stsp goto done;
946 eec2f5a9 2021-06-03 stsp }
947 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
948 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
949 eec2f5a9 2021-06-03 stsp goto done;
950 eec2f5a9 2021-06-03 stsp }
951 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
952 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
953 eec2f5a9 2021-06-03 stsp goto done;
954 eec2f5a9 2021-06-03 stsp }
955 eec2f5a9 2021-06-03 stsp done:
956 eec2f5a9 2021-06-03 stsp if (err)
957 eec2f5a9 2021-06-03 stsp fclose(f);
958 eec2f5a9 2021-06-03 stsp else
959 eec2f5a9 2021-06-03 stsp *outfile = f;
960 14c901f1 2019-08-08 stsp return err;
961 14c901f1 2019-08-08 stsp }
962 14c901f1 2019-08-08 stsp
963 14c901f1 2019-08-08 stsp /*
964 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
966 14c901f1 2019-08-08 stsp * acts as the second derived version.
967 14c901f1 2019-08-08 stsp */
968 14c901f1 2019-08-08 stsp static const struct got_error *
969 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
971 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
972 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
973 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
975 14c901f1 2019-08-08 stsp {
976 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
977 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
979 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
981 14c901f1 2019-08-08 stsp
982 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
983 14c901f1 2019-08-08 stsp
984 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
985 ed6b5030 2020-10-20 stsp if (err)
986 ed6b5030 2020-10-20 stsp return err;
987 14c901f1 2019-08-08 stsp
988 67a66647 2021-05-31 stsp if (blob_orig) {
989 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 67a66647 2021-05-31 stsp parent) == -1) {
991 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
992 67a66647 2021-05-31 stsp base_path = NULL;
993 67a66647 2021-05-31 stsp goto done;
994 67a66647 2021-05-31 stsp }
995 67a66647 2021-05-31 stsp
996 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
997 b90054ed 2022-11-01 stsp base_path, "");
998 67a66647 2021-05-31 stsp if (err)
999 67a66647 2021-05-31 stsp goto done;
1000 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 67a66647 2021-05-31 stsp blob_orig);
1002 67a66647 2021-05-31 stsp if (err)
1003 67a66647 2021-05-31 stsp goto done;
1004 67a66647 2021-05-31 stsp free(base_path);
1005 db590691 2021-06-02 stsp } else {
1006 db590691 2021-06-02 stsp /*
1007 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1008 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1009 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1010 db590691 2021-06-02 stsp */
1011 db590691 2021-06-02 stsp f_orig = got_opentemp();
1012 db590691 2021-06-02 stsp if (f_orig == NULL) {
1013 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1014 db590691 2021-06-02 stsp goto done;
1015 db590691 2021-06-02 stsp }
1016 67a66647 2021-05-31 stsp }
1017 67a66647 2021-05-31 stsp
1018 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1020 14c901f1 2019-08-08 stsp base_path = NULL;
1021 14c901f1 2019-08-08 stsp goto done;
1022 14c901f1 2019-08-08 stsp }
1023 14c901f1 2019-08-08 stsp
1024 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 14c901f1 2019-08-08 stsp if (err)
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 14c901f1 2019-08-08 stsp blob_deriv);
1029 14c901f1 2019-08-08 stsp if (err)
1030 14c901f1 2019-08-08 stsp goto done;
1031 14c901f1 2019-08-08 stsp
1032 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 14c901f1 2019-08-08 stsp if (err)
1034 14c901f1 2019-08-08 stsp goto done;
1035 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1036 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1038 14c901f1 2019-08-08 stsp goto done;
1039 eec2f5a9 2021-06-03 stsp }
1040 eec2f5a9 2021-06-03 stsp
1041 5e91dae4 2022-08-30 stsp /*
1042 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1043 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1044 eec2f5a9 2021-06-03 stsp */
1045 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1046 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 eec2f5a9 2021-06-03 stsp if (err)
1048 eec2f5a9 2021-06-03 stsp goto done;
1049 eec2f5a9 2021-06-03 stsp } else {
1050 eec2f5a9 2021-06-03 stsp int fd;
1051 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1053 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1054 eec2f5a9 2021-06-03 stsp goto done;
1055 eec2f5a9 2021-06-03 stsp }
1056 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1057 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1058 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1059 eec2f5a9 2021-06-03 stsp close(fd);
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp }
1062 14c901f1 2019-08-08 stsp }
1063 14c901f1 2019-08-08 stsp
1064 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 14c901f1 2019-08-08 stsp done:
1068 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1070 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1072 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1074 14c901f1 2019-08-08 stsp free(base_path);
1075 67a66647 2021-05-31 stsp if (blob_orig_path) {
1076 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1077 67a66647 2021-05-31 stsp free(blob_orig_path);
1078 67a66647 2021-05-31 stsp }
1079 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1080 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1081 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1082 14c901f1 2019-08-08 stsp }
1083 6353ad76 2019-02-08 stsp free(id_str);
1084 818c7501 2019-07-11 stsp free(label_deriv);
1085 ed6b5030 2020-10-20 stsp free(parent);
1086 4a1ddfc2 2019-01-12 stsp return err;
1087 4a1ddfc2 2019-01-12 stsp }
1088 4a1ddfc2 2019-01-12 stsp
1089 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1090 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1093 13d9040b 2019-03-27 stsp {
1094 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1095 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1096 13d9040b 2019-03-27 stsp
1097 65b05cec 2020-07-23 stsp *new_iep = NULL;
1098 65b05cec 2020-07-23 stsp
1099 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1100 054041d0 2020-06-24 stsp if (err)
1101 054041d0 2020-06-24 stsp return err;
1102 054041d0 2020-06-24 stsp
1103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1105 054041d0 2020-06-24 stsp if (err)
1106 054041d0 2020-06-24 stsp goto done;
1107 054041d0 2020-06-24 stsp
1108 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1109 054041d0 2020-06-24 stsp done:
1110 054041d0 2020-06-24 stsp if (err)
1111 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1112 65b05cec 2020-07-23 stsp else
1113 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1114 13d9040b 2019-03-27 stsp return err;
1115 1ebedb77 2019-10-19 stsp }
1116 1ebedb77 2019-10-19 stsp
1117 1ebedb77 2019-10-19 stsp static mode_t
1118 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1119 1ebedb77 2019-10-19 stsp {
1120 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1121 1ebedb77 2019-10-19 stsp
1122 1ebedb77 2019-10-19 stsp if (executable) {
1123 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1124 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1125 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1126 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1127 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1128 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1129 1ebedb77 2019-10-19 stsp }
1130 1ebedb77 2019-10-19 stsp
1131 b2b3fce1 2022-10-29 op return st_mode;
1132 b2b3fce1 2022-10-29 op }
1133 b2b3fce1 2022-10-29 op
1134 b2b3fce1 2022-10-29 op static mode_t
1135 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1136 b2b3fce1 2022-10-29 op {
1137 b2b3fce1 2022-10-29 op mode_t um;
1138 b2b3fce1 2022-10-29 op
1139 b2b3fce1 2022-10-29 op um = umask(000);
1140 b2b3fce1 2022-10-29 op umask(um);
1141 b2b3fce1 2022-10-29 op return mode & ~um;
1142 13d9040b 2019-03-27 stsp }
1143 13d9040b 2019-03-27 stsp
1144 8ba819a3 2020-07-23 stsp /* forward declaration */
1145 13d9040b 2019-03-27 stsp static const struct got_error *
1146 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1148 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1149 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1150 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1151 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 8ba819a3 2020-07-23 stsp
1153 5a1fbc73 2020-07-23 stsp /*
1154 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1155 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1156 5a1fbc73 2020-07-23 stsp */
1157 8ba819a3 2020-07-23 stsp static const struct got_error *
1158 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1160 5a1fbc73 2020-07-23 stsp {
1161 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1162 5a1fbc73 2020-07-23 stsp ssize_t elen;
1163 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1164 5a1fbc73 2020-07-23 stsp int fd;
1165 5a1fbc73 2020-07-23 stsp
1166 c6e8a826 2021-04-05 stsp *did_something = 0;
1167 c6e8a826 2021-04-05 stsp
1168 5a1fbc73 2020-07-23 stsp /*
1169 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1170 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1171 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1172 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1173 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1174 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1175 5a1fbc73 2020-07-23 stsp */
1176 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1178 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1179 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1180 5a1fbc73 2020-07-23 stsp
1181 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1182 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 5a1fbc73 2020-07-23 stsp if (elen == -1)
1184 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1185 5a1fbc73 2020-07-23 stsp
1186 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1187 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1188 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1189 5a1fbc73 2020-07-23 stsp }
1190 5a1fbc73 2020-07-23 stsp
1191 c6e8a826 2021-04-05 stsp *did_something = 1;
1192 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1193 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1194 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1195 5a1fbc73 2020-07-23 stsp return err;
1196 3c1ec782 2020-07-23 stsp }
1197 3c1ec782 2020-07-23 stsp
1198 3c1ec782 2020-07-23 stsp static const struct got_error *
1199 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 3c1ec782 2020-07-23 stsp {
1202 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1203 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1204 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1205 3c1ec782 2020-07-23 stsp
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1207 3c1ec782 2020-07-23 stsp
1208 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1209 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1210 3c1ec782 2020-07-23 stsp return NULL;
1211 3c1ec782 2020-07-23 stsp }
1212 3c1ec782 2020-07-23 stsp
1213 3c1ec782 2020-07-23 stsp /*
1214 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1215 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1216 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1217 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1218 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1219 3c1ec782 2020-07-23 stsp */
1220 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1221 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1222 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1223 ce031e9e 2020-10-20 stsp if (err)
1224 ce031e9e 2020-10-20 stsp return err;
1225 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1228 ce031e9e 2020-10-20 stsp }
1229 ce031e9e 2020-10-20 stsp free(parent);
1230 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1231 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 3c1ec782 2020-07-23 stsp free(abspath);
1233 3c1ec782 2020-07-23 stsp return err;
1234 3c1ec782 2020-07-23 stsp }
1235 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 3c1ec782 2020-07-23 stsp free(abspath);
1237 3c1ec782 2020-07-23 stsp if (err)
1238 3c1ec782 2020-07-23 stsp return err;
1239 3c1ec782 2020-07-23 stsp } else {
1240 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 3c1ec782 2020-07-23 stsp if (err)
1242 3c1ec782 2020-07-23 stsp return err;
1243 3c1ec782 2020-07-23 stsp }
1244 3c1ec782 2020-07-23 stsp
1245 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1246 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1248 3c1ec782 2020-07-23 stsp return NULL;
1249 3c1ec782 2020-07-23 stsp }
1250 3c1ec782 2020-07-23 stsp
1251 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1252 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1254 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1255 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1257 3c1ec782 2020-07-23 stsp
1258 3c1ec782 2020-07-23 stsp free(path_got);
1259 3c1ec782 2020-07-23 stsp return NULL;
1260 5a1fbc73 2020-07-23 stsp }
1261 5a1fbc73 2020-07-23 stsp
1262 5a1fbc73 2020-07-23 stsp static const struct got_error *
1263 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1266 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1267 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1268 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 9d31a1d8 2018-03-11 stsp {
1270 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1271 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1272 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1273 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 8ba819a3 2020-07-23 stsp
1276 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1277 2e1fa222 2020-07-23 stsp
1278 3c29341b 2022-07-21 florian /*
1279 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1280 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1281 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1282 8ba819a3 2020-07-23 stsp * in the blob object.
1283 8ba819a3 2020-07-23 stsp */
1284 8ba819a3 2020-07-23 stsp do {
1285 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1286 538b6881 2022-07-21 florian if (err)
1287 538b6881 2022-07-21 florian return err;
1288 538b6881 2022-07-21 florian
1289 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1290 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1291 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1292 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1293 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1294 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1296 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1297 3b9f0f87 2020-07-23 stsp progress_arg);
1298 8ba819a3 2020-07-23 stsp }
1299 8ba819a3 2020-07-23 stsp if (len > 0) {
1300 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1301 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1302 8ba819a3 2020-07-23 stsp len - hdrlen);
1303 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1304 8ba819a3 2020-07-23 stsp hdrlen = 0;
1305 8ba819a3 2020-07-23 stsp }
1306 8ba819a3 2020-07-23 stsp } while (len != 0);
1307 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1308 8ba819a3 2020-07-23 stsp
1309 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1311 3c1ec782 2020-07-23 stsp if (err)
1312 3c1ec782 2020-07-23 stsp return err;
1313 8ba819a3 2020-07-23 stsp
1314 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1315 906c123b 2020-07-23 stsp /* install as a regular file */
1316 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1317 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1318 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1320 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1321 3c29341b 2022-07-21 florian return err;
1322 906c123b 2020-07-23 stsp }
1323 906c123b 2020-07-23 stsp
1324 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1325 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1326 c6e8a826 2021-04-05 stsp int symlink_replaced;
1327 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1328 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1329 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1330 3c29341b 2022-07-21 florian return err;
1331 c90c8ce3 2020-07-23 stsp }
1332 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1333 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1334 5a1fbc73 2020-07-23 stsp if (err)
1335 3c29341b 2022-07-21 florian return err;
1336 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1337 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1338 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1339 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1342 c6e8a826 2021-04-05 stsp } else {
1343 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1344 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1345 c6e8a826 2021-04-05 stsp }
1346 f35fa46a 2020-07-23 stsp }
1347 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1348 f35fa46a 2020-07-23 stsp }
1349 f35fa46a 2020-07-23 stsp
1350 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1351 f4994adc 2020-10-20 stsp char *parent;
1352 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1353 f4994adc 2020-10-20 stsp if (err)
1354 3c29341b 2022-07-21 florian return err;
1355 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1356 f4994adc 2020-10-20 stsp free(parent);
1357 f35fa46a 2020-07-23 stsp if (err)
1358 3c29341b 2022-07-21 florian return err;
1359 f35fa46a 2020-07-23 stsp /*
1360 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1361 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1362 f35fa46a 2020-07-23 stsp */
1363 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1364 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1365 3c29341b 2022-07-21 florian return err;
1366 f35fa46a 2020-07-23 stsp }
1367 f35fa46a 2020-07-23 stsp }
1368 f35fa46a 2020-07-23 stsp
1369 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1370 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1371 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1372 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1373 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1374 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1375 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1377 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1378 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1379 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1380 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1381 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1382 8ba819a3 2020-07-23 stsp } else {
1383 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1384 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1385 8ba819a3 2020-07-23 stsp }
1386 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1387 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 8ba819a3 2020-07-23 stsp return err;
1390 8ba819a3 2020-07-23 stsp }
1391 8ba819a3 2020-07-23 stsp
1392 8ba819a3 2020-07-23 stsp static const struct got_error *
1393 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1395 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1396 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1397 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1398 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 8ba819a3 2020-07-23 stsp {
1400 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1401 507dc3bb 2018-12-29 stsp int fd = -1;
1402 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1403 507dc3bb 2018-12-29 stsp int update = 0;
1404 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1405 b2b3fce1 2022-10-29 op mode_t mode;
1406 8ba819a3 2020-07-23 stsp
1407 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1410 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1411 07fa9365 2023-03-10 stsp if (errno == ENOENT || errno == ENOTDIR) {
1412 f5375317 2020-10-20 stsp char *parent;
1413 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1414 f5375317 2020-10-20 stsp if (err)
1415 f5375317 2020-10-20 stsp return err;
1416 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1417 07fa9365 2023-03-10 stsp if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1418 07fa9365 2023-03-10 stsp err = got_error_path(path, err->code);
1419 f5375317 2020-10-20 stsp free(parent);
1420 21908da4 2019-01-13 stsp if (err)
1421 21908da4 2019-01-13 stsp return err;
1422 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1423 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 b2b3fce1 2022-10-29 op mode);
1425 21908da4 2019-01-13 stsp if (fd == -1)
1426 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1427 230a42bd 2019-05-11 jcs ondisk_path);
1428 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1429 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1430 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1431 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1432 3b9f0f87 2020-07-23 stsp goto done;
1433 3b9f0f87 2020-07-23 stsp }
1434 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1437 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1438 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1439 507dc3bb 2018-12-29 stsp goto done;
1440 d70b8e30 2018-12-27 stsp } else {
1441 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1442 b90054ed 2022-11-01 stsp ondisk_path, "");
1443 507dc3bb 2018-12-29 stsp if (err)
1444 507dc3bb 2018-12-29 stsp goto done;
1445 507dc3bb 2018-12-29 stsp update = 1;
1446 b2b3fce1 2022-10-29 op
1447 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1448 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1449 b2b3fce1 2022-10-29 op tmppath);
1450 b2b3fce1 2022-10-29 op goto done;
1451 b2b3fce1 2022-10-29 op }
1452 9d31a1d8 2018-03-11 stsp }
1453 507dc3bb 2018-12-29 stsp } else
1454 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1455 3818e3c4 2020-11-01 naddy }
1456 3818e3c4 2020-11-01 naddy
1457 bd6aa359 2020-07-23 stsp if (progress_cb) {
1458 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 bd6aa359 2020-07-23 stsp path);
1464 bd6aa359 2020-07-23 stsp else
1465 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1466 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 bd6aa359 2020-07-23 stsp if (err)
1468 bd6aa359 2020-07-23 stsp goto done;
1469 bd6aa359 2020-07-23 stsp }
1470 d7b62c98 2018-12-27 stsp
1471 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1472 9d31a1d8 2018-03-11 stsp do {
1473 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1475 9d31a1d8 2018-03-11 stsp if (err)
1476 9d31a1d8 2018-03-11 stsp break;
1477 9d31a1d8 2018-03-11 stsp if (len > 0) {
1478 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1479 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1480 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1481 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1482 b87c6f83 2018-12-24 stsp goto done;
1483 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1484 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1485 b87c6f83 2018-12-24 stsp goto done;
1486 9d31a1d8 2018-03-11 stsp }
1487 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1488 9d31a1d8 2018-03-11 stsp }
1489 9d31a1d8 2018-03-11 stsp } while (len != 0);
1490 9d31a1d8 2018-03-11 stsp
1491 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1493 816dc654 2019-02-16 stsp goto done;
1494 816dc654 2019-02-16 stsp }
1495 9d31a1d8 2018-03-11 stsp
1496 507dc3bb 2018-12-29 stsp if (update) {
1497 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1499 f6d8c0ac 2020-11-09 stsp goto done;
1500 f6d8c0ac 2020-11-09 stsp }
1501 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1502 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1503 230a42bd 2019-05-11 jcs ondisk_path);
1504 68ed9ba5 2019-02-10 stsp goto done;
1505 68ed9ba5 2019-02-10 stsp }
1506 63df146d 2020-11-09 stsp free(tmppath);
1507 63df146d 2020-11-09 stsp tmppath = NULL;
1508 507dc3bb 2018-12-29 stsp }
1509 507dc3bb 2018-12-29 stsp
1510 9d31a1d8 2018-03-11 stsp done:
1511 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1513 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1515 507dc3bb 2018-12-29 stsp free(tmppath);
1516 6353ad76 2019-02-08 stsp return err;
1517 6353ad76 2019-02-08 stsp }
1518 6353ad76 2019-02-08 stsp
1519 12383673 2023-02-18 mark /*
1520 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1521 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1522 12383673 2023-02-18 mark */
1523 6353ad76 2019-02-08 stsp static const struct got_error *
1524 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1525 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1526 12383673 2023-02-18 mark FILE *ondisk_file)
1527 7154f6ce 2019-03-27 stsp {
1528 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1529 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1530 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1531 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1532 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1533 7154f6ce 2019-03-27 stsp };
1534 d952957d 2023-02-19 mark FILE *f1 = NULL;
1535 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1536 d952957d 2023-02-19 mark struct diff_result *r;
1537 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1538 9bdd68dd 2021-01-02 naddy char *line = NULL;
1539 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1540 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1541 7154f6ce 2019-03-27 stsp
1542 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1543 d952957d 2023-02-19 mark return NULL;
1544 12383673 2023-02-18 mark
1545 d952957d 2023-02-19 mark f1 = got_opentemp();
1546 d952957d 2023-02-19 mark if (f1 == NULL)
1547 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1548 12383673 2023-02-18 mark
1549 d952957d 2023-02-19 mark if (blob) {
1550 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1551 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1552 12383673 2023-02-18 mark if (err)
1553 12383673 2023-02-18 mark goto done;
1554 d952957d 2023-02-19 mark }
1555 12383673 2023-02-18 mark
1556 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1557 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1558 d952957d 2023-02-19 mark if (err)
1559 d952957d 2023-02-19 mark goto done;
1560 d952957d 2023-02-19 mark
1561 d952957d 2023-02-19 mark r = diffreg_result->result;
1562 d952957d 2023-02-19 mark
1563 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1564 d952957d 2023-02-19 mark struct diff_chunk *c;
1565 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1566 7783f73c 2023-02-20 mark off_t pos;
1567 d952957d 2023-02-19 mark
1568 d952957d 2023-02-19 mark /*
1569 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1570 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1571 d952957d 2023-02-19 mark */
1572 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1573 d952957d 2023-02-19 mark
1574 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1575 d952957d 2023-02-19 mark nchunks_parsed = 1;
1576 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1577 7154f6ce 2019-03-27 stsp }
1578 7154f6ce 2019-03-27 stsp
1579 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1580 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 7783f73c 2023-02-20 mark goto done;
1583 7154f6ce 2019-03-27 stsp }
1584 d952957d 2023-02-19 mark
1585 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 7783f73c 2023-02-20 mark ln = cc.right.start;
1587 7783f73c 2023-02-20 mark
1588 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1589 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1590 d952957d 2023-02-19 mark if (linelen == -1) {
1591 d952957d 2023-02-19 mark if (feof(ondisk_file))
1592 d952957d 2023-02-19 mark break;
1593 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 d952957d 2023-02-19 mark break;
1595 d952957d 2023-02-19 mark }
1596 d952957d 2023-02-19 mark
1597 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1598 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1599 d952957d 2023-02-19 mark if (strcmp(markers[i],
1600 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1602 d952957d 2023-02-19 mark goto done;
1603 d952957d 2023-02-19 mark } else
1604 d952957d 2023-02-19 mark i++;
1605 d952957d 2023-02-19 mark }
1606 d952957d 2023-02-19 mark ++ln;
1607 d952957d 2023-02-19 mark }
1608 7154f6ce 2019-03-27 stsp }
1609 12383673 2023-02-18 mark
1610 12383673 2023-02-18 mark done:
1611 9bdd68dd 2021-01-02 naddy free(line);
1612 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1614 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1615 d952957d 2023-02-19 mark if (err == NULL)
1616 d952957d 2023-02-19 mark err = free_err;
1617 7154f6ce 2019-03-27 stsp
1618 7154f6ce 2019-03-27 stsp return err;
1619 e2b1e152 2019-07-13 stsp }
1620 e2b1e152 2019-07-13 stsp
1621 e2b1e152 2019-07-13 stsp static int
1622 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1623 1ebedb77 2019-10-19 stsp {
1624 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1626 1ebedb77 2019-10-19 stsp }
1627 1ebedb77 2019-10-19 stsp
1628 1ebedb77 2019-10-19 stsp static int
1629 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1630 e2b1e152 2019-07-13 stsp {
1631 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1636 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1637 c363b2c1 2019-08-03 stsp }
1638 c363b2c1 2019-08-03 stsp
1639 c363b2c1 2019-08-03 stsp static unsigned char
1640 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1641 c363b2c1 2019-08-03 stsp {
1642 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1645 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1647 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1648 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1649 c363b2c1 2019-08-03 stsp default:
1650 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1651 a919d5c4 2020-07-23 stsp }
1652 a919d5c4 2020-07-23 stsp }
1653 a919d5c4 2020-07-23 stsp
1654 a919d5c4 2020-07-23 stsp static const struct got_error *
1655 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1656 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1657 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1658 a919d5c4 2020-07-23 stsp {
1659 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1660 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1661 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1662 a919d5c4 2020-07-23 stsp ssize_t elen;
1663 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1664 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1666 a919d5c4 2020-07-23 stsp
1667 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1668 a919d5c4 2020-07-23 stsp
1669 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1670 a919d5c4 2020-07-23 stsp do {
1671 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1672 a919d5c4 2020-07-23 stsp if (err)
1673 a919d5c4 2020-07-23 stsp return err;
1674 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1675 a919d5c4 2020-07-23 stsp /*
1676 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1677 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1678 a919d5c4 2020-07-23 stsp */
1679 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1680 a919d5c4 2020-07-23 stsp }
1681 a919d5c4 2020-07-23 stsp if (len > 0) {
1682 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1683 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1684 a919d5c4 2020-07-23 stsp len - hdrlen);
1685 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1686 a919d5c4 2020-07-23 stsp hdrlen = 0;
1687 a919d5c4 2020-07-23 stsp }
1688 a919d5c4 2020-07-23 stsp } while (len != 0);
1689 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1690 a919d5c4 2020-07-23 stsp
1691 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1692 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 a919d5c4 2020-07-23 stsp if (elen == -1)
1694 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1695 a919d5c4 2020-07-23 stsp } else {
1696 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1697 a919d5c4 2020-07-23 stsp if (elen == -1)
1698 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1699 c363b2c1 2019-08-03 stsp }
1700 a919d5c4 2020-07-23 stsp
1701 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1703 a919d5c4 2020-07-23 stsp
1704 a919d5c4 2020-07-23 stsp return NULL;
1705 7154f6ce 2019-03-27 stsp }
1706 7154f6ce 2019-03-27 stsp
1707 7154f6ce 2019-03-27 stsp static const struct got_error *
1708 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1709 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1710 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1711 6353ad76 2019-02-08 stsp {
1712 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1713 6353ad76 2019-02-08 stsp struct got_object_id id;
1714 6353ad76 2019-02-08 stsp size_t hdrlen;
1715 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1716 6353ad76 2019-02-08 stsp FILE *f = NULL;
1717 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1718 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1719 6353ad76 2019-02-08 stsp size_t flen, blen;
1720 2c4740ad 2023-02-12 mark unsigned char staged_status;
1721 6353ad76 2019-02-08 stsp
1722 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1723 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1724 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1725 6353ad76 2019-02-08 stsp
1726 7f91a133 2019-12-13 stsp /*
1727 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1728 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1729 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1730 7f91a133 2019-12-13 stsp */
1731 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1732 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1734 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1735 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1736 882ef1b9 2019-12-13 stsp else
1737 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1738 882ef1b9 2019-12-13 stsp goto done;
1739 882ef1b9 2019-12-13 stsp }
1740 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1741 3d35a492 2019-12-13 stsp goto done;
1742 3d35a492 2019-12-13 stsp }
1743 7f91a133 2019-12-13 stsp } else {
1744 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1746 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1747 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1748 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1750 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1751 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1753 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1754 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1755 3d35a492 2019-12-13 stsp else
1756 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1757 3d35a492 2019-12-13 stsp goto done;
1758 3d35a492 2019-12-13 stsp }
1759 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1760 1338848f 2019-12-13 stsp goto done;
1761 a378724f 2019-02-10 stsp }
1762 a378724f 2019-02-10 stsp }
1763 6353ad76 2019-02-08 stsp
1764 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1766 1338848f 2019-12-13 stsp goto done;
1767 3f148bc6 2019-07-27 stsp }
1768 efdd40df 2019-07-27 stsp
1769 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1771 1338848f 2019-12-13 stsp goto done;
1772 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1773 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1774 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1775 1338848f 2019-12-13 stsp goto done;
1776 d00136be 2019-03-26 stsp }
1777 b8f41171 2019-02-10 stsp
1778 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1779 f179e66d 2020-07-23 stsp goto done;
1780 f179e66d 2020-07-23 stsp
1781 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1782 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1784 1338848f 2019-12-13 stsp goto done;
1785 f179e66d 2020-07-23 stsp }
1786 6353ad76 2019-02-08 stsp
1787 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1788 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1789 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1790 c363b2c1 2019-08-03 stsp else
1791 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1792 c363b2c1 2019-08-03 stsp
1793 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1794 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1795 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1796 eb81bc23 2022-06-28 tracey goto done;
1797 eb81bc23 2022-06-28 tracey }
1798 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 6353ad76 2019-02-08 stsp if (err)
1800 1338848f 2019-12-13 stsp goto done;
1801 6353ad76 2019-02-08 stsp
1802 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1803 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1804 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1805 a919d5c4 2020-07-23 stsp goto done;
1806 a919d5c4 2020-07-23 stsp }
1807 a919d5c4 2020-07-23 stsp
1808 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1809 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 ab0d4361 2019-12-13 stsp if (fd == -1) {
1811 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1812 ab0d4361 2019-12-13 stsp goto done;
1813 ab0d4361 2019-12-13 stsp }
1814 3d35a492 2019-12-13 stsp }
1815 3d35a492 2019-12-13 stsp
1816 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1817 6353ad76 2019-02-08 stsp if (f == NULL) {
1818 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1819 6353ad76 2019-02-08 stsp goto done;
1820 6353ad76 2019-02-08 stsp }
1821 1338848f 2019-12-13 stsp fd = -1;
1822 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1823 656b1f76 2019-05-11 jcs for (;;) {
1824 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1826 6353ad76 2019-02-08 stsp if (err)
1827 7154f6ce 2019-03-27 stsp goto done;
1828 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1829 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1831 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1832 7154f6ce 2019-03-27 stsp goto done;
1833 80c5c120 2019-02-19 stsp }
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1835 6353ad76 2019-02-08 stsp if (flen != 0)
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1837 6353ad76 2019-02-08 stsp break;
1838 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1839 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1841 6353ad76 2019-02-08 stsp break;
1842 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1843 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1844 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1846 6353ad76 2019-02-08 stsp break;
1847 6353ad76 2019-02-08 stsp }
1848 6353ad76 2019-02-08 stsp } else {
1849 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1850 6353ad76 2019-02-08 stsp break;
1851 6353ad76 2019-02-08 stsp }
1852 6353ad76 2019-02-08 stsp hdrlen = 0;
1853 6353ad76 2019-02-08 stsp }
1854 7154f6ce 2019-03-27 stsp
1855 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1856 7154f6ce 2019-03-27 stsp rewind(f);
1857 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1858 12383673 2023-02-18 mark sb, f);
1859 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1860 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1861 6353ad76 2019-02-08 stsp done:
1862 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1864 6353ad76 2019-02-08 stsp if (blob)
1865 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1866 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1867 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1868 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1869 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1870 9d31a1d8 2018-03-11 stsp return err;
1871 e2b1e152 2019-07-13 stsp }
1872 e2b1e152 2019-07-13 stsp
1873 e2b1e152 2019-07-13 stsp /*
1874 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1875 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1876 e2b1e152 2019-07-13 stsp */
1877 e2b1e152 2019-07-13 stsp static const struct got_error *
1878 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1880 e2b1e152 2019-07-13 stsp {
1881 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1883 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1884 e2b1e152 2019-07-13 stsp
1885 e2b1e152 2019-07-13 stsp return NULL;
1886 9d31a1d8 2018-03-11 stsp }
1887 9d31a1d8 2018-03-11 stsp
1888 07fa9365 2023-03-10 stsp static const struct got_error *remove_ondisk_file(const char *, const char *);
1889 07fa9365 2023-03-10 stsp
1890 9d31a1d8 2018-03-11 stsp static const struct got_error *
1891 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1892 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1894 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 0584f854 2019-04-06 stsp void *progress_arg)
1896 9d31a1d8 2018-03-11 stsp {
1897 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1898 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1899 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1900 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1901 b8f41171 2019-02-10 stsp struct stat sb;
1902 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1903 9d31a1d8 2018-03-11 stsp
1904 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1906 6353ad76 2019-02-08 stsp
1907 abb4604f 2019-07-27 stsp if (ie) {
1908 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 a76c42e6 2019-08-03 stsp goto done;
1911 a76c42e6 2019-08-03 stsp }
1912 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1913 7f91a133 2019-12-13 stsp repo);
1914 abb4604f 2019-07-27 stsp if (err)
1915 abb4604f 2019-07-27 stsp goto done;
1916 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1918 c90c8ce3 2020-07-23 stsp } else {
1919 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1920 07fa9365 2023-03-10 stsp if (errno != ENOENT && errno != ENOTDIR) {
1921 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1922 f6764181 2021-09-24 stsp ondisk_path);
1923 f6764181 2021-09-24 stsp goto done;
1924 f6764181 2021-09-24 stsp }
1925 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1927 f6764181 2021-09-24 stsp } else {
1928 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1930 f6764181 2021-09-24 stsp else
1931 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1932 f6764181 2021-09-24 stsp }
1933 c90c8ce3 2020-07-23 stsp }
1934 6353ad76 2019-02-08 stsp
1935 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1936 2c41dce7 2021-06-27 stsp if (ie)
1937 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1938 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1939 b8f41171 2019-02-10 stsp goto done;
1940 b8f41171 2019-02-10 stsp }
1941 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1942 a769b60b 2021-06-27 stsp if (ie)
1943 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1944 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1945 5036ab18 2020-04-18 stsp path);
1946 5036ab18 2020-04-18 stsp goto done;
1947 07fa9365 2023-03-10 stsp }
1948 07fa9365 2023-03-10 stsp
1949 07fa9365 2023-03-10 stsp if (S_ISDIR(te->mode)) { /* file changing into a directory */
1950 07fa9365 2023-03-10 stsp if (status == GOT_STATUS_UNVERSIONED) {
1951 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, status, path);
1952 07fa9365 2023-03-10 stsp } else if (status != GOT_STATUS_NO_CHANGE &&
1953 07fa9365 2023-03-10 stsp status != GOT_STATUS_DELETE &&
1954 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1955 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1956 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg,
1957 07fa9365 2023-03-10 stsp GOT_STATUS_CANNOT_DELETE, path);
1958 07fa9365 2023-03-10 stsp } else if (ie) {
1959 07fa9365 2023-03-10 stsp if (status != GOT_STATUS_DELETE &&
1960 07fa9365 2023-03-10 stsp status != GOT_STATUS_NONEXISTENT &&
1961 07fa9365 2023-03-10 stsp status != GOT_STATUS_MISSING) {
1962 07fa9365 2023-03-10 stsp err = remove_ondisk_file(worktree->root_path,
1963 07fa9365 2023-03-10 stsp ie->path);
1964 07fa9365 2023-03-10 stsp if (err && !(err->code == GOT_ERR_ERRNO &&
1965 07fa9365 2023-03-10 stsp errno == ENOENT))
1966 07fa9365 2023-03-10 stsp goto done;
1967 07fa9365 2023-03-10 stsp }
1968 07fa9365 2023-03-10 stsp got_fileindex_entry_remove(fileindex, ie);
1969 07fa9365 2023-03-10 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1970 07fa9365 2023-03-10 stsp ie->path);
1971 07fa9365 2023-03-10 stsp }
1972 07fa9365 2023-03-10 stsp goto done; /* nothing else to do */
1973 5036ab18 2020-04-18 stsp }
1974 b8f41171 2019-02-10 stsp
1975 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1977 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1978 c6e8a826 2021-04-05 stsp /*
1979 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1980 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1981 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1982 c6e8a826 2021-04-05 stsp * updating contents of this file.
1983 c6e8a826 2021-04-05 stsp */
1984 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1985 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1987 c6e8a826 2021-04-05 stsp /* Same commit. */
1988 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1989 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1990 e2b1e152 2019-07-13 stsp if (err)
1991 e2b1e152 2019-07-13 stsp goto done;
1992 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 b8f41171 2019-02-10 stsp path);
1994 6353ad76 2019-02-08 stsp goto done;
1995 a378724f 2019-02-10 stsp }
1996 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1997 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1998 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1999 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2000 fd785a9a 2023-04-12 stsp if (got_fileindex_entry_has_commit(ie)) {
2001 fd785a9a 2023-04-12 stsp /* Update the base commit ID of this file. */
2002 fd785a9a 2023-04-12 stsp memcpy(ie->commit_sha1,
2003 fd785a9a 2023-04-12 stsp worktree->base_commit_id->sha1,
2004 fd785a9a 2023-04-12 stsp sizeof(ie->commit_sha1));
2005 fd785a9a 2023-04-12 stsp }
2006 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2007 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2008 0f58026f 2021-04-05 stsp if (err)
2009 0f58026f 2021-04-05 stsp goto done;
2010 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2011 0f58026f 2021-04-05 stsp path);
2012 b8f41171 2019-02-10 stsp goto done;
2013 e2b1e152 2019-07-13 stsp }
2014 9d31a1d8 2018-03-11 stsp }
2015 9d31a1d8 2018-03-11 stsp
2016 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
2017 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
2018 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2019 eb81bc23 2022-06-28 tracey goto done;
2020 eb81bc23 2022-06-28 tracey }
2021 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2022 8da9e5f4 2019-01-12 stsp if (err)
2023 6353ad76 2019-02-08 stsp goto done;
2024 512f0d0e 2019-01-02 stsp
2025 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2026 234035bc 2019-06-01 stsp int update_timestamps;
2027 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2028 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2029 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2030 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2031 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2032 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2033 eb81bc23 2022-06-28 tracey goto done;
2034 eb81bc23 2022-06-28 tracey }
2035 234035bc 2019-06-01 stsp struct got_object_id id2;
2036 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2037 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2038 eb81bc23 2022-06-28 tracey fd2);
2039 234035bc 2019-06-01 stsp if (err)
2040 f69721c3 2019-10-21 stsp goto done;
2041 f69721c3 2019-10-21 stsp }
2042 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2043 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2044 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2045 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2046 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2047 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2048 f69721c3 2019-10-21 stsp goto done;
2049 f69721c3 2019-10-21 stsp }
2050 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2051 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2052 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2053 234035bc 2019-06-01 stsp goto done;
2054 f69721c3 2019-10-21 stsp }
2055 234035bc 2019-06-01 stsp }
2056 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2057 36bf999c 2020-07-23 stsp char *link_target;
2058 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2059 36bf999c 2020-07-23 stsp if (err)
2060 36bf999c 2020-07-23 stsp goto done;
2061 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2062 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2063 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2064 36bf999c 2020-07-23 stsp free(link_target);
2065 993e2a1b 2020-07-23 stsp } else {
2066 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2067 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2068 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2069 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2070 993e2a1b 2020-07-23 stsp }
2071 f69721c3 2019-10-21 stsp free(label_orig);
2072 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2073 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2074 eb81bc23 2022-06-28 tracey goto done;
2075 eb81bc23 2022-06-28 tracey }
2076 234035bc 2019-06-01 stsp if (blob2)
2077 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2078 909d120e 2019-09-22 stsp if (err)
2079 909d120e 2019-09-22 stsp goto done;
2080 234035bc 2019-06-01 stsp /*
2081 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2082 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2083 234035bc 2019-06-01 stsp * unmodified files again.
2084 234035bc 2019-06-01 stsp */
2085 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2086 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2087 234035bc 2019-06-01 stsp update_timestamps);
2088 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2089 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2090 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2091 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2092 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2093 1ee397ad 2019-07-12 stsp if (err)
2094 1ee397ad 2019-07-12 stsp goto done;
2095 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2096 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2097 13d9040b 2019-03-27 stsp if (err)
2098 13d9040b 2019-03-27 stsp goto done;
2099 13d9040b 2019-03-27 stsp } else {
2100 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2101 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2102 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2103 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2104 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2105 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2106 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2107 2e1fa222 2020-07-23 stsp } else {
2108 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2109 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2110 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2111 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2112 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2113 2e1fa222 2020-07-23 stsp }
2114 13d9040b 2019-03-27 stsp if (err)
2115 13d9040b 2019-03-27 stsp goto done;
2116 65b05cec 2020-07-23 stsp
2117 054041d0 2020-06-24 stsp if (ie) {
2118 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2119 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2120 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2121 054041d0 2020-06-24 stsp } else {
2122 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2123 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2124 054041d0 2020-06-24 stsp &blob->id);
2125 054041d0 2020-06-24 stsp }
2126 13d9040b 2019-03-27 stsp if (err)
2127 13d9040b 2019-03-27 stsp goto done;
2128 65b05cec 2020-07-23 stsp
2129 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2130 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2131 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2132 2e1fa222 2020-07-23 stsp }
2133 13d9040b 2019-03-27 stsp }
2134 eb81bc23 2022-06-28 tracey
2135 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2136 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2137 eb81bc23 2022-06-28 tracey goto done;
2138 eb81bc23 2022-06-28 tracey }
2139 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2140 6353ad76 2019-02-08 stsp done:
2141 6353ad76 2019-02-08 stsp free(ondisk_path);
2142 8da9e5f4 2019-01-12 stsp return err;
2143 90285c3b 2019-01-08 stsp }
2144 90285c3b 2019-01-08 stsp
2145 90285c3b 2019-01-08 stsp static const struct got_error *
2146 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2147 90285c3b 2019-01-08 stsp {
2148 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2149 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2150 90285c3b 2019-01-08 stsp
2151 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2152 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2153 90285c3b 2019-01-08 stsp
2154 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2155 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2156 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2157 c97665ae 2019-01-13 stsp } else {
2158 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2159 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2160 1c4cdd89 2021-06-20 stsp if (err)
2161 1c4cdd89 2021-06-20 stsp goto done;
2162 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2163 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2164 fddefe3b 2020-10-20 stsp free(ondisk_path);
2165 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2166 1c4cdd89 2021-06-20 stsp parent = NULL;
2167 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2168 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2169 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2170 fddefe3b 2020-10-20 stsp ondisk_path);
2171 90285c3b 2019-01-08 stsp break;
2172 90285c3b 2019-01-08 stsp }
2173 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2174 1c4cdd89 2021-06-20 stsp if (err)
2175 1c4cdd89 2021-06-20 stsp break;
2176 1c4cdd89 2021-06-20 stsp }
2177 90285c3b 2019-01-08 stsp }
2178 1c4cdd89 2021-06-20 stsp done:
2179 90285c3b 2019-01-08 stsp free(ondisk_path);
2180 1c4cdd89 2021-06-20 stsp free(parent);
2181 90285c3b 2019-01-08 stsp return err;
2182 512f0d0e 2019-01-02 stsp }
2183 512f0d0e 2019-01-02 stsp
2184 708d8e67 2019-03-27 stsp static const struct got_error *
2185 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2186 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2187 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2188 708d8e67 2019-03-27 stsp {
2189 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2190 708d8e67 2019-03-27 stsp unsigned char status;
2191 708d8e67 2019-03-27 stsp struct stat sb;
2192 708d8e67 2019-03-27 stsp char *ondisk_path;
2193 708d8e67 2019-03-27 stsp
2194 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2195 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2196 a76c42e6 2019-08-03 stsp
2197 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2198 708d8e67 2019-03-27 stsp == -1)
2199 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2200 708d8e67 2019-03-27 stsp
2201 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2202 708d8e67 2019-03-27 stsp if (err)
2203 5a58a424 2020-06-23 stsp goto done;
2204 708d8e67 2019-03-27 stsp
2205 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2206 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2207 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2208 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2209 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2210 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2211 993e2a1b 2020-07-23 stsp goto done;
2212 993e2a1b 2020-07-23 stsp }
2213 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2214 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2215 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2216 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2217 993e2a1b 2020-07-23 stsp if (err)
2218 993e2a1b 2020-07-23 stsp goto done;
2219 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2220 993e2a1b 2020-07-23 stsp ie->path);
2221 993e2a1b 2020-07-23 stsp goto done;
2222 993e2a1b 2020-07-23 stsp }
2223 993e2a1b 2020-07-23 stsp
2224 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2225 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2226 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2227 1ee397ad 2019-07-12 stsp if (err)
2228 5a58a424 2020-06-23 stsp goto done;
2229 fc6346c4 2019-03-27 stsp /*
2230 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2231 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2232 fc6346c4 2019-03-27 stsp */
2233 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2234 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2235 fc6346c4 2019-03-27 stsp } else {
2236 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2237 1ee397ad 2019-07-12 stsp if (err)
2238 5a58a424 2020-06-23 stsp goto done;
2239 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2240 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2241 fc6346c4 2019-03-27 stsp if (err)
2242 5a58a424 2020-06-23 stsp goto done;
2243 fc6346c4 2019-03-27 stsp }
2244 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2245 708d8e67 2019-03-27 stsp }
2246 5a58a424 2020-06-23 stsp done:
2247 5a58a424 2020-06-23 stsp free(ondisk_path);
2248 fc6346c4 2019-03-27 stsp return err;
2249 708d8e67 2019-03-27 stsp }
2250 708d8e67 2019-03-27 stsp
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2252 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2253 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2254 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2255 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2256 8da9e5f4 2019-01-12 stsp void *progress_arg;
2257 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2258 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2259 8da9e5f4 2019-01-12 stsp };
2260 8da9e5f4 2019-01-12 stsp
2261 512f0d0e 2019-01-02 stsp static const struct got_error *
2262 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2263 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2264 512f0d0e 2019-01-02 stsp {
2265 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2266 0584f854 2019-04-06 stsp
2267 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2269 512f0d0e 2019-01-02 stsp
2270 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2271 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2272 8da9e5f4 2019-01-12 stsp }
2273 512f0d0e 2019-01-02 stsp
2274 8da9e5f4 2019-01-12 stsp static const struct got_error *
2275 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2276 8da9e5f4 2019-01-12 stsp {
2277 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2278 7a9df742 2019-01-08 stsp
2279 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2280 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2281 0584f854 2019-04-06 stsp
2282 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2283 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2284 9d31a1d8 2018-03-11 stsp }
2285 9d31a1d8 2018-03-11 stsp
2286 9d31a1d8 2018-03-11 stsp static const struct got_error *
2287 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2288 9d31a1d8 2018-03-11 stsp {
2289 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2290 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2291 8da9e5f4 2019-01-12 stsp char *path;
2292 9d31a1d8 2018-03-11 stsp
2293 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2294 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2295 0584f854 2019-04-06 stsp
2296 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2297 63c5ca5d 2019-08-24 stsp return NULL;
2298 63c5ca5d 2019-08-24 stsp
2299 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2300 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2301 8da9e5f4 2019-01-12 stsp == -1)
2302 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2303 9d31a1d8 2018-03-11 stsp
2304 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2305 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2306 8da9e5f4 2019-01-12 stsp else
2307 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2308 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2309 9d31a1d8 2018-03-11 stsp
2310 8da9e5f4 2019-01-12 stsp free(path);
2311 0cd1c46a 2019-03-11 stsp return err;
2312 0cd1c46a 2019-03-11 stsp }
2313 0cd1c46a 2019-03-11 stsp
2314 b2118c49 2020-07-28 stsp const struct got_error *
2315 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2316 b2118c49 2020-07-28 stsp {
2317 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2318 b2118c49 2020-07-28 stsp
2319 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2320 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2321 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2322 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2323 b2118c49 2020-07-28 stsp }
2324 b2118c49 2020-07-28 stsp
2325 b2118c49 2020-07-28 stsp return NULL;
2326 b2118c49 2020-07-28 stsp }
2327 b2118c49 2020-07-28 stsp
2328 818c7501 2019-07-11 stsp static const struct got_error *
2329 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2330 0cd1c46a 2019-03-11 stsp {
2331 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2332 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2333 0cd1c46a 2019-03-11 stsp
2334 517bab73 2019-03-11 stsp *refname = NULL;
2335 517bab73 2019-03-11 stsp
2336 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2337 b2118c49 2020-07-28 stsp if (err)
2338 b2118c49 2020-07-28 stsp return err;
2339 0cd1c46a 2019-03-11 stsp
2340 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2341 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2342 0647c563 2019-03-11 stsp *refname = NULL;
2343 0cd1c46a 2019-03-11 stsp }
2344 517bab73 2019-03-11 stsp free(uuidstr);
2345 517bab73 2019-03-11 stsp return err;
2346 517bab73 2019-03-11 stsp }
2347 517bab73 2019-03-11 stsp
2348 818c7501 2019-07-11 stsp const struct got_error *
2349 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2350 9587e6cc 2023-01-28 mark const char *prefix)
2351 9587e6cc 2023-01-28 mark {
2352 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2353 9587e6cc 2023-01-28 mark }
2354 9587e6cc 2023-01-28 mark
2355 9587e6cc 2023-01-28 mark const struct got_error *
2356 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2357 818c7501 2019-07-11 stsp {
2358 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2359 818c7501 2019-07-11 stsp }
2360 818c7501 2019-07-11 stsp
2361 818c7501 2019-07-11 stsp static const struct got_error *
2362 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2363 818c7501 2019-07-11 stsp {
2364 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2365 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2366 818c7501 2019-07-11 stsp }
2367 818c7501 2019-07-11 stsp
2368 818c7501 2019-07-11 stsp static const struct got_error *
2369 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2370 818c7501 2019-07-11 stsp {
2371 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2372 818c7501 2019-07-11 stsp }
2373 818c7501 2019-07-11 stsp
2374 818c7501 2019-07-11 stsp static const struct got_error *
2375 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2376 818c7501 2019-07-11 stsp {
2377 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2378 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2379 818c7501 2019-07-11 stsp }
2380 818c7501 2019-07-11 stsp
2381 818c7501 2019-07-11 stsp static const struct got_error *
2382 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2383 818c7501 2019-07-11 stsp {
2384 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2385 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2386 0ebf8283 2019-07-24 stsp }
2387 0ebf8283 2019-07-24 stsp
2388 0ebf8283 2019-07-24 stsp static const struct got_error *
2389 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2390 0ebf8283 2019-07-24 stsp {
2391 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2392 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2393 0ebf8283 2019-07-24 stsp }
2394 0ebf8283 2019-07-24 stsp
2395 0ebf8283 2019-07-24 stsp static const struct got_error *
2396 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2397 0ebf8283 2019-07-24 stsp {
2398 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2399 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2400 0ebf8283 2019-07-24 stsp }
2401 0ebf8283 2019-07-24 stsp
2402 0ebf8283 2019-07-24 stsp static const struct got_error *
2403 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 0ebf8283 2019-07-24 stsp {
2405 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2406 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2407 818c7501 2019-07-11 stsp }
2408 818c7501 2019-07-11 stsp
2409 0ebf8283 2019-07-24 stsp static const struct got_error *
2410 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2411 0ebf8283 2019-07-24 stsp {
2412 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2413 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2414 0ebf8283 2019-07-24 stsp }
2415 818c7501 2019-07-11 stsp
2416 0ebf8283 2019-07-24 stsp const struct got_error *
2417 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2418 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2419 0ebf8283 2019-07-24 stsp {
2420 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2421 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2422 0ebf8283 2019-07-24 stsp *path = NULL;
2423 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2424 0ebf8283 2019-07-24 stsp }
2425 0ebf8283 2019-07-24 stsp return NULL;
2426 f259c4c1 2021-09-24 stsp }
2427 f259c4c1 2021-09-24 stsp
2428 f259c4c1 2021-09-24 stsp static const struct got_error *
2429 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2430 f259c4c1 2021-09-24 stsp {
2431 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2432 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2433 0ebf8283 2019-07-24 stsp }
2434 0ebf8283 2019-07-24 stsp
2435 f259c4c1 2021-09-24 stsp static const struct got_error *
2436 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2437 f259c4c1 2021-09-24 stsp {
2438 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2439 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2440 f259c4c1 2021-09-24 stsp }
2441 f259c4c1 2021-09-24 stsp
2442 517bab73 2019-03-11 stsp /*
2443 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2444 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2445 517bab73 2019-03-11 stsp */
2446 517bab73 2019-03-11 stsp static const struct got_error *
2447 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2448 517bab73 2019-03-11 stsp {
2449 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2450 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2451 517bab73 2019-03-11 stsp char *refname;
2452 517bab73 2019-03-11 stsp
2453 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2454 517bab73 2019-03-11 stsp if (err)
2455 517bab73 2019-03-11 stsp return err;
2456 0cd1c46a 2019-03-11 stsp
2457 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2458 0cd1c46a 2019-03-11 stsp if (err)
2459 0cd1c46a 2019-03-11 stsp goto done;
2460 0cd1c46a 2019-03-11 stsp
2461 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2462 0cd1c46a 2019-03-11 stsp done:
2463 0cd1c46a 2019-03-11 stsp free(refname);
2464 0cd1c46a 2019-03-11 stsp if (ref)
2465 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2466 3e3a69f1 2019-07-25 stsp return err;
2467 3e3a69f1 2019-07-25 stsp }
2468 3e3a69f1 2019-07-25 stsp
2469 3e3a69f1 2019-07-25 stsp static const struct got_error *
2470 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2471 3e3a69f1 2019-07-25 stsp {
2472 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2473 3e3a69f1 2019-07-25 stsp
2474 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2475 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2476 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2477 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2478 3e3a69f1 2019-07-25 stsp }
2479 9d31a1d8 2018-03-11 stsp return err;
2480 9d31a1d8 2018-03-11 stsp }
2481 9d31a1d8 2018-03-11 stsp
2482 3e3a69f1 2019-07-25 stsp
2483 ebf99748 2019-05-09 stsp static const struct got_error *
2484 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2485 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2486 ebf99748 2019-05-09 stsp {
2487 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2488 ebf99748 2019-05-09 stsp FILE *index = NULL;
2489 0cd1c46a 2019-03-11 stsp
2490 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2491 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2492 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2493 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2494 ebf99748 2019-05-09 stsp
2495 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2496 3e3a69f1 2019-07-25 stsp if (err)
2497 ebf99748 2019-05-09 stsp goto done;
2498 ebf99748 2019-05-09 stsp
2499 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2500 ebf99748 2019-05-09 stsp if (index == NULL) {
2501 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2502 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2503 ebf99748 2019-05-09 stsp } else {
2504 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2505 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2506 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2507 ebf99748 2019-05-09 stsp }
2508 ebf99748 2019-05-09 stsp done:
2509 ebf99748 2019-05-09 stsp if (err) {
2510 ebf99748 2019-05-09 stsp free(*fileindex_path);
2511 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2512 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2513 ebf99748 2019-05-09 stsp *fileindex = NULL;
2514 ebf99748 2019-05-09 stsp }
2515 ebf99748 2019-05-09 stsp return err;
2516 ebf99748 2019-05-09 stsp }
2517 c932eeeb 2019-05-22 stsp
2518 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2519 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2520 c932eeeb 2019-05-22 stsp const char *path;
2521 c932eeeb 2019-05-22 stsp size_t path_len;
2522 c932eeeb 2019-05-22 stsp const char *entry_name;
2523 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2524 a484d721 2019-06-10 stsp void *progress_arg;
2525 c932eeeb 2019-05-22 stsp };
2526 ebf99748 2019-05-09 stsp
2527 c932eeeb 2019-05-22 stsp static const struct got_error *
2528 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2529 c932eeeb 2019-05-22 stsp {
2530 1ee397ad 2019-07-12 stsp const struct got_error *err;
2531 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2532 c932eeeb 2019-05-22 stsp
2533 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2534 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2535 c932eeeb 2019-05-22 stsp return NULL;
2536 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2537 102ff934 2019-06-11 stsp return NULL;
2538 102ff934 2019-06-11 stsp
2539 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2540 a769b60b 2021-06-27 stsp return NULL;
2541 a769b60b 2021-06-27 stsp
2542 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2543 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2544 c932eeeb 2019-05-22 stsp return NULL;
2545 c932eeeb 2019-05-22 stsp
2546 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2547 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2548 edd02c5e 2019-07-11 stsp ie->path);
2549 1ee397ad 2019-07-12 stsp if (err)
2550 1ee397ad 2019-07-12 stsp return err;
2551 1ee397ad 2019-07-12 stsp }
2552 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2553 c932eeeb 2019-05-22 stsp return NULL;
2554 a615e0e7 2020-12-16 stsp }
2555 a615e0e7 2020-12-16 stsp
2556 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2557 a615e0e7 2020-12-16 stsp static const struct got_error *
2558 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2559 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2560 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2561 a615e0e7 2020-12-16 stsp {
2562 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2563 a615e0e7 2020-12-16 stsp
2564 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2565 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2566 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2567 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2568 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2569 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2570 a615e0e7 2020-12-16 stsp
2571 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2572 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2573 9c6338c4 2019-06-02 stsp }
2574 9c6338c4 2019-06-02 stsp
2575 9c6338c4 2019-06-02 stsp static const struct got_error *
2576 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2577 9c6338c4 2019-06-02 stsp {
2578 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2579 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2580 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2581 867630bb 2020-01-17 stsp struct timespec timeout;
2582 9c6338c4 2019-06-02 stsp
2583 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2584 b90054ed 2022-11-01 stsp fileindex_path, "");
2585 9c6338c4 2019-06-02 stsp if (err)
2586 9c6338c4 2019-06-02 stsp goto done;
2587 9c6338c4 2019-06-02 stsp
2588 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2589 9c6338c4 2019-06-02 stsp if (err)
2590 9c6338c4 2019-06-02 stsp goto done;
2591 9c6338c4 2019-06-02 stsp
2592 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2593 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2594 9c6338c4 2019-06-02 stsp fileindex_path);
2595 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2596 9c6338c4 2019-06-02 stsp }
2597 867630bb 2020-01-17 stsp
2598 867630bb 2020-01-17 stsp /*
2599 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2600 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2601 867630bb 2020-01-17 stsp * was recorded in the file index.
2602 867630bb 2020-01-17 stsp */
2603 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2604 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2605 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2606 9c6338c4 2019-06-02 stsp done:
2607 9c6338c4 2019-06-02 stsp if (new_index)
2608 9c6338c4 2019-06-02 stsp fclose(new_index);
2609 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2610 9c6338c4 2019-06-02 stsp return err;
2611 c932eeeb 2019-05-22 stsp }
2612 6ced4176 2019-07-12 stsp
2613 6ced4176 2019-07-12 stsp static const struct got_error *
2614 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2615 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2616 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2617 a44927cc 2022-04-07 stsp struct got_repository *repo)
2618 6ced4176 2019-07-12 stsp {
2619 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2620 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2621 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2622 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2623 6ced4176 2019-07-12 stsp
2624 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2625 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2626 6ced4176 2019-07-12 stsp *tree_id = NULL;
2627 6ced4176 2019-07-12 stsp
2628 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2629 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2630 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2631 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2632 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2633 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2634 6ced4176 2019-07-12 stsp goto done;
2635 6ced4176 2019-07-12 stsp }
2636 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2637 a44927cc 2022-04-07 stsp worktree->path_prefix);
2638 6ced4176 2019-07-12 stsp if (err)
2639 6ced4176 2019-07-12 stsp goto done;
2640 6ced4176 2019-07-12 stsp return NULL;
2641 6ced4176 2019-07-12 stsp }
2642 6ced4176 2019-07-12 stsp
2643 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2644 6ced4176 2019-07-12 stsp
2645 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2646 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2647 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2648 6ced4176 2019-07-12 stsp goto done;
2649 6ced4176 2019-07-12 stsp }
2650 6ced4176 2019-07-12 stsp
2651 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2652 6ced4176 2019-07-12 stsp if (err)
2653 6ced4176 2019-07-12 stsp goto done;
2654 6ced4176 2019-07-12 stsp
2655 6ced4176 2019-07-12 stsp free(in_repo_path);
2656 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2657 6ced4176 2019-07-12 stsp
2658 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2659 6ced4176 2019-07-12 stsp if (err)
2660 6ced4176 2019-07-12 stsp goto done;
2661 c932eeeb 2019-05-22 stsp
2662 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2663 6ced4176 2019-07-12 stsp /* Check out a single file. */
2664 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2665 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2666 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2667 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2668 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2669 6ced4176 2019-07-12 stsp goto done;
2670 6ced4176 2019-07-12 stsp }
2671 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2672 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2673 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2674 6ced4176 2019-07-12 stsp goto done;
2675 6ced4176 2019-07-12 stsp }
2676 6ced4176 2019-07-12 stsp } else {
2677 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2678 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2679 6ced4176 2019-07-12 stsp if (err)
2680 6ced4176 2019-07-12 stsp return err;
2681 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2682 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2683 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2684 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2685 6ced4176 2019-07-12 stsp goto done;
2686 6ced4176 2019-07-12 stsp }
2687 6ced4176 2019-07-12 stsp }
2688 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2689 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2690 6ced4176 2019-07-12 stsp } else {
2691 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2692 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2693 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2694 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2695 6ced4176 2019-07-12 stsp goto done;
2696 6ced4176 2019-07-12 stsp }
2697 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2698 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2699 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2700 6ced4176 2019-07-12 stsp goto done;
2701 6ced4176 2019-07-12 stsp }
2702 6ced4176 2019-07-12 stsp }
2703 6ced4176 2019-07-12 stsp done:
2704 6ced4176 2019-07-12 stsp free(id);
2705 6ced4176 2019-07-12 stsp free(in_repo_path);
2706 6ced4176 2019-07-12 stsp if (err) {
2707 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2708 6ced4176 2019-07-12 stsp free(*tree_relpath);
2709 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2710 6ced4176 2019-07-12 stsp free(*tree_id);
2711 6ced4176 2019-07-12 stsp *tree_id = NULL;
2712 6ced4176 2019-07-12 stsp }
2713 07ed472d 2019-07-12 stsp return err;
2714 07ed472d 2019-07-12 stsp }
2715 07ed472d 2019-07-12 stsp
2716 07ed472d 2019-07-12 stsp static const struct got_error *
2717 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2718 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2719 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2720 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2721 07ed472d 2019-07-12 stsp {
2722 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2723 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2724 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2725 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2726 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2727 07ed472d 2019-07-12 stsp
2728 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2729 7f47418f 2019-12-20 stsp if (err) {
2730 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2731 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2732 7f47418f 2019-12-20 stsp goto done;
2733 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2734 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2735 7f47418f 2019-12-20 stsp if (err)
2736 7f47418f 2019-12-20 stsp return err;
2737 7f47418f 2019-12-20 stsp }
2738 07ed472d 2019-07-12 stsp
2739 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2740 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2741 07ed472d 2019-07-12 stsp if (err)
2742 07ed472d 2019-07-12 stsp goto done;
2743 07ed472d 2019-07-12 stsp
2744 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2745 07ed472d 2019-07-12 stsp if (err)
2746 07ed472d 2019-07-12 stsp goto done;
2747 07ed472d 2019-07-12 stsp
2748 07ed472d 2019-07-12 stsp if (entry_name &&
2749 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2750 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2751 07ed472d 2019-07-12 stsp goto done;
2752 07ed472d 2019-07-12 stsp }
2753 07ed472d 2019-07-12 stsp
2754 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2755 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2756 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2757 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2758 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2759 07ed472d 2019-07-12 stsp arg.repo = repo;
2760 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2761 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2762 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2763 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2764 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2765 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2766 07ed472d 2019-07-12 stsp done:
2767 07ed472d 2019-07-12 stsp if (tree)
2768 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2769 07ed472d 2019-07-12 stsp if (commit)
2770 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2771 6ced4176 2019-07-12 stsp return err;
2772 6ced4176 2019-07-12 stsp }
2773 6ced4176 2019-07-12 stsp
2774 9d31a1d8 2018-03-11 stsp const struct got_error *
2775 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2776 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2777 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2778 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2779 9d31a1d8 2018-03-11 stsp {
2780 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2781 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2782 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2783 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2784 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2785 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2786 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2787 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2788 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2789 f2ea84fa 2019-07-27 stsp int entry_type;
2790 f2ea84fa 2019-07-27 stsp char *relpath;
2791 f2ea84fa 2019-07-27 stsp char *entry_name;
2792 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2793 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2794 9d31a1d8 2018-03-11 stsp
2795 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2796 f2ea84fa 2019-07-27 stsp
2797 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2798 9d31a1d8 2018-03-11 stsp if (err)
2799 9d31a1d8 2018-03-11 stsp return err;
2800 a44927cc 2022-04-07 stsp
2801 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2802 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2803 a44927cc 2022-04-07 stsp if (err)
2804 a44927cc 2022-04-07 stsp goto done;
2805 93a30277 2018-12-24 stsp
2806 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2808 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2809 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2810 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2811 f2ea84fa 2019-07-27 stsp goto done;
2812 f2ea84fa 2019-07-27 stsp }
2813 6ced4176 2019-07-12 stsp
2814 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2815 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2816 a44927cc 2022-04-07 stsp worktree, repo);
2817 f2ea84fa 2019-07-27 stsp if (err) {
2818 f2ea84fa 2019-07-27 stsp free(tpd);
2819 6ced4176 2019-07-12 stsp goto done;
2820 6ced4176 2019-07-12 stsp }
2821 f2ea84fa 2019-07-27 stsp
2822 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2823 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2824 f2ea84fa 2019-07-27 stsp if (err) {
2825 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2826 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2827 f2ea84fa 2019-07-27 stsp free(tpd);
2828 f2ea84fa 2019-07-27 stsp goto done;
2829 f2ea84fa 2019-07-27 stsp }
2830 f2ea84fa 2019-07-27 stsp } else
2831 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2832 f2ea84fa 2019-07-27 stsp
2833 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2834 6ced4176 2019-07-12 stsp }
2835 6ced4176 2019-07-12 stsp
2836 51514078 2018-12-25 stsp /*
2837 51514078 2018-12-25 stsp * Read the file index.
2838 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2839 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2840 51514078 2018-12-25 stsp */
2841 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2842 8da9e5f4 2019-01-12 stsp if (err)
2843 c4cdcb68 2019-04-03 stsp goto done;
2844 c4cdcb68 2019-04-03 stsp
2845 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2846 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2847 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2848 f2ea84fa 2019-07-27 stsp
2849 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2850 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2851 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2852 f2ea84fa 2019-07-27 stsp if (err)
2853 f2ea84fa 2019-07-27 stsp break;
2854 f2ea84fa 2019-07-27 stsp
2855 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2856 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2857 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2858 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2859 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2860 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2861 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2862 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2863 f2ea84fa 2019-07-27 stsp if (err)
2864 f2ea84fa 2019-07-27 stsp break;
2865 f2ea84fa 2019-07-27 stsp
2866 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2867 711d9cd9 2019-07-12 stsp }
2868 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2869 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2870 af12c6b9 2019-06-04 stsp err = sync_err;
2871 9d31a1d8 2018-03-11 stsp done:
2872 9c6338c4 2019-06-02 stsp free(fileindex_path);
2873 edfa7d7f 2018-09-11 stsp if (tree)
2874 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2875 9d31a1d8 2018-03-11 stsp if (commit)
2876 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2877 5ade8233 2019-07-12 stsp if (fileindex)
2878 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2879 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2880 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2881 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2882 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2883 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2884 f2ea84fa 2019-07-27 stsp free(tpd);
2885 f2ea84fa 2019-07-27 stsp }
2886 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2887 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2888 9d31a1d8 2018-03-11 stsp err = unlockerr;
2889 f8d1f275 2019-02-04 stsp return err;
2890 f8d1f275 2019-02-04 stsp }
2891 f8d1f275 2019-02-04 stsp
2892 9a298e5c 2023-03-10 stsp static const struct got_error *
2893 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2894 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2895 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2896 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2897 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2898 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2899 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2900 9a298e5c 2023-03-10 stsp {
2901 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2902 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2903 9a298e5c 2023-03-10 stsp
2904 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2905 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2906 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2907 9a298e5c 2023-03-10 stsp restoring_missing_file,
2908 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2909 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2910 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2911 9a298e5c 2023-03-10 stsp } else {
2912 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2913 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2914 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2915 9a298e5c 2023-03-10 stsp path_is_unversioned, repo, progress_cb, progress_arg);
2916 9a298e5c 2023-03-10 stsp }
2917 9a298e5c 2023-03-10 stsp if (err)
2918 9a298e5c 2023-03-10 stsp return err;
2919 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2920 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2921 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2922 9a298e5c 2023-03-10 stsp if (err)
2923 9a298e5c 2023-03-10 stsp return err;
2924 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2925 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2926 9a298e5c 2023-03-10 stsp if (err) {
2927 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2928 9a298e5c 2023-03-10 stsp return err;
2929 9a298e5c 2023-03-10 stsp }
2930 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2931 9a298e5c 2023-03-10 stsp if (err) {
2932 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2933 9a298e5c 2023-03-10 stsp return err;
2934 9a298e5c 2023-03-10 stsp }
2935 9a298e5c 2023-03-10 stsp } else {
2936 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2937 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2938 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2939 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2940 9a298e5c 2023-03-10 stsp if (err)
2941 9a298e5c 2023-03-10 stsp return err;
2942 9a298e5c 2023-03-10 stsp }
2943 9a298e5c 2023-03-10 stsp
2944 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2945 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2946 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2947 9a298e5c 2023-03-10 stsp }
2948 9a298e5c 2023-03-10 stsp
2949 9a298e5c 2023-03-10 stsp return NULL;
2950 9a298e5c 2023-03-10 stsp }
2951 9a298e5c 2023-03-10 stsp
2952 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2953 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2954 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2955 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2956 234035bc 2019-06-01 stsp void *progress_arg;
2957 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2958 234035bc 2019-06-01 stsp void *cancel_arg;
2959 f69721c3 2019-10-21 stsp const char *label_orig;
2960 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2961 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2962 234035bc 2019-06-01 stsp };
2963 234035bc 2019-06-01 stsp
2964 234035bc 2019-06-01 stsp static const struct got_error *
2965 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2966 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2967 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2968 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2969 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2970 234035bc 2019-06-01 stsp {
2971 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2972 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2973 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2974 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2975 234035bc 2019-06-01 stsp struct stat sb;
2976 234035bc 2019-06-01 stsp unsigned char status;
2977 234035bc 2019-06-01 stsp int local_changes_subsumed;
2978 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2979 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2980 234035bc 2019-06-01 stsp
2981 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2982 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2983 d6c87207 2019-08-02 stsp strlen(path2));
2984 1ee397ad 2019-07-12 stsp if (ie == NULL)
2985 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2986 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2987 234035bc 2019-06-01 stsp
2988 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2989 234035bc 2019-06-01 stsp path2) == -1)
2990 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2991 234035bc 2019-06-01 stsp
2992 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2993 7f91a133 2019-12-13 stsp repo);
2994 234035bc 2019-06-01 stsp if (err)
2995 234035bc 2019-06-01 stsp goto done;
2996 234035bc 2019-06-01 stsp
2997 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2998 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2999 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
3000 234035bc 2019-06-01 stsp goto done;
3001 234035bc 2019-06-01 stsp }
3002 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3003 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3004 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3005 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3006 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
3007 234035bc 2019-06-01 stsp goto done;
3008 234035bc 2019-06-01 stsp }
3009 234035bc 2019-06-01 stsp
3010 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3011 36bf999c 2020-07-23 stsp char *link_target2;
3012 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
3013 36bf999c 2020-07-23 stsp if (err)
3014 36bf999c 2020-07-23 stsp goto done;
3015 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
3016 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
3017 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
3018 36bf999c 2020-07-23 stsp free(link_target2);
3019 af57b12a 2020-07-23 stsp } else {
3020 1af628f4 2021-06-03 stsp int fd;
3021 1af628f4 2021-06-03 stsp
3022 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
3023 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
3024 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
3025 1af628f4 2021-06-03 stsp goto done;
3026 1af628f4 2021-06-03 stsp }
3027 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3028 1af628f4 2021-06-03 stsp f_orig, blob1);
3029 1af628f4 2021-06-03 stsp if (err)
3030 1af628f4 2021-06-03 stsp goto done;
3031 1af628f4 2021-06-03 stsp
3032 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
3033 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
3034 1af628f4 2021-06-03 stsp goto done;
3035 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3037 1af628f4 2021-06-03 stsp if (err)
3038 1af628f4 2021-06-03 stsp goto done;
3039 1af628f4 2021-06-03 stsp
3040 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3041 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3042 1af628f4 2021-06-03 stsp if (fd == -1) {
3043 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3044 1af628f4 2021-06-03 stsp ondisk_path);
3045 1af628f4 2021-06-03 stsp goto done;
3046 1af628f4 2021-06-03 stsp }
3047 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3048 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3049 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3050 1af628f4 2021-06-03 stsp ondisk_path);
3051 1af628f4 2021-06-03 stsp close(fd);
3052 1af628f4 2021-06-03 stsp goto done;
3053 1af628f4 2021-06-03 stsp }
3054 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3055 1af628f4 2021-06-03 stsp if (err)
3056 1af628f4 2021-06-03 stsp goto done;
3057 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3058 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3059 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3060 1af628f4 2021-06-03 stsp goto done;
3061 1af628f4 2021-06-03 stsp }
3062 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3063 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3064 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3065 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3066 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3067 af57b12a 2020-07-23 stsp }
3068 234035bc 2019-06-01 stsp } else if (blob1) {
3069 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3070 d6c87207 2019-08-02 stsp strlen(path1));
3071 1ee397ad 2019-07-12 stsp if (ie == NULL)
3072 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3073 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3074 2b92fad7 2019-06-02 stsp
3075 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3076 2b92fad7 2019-06-02 stsp path1) == -1)
3077 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3078 2b92fad7 2019-06-02 stsp
3079 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3080 7f91a133 2019-12-13 stsp repo);
3081 2b92fad7 2019-06-02 stsp if (err)
3082 2b92fad7 2019-06-02 stsp goto done;
3083 2b92fad7 2019-06-02 stsp
3084 2b92fad7 2019-06-02 stsp switch (status) {
3085 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3086 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3087 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3088 1ee397ad 2019-07-12 stsp if (err)
3089 1ee397ad 2019-07-12 stsp goto done;
3090 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3091 2b92fad7 2019-06-02 stsp if (err)
3092 2b92fad7 2019-06-02 stsp goto done;
3093 2b92fad7 2019-06-02 stsp if (ie)
3094 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3095 2b92fad7 2019-06-02 stsp break;
3096 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3097 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3098 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3099 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3100 1ee397ad 2019-07-12 stsp if (err)
3101 1ee397ad 2019-07-12 stsp goto done;
3102 2b92fad7 2019-06-02 stsp if (ie)
3103 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3104 2b92fad7 2019-06-02 stsp break;
3105 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3106 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3107 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3108 72840534 2022-01-19 stsp off_t blob1_size;
3109 0a22ca1a 2020-09-23 stsp /*
3110 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3111 0a22ca1a 2020-09-23 stsp * exists in the repository.
3112 0a22ca1a 2020-09-23 stsp */
3113 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3114 72840534 2022-01-19 stsp &blob1_size, path1);
3115 0a22ca1a 2020-09-23 stsp if (err)
3116 0a22ca1a 2020-09-23 stsp goto done;
3117 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3118 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3119 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3120 0a22ca1a 2020-09-23 stsp if (err)
3121 0a22ca1a 2020-09-23 stsp goto done;
3122 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3123 0a22ca1a 2020-09-23 stsp path1);
3124 0a22ca1a 2020-09-23 stsp if (err)
3125 0a22ca1a 2020-09-23 stsp goto done;
3126 0a22ca1a 2020-09-23 stsp if (ie)
3127 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3128 0a22ca1a 2020-09-23 stsp ie);
3129 0a22ca1a 2020-09-23 stsp } else {
3130 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3131 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3132 0a22ca1a 2020-09-23 stsp }
3133 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3134 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3135 0a22ca1a 2020-09-23 stsp free(id);
3136 0a22ca1a 2020-09-23 stsp if (err)
3137 0a22ca1a 2020-09-23 stsp goto done;
3138 0a22ca1a 2020-09-23 stsp break;
3139 0a22ca1a 2020-09-23 stsp }
3140 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3141 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3142 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3143 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3144 1ee397ad 2019-07-12 stsp if (err)
3145 1ee397ad 2019-07-12 stsp goto done;
3146 2b92fad7 2019-06-02 stsp break;
3147 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3148 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3149 1ee397ad 2019-07-12 stsp if (err)
3150 1ee397ad 2019-07-12 stsp goto done;
3151 2b92fad7 2019-06-02 stsp break;
3152 2b92fad7 2019-06-02 stsp default:
3153 2b92fad7 2019-06-02 stsp break;
3154 2b92fad7 2019-06-02 stsp }
3155 234035bc 2019-06-01 stsp } else if (blob2) {
3156 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3157 234035bc 2019-06-01 stsp path2) == -1)
3158 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3159 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3160 d6c87207 2019-08-02 stsp strlen(path2));
3161 234035bc 2019-06-01 stsp if (ie) {
3162 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3163 7f91a133 2019-12-13 stsp -1, NULL, repo);
3164 234035bc 2019-06-01 stsp if (err)
3165 234035bc 2019-06-01 stsp goto done;
3166 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3167 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3168 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3169 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3170 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3171 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3172 1ee397ad 2019-07-12 stsp status, path2);
3173 234035bc 2019-06-01 stsp goto done;
3174 234035bc 2019-06-01 stsp }
3175 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3176 36bf999c 2020-07-23 stsp char *link_target2;
3177 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3178 36bf999c 2020-07-23 stsp blob2);
3179 36bf999c 2020-07-23 stsp if (err)
3180 36bf999c 2020-07-23 stsp goto done;
3181 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3182 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3183 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3184 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3185 36bf999c 2020-07-23 stsp free(link_target2);
3186 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3187 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3188 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3189 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3190 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3191 526a746f 2020-07-23 stsp a->progress_arg);
3192 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3193 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3194 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3195 526a746f 2020-07-23 stsp }
3196 dfe9fba0 2020-07-23 stsp if (err)
3197 dfe9fba0 2020-07-23 stsp goto done;
3198 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3199 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3200 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3201 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3202 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3203 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3204 234035bc 2019-06-01 stsp if (err)
3205 234035bc 2019-06-01 stsp goto done;
3206 234035bc 2019-06-01 stsp }
3207 234035bc 2019-06-01 stsp } else {
3208 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3209 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3210 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3211 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3212 234035bc 2019-06-01 stsp if (err)
3213 2b92fad7 2019-06-02 stsp goto done;
3214 234035bc 2019-06-01 stsp }
3215 234035bc 2019-06-01 stsp }
3216 234035bc 2019-06-01 stsp done:
3217 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3218 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3219 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3220 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3221 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3222 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3223 1af628f4 2021-06-03 stsp free(id_str);
3224 54d5be07 2021-06-03 stsp free(label_deriv2);
3225 234035bc 2019-06-01 stsp free(ondisk_path);
3226 234035bc 2019-06-01 stsp return err;
3227 234035bc 2019-06-01 stsp }
3228 234035bc 2019-06-01 stsp
3229 69de9dd4 2021-09-03 stsp static const struct got_error *
3230 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3231 69de9dd4 2021-09-03 stsp {
3232 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3233 69de9dd4 2021-09-03 stsp
3234 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3235 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3236 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3237 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3238 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3239 69de9dd4 2021-09-03 stsp
3240 69de9dd4 2021-09-03 stsp return NULL;
3241 69de9dd4 2021-09-03 stsp }
3242 69de9dd4 2021-09-03 stsp
3243 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3244 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3245 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3246 234035bc 2019-06-01 stsp struct got_repository *repo;
3247 234035bc 2019-06-01 stsp };
3248 234035bc 2019-06-01 stsp
3249 234035bc 2019-06-01 stsp static const struct got_error *
3250 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3251 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3252 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3253 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3254 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3255 234035bc 2019-06-01 stsp {
3256 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3257 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3258 234035bc 2019-06-01 stsp unsigned char status;
3259 234035bc 2019-06-01 stsp struct stat sb;
3260 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3261 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3262 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3263 234035bc 2019-06-01 stsp char *ondisk_path;
3264 234035bc 2019-06-01 stsp
3265 69de9dd4 2021-09-03 stsp if (id == NULL)
3266 69de9dd4 2021-09-03 stsp return NULL;
3267 234035bc 2019-06-01 stsp
3268 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3269 69de9dd4 2021-09-03 stsp if (ie == NULL)
3270 69de9dd4 2021-09-03 stsp return NULL;
3271 69de9dd4 2021-09-03 stsp
3272 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3273 234035bc 2019-06-01 stsp == -1)
3274 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3275 234035bc 2019-06-01 stsp
3276 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3277 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3278 5546d466 2021-09-02 stsp free(ondisk_path);
3279 234035bc 2019-06-01 stsp if (err)
3280 234035bc 2019-06-01 stsp return err;
3281 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3282 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3283 234035bc 2019-06-01 stsp
3284 234035bc 2019-06-01 stsp return NULL;
3285 234035bc 2019-06-01 stsp }
3286 234035bc 2019-06-01 stsp
3287 818c7501 2019-07-11 stsp static const struct got_error *
3288 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3289 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3290 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3291 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3292 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3293 234035bc 2019-06-01 stsp {
3294 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3295 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3296 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3297 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3298 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3299 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3300 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3301 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3302 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3303 234035bc 2019-06-01 stsp
3304 03415a1a 2019-06-02 stsp if (commit_id1) {
3305 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3306 a44927cc 2022-04-07 stsp if (err)
3307 a44927cc 2022-04-07 stsp goto done;
3308 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3309 03415a1a 2019-06-02 stsp worktree->path_prefix);
3310 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3311 03415a1a 2019-06-02 stsp goto done;
3312 69d57f3d 2020-07-31 stsp }
3313 69d57f3d 2020-07-31 stsp if (tree_id1) {
3314 69d57f3d 2020-07-31 stsp char *id_str;
3315 03415a1a 2019-06-02 stsp
3316 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3317 03415a1a 2019-06-02 stsp if (err)
3318 03415a1a 2019-06-02 stsp goto done;
3319 f69721c3 2019-10-21 stsp
3320 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3321 f69721c3 2019-10-21 stsp if (err)
3322 f69721c3 2019-10-21 stsp goto done;
3323 f69721c3 2019-10-21 stsp
3324 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3325 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3326 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3327 f69721c3 2019-10-21 stsp free(id_str);
3328 f69721c3 2019-10-21 stsp goto done;
3329 f69721c3 2019-10-21 stsp }
3330 f69721c3 2019-10-21 stsp free(id_str);
3331 b72706c3 2022-06-01 stsp
3332 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3333 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3334 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3335 b72706c3 2022-06-01 stsp goto done;
3336 b72706c3 2022-06-01 stsp }
3337 03415a1a 2019-06-02 stsp }
3338 234035bc 2019-06-01 stsp
3339 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3340 a44927cc 2022-04-07 stsp if (err)
3341 a44927cc 2022-04-07 stsp goto done;
3342 a44927cc 2022-04-07 stsp
3343 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3344 234035bc 2019-06-01 stsp worktree->path_prefix);
3345 234035bc 2019-06-01 stsp if (err)
3346 234035bc 2019-06-01 stsp goto done;
3347 234035bc 2019-06-01 stsp
3348 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3349 234035bc 2019-06-01 stsp if (err)
3350 234035bc 2019-06-01 stsp goto done;
3351 234035bc 2019-06-01 stsp
3352 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3353 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3354 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3355 f9d37699 2022-06-28 stsp goto done;
3356 f9d37699 2022-06-28 stsp }
3357 f9d37699 2022-06-28 stsp
3358 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3359 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3360 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3361 b72706c3 2022-06-01 stsp goto done;
3362 b72706c3 2022-06-01 stsp }
3363 b72706c3 2022-06-01 stsp
3364 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3365 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3366 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3367 f9d37699 2022-06-28 stsp goto done;
3368 f9d37699 2022-06-28 stsp }
3369 f9d37699 2022-06-28 stsp
3370 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3371 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3372 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3373 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3374 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3375 69de9dd4 2021-09-03 stsp if (err)
3376 69de9dd4 2021-09-03 stsp goto done;
3377 69de9dd4 2021-09-03 stsp
3378 234035bc 2019-06-01 stsp arg.worktree = worktree;
3379 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3380 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3381 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3382 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3383 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3384 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3385 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3386 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3387 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3388 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3389 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3390 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3391 af12c6b9 2019-06-04 stsp err = sync_err;
3392 234035bc 2019-06-01 stsp done:
3393 a44927cc 2022-04-07 stsp if (commit1)
3394 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3395 a44927cc 2022-04-07 stsp if (commit2)
3396 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3397 234035bc 2019-06-01 stsp if (tree1)
3398 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3399 234035bc 2019-06-01 stsp if (tree2)
3400 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3401 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3402 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3403 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3404 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3405 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3406 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3407 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3408 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3409 f69721c3 2019-10-21 stsp free(label_orig);
3410 818c7501 2019-07-11 stsp return err;
3411 818c7501 2019-07-11 stsp }
3412 234035bc 2019-06-01 stsp
3413 818c7501 2019-07-11 stsp const struct got_error *
3414 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3415 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3416 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3417 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3418 818c7501 2019-07-11 stsp {
3419 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3420 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3421 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3422 818c7501 2019-07-11 stsp
3423 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3424 818c7501 2019-07-11 stsp if (err)
3425 818c7501 2019-07-11 stsp return err;
3426 818c7501 2019-07-11 stsp
3427 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3428 818c7501 2019-07-11 stsp if (err)
3429 818c7501 2019-07-11 stsp goto done;
3430 818c7501 2019-07-11 stsp
3431 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3432 69de9dd4 2021-09-03 stsp worktree);
3433 818c7501 2019-07-11 stsp if (err)
3434 818c7501 2019-07-11 stsp goto done;
3435 818c7501 2019-07-11 stsp
3436 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3437 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3438 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3439 818c7501 2019-07-11 stsp done:
3440 818c7501 2019-07-11 stsp if (fileindex)
3441 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3442 818c7501 2019-07-11 stsp free(fileindex_path);
3443 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3444 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3445 234035bc 2019-06-01 stsp err = unlockerr;
3446 234035bc 2019-06-01 stsp return err;
3447 234035bc 2019-06-01 stsp }
3448 234035bc 2019-06-01 stsp
3449 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3450 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3451 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3452 927df6b7 2019-02-10 stsp const char *status_path;
3453 927df6b7 2019-02-10 stsp size_t status_path_len;
3454 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3455 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3456 f8d1f275 2019-02-04 stsp void *status_arg;
3457 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3458 f8d1f275 2019-02-04 stsp void *cancel_arg;
3459 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3460 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3461 f2a9dc41 2019-12-13 tracey int report_unchanged;
3462 3143d852 2020-06-25 stsp int no_ignores;
3463 f8d1f275 2019-02-04 stsp };
3464 88d0e355 2019-08-03 stsp
3465 f8d1f275 2019-02-04 stsp static const struct got_error *
3466 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3467 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3468 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3469 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3470 927df6b7 2019-02-10 stsp {
3471 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3472 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3473 2c4740ad 2023-02-12 mark unsigned char staged_status;
3474 927df6b7 2019-02-10 stsp struct stat sb;
3475 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3476 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3477 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3478 927df6b7 2019-02-10 stsp
3479 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3480 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3481 98eaaa12 2019-08-03 stsp if (err)
3482 98eaaa12 2019-08-03 stsp return err;
3483 98eaaa12 2019-08-03 stsp
3484 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3485 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3486 98eaaa12 2019-08-03 stsp return NULL;
3487 98eaaa12 2019-08-03 stsp
3488 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3489 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3490 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3491 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3492 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3493 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3494 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3495 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3496 98eaaa12 2019-08-03 stsp }
3497 98eaaa12 2019-08-03 stsp
3498 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3499 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3500 927df6b7 2019-02-10 stsp }
3501 927df6b7 2019-02-10 stsp
3502 927df6b7 2019-02-10 stsp static const struct got_error *
3503 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3504 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3505 f8d1f275 2019-02-04 stsp {
3506 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3507 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3508 f8d1f275 2019-02-04 stsp char *abspath;
3509 f8d1f275 2019-02-04 stsp
3510 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3511 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3512 0584f854 2019-04-06 stsp
3513 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3514 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3515 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3516 927df6b7 2019-02-10 stsp return NULL;
3517 927df6b7 2019-02-10 stsp
3518 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3519 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3520 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3521 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3522 f8d1f275 2019-02-04 stsp } else {
3523 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3524 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3525 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3526 f8d1f275 2019-02-04 stsp }
3527 f8d1f275 2019-02-04 stsp
3528 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3529 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3530 f8d1f275 2019-02-04 stsp free(abspath);
3531 9d31a1d8 2018-03-11 stsp return err;
3532 9d31a1d8 2018-03-11 stsp }
3533 f8d1f275 2019-02-04 stsp
3534 f8d1f275 2019-02-04 stsp static const struct got_error *
3535 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3536 f8d1f275 2019-02-04 stsp {
3537 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3538 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3539 2ec1f75b 2019-03-26 stsp unsigned char status;
3540 927df6b7 2019-02-10 stsp
3541 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3542 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3543 0584f854 2019-04-06 stsp
3544 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3545 927df6b7 2019-02-10 stsp return NULL;
3546 927df6b7 2019-02-10 stsp
3547 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3548 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3549 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3550 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3551 2ec1f75b 2019-03-26 stsp else
3552 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3553 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3554 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3555 6841da00 2019-08-08 stsp }
3556 6841da00 2019-08-08 stsp
3557 336075a4 2022-06-25 op static void
3558 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3559 6841da00 2019-08-08 stsp {
3560 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3561 6841da00 2019-08-08 stsp
3562 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3563 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3564 d8bacb93 2023-01-10 mark
3565 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3566 6841da00 2019-08-08 stsp }
3567 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3568 6841da00 2019-08-08 stsp }
3569 6841da00 2019-08-08 stsp
3570 6841da00 2019-08-08 stsp static const struct got_error *
3571 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3572 6841da00 2019-08-08 stsp {
3573 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3574 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3575 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3576 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3577 6841da00 2019-08-08 stsp size_t linesize = 0;
3578 6841da00 2019-08-08 stsp ssize_t linelen;
3579 6841da00 2019-08-08 stsp
3580 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3581 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3582 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3583 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3584 6841da00 2019-08-08 stsp
3585 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3586 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3587 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3588 bd8de430 2019-10-04 stsp
3589 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3590 bd8de430 2019-10-04 stsp if (line[0] == '#')
3591 bd8de430 2019-10-04 stsp continue;
3592 bd8de430 2019-10-04 stsp
3593 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3594 bd8de430 2019-10-04 stsp if (line[0] == '!')
3595 bd8de430 2019-10-04 stsp continue;
3596 bd8de430 2019-10-04 stsp
3597 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3598 6841da00 2019-08-08 stsp line) == -1) {
3599 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3600 6841da00 2019-08-08 stsp goto done;
3601 6841da00 2019-08-08 stsp }
3602 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3603 6841da00 2019-08-08 stsp if (err)
3604 6841da00 2019-08-08 stsp goto done;
3605 6841da00 2019-08-08 stsp }
3606 6841da00 2019-08-08 stsp if (ferror(f)) {
3607 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3608 6841da00 2019-08-08 stsp goto done;
3609 6841da00 2019-08-08 stsp }
3610 6841da00 2019-08-08 stsp
3611 6841da00 2019-08-08 stsp dirpath = strdup(path);
3612 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3613 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3614 6841da00 2019-08-08 stsp goto done;
3615 6841da00 2019-08-08 stsp }
3616 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3617 6841da00 2019-08-08 stsp done:
3618 6841da00 2019-08-08 stsp free(line);
3619 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3620 6841da00 2019-08-08 stsp free(dirpath);
3621 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3622 6841da00 2019-08-08 stsp }
3623 6841da00 2019-08-08 stsp return err;
3624 249b637c 2023-02-20 stsp }
3625 249b637c 2023-02-20 stsp
3626 249b637c 2023-02-20 stsp static int
3627 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3628 249b637c 2023-02-20 stsp int flags)
3629 249b637c 2023-02-20 stsp {
3630 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3631 249b637c 2023-02-20 stsp
3632 249b637c 2023-02-20 stsp /*
3633 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3634 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3635 249b637c 2023-02-20 stsp */
3636 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3637 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3638 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3639 249b637c 2023-02-20 stsp
3640 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3641 249b637c 2023-02-20 stsp }
3642 249b637c 2023-02-20 stsp
3643 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3644 6841da00 2019-08-08 stsp }
3645 6841da00 2019-08-08 stsp
3646 336075a4 2022-06-25 op static int
3647 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3648 6841da00 2019-08-08 stsp {
3649 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3650 bd8de430 2019-10-04 stsp
3651 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3652 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3653 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3654 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3655 bd8de430 2019-10-04 stsp
3656 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3657 249b637c 2023-02-20 stsp const char *p;
3658 6841da00 2019-08-08 stsp
3659 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3660 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3661 bd8de430 2019-10-04 stsp continue;
3662 bd8de430 2019-10-04 stsp p = path;
3663 bd8de430 2019-10-04 stsp while (*p) {
3664 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3665 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3666 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3667 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3668 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3669 bd8de430 2019-10-04 stsp p++;
3670 bd8de430 2019-10-04 stsp while (*p == '/')
3671 bd8de430 2019-10-04 stsp p++;
3672 bd8de430 2019-10-04 stsp continue;
3673 bd8de430 2019-10-04 stsp }
3674 bd8de430 2019-10-04 stsp return 1;
3675 bd8de430 2019-10-04 stsp }
3676 bd8de430 2019-10-04 stsp }
3677 bd8de430 2019-10-04 stsp }
3678 bd8de430 2019-10-04 stsp
3679 6841da00 2019-08-08 stsp /*
3680 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3681 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3682 6841da00 2019-08-08 stsp * ignores backwards.
3683 6841da00 2019-08-08 stsp */
3684 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3685 6841da00 2019-08-08 stsp while (pe) {
3686 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3687 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3688 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3689 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3690 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3691 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3692 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3693 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3694 249b637c 2023-02-20 stsp path, flags))
3695 6841da00 2019-08-08 stsp continue;
3696 6841da00 2019-08-08 stsp return 1;
3697 6841da00 2019-08-08 stsp }
3698 6841da00 2019-08-08 stsp }
3699 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3700 6841da00 2019-08-08 stsp }
3701 6841da00 2019-08-08 stsp
3702 6841da00 2019-08-08 stsp return 0;
3703 6841da00 2019-08-08 stsp }
3704 6841da00 2019-08-08 stsp
3705 6841da00 2019-08-08 stsp static const struct got_error *
3706 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3707 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3708 6841da00 2019-08-08 stsp {
3709 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3710 6841da00 2019-08-08 stsp char *ignorespath;
3711 886cec17 2019-12-15 stsp int fd = -1;
3712 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3713 6841da00 2019-08-08 stsp
3714 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3715 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3716 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3717 6841da00 2019-08-08 stsp
3718 886cec17 2019-12-15 stsp if (dirfd != -1) {
3719 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3720 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3721 886cec17 2019-12-15 stsp if (fd == -1) {
3722 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3723 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3724 886cec17 2019-12-15 stsp ignorespath);
3725 886cec17 2019-12-15 stsp } else {
3726 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3727 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3728 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3729 886cec17 2019-12-15 stsp ignorespath);
3730 886cec17 2019-12-15 stsp else {
3731 886cec17 2019-12-15 stsp fd = -1;
3732 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3733 886cec17 2019-12-15 stsp }
3734 886cec17 2019-12-15 stsp }
3735 886cec17 2019-12-15 stsp } else {
3736 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3737 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3738 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3739 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3740 886cec17 2019-12-15 stsp ignorespath);
3741 886cec17 2019-12-15 stsp } else
3742 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3743 886cec17 2019-12-15 stsp }
3744 6841da00 2019-08-08 stsp
3745 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3746 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3747 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3748 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3749 6841da00 2019-08-08 stsp free(ignorespath);
3750 6841da00 2019-08-08 stsp return err;
3751 f8d1f275 2019-02-04 stsp }
3752 f8d1f275 2019-02-04 stsp
3753 f8d1f275 2019-02-04 stsp static const struct got_error *
3754 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3755 62da3196 2021-10-01 stsp int dirfd)
3756 f8d1f275 2019-02-04 stsp {
3757 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3758 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3759 f8d1f275 2019-02-04 stsp char *path = NULL;
3760 62da3196 2021-10-01 stsp
3761 62da3196 2021-10-01 stsp if (ignore != NULL)
3762 62da3196 2021-10-01 stsp *ignore = 0;
3763 f8d1f275 2019-02-04 stsp
3764 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3765 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3766 0584f854 2019-04-06 stsp
3767 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3768 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3769 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3770 f8d1f275 2019-02-04 stsp } else {
3771 f8d1f275 2019-02-04 stsp path = de->d_name;
3772 f8d1f275 2019-02-04 stsp }
3773 f8d1f275 2019-02-04 stsp
3774 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3775 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3776 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3777 62da3196 2021-10-01 stsp *ignore = 1;
3778 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3779 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3780 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3781 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3782 f8d1f275 2019-02-04 stsp if (parent_path[0])
3783 f8d1f275 2019-02-04 stsp free(path);
3784 b72f483a 2019-02-05 stsp return err;
3785 f8d1f275 2019-02-04 stsp }
3786 f8d1f275 2019-02-04 stsp
3787 347d1d3e 2019-07-12 stsp static const struct got_error *
3788 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3789 3143d852 2020-06-25 stsp {
3790 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3791 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3792 3143d852 2020-06-25 stsp
3793 3143d852 2020-06-25 stsp if (a->no_ignores)
3794 3143d852 2020-06-25 stsp return NULL;
3795 3143d852 2020-06-25 stsp
3796 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3797 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3798 3143d852 2020-06-25 stsp if (err)
3799 3143d852 2020-06-25 stsp return err;
3800 3143d852 2020-06-25 stsp
3801 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3802 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3803 3143d852 2020-06-25 stsp
3804 3143d852 2020-06-25 stsp return err;
3805 3143d852 2020-06-25 stsp }
3806 3143d852 2020-06-25 stsp
3807 3143d852 2020-06-25 stsp static const struct got_error *
3808 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3809 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3810 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3811 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3812 abb4604f 2019-07-27 stsp {
3813 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3814 abb4604f 2019-07-27 stsp struct stat sb;
3815 ff56836b 2021-07-08 stsp
3816 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3817 abb4604f 2019-07-27 stsp if (ie)
3818 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3819 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3820 abb4604f 2019-07-27 stsp
3821 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3822 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3823 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3824 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3825 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3826 abb4604f 2019-07-27 stsp }
3827 abb4604f 2019-07-27 stsp
3828 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3829 916237f3 2022-02-11 stsp return NULL;
3830 916237f3 2022-02-11 stsp
3831 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3832 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3833 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3834 abb4604f 2019-07-27 stsp
3835 abb4604f 2019-07-27 stsp return NULL;
3836 3143d852 2020-06-25 stsp }
3837 3143d852 2020-06-25 stsp
3838 3143d852 2020-06-25 stsp static const struct got_error *
3839 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3840 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3841 3143d852 2020-06-25 stsp {
3842 3143d852 2020-06-25 stsp const struct got_error *err;
3843 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3844 3143d852 2020-06-25 stsp
3845 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3846 3143d852 2020-06-25 stsp ".cvsignore");
3847 3143d852 2020-06-25 stsp if (err)
3848 3143d852 2020-06-25 stsp return err;
3849 3143d852 2020-06-25 stsp
3850 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3851 3143d852 2020-06-25 stsp ".gitignore");
3852 3143d852 2020-06-25 stsp if (err)
3853 3143d852 2020-06-25 stsp return err;
3854 3143d852 2020-06-25 stsp
3855 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3856 3143d852 2020-06-25 stsp if (err) {
3857 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3858 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3859 3143d852 2020-06-25 stsp return err;
3860 3143d852 2020-06-25 stsp }
3861 3143d852 2020-06-25 stsp for (;;) {
3862 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3863 3143d852 2020-06-25 stsp ".cvsignore");
3864 3143d852 2020-06-25 stsp if (err)
3865 3143d852 2020-06-25 stsp break;
3866 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3867 3143d852 2020-06-25 stsp ".gitignore");
3868 3143d852 2020-06-25 stsp if (err)
3869 3143d852 2020-06-25 stsp break;
3870 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3871 3143d852 2020-06-25 stsp if (err) {
3872 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3873 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3874 3143d852 2020-06-25 stsp break;
3875 3143d852 2020-06-25 stsp }
3876 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3877 ff56836b 2021-07-08 stsp break;
3878 b737c85a 2020-06-26 stsp free(parent_path);
3879 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3880 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3881 3143d852 2020-06-25 stsp }
3882 3143d852 2020-06-25 stsp
3883 b737c85a 2020-06-26 stsp free(parent_path);
3884 b737c85a 2020-06-26 stsp free(next_parent_path);
3885 3143d852 2020-06-25 stsp return err;
3886 abb4604f 2019-07-27 stsp }
3887 31cf15ec 2023-04-12 stsp
3888 31cf15ec 2023-04-12 stsp struct find_missing_children_args {
3889 31cf15ec 2023-04-12 stsp const char *parent_path;
3890 31cf15ec 2023-04-12 stsp size_t parent_len;
3891 31cf15ec 2023-04-12 stsp struct got_pathlist_head *children;
3892 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb;
3893 31cf15ec 2023-04-12 stsp void *cancel_arg;
3894 31cf15ec 2023-04-12 stsp };
3895 31cf15ec 2023-04-12 stsp
3896 abb4604f 2019-07-27 stsp static const struct got_error *
3897 31cf15ec 2023-04-12 stsp find_missing_children(void *arg, struct got_fileindex_entry *ie)
3898 31cf15ec 2023-04-12 stsp {
3899 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
3900 31cf15ec 2023-04-12 stsp struct find_missing_children_args *a = arg;
3901 31cf15ec 2023-04-12 stsp
3902 31cf15ec 2023-04-12 stsp if (a->cancel_cb) {
3903 31cf15ec 2023-04-12 stsp err = a->cancel_cb(a->cancel_arg);
3904 31cf15ec 2023-04-12 stsp if (err)
3905 31cf15ec 2023-04-12 stsp return err;
3906 31cf15ec 2023-04-12 stsp }
3907 31cf15ec 2023-04-12 stsp
3908 31cf15ec 2023-04-12 stsp if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3909 31cf15ec 2023-04-12 stsp err = got_pathlist_append(a->children, ie->path, NULL);
3910 31cf15ec 2023-04-12 stsp
3911 31cf15ec 2023-04-12 stsp return err;
3912 31cf15ec 2023-04-12 stsp }
3913 31cf15ec 2023-04-12 stsp
3914 31cf15ec 2023-04-12 stsp static const struct got_error *
3915 31cf15ec 2023-04-12 stsp report_children(struct got_pathlist_head *children,
3916 31cf15ec 2023-04-12 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3917 31cf15ec 2023-04-12 stsp struct got_repository *repo, int is_root_dir, int report_unchanged,
3918 31cf15ec 2023-04-12 stsp struct got_pathlist_head *ignores, int no_ignores,
3919 31cf15ec 2023-04-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3920 31cf15ec 2023-04-12 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3921 31cf15ec 2023-04-12 stsp {
3922 31cf15ec 2023-04-12 stsp const struct got_error *err = NULL;
3923 31cf15ec 2023-04-12 stsp struct got_pathlist_entry *pe;
3924 31cf15ec 2023-04-12 stsp char *ondisk_path = NULL;
3925 31cf15ec 2023-04-12 stsp
3926 31cf15ec 2023-04-12 stsp TAILQ_FOREACH(pe, children, entry) {
3927 31cf15ec 2023-04-12 stsp if (cancel_cb) {
3928 31cf15ec 2023-04-12 stsp err = cancel_cb(cancel_arg);
3929 31cf15ec 2023-04-12 stsp if (err)
3930 31cf15ec 2023-04-12 stsp break;
3931 31cf15ec 2023-04-12 stsp }
3932 31cf15ec 2023-04-12 stsp
3933 31cf15ec 2023-04-12 stsp if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3934 31cf15ec 2023-04-12 stsp !is_root_dir ? "/" : "", pe->path) == -1) {
3935 31cf15ec 2023-04-12 stsp err = got_error_from_errno("asprintf");
3936 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
3937 31cf15ec 2023-04-12 stsp break;
3938 31cf15ec 2023-04-12 stsp }
3939 31cf15ec 2023-04-12 stsp
3940 31cf15ec 2023-04-12 stsp err = report_single_file_status(pe->path, ondisk_path,
3941 31cf15ec 2023-04-12 stsp fileindex, status_cb, status_arg, repo, report_unchanged,
3942 31cf15ec 2023-04-12 stsp ignores, no_ignores);
3943 31cf15ec 2023-04-12 stsp if (err)
3944 31cf15ec 2023-04-12 stsp break;
3945 31cf15ec 2023-04-12 stsp
3946 31cf15ec 2023-04-12 stsp free(ondisk_path);
3947 31cf15ec 2023-04-12 stsp ondisk_path = NULL;
3948 31cf15ec 2023-04-12 stsp }
3949 31cf15ec 2023-04-12 stsp
3950 31cf15ec 2023-04-12 stsp free(ondisk_path);
3951 31cf15ec 2023-04-12 stsp return err;
3952 31cf15ec 2023-04-12 stsp }
3953 31cf15ec 2023-04-12 stsp
3954 31cf15ec 2023-04-12 stsp static const struct got_error *
3955 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3956 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3957 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3958 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3959 f2a9dc41 2019-12-13 tracey int report_unchanged)
3960 f8d1f275 2019-02-04 stsp {
3961 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3962 6fc93f37 2019-12-13 stsp int fd = -1;
3963 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3964 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3965 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3966 31cf15ec 2023-04-12 stsp struct got_pathlist_head ignores, missing_children;
3967 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3968 3143d852 2020-06-25 stsp
3969 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3970 31cf15ec 2023-04-12 stsp TAILQ_INIT(&missing_children);
3971 f8d1f275 2019-02-04 stsp
3972 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3973 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3974 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3975 8dc303cc 2019-07-27 stsp
3976 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3977 a84c0d30 2022-03-12 stsp if (ie) {
3978 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3979 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3980 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3981 a84c0d30 2022-03-12 stsp goto done;
3982 31cf15ec 2023-04-12 stsp } else {
3983 31cf15ec 2023-04-12 stsp struct find_missing_children_args fmca;
3984 31cf15ec 2023-04-12 stsp fmca.parent_path = path;
3985 31cf15ec 2023-04-12 stsp fmca.parent_len = strlen(path);
3986 31cf15ec 2023-04-12 stsp fmca.children = &missing_children;
3987 31cf15ec 2023-04-12 stsp fmca.cancel_cb = cancel_cb;
3988 31cf15ec 2023-04-12 stsp fmca.cancel_arg = cancel_arg;
3989 31cf15ec 2023-04-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
3990 31cf15ec 2023-04-12 stsp find_missing_children, &fmca);
3991 31cf15ec 2023-04-12 stsp if (err)
3992 31cf15ec 2023-04-12 stsp goto done;
3993 a84c0d30 2022-03-12 stsp }
3994 a84c0d30 2022-03-12 stsp
3995 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3996 6fc93f37 2019-12-13 stsp if (fd == -1) {
3997 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3998 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3999 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
4000 ff56836b 2021-07-08 stsp else {
4001 ff56836b 2021-07-08 stsp if (!no_ignores) {
4002 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4003 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
4004 ff56836b 2021-07-08 stsp if (err)
4005 ff56836b 2021-07-08 stsp goto done;
4006 ff56836b 2021-07-08 stsp }
4007 31cf15ec 2023-04-12 stsp if (TAILQ_EMPTY(&missing_children)) {
4008 31cf15ec 2023-04-12 stsp err = report_single_file_status(path,
4009 31cf15ec 2023-04-12 stsp ondisk_path, fileindex,
4010 31cf15ec 2023-04-12 stsp status_cb, status_arg, repo,
4011 31cf15ec 2023-04-12 stsp report_unchanged, &ignores, no_ignores);
4012 31cf15ec 2023-04-12 stsp if (err)
4013 31cf15ec 2023-04-12 stsp goto done;
4014 31cf15ec 2023-04-12 stsp } else {
4015 31cf15ec 2023-04-12 stsp err = report_children(&missing_children,
4016 31cf15ec 2023-04-12 stsp worktree, fileindex, repo,
4017 31cf15ec 2023-04-12 stsp (path[0] == '\0'), report_unchanged,
4018 31cf15ec 2023-04-12 stsp &ignores, no_ignores,
4019 31cf15ec 2023-04-12 stsp status_cb, status_arg,
4020 31cf15ec 2023-04-12 stsp cancel_cb, cancel_arg);
4021 46108e23 2023-04-12 stsp if (err)
4022 46108e23 2023-04-12 stsp goto done;
4023 31cf15ec 2023-04-12 stsp }
4024 ff56836b 2021-07-08 stsp }
4025 abb4604f 2019-07-27 stsp } else {
4026 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
4027 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
4028 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
4029 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
4030 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
4031 abb4604f 2019-07-27 stsp arg.worktree = worktree;
4032 abb4604f 2019-07-27 stsp arg.status_path = path;
4033 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
4034 abb4604f 2019-07-27 stsp arg.repo = repo;
4035 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
4036 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
4037 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
4038 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
4039 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
4040 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
4041 022fae89 2019-12-06 tracey if (!no_ignores) {
4042 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
4043 3143d852 2020-06-25 stsp worktree->root_path, path);
4044 3143d852 2020-06-25 stsp if (err)
4045 3143d852 2020-06-25 stsp goto done;
4046 022fae89 2019-12-06 tracey }
4047 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
4048 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
4049 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
4050 927df6b7 2019-02-10 stsp }
4051 3143d852 2020-06-25 stsp done:
4052 ff56836b 2021-07-08 stsp free_ignores(&ignores);
4053 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4054 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
4055 927df6b7 2019-02-10 stsp free(ondisk_path);
4056 347d1d3e 2019-07-12 stsp return err;
4057 347d1d3e 2019-07-12 stsp }
4058 347d1d3e 2019-07-12 stsp
4059 347d1d3e 2019-07-12 stsp const struct got_error *
4060 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
4061 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
4062 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4063 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
4064 347d1d3e 2019-07-12 stsp {
4065 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
4066 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
4067 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4068 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
4069 347d1d3e 2019-07-12 stsp
4070 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4071 347d1d3e 2019-07-12 stsp if (err)
4072 347d1d3e 2019-07-12 stsp return err;
4073 347d1d3e 2019-07-12 stsp
4074 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
4075 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4076 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
4077 f6343036 2021-06-22 stsp no_ignores, 0);
4078 72ea6654 2019-07-27 stsp if (err)
4079 72ea6654 2019-07-27 stsp break;
4080 72ea6654 2019-07-27 stsp }
4081 f8d1f275 2019-02-04 stsp free(fileindex_path);
4082 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
4083 f8d1f275 2019-02-04 stsp return err;
4084 f8d1f275 2019-02-04 stsp }
4085 6c7ab921 2019-03-18 stsp
4086 6c7ab921 2019-03-18 stsp const struct got_error *
4087 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4088 6c7ab921 2019-03-18 stsp const char *arg)
4089 6c7ab921 2019-03-18 stsp {
4090 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
4091 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
4092 6c7ab921 2019-03-18 stsp size_t len;
4093 00bb5ea0 2020-07-23 stsp struct stat sb;
4094 01740607 2020-11-04 stsp char *abspath = NULL;
4095 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
4096 6c7ab921 2019-03-18 stsp
4097 6c7ab921 2019-03-18 stsp *wt_path = NULL;
4098 6c7ab921 2019-03-18 stsp
4099 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
4100 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
4101 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
4102 00bb5ea0 2020-07-23 stsp
4103 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
4104 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4105 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
4106 00bb5ea0 2020-07-23 stsp goto done;
4107 00bb5ea0 2020-07-23 stsp }
4108 727173c3 2020-11-06 stsp sb.st_mode = 0;
4109 00bb5ea0 2020-07-23 stsp }
4110 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
4111 00bb5ea0 2020-07-23 stsp /*
4112 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
4113 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
4114 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
4115 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
4116 00bb5ea0 2020-07-23 stsp */
4117 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
4118 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4119 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4120 00bb5ea0 2020-07-23 stsp goto done;
4121 00bb5ea0 2020-07-23 stsp }
4122 00bb5ea0 2020-07-23 stsp
4123 00bb5ea0 2020-07-23 stsp }
4124 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
4125 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
4126 00bb5ea0 2020-07-23 stsp if (err)
4127 d0710d08 2019-07-22 stsp goto done;
4128 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4129 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4130 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4131 00bb5ea0 2020-07-23 stsp goto done;
4132 00bb5ea0 2020-07-23 stsp }
4133 00bb5ea0 2020-07-23 stsp } else {
4134 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4135 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4136 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4137 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4138 00bb5ea0 2020-07-23 stsp goto done;
4139 00bb5ea0 2020-07-23 stsp }
4140 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4141 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4142 01740607 2020-11-04 stsp goto done;
4143 01740607 2020-11-04 stsp }
4144 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4145 01740607 2020-11-04 stsp sizeof(canonpath));
4146 01740607 2020-11-04 stsp if (err)
4147 00bb5ea0 2020-07-23 stsp goto done;
4148 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4149 01740607 2020-11-04 stsp if (resolved == NULL) {
4150 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4151 01740607 2020-11-04 stsp goto done;
4152 00bb5ea0 2020-07-23 stsp }
4153 d0710d08 2019-07-22 stsp }
4154 d0710d08 2019-07-22 stsp }
4155 6c7ab921 2019-03-18 stsp
4156 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4157 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4158 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4159 6c7ab921 2019-03-18 stsp goto done;
4160 6c7ab921 2019-03-18 stsp }
4161 6c7ab921 2019-03-18 stsp
4162 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4163 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4164 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4165 f6d88e1a 2019-05-29 stsp if (err)
4166 f6d88e1a 2019-05-29 stsp goto done;
4167 f6d88e1a 2019-05-29 stsp } else {
4168 f6d88e1a 2019-05-29 stsp path = strdup("");
4169 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4170 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4171 f6d88e1a 2019-05-29 stsp goto done;
4172 f6d88e1a 2019-05-29 stsp }
4173 6c7ab921 2019-03-18 stsp }
4174 6c7ab921 2019-03-18 stsp
4175 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4176 6c7ab921 2019-03-18 stsp len = strlen(path);
4177 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4178 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4179 6c7ab921 2019-03-18 stsp len--;
4180 6c7ab921 2019-03-18 stsp }
4181 6c7ab921 2019-03-18 stsp done:
4182 01740607 2020-11-04 stsp free(abspath);
4183 6c7ab921 2019-03-18 stsp free(resolved);
4184 d0710d08 2019-07-22 stsp free(cwd);
4185 6c7ab921 2019-03-18 stsp if (err == NULL)
4186 6c7ab921 2019-03-18 stsp *wt_path = path;
4187 6c7ab921 2019-03-18 stsp else
4188 6c7ab921 2019-03-18 stsp free(path);
4189 d00136be 2019-03-26 stsp return err;
4190 d00136be 2019-03-26 stsp }
4191 d00136be 2019-03-26 stsp
4192 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4193 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4194 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4195 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4196 4e68cba3 2019-11-23 stsp void *progress_arg;
4197 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4198 4e68cba3 2019-11-23 stsp };
4199 4e68cba3 2019-11-23 stsp
4200 4e68cba3 2019-11-23 stsp static const struct got_error *
4201 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4202 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4203 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4204 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4205 07f5b47a 2019-06-02 stsp {
4206 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4207 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4208 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4209 6d022e97 2019-08-04 stsp struct stat sb;
4210 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4211 07f5b47a 2019-06-02 stsp
4212 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4213 dbb83fbd 2019-12-12 stsp relpath) == -1)
4214 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4215 dbb83fbd 2019-12-12 stsp
4216 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4217 6d022e97 2019-08-04 stsp if (ie) {
4218 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4219 12463d8b 2019-12-13 stsp de_name, a->repo);
4220 6d022e97 2019-08-04 stsp if (err)
4221 dbb83fbd 2019-12-12 stsp goto done;
4222 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4223 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4224 dbb83fbd 2019-12-12 stsp goto done;
4225 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4226 dbb83fbd 2019-12-12 stsp if (err)
4227 dbb83fbd 2019-12-12 stsp goto done;
4228 6d022e97 2019-08-04 stsp }
4229 07f5b47a 2019-06-02 stsp
4230 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4231 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4232 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4233 9b4603c0 2022-01-31 stsp else
4234 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4235 dbb83fbd 2019-12-12 stsp goto done;
4236 4e68cba3 2019-11-23 stsp }
4237 07f5b47a 2019-06-02 stsp
4238 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4239 4e68cba3 2019-11-23 stsp if (err)
4240 4e68cba3 2019-11-23 stsp goto done;
4241 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4242 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4243 3969253a 2020-03-07 stsp if (err) {
4244 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4245 3969253a 2020-03-07 stsp goto done;
4246 3969253a 2020-03-07 stsp }
4247 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4248 07f5b47a 2019-06-02 stsp if (err) {
4249 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4250 4e68cba3 2019-11-23 stsp goto done;
4251 07f5b47a 2019-06-02 stsp }
4252 4e68cba3 2019-11-23 stsp done:
4253 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4254 dbb83fbd 2019-12-12 stsp if (err)
4255 dbb83fbd 2019-12-12 stsp return err;
4256 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4257 dbb83fbd 2019-12-12 stsp return NULL;
4258 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4259 07f5b47a 2019-06-02 stsp }
4260 07f5b47a 2019-06-02 stsp
4261 d00136be 2019-03-26 stsp const struct got_error *
4262 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4263 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4264 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4265 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4266 d00136be 2019-03-26 stsp {
4267 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4268 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4269 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4270 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4271 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4272 d00136be 2019-03-26 stsp
4273 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4274 d00136be 2019-03-26 stsp if (err)
4275 d00136be 2019-03-26 stsp return err;
4276 d00136be 2019-03-26 stsp
4277 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4278 d00136be 2019-03-26 stsp if (err)
4279 d00136be 2019-03-26 stsp goto done;
4280 d00136be 2019-03-26 stsp
4281 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4282 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4283 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4284 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4285 4e68cba3 2019-11-23 stsp saa.repo = repo;
4286 4e68cba3 2019-11-23 stsp
4287 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4288 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4289 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4290 1dd54920 2019-05-11 stsp if (err)
4291 af12c6b9 2019-06-04 stsp break;
4292 1dd54920 2019-05-11 stsp }
4293 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4294 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4295 af12c6b9 2019-06-04 stsp err = sync_err;
4296 d00136be 2019-03-26 stsp done:
4297 fb399478 2019-07-12 stsp free(fileindex_path);
4298 d00136be 2019-03-26 stsp if (fileindex)
4299 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4300 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4301 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4302 d00136be 2019-03-26 stsp err = unlockerr;
4303 6c7ab921 2019-03-18 stsp return err;
4304 6c7ab921 2019-03-18 stsp }
4305 17ed4618 2019-06-02 stsp
4306 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4307 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4308 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4309 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4310 f2a9dc41 2019-12-13 tracey void *progress_arg;
4311 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4312 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4313 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4314 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4315 766841c2 2020-08-13 stsp const char *status_codes;
4316 f2a9dc41 2019-12-13 tracey };
4317 f2a9dc41 2019-12-13 tracey
4318 f2a9dc41 2019-12-13 tracey static const struct got_error *
4319 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4320 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4321 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4322 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4323 17ed4618 2019-06-02 stsp {
4324 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4325 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4326 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4327 17ed4618 2019-06-02 stsp struct stat sb;
4328 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4329 4e12cd97 2022-01-25 stsp
4330 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4331 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4332 4e12cd97 2022-01-25 stsp return NULL;
4333 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4334 4e12cd97 2022-01-25 stsp }
4335 17ed4618 2019-06-02 stsp
4336 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4337 17ed4618 2019-06-02 stsp if (ie == NULL)
4338 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4339 2ec1f75b 2019-03-26 stsp
4340 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4341 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4342 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4343 9acbc4fa 2019-08-03 stsp return NULL;
4344 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4345 9acbc4fa 2019-08-03 stsp }
4346 9acbc4fa 2019-08-03 stsp
4347 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4348 f2a9dc41 2019-12-13 tracey relpath) == -1)
4349 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4350 f2a9dc41 2019-12-13 tracey
4351 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4352 12463d8b 2019-12-13 stsp a->repo);
4353 17ed4618 2019-06-02 stsp if (err)
4354 f2a9dc41 2019-12-13 tracey goto done;
4355 17ed4618 2019-06-02 stsp
4356 766841c2 2020-08-13 stsp if (a->status_codes) {
4357 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4358 766841c2 2020-08-13 stsp int i;
4359 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4360 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4361 766841c2 2020-08-13 stsp break;
4362 766841c2 2020-08-13 stsp }
4363 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4364 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4365 766841c2 2020-08-13 stsp free(ondisk_path);
4366 766841c2 2020-08-13 stsp return NULL;
4367 766841c2 2020-08-13 stsp }
4368 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4369 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4370 766841c2 2020-08-13 stsp static char msg[64];
4371 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4372 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4373 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4374 766841c2 2020-08-13 stsp goto done;
4375 766841c2 2020-08-13 stsp }
4376 766841c2 2020-08-13 stsp }
4377 766841c2 2020-08-13 stsp
4378 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4379 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4380 f2a9dc41 2019-12-13 tracey goto done;
4381 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4382 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4383 f2a9dc41 2019-12-13 tracey goto done;
4384 f2a9dc41 2019-12-13 tracey }
4385 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4386 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4387 4e12cd97 2022-01-25 stsp goto done;
4388 4e12cd97 2022-01-25 stsp }
4389 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4390 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4391 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4392 f2a9dc41 2019-12-13 tracey goto done;
4393 f2a9dc41 2019-12-13 tracey }
4394 17ed4618 2019-06-02 stsp }
4395 17ed4618 2019-06-02 stsp
4396 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4397 20a2ad1c 2020-10-20 stsp size_t root_len;
4398 20a2ad1c 2020-10-20 stsp
4399 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4400 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4401 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4402 12463d8b 2019-12-13 stsp ondisk_path);
4403 12463d8b 2019-12-13 stsp goto done;
4404 12463d8b 2019-12-13 stsp }
4405 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4406 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4407 12463d8b 2019-12-13 stsp goto done;
4408 15341bfd 2020-03-05 tracey }
4409 15341bfd 2020-03-05 tracey
4410 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4411 20a2ad1c 2020-10-20 stsp do {
4412 20a2ad1c 2020-10-20 stsp char *parent;
4413 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4414 20a2ad1c 2020-10-20 stsp if (err)
4415 2513f20a 2020-10-20 stsp goto done;
4416 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4417 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4418 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4419 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4420 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4421 20a2ad1c 2020-10-20 stsp ondisk_path);
4422 15341bfd 2020-03-05 tracey break;
4423 15341bfd 2020-03-05 tracey }
4424 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4425 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4426 f2a9dc41 2019-12-13 tracey }
4427 17ed4618 2019-06-02 stsp
4428 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4429 f2a9dc41 2019-12-13 tracey done:
4430 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4431 f2a9dc41 2019-12-13 tracey if (err)
4432 f2a9dc41 2019-12-13 tracey return err;
4433 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4434 f2a9dc41 2019-12-13 tracey return NULL;
4435 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4436 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4437 17ed4618 2019-06-02 stsp }
4438 17ed4618 2019-06-02 stsp
4439 2ec1f75b 2019-03-26 stsp const struct got_error *
4440 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4441 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4442 766841c2 2020-08-13 stsp const char *status_codes,
4443 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4444 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4445 2ec1f75b 2019-03-26 stsp {
4446 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4447 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4448 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4449 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4450 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4451 2ec1f75b 2019-03-26 stsp
4452 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4453 2ec1f75b 2019-03-26 stsp if (err)
4454 2ec1f75b 2019-03-26 stsp return err;
4455 2ec1f75b 2019-03-26 stsp
4456 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4457 2ec1f75b 2019-03-26 stsp if (err)
4458 2ec1f75b 2019-03-26 stsp goto done;
4459 f2a9dc41 2019-12-13 tracey
4460 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4461 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4462 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4463 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4464 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4465 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4466 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4467 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4468 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4469 2ec1f75b 2019-03-26 stsp
4470 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4471 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4472 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4473 17ed4618 2019-06-02 stsp if (err)
4474 af12c6b9 2019-06-04 stsp break;
4475 2ec1f75b 2019-03-26 stsp }
4476 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4477 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4478 af12c6b9 2019-06-04 stsp err = sync_err;
4479 2ec1f75b 2019-03-26 stsp done:
4480 fb399478 2019-07-12 stsp free(fileindex_path);
4481 2ec1f75b 2019-03-26 stsp if (fileindex)
4482 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4483 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4484 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4485 2ec1f75b 2019-03-26 stsp err = unlockerr;
4486 33aa809d 2019-08-08 stsp return err;
4487 33aa809d 2019-08-08 stsp }
4488 33aa809d 2019-08-08 stsp
4489 33aa809d 2019-08-08 stsp static const struct got_error *
4490 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4491 33aa809d 2019-08-08 stsp {
4492 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4493 33aa809d 2019-08-08 stsp char *line = NULL;
4494 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4495 33aa809d 2019-08-08 stsp ssize_t linelen;
4496 33aa809d 2019-08-08 stsp
4497 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4498 33aa809d 2019-08-08 stsp if (linelen == -1) {
4499 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4500 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4501 33aa809d 2019-08-08 stsp goto done;
4502 33aa809d 2019-08-08 stsp }
4503 33aa809d 2019-08-08 stsp return NULL;
4504 33aa809d 2019-08-08 stsp }
4505 33aa809d 2019-08-08 stsp if (outfile) {
4506 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4507 33aa809d 2019-08-08 stsp if (n != linelen) {
4508 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4509 33aa809d 2019-08-08 stsp goto done;
4510 33aa809d 2019-08-08 stsp }
4511 33aa809d 2019-08-08 stsp }
4512 33aa809d 2019-08-08 stsp if (rejectfile) {
4513 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4514 33aa809d 2019-08-08 stsp if (n != linelen)
4515 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4516 33aa809d 2019-08-08 stsp }
4517 33aa809d 2019-08-08 stsp done:
4518 33aa809d 2019-08-08 stsp free(line);
4519 2ec1f75b 2019-03-26 stsp return err;
4520 2ec1f75b 2019-03-26 stsp }
4521 1f1abb7e 2019-08-08 stsp
4522 33aa809d 2019-08-08 stsp static const struct got_error *
4523 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4524 33aa809d 2019-08-08 stsp {
4525 33aa809d 2019-08-08 stsp char *line = NULL;
4526 33aa809d 2019-08-08 stsp size_t linesize = 0;
4527 33aa809d 2019-08-08 stsp ssize_t linelen;
4528 33aa809d 2019-08-08 stsp
4529 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4530 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4531 500467ff 2019-09-25 hiltjo if (ferror(f))
4532 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4533 500467ff 2019-09-25 hiltjo return NULL;
4534 500467ff 2019-09-25 hiltjo }
4535 33aa809d 2019-08-08 stsp free(line);
4536 33aa809d 2019-08-08 stsp return NULL;
4537 33aa809d 2019-08-08 stsp }
4538 33aa809d 2019-08-08 stsp
4539 33aa809d 2019-08-08 stsp static const struct got_error *
4540 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4541 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4542 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4543 abc59930 2021-09-05 naddy {
4544 33aa809d 2019-08-08 stsp const struct got_error *err;
4545 33aa809d 2019-08-08 stsp
4546 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4547 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4548 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4549 33aa809d 2019-08-08 stsp if (err)
4550 33aa809d 2019-08-08 stsp return err;
4551 33aa809d 2019-08-08 stsp (*line_cur1)++;
4552 33aa809d 2019-08-08 stsp }
4553 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4554 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4555 33aa809d 2019-08-08 stsp if (rejectfile)
4556 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4557 33aa809d 2019-08-08 stsp else
4558 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4559 33aa809d 2019-08-08 stsp if (err)
4560 33aa809d 2019-08-08 stsp return err;
4561 33aa809d 2019-08-08 stsp (*line_cur2)++;
4562 33aa809d 2019-08-08 stsp }
4563 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4564 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4565 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4566 33aa809d 2019-08-08 stsp if (err)
4567 33aa809d 2019-08-08 stsp return err;
4568 33aa809d 2019-08-08 stsp (*line_cur2)++;
4569 33aa809d 2019-08-08 stsp }
4570 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4571 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4572 33aa809d 2019-08-08 stsp if (rejectfile)
4573 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4574 33aa809d 2019-08-08 stsp else
4575 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4576 33aa809d 2019-08-08 stsp if (err)
4577 33aa809d 2019-08-08 stsp return err;
4578 33aa809d 2019-08-08 stsp (*line_cur1)++;
4579 33aa809d 2019-08-08 stsp }
4580 f1e81a05 2019-08-10 stsp
4581 f1e81a05 2019-08-10 stsp return NULL;
4582 f1e81a05 2019-08-10 stsp }
4583 f1e81a05 2019-08-10 stsp
4584 f1e81a05 2019-08-10 stsp static const struct got_error *
4585 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4586 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4587 f1e81a05 2019-08-10 stsp {
4588 f1e81a05 2019-08-10 stsp const struct got_error *err;
4589 f1e81a05 2019-08-10 stsp
4590 f1e81a05 2019-08-10 stsp if (outfile) {
4591 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4592 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4593 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4594 f1e81a05 2019-08-10 stsp if (err)
4595 f1e81a05 2019-08-10 stsp return err;
4596 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4597 f1e81a05 2019-08-10 stsp }
4598 f1e81a05 2019-08-10 stsp }
4599 f1e81a05 2019-08-10 stsp if (rejectfile) {
4600 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4601 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4602 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4603 f1e81a05 2019-08-10 stsp if (err)
4604 f1e81a05 2019-08-10 stsp return err;
4605 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4606 f1e81a05 2019-08-10 stsp }
4607 33aa809d 2019-08-08 stsp }
4608 33aa809d 2019-08-08 stsp
4609 33aa809d 2019-08-08 stsp return NULL;
4610 33aa809d 2019-08-08 stsp }
4611 33aa809d 2019-08-08 stsp
4612 33aa809d 2019-08-08 stsp static const struct got_error *
4613 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4614 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4615 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4616 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4617 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4618 33aa809d 2019-08-08 stsp {
4619 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4620 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4621 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4622 33aa809d 2019-08-08 stsp FILE *hunkfile;
4623 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4624 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4625 fe621944 2020-11-10 stsp int rc;
4626 33aa809d 2019-08-08 stsp
4627 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4628 33aa809d 2019-08-08 stsp
4629 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4630 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4631 fe621944 2020-11-10 stsp start_old = cc.left.start;
4632 fe621944 2020-11-10 stsp end_old = cc.left.end;
4633 fe621944 2020-11-10 stsp start_new = cc.right.start;
4634 fe621944 2020-11-10 stsp end_new = cc.right.end;
4635 33aa809d 2019-08-08 stsp
4636 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4637 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4638 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4639 33aa809d 2019-08-08 stsp
4640 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4641 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4642 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4643 33aa809d 2019-08-08 stsp
4644 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4645 fe621944 2020-11-10 stsp if (diff_state == NULL)
4646 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4647 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4648 fe621944 2020-11-10 stsp
4649 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4650 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4651 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4652 33aa809d 2019-08-08 stsp goto done;
4653 33aa809d 2019-08-08 stsp }
4654 fe621944 2020-11-10 stsp
4655 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4656 fe621944 2020-11-10 stsp diff_result, &cc);
4657 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4658 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4659 33aa809d 2019-08-08 stsp goto done;
4660 33aa809d 2019-08-08 stsp }
4661 fe621944 2020-11-10 stsp
4662 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4663 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4664 33aa809d 2019-08-08 stsp goto done;
4665 33aa809d 2019-08-08 stsp }
4666 33aa809d 2019-08-08 stsp
4667 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4668 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4669 33aa809d 2019-08-08 stsp if (err)
4670 33aa809d 2019-08-08 stsp goto done;
4671 33aa809d 2019-08-08 stsp
4672 33aa809d 2019-08-08 stsp switch (*choice) {
4673 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4674 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4675 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4676 33aa809d 2019-08-08 stsp break;
4677 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4678 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4679 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4680 33aa809d 2019-08-08 stsp break;
4681 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4682 33aa809d 2019-08-08 stsp break;
4683 33aa809d 2019-08-08 stsp default:
4684 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4685 33aa809d 2019-08-08 stsp break;
4686 33aa809d 2019-08-08 stsp }
4687 33aa809d 2019-08-08 stsp done:
4688 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4689 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4690 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4691 33aa809d 2019-08-08 stsp return err;
4692 33aa809d 2019-08-08 stsp }
4693 33aa809d 2019-08-08 stsp
4694 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4695 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4696 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4697 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4698 1f1abb7e 2019-08-08 stsp void *progress_arg;
4699 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4700 33aa809d 2019-08-08 stsp void *patch_arg;
4701 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4702 f259c4c1 2021-09-24 stsp int unlink_added_files;
4703 af179be7 2023-04-14 stsp struct got_pathlist_head *added_files_to_unlink;
4704 1f1abb7e 2019-08-08 stsp };
4705 a129376b 2019-03-28 stsp
4706 e20a8b6f 2019-06-04 stsp static const struct got_error *
4707 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4708 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4709 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4710 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4711 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4712 33aa809d 2019-08-08 stsp {
4713 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4714 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4715 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4716 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4717 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4718 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4719 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4720 fe621944 2020-11-10 stsp struct stat sb2;
4721 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4722 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4723 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4724 33aa809d 2019-08-08 stsp
4725 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4726 33aa809d 2019-08-08 stsp
4727 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4728 33aa809d 2019-08-08 stsp if (err)
4729 33aa809d 2019-08-08 stsp return err;
4730 33aa809d 2019-08-08 stsp
4731 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4732 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4733 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4734 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4735 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4736 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4737 fa3cef63 2020-07-23 stsp goto done;
4738 fa3cef63 2020-07-23 stsp }
4739 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4740 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4741 c0df5966 2021-12-31 stsp if (link_len == -1) {
4742 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4743 c0df5966 2021-12-31 stsp path2);
4744 c0df5966 2021-12-31 stsp }
4745 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4746 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4747 12463d8b 2019-12-13 stsp }
4748 12463d8b 2019-12-13 stsp } else {
4749 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4750 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4751 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4752 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4753 fa3cef63 2020-07-23 stsp goto done;
4754 fa3cef63 2020-07-23 stsp }
4755 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4756 fa3cef63 2020-07-23 stsp sizeof(link_target));
4757 fa3cef63 2020-07-23 stsp if (link_len == -1)
4758 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4759 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4760 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4761 12463d8b 2019-12-13 stsp }
4762 1ebedb77 2019-10-19 stsp }
4763 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4764 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4765 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4766 fa3cef63 2020-07-23 stsp goto done;
4767 fa3cef63 2020-07-23 stsp }
4768 1ebedb77 2019-10-19 stsp
4769 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4770 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4771 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4772 fa3cef63 2020-07-23 stsp goto done;
4773 fa3cef63 2020-07-23 stsp }
4774 fa3cef63 2020-07-23 stsp fd2 = -1;
4775 fa3cef63 2020-07-23 stsp } else {
4776 fa3cef63 2020-07-23 stsp size_t n;
4777 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4778 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4779 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4780 fa3cef63 2020-07-23 stsp goto done;
4781 fa3cef63 2020-07-23 stsp }
4782 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4783 fa3cef63 2020-07-23 stsp if (n != link_len) {
4784 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4785 fa3cef63 2020-07-23 stsp goto done;
4786 fa3cef63 2020-07-23 stsp }
4787 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4788 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4789 fa3cef63 2020-07-23 stsp goto done;
4790 fa3cef63 2020-07-23 stsp }
4791 fa3cef63 2020-07-23 stsp rewind(f2);
4792 33aa809d 2019-08-08 stsp }
4793 33aa809d 2019-08-08 stsp
4794 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4795 eb81bc23 2022-06-28 tracey if (fd == -1) {
4796 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4797 eb81bc23 2022-06-28 tracey goto done;
4798 eb81bc23 2022-06-28 tracey }
4799 eb81bc23 2022-06-28 tracey
4800 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4801 33aa809d 2019-08-08 stsp if (err)
4802 33aa809d 2019-08-08 stsp goto done;
4803 33aa809d 2019-08-08 stsp
4804 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4805 33aa809d 2019-08-08 stsp if (err)
4806 33aa809d 2019-08-08 stsp goto done;
4807 33aa809d 2019-08-08 stsp
4808 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4809 33aa809d 2019-08-08 stsp if (err)
4810 33aa809d 2019-08-08 stsp goto done;
4811 33aa809d 2019-08-08 stsp
4812 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4813 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4814 33aa809d 2019-08-08 stsp if (err)
4815 33aa809d 2019-08-08 stsp goto done;
4816 33aa809d 2019-08-08 stsp
4817 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4818 b90054ed 2022-11-01 stsp "");
4819 33aa809d 2019-08-08 stsp if (err)
4820 33aa809d 2019-08-08 stsp goto done;
4821 33aa809d 2019-08-08 stsp
4822 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4823 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4824 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4825 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4826 fe621944 2020-11-10 stsp
4827 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4828 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4829 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4830 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4831 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4832 fe621944 2020-11-10 stsp nchanges++;
4833 fe621944 2020-11-10 stsp }
4834 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4835 33aa809d 2019-08-08 stsp int choice;
4836 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4837 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4838 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4839 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4840 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4841 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4842 33aa809d 2019-08-08 stsp if (err)
4843 33aa809d 2019-08-08 stsp goto done;
4844 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4845 33aa809d 2019-08-08 stsp have_content = 1;
4846 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4847 33aa809d 2019-08-08 stsp break;
4848 33aa809d 2019-08-08 stsp }
4849 1ebedb77 2019-10-19 stsp if (have_content) {
4850 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4851 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4852 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4853 1ebedb77 2019-10-19 stsp if (err)
4854 1ebedb77 2019-10-19 stsp goto done;
4855 1ebedb77 2019-10-19 stsp
4856 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4857 b2b3fce1 2022-10-29 op mode_t mode;
4858 b2b3fce1 2022-10-29 op
4859 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4860 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4861 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4862 fa3cef63 2020-07-23 stsp goto done;
4863 fa3cef63 2020-07-23 stsp }
4864 1ebedb77 2019-10-19 stsp }
4865 1ebedb77 2019-10-19 stsp }
4866 33aa809d 2019-08-08 stsp done:
4867 33aa809d 2019-08-08 stsp free(id_str);
4868 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4869 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4870 33aa809d 2019-08-08 stsp if (blob)
4871 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4872 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4873 fe621944 2020-11-10 stsp if (err == NULL)
4874 fe621944 2020-11-10 stsp err = free_err;
4875 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4876 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4877 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4878 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4879 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4880 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4881 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4882 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4883 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4884 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4885 33aa809d 2019-08-08 stsp if (err || !have_content) {
4886 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4887 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4888 33aa809d 2019-08-08 stsp free(*path_outfile);
4889 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4890 33aa809d 2019-08-08 stsp }
4891 33aa809d 2019-08-08 stsp free(path1);
4892 33aa809d 2019-08-08 stsp return err;
4893 33aa809d 2019-08-08 stsp }
4894 33aa809d 2019-08-08 stsp
4895 33aa809d 2019-08-08 stsp static const struct got_error *
4896 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4897 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4898 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4899 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4900 a129376b 2019-03-28 stsp {
4901 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4902 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4903 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4904 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4905 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4906 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4907 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4908 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4909 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4910 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4911 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4912 eb81bc23 2022-06-28 tracey int fd = -1;
4913 a129376b 2019-03-28 stsp
4914 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4915 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4916 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4917 d3bcc3d1 2019-08-08 stsp return NULL;
4918 3d69ad8d 2019-08-17 semarie
4919 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4920 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4921 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4922 d3bcc3d1 2019-08-08 stsp
4923 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4924 65084dad 2019-08-08 stsp if (ie == NULL)
4925 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4926 a129376b 2019-03-28 stsp
4927 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4928 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4929 a129376b 2019-03-28 stsp if (err) {
4930 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4931 a129376b 2019-03-28 stsp goto done;
4932 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4933 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4934 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4935 e20a8b6f 2019-06-04 stsp goto done;
4936 e20a8b6f 2019-06-04 stsp }
4937 a129376b 2019-03-28 stsp }
4938 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4939 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4940 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4941 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4942 a129376b 2019-03-28 stsp goto done;
4943 a129376b 2019-03-28 stsp }
4944 a129376b 2019-03-28 stsp } else {
4945 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4946 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4947 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4948 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4949 a129376b 2019-03-28 stsp goto done;
4950 a129376b 2019-03-28 stsp }
4951 a129376b 2019-03-28 stsp } else {
4952 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4953 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4954 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4955 a129376b 2019-03-28 stsp goto done;
4956 a129376b 2019-03-28 stsp }
4957 a129376b 2019-03-28 stsp }
4958 a129376b 2019-03-28 stsp }
4959 a129376b 2019-03-28 stsp
4960 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4961 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4962 a44927cc 2022-04-07 stsp if (err)
4963 a44927cc 2022-04-07 stsp goto done;
4964 a44927cc 2022-04-07 stsp
4965 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4966 a9fa2909 2019-07-27 stsp if (err) {
4967 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4968 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4969 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4970 a9fa2909 2019-07-27 stsp goto done;
4971 a9fa2909 2019-07-27 stsp } else {
4972 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4973 a9fa2909 2019-07-27 stsp if (err)
4974 a9fa2909 2019-07-27 stsp goto done;
4975 a9fa2909 2019-07-27 stsp
4976 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4977 1233e6b6 2020-10-19 stsp if (err)
4978 a9fa2909 2019-07-27 stsp goto done;
4979 a9fa2909 2019-07-27 stsp
4980 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4981 1233e6b6 2020-10-19 stsp free(te_name);
4982 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4983 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4984 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4985 a9fa2909 2019-07-27 stsp goto done;
4986 a9fa2909 2019-07-27 stsp }
4987 a129376b 2019-03-28 stsp }
4988 a129376b 2019-03-28 stsp
4989 a129376b 2019-03-28 stsp switch (status) {
4990 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4991 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4992 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4993 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4994 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4995 33aa809d 2019-08-08 stsp if (err)
4996 33aa809d 2019-08-08 stsp goto done;
4997 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4998 33aa809d 2019-08-08 stsp break;
4999 33aa809d 2019-08-08 stsp }
5000 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5001 1f1abb7e 2019-08-08 stsp ie->path);
5002 1ee397ad 2019-07-12 stsp if (err)
5003 1ee397ad 2019-07-12 stsp goto done;
5004 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
5005 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
5006 af179be7 2023-04-14 stsp int do_unlink = a->added_files_to_unlink ? 0 : 1;
5007 af179be7 2023-04-14 stsp
5008 af179be7 2023-04-14 stsp if (a->added_files_to_unlink) {
5009 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
5010 af179be7 2023-04-14 stsp
5011 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, a->added_files_to_unlink,
5012 af179be7 2023-04-14 stsp entry) {
5013 af179be7 2023-04-14 stsp if (got_path_cmp(pe->path, relpath,
5014 af179be7 2023-04-14 stsp pe->path_len, strlen(relpath)))
5015 af179be7 2023-04-14 stsp continue;
5016 af179be7 2023-04-14 stsp do_unlink = 1;
5017 af179be7 2023-04-14 stsp break;
5018 af179be7 2023-04-14 stsp }
5019 f259c4c1 2021-09-24 stsp }
5020 af179be7 2023-04-14 stsp
5021 af179be7 2023-04-14 stsp if (do_unlink) {
5022 af179be7 2023-04-14 stsp if (asprintf(&ondisk_path, "%s/%s",
5023 af179be7 2023-04-14 stsp got_worktree_get_root_path(a->worktree),
5024 af179be7 2023-04-14 stsp relpath) == -1) {
5025 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
5026 af179be7 2023-04-14 stsp goto done;
5027 af179be7 2023-04-14 stsp }
5028 af179be7 2023-04-14 stsp if (unlink(ondisk_path) == -1) {
5029 af179be7 2023-04-14 stsp err = got_error_from_errno2("unlink",
5030 af179be7 2023-04-14 stsp ondisk_path);
5031 af179be7 2023-04-14 stsp break;
5032 af179be7 2023-04-14 stsp }
5033 f259c4c1 2021-09-24 stsp }
5034 f259c4c1 2021-09-24 stsp }
5035 a129376b 2019-03-28 stsp break;
5036 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
5037 33aa809d 2019-08-08 stsp if (a->patch_cb) {
5038 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
5039 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
5040 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
5041 33aa809d 2019-08-08 stsp if (err)
5042 33aa809d 2019-08-08 stsp goto done;
5043 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
5044 33aa809d 2019-08-08 stsp break;
5045 33aa809d 2019-08-08 stsp }
5046 33aa809d 2019-08-08 stsp /* fall through */
5047 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
5048 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
5049 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
5050 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
5051 e20a8b6f 2019-06-04 stsp struct got_object_id id;
5052 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5053 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
5054 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
5055 b4b2adf5 2023-02-09 op else
5056 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
5057 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5058 eb81bc23 2022-06-28 tracey if (fd == -1) {
5059 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5060 eb81bc23 2022-06-28 tracey goto done;
5061 eb81bc23 2022-06-28 tracey }
5062 eb81bc23 2022-06-28 tracey
5063 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5064 a129376b 2019-03-28 stsp if (err)
5065 65084dad 2019-08-08 stsp goto done;
5066 65084dad 2019-08-08 stsp
5067 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5068 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
5069 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
5070 a129376b 2019-03-28 stsp goto done;
5071 65084dad 2019-08-08 stsp }
5072 65084dad 2019-08-08 stsp
5073 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5074 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
5075 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
5076 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
5077 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
5078 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
5079 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
5080 33aa809d 2019-08-08 stsp break;
5081 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5082 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
5083 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
5084 369fd7e5 2020-07-23 stsp path_content);
5085 369fd7e5 2020-07-23 stsp break;
5086 369fd7e5 2020-07-23 stsp }
5087 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5088 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5089 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5090 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
5091 369fd7e5 2020-07-23 stsp } else {
5092 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
5093 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
5094 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
5095 369fd7e5 2020-07-23 stsp goto done;
5096 369fd7e5 2020-07-23 stsp }
5097 33aa809d 2019-08-08 stsp }
5098 33aa809d 2019-08-08 stsp } else {
5099 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5100 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
5101 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
5102 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
5103 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
5104 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
5105 2e1fa222 2020-07-23 stsp } else {
5106 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
5107 2e1fa222 2020-07-23 stsp ie->path,
5108 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
5109 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
5110 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
5111 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
5112 2e1fa222 2020-07-23 stsp }
5113 a129376b 2019-03-28 stsp if (err)
5114 a129376b 2019-03-28 stsp goto done;
5115 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
5116 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
5117 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
5118 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
5119 437adc9d 2020-12-10 yzhong blob->id.sha1,
5120 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
5121 2e1fa222 2020-07-23 stsp if (err)
5122 2e1fa222 2020-07-23 stsp goto done;
5123 2e1fa222 2020-07-23 stsp }
5124 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
5125 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5126 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5127 33aa809d 2019-08-08 stsp }
5128 a129376b 2019-03-28 stsp }
5129 a129376b 2019-03-28 stsp break;
5130 e20a8b6f 2019-06-04 stsp }
5131 a129376b 2019-03-28 stsp default:
5132 1f1abb7e 2019-08-08 stsp break;
5133 a129376b 2019-03-28 stsp }
5134 a129376b 2019-03-28 stsp done:
5135 1f1abb7e 2019-08-08 stsp free(ondisk_path);
5136 33aa809d 2019-08-08 stsp free(path_content);
5137 e20a8b6f 2019-06-04 stsp free(parent_path);
5138 a129376b 2019-03-28 stsp free(tree_path);
5139 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5140 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5141 a129376b 2019-03-28 stsp if (blob)
5142 a129376b 2019-03-28 stsp got_object_blob_close(blob);
5143 a129376b 2019-03-28 stsp if (tree)
5144 a129376b 2019-03-28 stsp got_object_tree_close(tree);
5145 a129376b 2019-03-28 stsp free(tree_id);
5146 a44927cc 2022-04-07 stsp if (base_commit)
5147 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5148 e20a8b6f 2019-06-04 stsp return err;
5149 e20a8b6f 2019-06-04 stsp }
5150 e20a8b6f 2019-06-04 stsp
5151 e20a8b6f 2019-06-04 stsp const struct got_error *
5152 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5153 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5154 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5155 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5156 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5157 e20a8b6f 2019-06-04 stsp {
5158 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5159 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5160 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5161 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5162 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5163 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5164 e20a8b6f 2019-06-04 stsp
5165 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5166 e20a8b6f 2019-06-04 stsp if (err)
5167 e20a8b6f 2019-06-04 stsp return err;
5168 e20a8b6f 2019-06-04 stsp
5169 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5170 e20a8b6f 2019-06-04 stsp if (err)
5171 e20a8b6f 2019-06-04 stsp goto done;
5172 e20a8b6f 2019-06-04 stsp
5173 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5174 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5175 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5176 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5177 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5178 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5179 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5180 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5181 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5182 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5183 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5184 e20a8b6f 2019-06-04 stsp if (err)
5185 af12c6b9 2019-06-04 stsp break;
5186 e20a8b6f 2019-06-04 stsp }
5187 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5188 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5189 e20a8b6f 2019-06-04 stsp err = sync_err;
5190 af12c6b9 2019-06-04 stsp done:
5191 fb399478 2019-07-12 stsp free(fileindex_path);
5192 a129376b 2019-03-28 stsp if (fileindex)
5193 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5194 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5195 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5196 a129376b 2019-03-28 stsp err = unlockerr;
5197 c4296144 2019-05-09 stsp return err;
5198 c4296144 2019-05-09 stsp }
5199 c4296144 2019-05-09 stsp
5200 cf066bf8 2019-05-09 stsp static void
5201 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5202 cf066bf8 2019-05-09 stsp {
5203 24519714 2019-05-09 stsp free(ct->path);
5204 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5205 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5206 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5207 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5208 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5209 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5210 cf066bf8 2019-05-09 stsp free(ct);
5211 cf066bf8 2019-05-09 stsp }
5212 24519714 2019-05-09 stsp
5213 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5214 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5215 24519714 2019-05-09 stsp struct got_repository *repo;
5216 24519714 2019-05-09 stsp struct got_worktree *worktree;
5217 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5218 5f8a88c6 2019-08-03 stsp int have_staged_files;
5219 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5220 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5221 12383673 2023-02-18 mark int commit_conflicts;
5222 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5223 2a47b1e5 2022-11-01 stsp FILE *f1;
5224 2a47b1e5 2022-11-01 stsp FILE *f2;
5225 24519714 2019-05-09 stsp };
5226 2a47b1e5 2022-11-01 stsp
5227 2a47b1e5 2022-11-01 stsp /*
5228 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5229 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5230 2a47b1e5 2022-11-01 stsp */
5231 2a47b1e5 2022-11-01 stsp static const struct got_error *
5232 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5233 2a47b1e5 2022-11-01 stsp const char *abspath)
5234 2a47b1e5 2022-11-01 stsp {
5235 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5236 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5237 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5238 2a47b1e5 2022-11-01 stsp
5239 2a47b1e5 2022-11-01 stsp *fd = -1;
5240 2a47b1e5 2022-11-01 stsp
5241 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5242 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5243 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5244 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5245 2a47b1e5 2022-11-01 stsp } else {
5246 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5247 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5248 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5249 2a47b1e5 2022-11-01 stsp }
5250 2a47b1e5 2022-11-01 stsp
5251 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5252 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5253 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5254 2a47b1e5 2022-11-01 stsp
5255 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5256 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5257 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5258 2a47b1e5 2022-11-01 stsp goto done;
5259 2a47b1e5 2022-11-01 stsp }
5260 2a47b1e5 2022-11-01 stsp
5261 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5262 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5263 2a47b1e5 2022-11-01 stsp goto done;
5264 2a47b1e5 2022-11-01 stsp }
5265 2a47b1e5 2022-11-01 stsp done:
5266 2a47b1e5 2022-11-01 stsp if (err) {
5267 2a47b1e5 2022-11-01 stsp close(*fd);
5268 2a47b1e5 2022-11-01 stsp *fd = -1;
5269 2a47b1e5 2022-11-01 stsp }
5270 2a47b1e5 2022-11-01 stsp return err;
5271 2a47b1e5 2022-11-01 stsp }
5272 2a47b1e5 2022-11-01 stsp
5273 2a47b1e5 2022-11-01 stsp static const struct got_error *
5274 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5275 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5276 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5277 2a47b1e5 2022-11-01 stsp {
5278 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5279 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5280 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5281 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5282 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5283 2a47b1e5 2022-11-01 stsp struct stat sb;
5284 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5285 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5286 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5287 2a47b1e5 2022-11-01 stsp
5288 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5289 4ba5cca9 2022-11-01 stsp
5290 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5291 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5292 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5293 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5294 4ba5cca9 2022-11-01 stsp return NULL;
5295 4ba5cca9 2022-11-01 stsp } else {
5296 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5297 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5298 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5299 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5300 4ba5cca9 2022-11-01 stsp return NULL;
5301 4ba5cca9 2022-11-01 stsp }
5302 2a47b1e5 2022-11-01 stsp
5303 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5304 2a47b1e5 2022-11-01 stsp if (err)
5305 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5306 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5307 2a47b1e5 2022-11-01 stsp if (err)
5308 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5309 2a47b1e5 2022-11-01 stsp
5310 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5311 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5312 2a47b1e5 2022-11-01 stsp if (err)
5313 2a47b1e5 2022-11-01 stsp return err;
5314 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5315 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5316 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5317 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5318 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5319 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5320 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5321 2a47b1e5 2022-11-01 stsp }
5322 2a47b1e5 2022-11-01 stsp
5323 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5324 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5325 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5326 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5327 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5328 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5329 2a47b1e5 2022-11-01 stsp break;
5330 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5331 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5332 2a47b1e5 2022-11-01 stsp break;
5333 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5334 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5335 2a47b1e5 2022-11-01 stsp break;
5336 2a47b1e5 2022-11-01 stsp default:
5337 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5338 2a47b1e5 2022-11-01 stsp }
5339 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5340 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5341 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5342 2a47b1e5 2022-11-01 stsp goto done;
5343 2a47b1e5 2022-11-01 stsp }
5344 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5345 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5346 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5347 2a47b1e5 2022-11-01 stsp goto done;
5348 2a47b1e5 2022-11-01 stsp }
5349 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5350 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5351 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5352 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5353 2a47b1e5 2022-11-01 stsp goto done;
5354 2a47b1e5 2022-11-01 stsp }
5355 2a47b1e5 2022-11-01 stsp
5356 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5357 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5358 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5359 2a47b1e5 2022-11-01 stsp goto done;
5360 2a47b1e5 2022-11-01 stsp }
5361 2a47b1e5 2022-11-01 stsp
5362 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5363 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5364 2a47b1e5 2022-11-01 stsp 8192, fd1);
5365 2a47b1e5 2022-11-01 stsp if (err)
5366 2a47b1e5 2022-11-01 stsp goto done;
5367 2a47b1e5 2022-11-01 stsp }
5368 2a47b1e5 2022-11-01 stsp
5369 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5370 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5371 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5372 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5373 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5374 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5375 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5376 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5377 2a47b1e5 2022-11-01 stsp goto done;
5378 2a47b1e5 2022-11-01 stsp }
5379 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5380 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5381 2a47b1e5 2022-11-01 stsp if (err)
5382 2a47b1e5 2022-11-01 stsp goto done;
5383 2a47b1e5 2022-11-01 stsp }
5384 2a47b1e5 2022-11-01 stsp } else {
5385 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5386 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5387 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5388 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5389 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5390 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5391 2a47b1e5 2022-11-01 stsp goto done;
5392 2a47b1e5 2022-11-01 stsp }
5393 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5394 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5395 2a47b1e5 2022-11-01 stsp if (err)
5396 2a47b1e5 2022-11-01 stsp goto done;
5397 2a47b1e5 2022-11-01 stsp }
5398 2a47b1e5 2022-11-01 stsp }
5399 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5400 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5401 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5402 2a47b1e5 2022-11-01 stsp goto done;
5403 2a47b1e5 2022-11-01 stsp }
5404 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5405 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5406 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5407 2a47b1e5 2022-11-01 stsp goto done;
5408 2a47b1e5 2022-11-01 stsp }
5409 2a47b1e5 2022-11-01 stsp fd = -1;
5410 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5411 2a47b1e5 2022-11-01 stsp }
5412 2a47b1e5 2022-11-01 stsp
5413 2a47b1e5 2022-11-01 stsp if (blob1) {
5414 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5415 2a47b1e5 2022-11-01 stsp f1, blob1);
5416 2a47b1e5 2022-11-01 stsp if (err)
5417 2a47b1e5 2022-11-01 stsp goto done;
5418 2a47b1e5 2022-11-01 stsp }
5419 2a47b1e5 2022-11-01 stsp
5420 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5421 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5422 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5423 2a47b1e5 2022-11-01 stsp done:
5424 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5425 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5426 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5427 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5428 2a47b1e5 2022-11-01 stsp if (blob1)
5429 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5430 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5431 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5432 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5433 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5434 2a47b1e5 2022-11-01 stsp return err;
5435 2a47b1e5 2022-11-01 stsp }
5436 cf066bf8 2019-05-09 stsp
5437 c4296144 2019-05-09 stsp static const struct got_error *
5438 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5439 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5440 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5441 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5442 c4296144 2019-05-09 stsp {
5443 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5444 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5445 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5446 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5447 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5448 768aea60 2019-05-09 stsp struct stat sb;
5449 c4296144 2019-05-09 stsp
5450 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5451 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5452 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5453 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5454 5f8a88c6 2019-08-03 stsp return NULL;
5455 5f8a88c6 2019-08-03 stsp } else {
5456 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5457 12383673 2023-02-18 mark printf("C %s\n", relpath);
5458 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5459 12383673 2023-02-18 mark }
5460 c4296144 2019-05-09 stsp
5461 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5462 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5463 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5464 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5465 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5466 5f8a88c6 2019-08-03 stsp return NULL;
5467 5f8a88c6 2019-08-03 stsp }
5468 0b5cc0d6 2019-05-09 stsp
5469 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5470 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5471 036813ee 2019-05-09 stsp goto done;
5472 036813ee 2019-05-09 stsp }
5473 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5474 036813ee 2019-05-09 stsp parent_path = strdup("");
5475 69960a46 2019-05-09 stsp if (parent_path == NULL)
5476 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5477 69960a46 2019-05-09 stsp } else {
5478 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5479 69960a46 2019-05-09 stsp if (err)
5480 69960a46 2019-05-09 stsp return err;
5481 69960a46 2019-05-09 stsp }
5482 c4296144 2019-05-09 stsp
5483 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5484 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5485 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5486 c4296144 2019-05-09 stsp goto done;
5487 768aea60 2019-05-09 stsp }
5488 768aea60 2019-05-09 stsp
5489 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5490 768aea60 2019-05-09 stsp relpath) == -1) {
5491 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5492 768aea60 2019-05-09 stsp goto done;
5493 768aea60 2019-05-09 stsp }
5494 0aeb8099 2020-07-23 stsp
5495 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5496 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5497 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5498 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5499 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5500 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5501 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5502 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5503 0aeb8099 2020-07-23 stsp break;
5504 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5505 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5506 0aeb8099 2020-07-23 stsp break;
5507 0aeb8099 2020-07-23 stsp default:
5508 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5509 0aeb8099 2020-07-23 stsp goto done;
5510 0aeb8099 2020-07-23 stsp }
5511 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5512 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5513 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5514 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5515 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5516 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5517 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5518 12463d8b 2019-12-13 stsp ct->ondisk_path);
5519 12463d8b 2019-12-13 stsp goto done;
5520 12463d8b 2019-12-13 stsp }
5521 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5522 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5523 768aea60 2019-05-09 stsp goto done;
5524 768aea60 2019-05-09 stsp }
5525 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5526 c4296144 2019-05-09 stsp }
5527 c4296144 2019-05-09 stsp
5528 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5529 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5530 44d03001 2019-05-09 stsp relpath) == -1) {
5531 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5532 44d03001 2019-05-09 stsp goto done;
5533 44d03001 2019-05-09 stsp }
5534 44d03001 2019-05-09 stsp
5535 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5536 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5537 35213c7c 2020-07-23 stsp int is_bad_symlink;
5538 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5539 35213c7c 2020-07-23 stsp ssize_t target_len;
5540 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5541 35213c7c 2020-07-23 stsp sizeof(target_path));
5542 35213c7c 2020-07-23 stsp if (target_len == -1) {
5543 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5544 35213c7c 2020-07-23 stsp ct->ondisk_path);
5545 35213c7c 2020-07-23 stsp goto done;
5546 35213c7c 2020-07-23 stsp }
5547 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5548 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5549 35213c7c 2020-07-23 stsp if (err)
5550 35213c7c 2020-07-23 stsp goto done;
5551 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5552 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5553 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5554 35213c7c 2020-07-23 stsp goto done;
5555 35213c7c 2020-07-23 stsp }
5556 35213c7c 2020-07-23 stsp }
5557 35213c7c 2020-07-23 stsp
5558 35213c7c 2020-07-23 stsp
5559 cf066bf8 2019-05-09 stsp ct->status = status;
5560 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5561 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5562 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5563 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5564 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5565 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5566 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5567 b416585c 2019-05-13 stsp goto done;
5568 b416585c 2019-05-13 stsp }
5569 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5570 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5571 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5572 f0b75401 2019-08-03 stsp goto done;
5573 f0b75401 2019-08-03 stsp }
5574 f0b75401 2019-08-03 stsp }
5575 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5576 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5577 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5578 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5579 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5580 036813ee 2019-05-09 stsp goto done;
5581 036813ee 2019-05-09 stsp }
5582 ca2503ea 2019-05-09 stsp }
5583 24519714 2019-05-09 stsp ct->path = strdup(path);
5584 24519714 2019-05-09 stsp if (ct->path == NULL) {
5585 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5586 24519714 2019-05-09 stsp goto done;
5587 24519714 2019-05-09 stsp }
5588 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5589 2a47b1e5 2022-11-01 stsp if (err)
5590 2a47b1e5 2022-11-01 stsp goto done;
5591 2a47b1e5 2022-11-01 stsp
5592 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5593 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5594 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5595 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5596 2a47b1e5 2022-11-01 stsp if (err)
5597 2a47b1e5 2022-11-01 stsp goto done;
5598 2a47b1e5 2022-11-01 stsp }
5599 c4296144 2019-05-09 stsp done:
5600 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5601 ed175427 2019-05-09 stsp free_commitable(ct);
5602 24519714 2019-05-09 stsp free(parent_path);
5603 036813ee 2019-05-09 stsp free(path);
5604 a129376b 2019-03-28 stsp return err;
5605 a129376b 2019-03-28 stsp }
5606 c4296144 2019-05-09 stsp
5607 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5608 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5609 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5610 036813ee 2019-05-09 stsp struct got_repository *);
5611 ed175427 2019-05-09 stsp
5612 ed175427 2019-05-09 stsp static const struct got_error *
5613 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5614 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5615 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5616 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5617 afa376bf 2019-05-09 stsp struct got_repository *repo)
5618 ed175427 2019-05-09 stsp {
5619 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5620 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5621 036813ee 2019-05-09 stsp char *subpath;
5622 ed175427 2019-05-09 stsp
5623 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5624 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5625 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5626 ed175427 2019-05-09 stsp
5627 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5628 ed175427 2019-05-09 stsp if (err)
5629 ed175427 2019-05-09 stsp return err;
5630 ed175427 2019-05-09 stsp
5631 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5632 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5633 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5634 036813ee 2019-05-09 stsp free(subpath);
5635 036813ee 2019-05-09 stsp return err;
5636 036813ee 2019-05-09 stsp }
5637 ed175427 2019-05-09 stsp
5638 036813ee 2019-05-09 stsp static const struct got_error *
5639 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5640 036813ee 2019-05-09 stsp {
5641 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5642 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5643 ed175427 2019-05-09 stsp
5644 036813ee 2019-05-09 stsp *match = 0;
5645 ed175427 2019-05-09 stsp
5646 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5647 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5648 0f63689d 2019-05-10 stsp return NULL;
5649 036813ee 2019-05-09 stsp }
5650 0b5cc0d6 2019-05-09 stsp
5651 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5652 0f63689d 2019-05-10 stsp if (err)
5653 0f63689d 2019-05-10 stsp return err;
5654 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5655 036813ee 2019-05-09 stsp free(ct_parent_path);
5656 036813ee 2019-05-09 stsp return err;
5657 036813ee 2019-05-09 stsp }
5658 0b5cc0d6 2019-05-09 stsp
5659 768aea60 2019-05-09 stsp static mode_t
5660 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5661 768aea60 2019-05-09 stsp {
5662 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5663 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5664 3d9a4ec4 2020-07-23 stsp
5665 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5666 768aea60 2019-05-09 stsp }
5667 768aea60 2019-05-09 stsp
5668 036813ee 2019-05-09 stsp static const struct got_error *
5669 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5670 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5671 036813ee 2019-05-09 stsp {
5672 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5673 ca2503ea 2019-05-09 stsp
5674 036813ee 2019-05-09 stsp *new_te = NULL;
5675 0b5cc0d6 2019-05-09 stsp
5676 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5677 036813ee 2019-05-09 stsp if (err)
5678 036813ee 2019-05-09 stsp goto done;
5679 0b5cc0d6 2019-05-09 stsp
5680 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5681 036813ee 2019-05-09 stsp
5682 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5683 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5684 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5685 5f8a88c6 2019-08-03 stsp else
5686 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5687 036813ee 2019-05-09 stsp done:
5688 036813ee 2019-05-09 stsp if (err && *new_te) {
5689 56e0773d 2019-11-28 stsp free(*new_te);
5690 036813ee 2019-05-09 stsp *new_te = NULL;
5691 036813ee 2019-05-09 stsp }
5692 036813ee 2019-05-09 stsp return err;
5693 036813ee 2019-05-09 stsp }
5694 036813ee 2019-05-09 stsp
5695 036813ee 2019-05-09 stsp static const struct got_error *
5696 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5697 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5698 036813ee 2019-05-09 stsp {
5699 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5700 102b254e 2020-10-19 stsp char *ct_name = NULL;
5701 036813ee 2019-05-09 stsp
5702 036813ee 2019-05-09 stsp *new_te = NULL;
5703 036813ee 2019-05-09 stsp
5704 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5705 036813ee 2019-05-09 stsp if (*new_te == NULL)
5706 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5707 036813ee 2019-05-09 stsp
5708 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5709 102b254e 2020-10-19 stsp if (err)
5710 036813ee 2019-05-09 stsp goto done;
5711 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5712 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5713 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5714 036813ee 2019-05-09 stsp goto done;
5715 036813ee 2019-05-09 stsp }
5716 036813ee 2019-05-09 stsp
5717 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5718 036813ee 2019-05-09 stsp
5719 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5720 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5721 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5722 5f8a88c6 2019-08-03 stsp else
5723 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5724 036813ee 2019-05-09 stsp done:
5725 102b254e 2020-10-19 stsp free(ct_name);
5726 036813ee 2019-05-09 stsp if (err && *new_te) {
5727 56e0773d 2019-11-28 stsp free(*new_te);
5728 036813ee 2019-05-09 stsp *new_te = NULL;
5729 036813ee 2019-05-09 stsp }
5730 036813ee 2019-05-09 stsp return err;
5731 036813ee 2019-05-09 stsp }
5732 036813ee 2019-05-09 stsp
5733 036813ee 2019-05-09 stsp static const struct got_error *
5734 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5735 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5736 036813ee 2019-05-09 stsp {
5737 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5738 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5739 036813ee 2019-05-09 stsp
5740 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5741 036813ee 2019-05-09 stsp if (err)
5742 036813ee 2019-05-09 stsp return err;
5743 036813ee 2019-05-09 stsp if (new_pe == NULL)
5744 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5745 036813ee 2019-05-09 stsp return NULL;
5746 afa376bf 2019-05-09 stsp }
5747 afa376bf 2019-05-09 stsp
5748 afa376bf 2019-05-09 stsp static const struct got_error *
5749 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5750 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5751 afa376bf 2019-05-09 stsp {
5752 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5753 5f8a88c6 2019-08-03 stsp unsigned char status;
5754 0ff8d236 2021-09-28 stsp
5755 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5756 0ff8d236 2021-09-28 stsp return NULL;
5757 5f8a88c6 2019-08-03 stsp
5758 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5759 afa376bf 2019-05-09 stsp ct_path++;
5760 5f8a88c6 2019-08-03 stsp
5761 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5762 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5763 5f8a88c6 2019-08-03 stsp else
5764 5f8a88c6 2019-08-03 stsp status = ct->status;
5765 5f8a88c6 2019-08-03 stsp
5766 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5767 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5768 036813ee 2019-05-09 stsp }
5769 036813ee 2019-05-09 stsp
5770 036813ee 2019-05-09 stsp static const struct got_error *
5771 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5772 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5773 44d03001 2019-05-09 stsp {
5774 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5775 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5776 44d03001 2019-05-09 stsp char *te_path;
5777 44d03001 2019-05-09 stsp
5778 44d03001 2019-05-09 stsp *modified = 0;
5779 44d03001 2019-05-09 stsp
5780 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5781 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5782 44d03001 2019-05-09 stsp te->name) == -1)
5783 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5784 44d03001 2019-05-09 stsp
5785 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5786 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5787 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5788 44d03001 2019-05-09 stsp strlen(te_path));
5789 62d463ca 2020-10-20 naddy if (*modified)
5790 44d03001 2019-05-09 stsp break;
5791 44d03001 2019-05-09 stsp }
5792 44d03001 2019-05-09 stsp
5793 44d03001 2019-05-09 stsp free(te_path);
5794 44d03001 2019-05-09 stsp return err;
5795 44d03001 2019-05-09 stsp }
5796 44d03001 2019-05-09 stsp
5797 44d03001 2019-05-09 stsp static const struct got_error *
5798 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5799 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5800 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5801 036813ee 2019-05-09 stsp {
5802 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5803 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5804 036813ee 2019-05-09 stsp
5805 036813ee 2019-05-09 stsp *ctp = NULL;
5806 036813ee 2019-05-09 stsp
5807 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5808 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5809 036813ee 2019-05-09 stsp char *ct_name = NULL;
5810 036813ee 2019-05-09 stsp int path_matches;
5811 036813ee 2019-05-09 stsp
5812 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5813 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5814 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5815 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5816 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5817 5f8a88c6 2019-08-03 stsp continue;
5818 5f8a88c6 2019-08-03 stsp } else {
5819 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5820 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5821 5f8a88c6 2019-08-03 stsp continue;
5822 5f8a88c6 2019-08-03 stsp }
5823 036813ee 2019-05-09 stsp
5824 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5825 036813ee 2019-05-09 stsp continue;
5826 036813ee 2019-05-09 stsp
5827 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5828 62d463ca 2020-10-20 naddy if (err)
5829 036813ee 2019-05-09 stsp return err;
5830 036813ee 2019-05-09 stsp if (!path_matches)
5831 036813ee 2019-05-09 stsp continue;
5832 036813ee 2019-05-09 stsp
5833 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5834 d34b633e 2020-10-19 stsp if (err)
5835 d34b633e 2020-10-19 stsp return err;
5836 d34b633e 2020-10-19 stsp
5837 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5838 d34b633e 2020-10-19 stsp free(ct_name);
5839 036813ee 2019-05-09 stsp continue;
5840 d34b633e 2020-10-19 stsp }
5841 d34b633e 2020-10-19 stsp free(ct_name);
5842 036813ee 2019-05-09 stsp
5843 036813ee 2019-05-09 stsp *ctp = ct;
5844 036813ee 2019-05-09 stsp break;
5845 036813ee 2019-05-09 stsp }
5846 2b496619 2019-07-10 stsp
5847 2b496619 2019-07-10 stsp return err;
5848 2b496619 2019-07-10 stsp }
5849 2b496619 2019-07-10 stsp
5850 2b496619 2019-07-10 stsp static const struct got_error *
5851 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5852 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5853 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5854 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5855 2b496619 2019-07-10 stsp struct got_repository *repo)
5856 2b496619 2019-07-10 stsp {
5857 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5858 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5859 2b496619 2019-07-10 stsp char *subtree_path;
5860 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5861 ba580f68 2020-03-22 stsp int nentries;
5862 2b496619 2019-07-10 stsp
5863 2b496619 2019-07-10 stsp *new_tep = NULL;
5864 2b496619 2019-07-10 stsp
5865 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5866 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5867 2b496619 2019-07-10 stsp child_path) == -1)
5868 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5869 036813ee 2019-05-09 stsp
5870 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5871 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5872 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5873 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5874 56e0773d 2019-11-28 stsp
5875 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5876 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5877 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5878 2b496619 2019-07-10 stsp goto done;
5879 2b496619 2019-07-10 stsp }
5880 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5881 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5882 2b496619 2019-07-10 stsp if (err) {
5883 56e0773d 2019-11-28 stsp free(new_te);
5884 2b496619 2019-07-10 stsp goto done;
5885 2b496619 2019-07-10 stsp }
5886 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5887 2b496619 2019-07-10 stsp done:
5888 56e0773d 2019-11-28 stsp free(id);
5889 2b496619 2019-07-10 stsp free(subtree_path);
5890 2b496619 2019-07-10 stsp if (err == NULL)
5891 2b496619 2019-07-10 stsp *new_tep = new_te;
5892 036813ee 2019-05-09 stsp return err;
5893 036813ee 2019-05-09 stsp }
5894 036813ee 2019-05-09 stsp
5895 036813ee 2019-05-09 stsp static const struct got_error *
5896 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5897 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5898 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5899 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5900 036813ee 2019-05-09 stsp struct got_repository *repo)
5901 036813ee 2019-05-09 stsp {
5902 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5903 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5904 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5905 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5906 036813ee 2019-05-09 stsp
5907 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5908 ba580f68 2020-03-22 stsp *nentries = 0;
5909 036813ee 2019-05-09 stsp
5910 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5911 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5912 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5913 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5914 036813ee 2019-05-09 stsp
5915 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5916 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5917 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5918 036813ee 2019-05-09 stsp continue;
5919 036813ee 2019-05-09 stsp
5920 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5921 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5922 036813ee 2019-05-09 stsp continue;
5923 036813ee 2019-05-09 stsp
5924 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5925 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5926 036813ee 2019-05-09 stsp if (err)
5927 036813ee 2019-05-09 stsp goto done;
5928 036813ee 2019-05-09 stsp
5929 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5930 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5931 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5932 036813ee 2019-05-09 stsp if (err)
5933 036813ee 2019-05-09 stsp goto done;
5934 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5935 afa376bf 2019-05-09 stsp if (err)
5936 afa376bf 2019-05-09 stsp goto done;
5937 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5938 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5939 2b496619 2019-07-10 stsp if (err)
5940 2f51b5b3 2019-05-09 stsp goto done;
5941 ba580f68 2020-03-22 stsp (*nentries)++;
5942 2b496619 2019-07-10 stsp } else {
5943 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5944 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5945 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5946 2b496619 2019-07-10 stsp == NULL) {
5947 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5948 2b496619 2019-07-10 stsp child_path, path_base_tree,
5949 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5950 2b496619 2019-07-10 stsp repo);
5951 2b496619 2019-07-10 stsp if (err)
5952 2b496619 2019-07-10 stsp goto done;
5953 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5954 2b496619 2019-07-10 stsp if (err)
5955 2b496619 2019-07-10 stsp goto done;
5956 ba580f68 2020-03-22 stsp (*nentries)++;
5957 9ba0479c 2019-05-10 stsp }
5958 ed175427 2019-05-09 stsp }
5959 2f51b5b3 2019-05-09 stsp }
5960 2f51b5b3 2019-05-09 stsp
5961 2f51b5b3 2019-05-09 stsp if (base_tree) {
5962 56e0773d 2019-11-28 stsp int i, nbase_entries;
5963 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5964 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5965 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5966 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5967 63c5ca5d 2019-08-24 stsp
5968 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5969 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5970 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5971 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5972 63c5ca5d 2019-08-24 stsp if (err)
5973 63c5ca5d 2019-08-24 stsp goto done;
5974 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5975 63c5ca5d 2019-08-24 stsp if (err)
5976 63c5ca5d 2019-08-24 stsp goto done;
5977 ba580f68 2020-03-22 stsp (*nentries)++;
5978 63c5ca5d 2019-08-24 stsp continue;
5979 63c5ca5d 2019-08-24 stsp }
5980 2f51b5b3 2019-05-09 stsp
5981 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5982 44d03001 2019-05-09 stsp int modified;
5983 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5984 036813ee 2019-05-09 stsp if (err)
5985 036813ee 2019-05-09 stsp goto done;
5986 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5987 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5988 2f51b5b3 2019-05-09 stsp if (err)
5989 2f51b5b3 2019-05-09 stsp goto done;
5990 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5991 44d03001 2019-05-09 stsp if (modified) {
5992 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5993 ba580f68 2020-03-22 stsp int nsubentries;
5994 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5995 ba580f68 2020-03-22 stsp &nsubentries, te,
5996 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5997 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5998 44d03001 2019-05-09 stsp if (err)
5999 44d03001 2019-05-09 stsp goto done;
6000 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
6001 ba580f68 2020-03-22 stsp /* All entries were deleted. */
6002 ba580f68 2020-03-22 stsp free(new_id);
6003 ba580f68 2020-03-22 stsp continue;
6004 ba580f68 2020-03-22 stsp }
6005 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
6006 56e0773d 2019-11-28 stsp sizeof(new_te->id));
6007 56e0773d 2019-11-28 stsp free(new_id);
6008 44d03001 2019-05-09 stsp }
6009 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6010 036813ee 2019-05-09 stsp if (err)
6011 036813ee 2019-05-09 stsp goto done;
6012 ba580f68 2020-03-22 stsp (*nentries)++;
6013 2f51b5b3 2019-05-09 stsp continue;
6014 036813ee 2019-05-09 stsp }
6015 2f51b5b3 2019-05-09 stsp
6016 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
6017 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
6018 f66c734c 2019-09-22 stsp if (err)
6019 f66c734c 2019-09-22 stsp goto done;
6020 2f51b5b3 2019-05-09 stsp if (ct) {
6021 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
6022 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
6023 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
6024 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
6025 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6026 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
6027 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
6028 2f51b5b3 2019-05-09 stsp if (err)
6029 2f51b5b3 2019-05-09 stsp goto done;
6030 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6031 2f51b5b3 2019-05-09 stsp if (err)
6032 2f51b5b3 2019-05-09 stsp goto done;
6033 ba580f68 2020-03-22 stsp (*nentries)++;
6034 2f51b5b3 2019-05-09 stsp }
6035 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
6036 b416585c 2019-05-13 stsp status_arg);
6037 afa376bf 2019-05-09 stsp if (err)
6038 afa376bf 2019-05-09 stsp goto done;
6039 2f51b5b3 2019-05-09 stsp } else {
6040 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
6041 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
6042 2f51b5b3 2019-05-09 stsp if (err)
6043 2f51b5b3 2019-05-09 stsp goto done;
6044 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
6045 2f51b5b3 2019-05-09 stsp if (err)
6046 2f51b5b3 2019-05-09 stsp goto done;
6047 ba580f68 2020-03-22 stsp (*nentries)++;
6048 2f51b5b3 2019-05-09 stsp }
6049 ca2503ea 2019-05-09 stsp }
6050 ed175427 2019-05-09 stsp }
6051 0b5cc0d6 2019-05-09 stsp
6052 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
6053 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6054 ed175427 2019-05-09 stsp done:
6055 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6056 2e1fa222 2020-07-23 stsp return err;
6057 2e1fa222 2020-07-23 stsp }
6058 2e1fa222 2020-07-23 stsp
6059 2e1fa222 2020-07-23 stsp static const struct got_error *
6060 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
6061 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
6062 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
6063 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
6064 ebf99748 2019-05-09 stsp {
6065 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
6066 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
6067 437adc9d 2020-12-10 yzhong char *relpath = NULL;
6068 ebf99748 2019-05-09 stsp
6069 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6070 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
6071 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6072 ebf99748 2019-05-09 stsp
6073 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6074 437adc9d 2020-12-10 yzhong
6075 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
6076 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
6077 437adc9d 2020-12-10 yzhong if (err)
6078 437adc9d 2020-12-10 yzhong goto done;
6079 437adc9d 2020-12-10 yzhong
6080 ebf99748 2019-05-09 stsp if (ie) {
6081 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
6082 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
6083 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
6084 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
6085 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
6086 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6087 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6088 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
6089 437adc9d 2020-12-10 yzhong
6090 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
6091 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6092 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
6093 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6094 72fd46fa 2019-09-06 stsp !have_staged_files);
6095 ebf99748 2019-05-09 stsp } else
6096 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
6097 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
6098 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
6099 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
6100 72fd46fa 2019-09-06 stsp !have_staged_files);
6101 ebf99748 2019-05-09 stsp } else {
6102 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
6103 ebf99748 2019-05-09 stsp if (err)
6104 437adc9d 2020-12-10 yzhong goto done;
6105 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
6106 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
6107 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
6108 3969253a 2020-03-07 stsp if (err) {
6109 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6110 437adc9d 2020-12-10 yzhong goto done;
6111 3969253a 2020-03-07 stsp }
6112 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
6113 3969253a 2020-03-07 stsp if (err) {
6114 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
6115 437adc9d 2020-12-10 yzhong goto done;
6116 3969253a 2020-03-07 stsp }
6117 ebf99748 2019-05-09 stsp }
6118 437adc9d 2020-12-10 yzhong free(relpath);
6119 437adc9d 2020-12-10 yzhong relpath = NULL;
6120 ebf99748 2019-05-09 stsp }
6121 437adc9d 2020-12-10 yzhong done:
6122 437adc9d 2020-12-10 yzhong free(relpath);
6123 d56d26ce 2019-05-10 stsp return err;
6124 d56d26ce 2019-05-10 stsp }
6125 735ef5ac 2019-08-03 stsp
6126 d56d26ce 2019-05-10 stsp
6127 d56d26ce 2019-05-10 stsp static const struct got_error *
6128 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
6129 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
6130 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
6131 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
6132 735ef5ac 2019-08-03 stsp int ood_errcode)
6133 d56d26ce 2019-05-10 stsp {
6134 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
6135 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6136 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
6137 1a36436d 2019-06-10 stsp
6138 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6139 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
6140 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6141 1a36436d 2019-06-10 stsp return NULL;
6142 9bead371 2019-07-28 stsp /*
6143 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
6144 9bead371 2019-07-28 stsp * on matches file content in the branch head.
6145 9bead371 2019-07-28 stsp */
6146 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6147 a44927cc 2022-04-07 stsp if (err)
6148 a44927cc 2022-04-07 stsp goto done;
6149 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6150 9bead371 2019-07-28 stsp if (err) {
6151 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6152 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6153 1a36436d 2019-06-10 stsp goto done;
6154 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6155 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6156 1a36436d 2019-06-10 stsp } else {
6157 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6158 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6159 a44927cc 2022-04-07 stsp if (err)
6160 a44927cc 2022-04-07 stsp goto done;
6161 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6162 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6163 1a36436d 2019-06-10 stsp goto done;
6164 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6165 1a36436d 2019-06-10 stsp }
6166 1a36436d 2019-06-10 stsp done:
6167 1a36436d 2019-06-10 stsp free(id);
6168 a44927cc 2022-04-07 stsp if (commit)
6169 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6170 1a36436d 2019-06-10 stsp return err;
6171 ebf99748 2019-05-09 stsp }
6172 ebf99748 2019-05-09 stsp
6173 336075a4 2022-06-25 op static const struct got_error *
6174 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6175 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6176 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6177 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6178 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6179 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6180 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6181 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6182 afa376bf 2019-05-09 stsp struct got_repository *repo)
6183 c4296144 2019-05-09 stsp {
6184 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6185 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6186 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6187 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6188 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6189 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6190 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6191 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6192 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6193 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6194 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6195 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6196 f8399b8f 2022-07-22 stsp time_t timestamp;
6197 c4296144 2019-05-09 stsp
6198 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6199 c4296144 2019-05-09 stsp
6200 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6201 675c7539 2019-05-09 stsp
6202 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6203 588edf97 2019-05-10 stsp if (err)
6204 588edf97 2019-05-10 stsp goto done;
6205 de18fc63 2019-05-09 stsp
6206 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6207 588edf97 2019-05-10 stsp if (err)
6208 588edf97 2019-05-10 stsp goto done;
6209 588edf97 2019-05-10 stsp
6210 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6211 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6212 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6213 33ad4cbe 2019-05-12 jcs if (err)
6214 33ad4cbe 2019-05-12 jcs goto done;
6215 33ad4cbe 2019-05-12 jcs }
6216 c4296144 2019-05-09 stsp
6217 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6218 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6219 33ad4cbe 2019-05-12 jcs goto done;
6220 33ad4cbe 2019-05-12 jcs }
6221 33ad4cbe 2019-05-12 jcs
6222 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6223 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6224 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6225 cf066bf8 2019-05-09 stsp char *ondisk_path;
6226 cf066bf8 2019-05-09 stsp
6227 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6228 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6229 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6230 5f8a88c6 2019-08-03 stsp continue;
6231 5f8a88c6 2019-08-03 stsp
6232 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6233 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6234 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6235 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6236 cf066bf8 2019-05-09 stsp continue;
6237 cf066bf8 2019-05-09 stsp
6238 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6239 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6241 cf066bf8 2019-05-09 stsp goto done;
6242 cf066bf8 2019-05-09 stsp }
6243 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6244 2e1fa222 2020-07-23 stsp free(ondisk_path);
6245 2e1fa222 2020-07-23 stsp if (err)
6246 cf066bf8 2019-05-09 stsp goto done;
6247 cf066bf8 2019-05-09 stsp }
6248 036813ee 2019-05-09 stsp
6249 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6250 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6251 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6252 036813ee 2019-05-09 stsp if (err)
6253 036813ee 2019-05-09 stsp goto done;
6254 036813ee 2019-05-09 stsp
6255 2b706956 2023-04-17 stsp err = got_object_qid_alloc(&pid, head_commit_id);
6256 de18fc63 2019-05-09 stsp if (err)
6257 de18fc63 2019-05-09 stsp goto done;
6258 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6259 f259c4c1 2021-09-24 stsp nparents++;
6260 f259c4c1 2021-09-24 stsp if (parent_id2) {
6261 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6262 f259c4c1 2021-09-24 stsp if (err)
6263 f259c4c1 2021-09-24 stsp goto done;
6264 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6265 f259c4c1 2021-09-24 stsp nparents++;
6266 f259c4c1 2021-09-24 stsp }
6267 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6268 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6269 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6270 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6271 33ad4cbe 2019-05-12 jcs free(logmsg);
6272 9d40349a 2019-05-09 stsp if (err)
6273 9d40349a 2019-05-09 stsp goto done;
6274 9d40349a 2019-05-09 stsp
6275 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6276 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6277 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6278 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6279 09f5bd90 2019-05-09 stsp goto done;
6280 09f5bd90 2019-05-09 stsp }
6281 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6282 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6283 09f5bd90 2019-05-09 stsp if (err)
6284 09f5bd90 2019-05-09 stsp goto done;
6285 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6286 ebf99748 2019-05-09 stsp if (err)
6287 ebf99748 2019-05-09 stsp goto done;
6288 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6289 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6290 09f5bd90 2019-05-09 stsp goto done;
6291 09f5bd90 2019-05-09 stsp }
6292 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6293 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6294 f2c16586 2019-05-09 stsp if (err)
6295 f2c16586 2019-05-09 stsp goto done;
6296 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6297 f2c16586 2019-05-09 stsp if (err)
6298 f2c16586 2019-05-09 stsp goto done;
6299 f2c16586 2019-05-09 stsp
6300 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6301 09f5bd90 2019-05-09 stsp if (err)
6302 09f5bd90 2019-05-09 stsp goto done;
6303 09f5bd90 2019-05-09 stsp
6304 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6305 ebf99748 2019-05-09 stsp if (err)
6306 ebf99748 2019-05-09 stsp goto done;
6307 c4296144 2019-05-09 stsp done:
6308 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6309 588edf97 2019-05-10 stsp if (head_tree)
6310 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6311 588edf97 2019-05-10 stsp if (head_commit)
6312 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6313 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6314 2f17228e 2019-05-12 stsp if (head_ref2) {
6315 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6316 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6317 2f17228e 2019-05-12 stsp err = unlockerr;
6318 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6319 39cd0ff6 2019-07-12 stsp }
6320 39cd0ff6 2019-07-12 stsp return err;
6321 39cd0ff6 2019-07-12 stsp }
6322 5c1e53bc 2019-07-28 stsp
6323 5c1e53bc 2019-07-28 stsp static const struct got_error *
6324 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6325 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6326 5c1e53bc 2019-07-28 stsp {
6327 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6328 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6329 39cd0ff6 2019-07-12 stsp
6330 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6331 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6332 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6333 5c1e53bc 2019-07-28 stsp
6334 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6335 5c1e53bc 2019-07-28 stsp ct_path++;
6336 5c1e53bc 2019-07-28 stsp
6337 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6338 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6339 5c1e53bc 2019-07-28 stsp break;
6340 5c1e53bc 2019-07-28 stsp }
6341 5c1e53bc 2019-07-28 stsp
6342 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6343 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6344 5c1e53bc 2019-07-28 stsp
6345 5c1e53bc 2019-07-28 stsp return NULL;
6346 5c1e53bc 2019-07-28 stsp }
6347 5c1e53bc 2019-07-28 stsp
6348 f0b75401 2019-08-03 stsp static const struct got_error *
6349 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6350 f0b75401 2019-08-03 stsp {
6351 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6352 f0b75401 2019-08-03 stsp
6353 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6354 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6355 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6356 f0b75401 2019-08-03 stsp }
6357 f0b75401 2019-08-03 stsp
6358 f0b75401 2019-08-03 stsp return NULL;
6359 f0b75401 2019-08-03 stsp }
6360 f0b75401 2019-08-03 stsp
6361 5f8a88c6 2019-08-03 stsp static const struct got_error *
6362 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6363 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6364 5f8a88c6 2019-08-03 stsp {
6365 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6366 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6367 5f8a88c6 2019-08-03 stsp
6368 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6369 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6370 5f8a88c6 2019-08-03 stsp continue;
6371 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6372 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6373 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6374 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6375 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6376 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6377 5f8a88c6 2019-08-03 stsp }
6378 5f8a88c6 2019-08-03 stsp
6379 5f8a88c6 2019-08-03 stsp return NULL;
6380 5f8a88c6 2019-08-03 stsp }
6381 5f8a88c6 2019-08-03 stsp
6382 39cd0ff6 2019-07-12 stsp const struct got_error *
6383 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6384 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6385 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6386 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6387 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6388 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6389 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6390 39cd0ff6 2019-07-12 stsp {
6391 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6392 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6393 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6394 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6395 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6396 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6397 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6398 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6399 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6400 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6401 39cd0ff6 2019-07-12 stsp
6402 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6403 39cd0ff6 2019-07-12 stsp
6404 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6405 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6406 39cd0ff6 2019-07-12 stsp
6407 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6408 39cd0ff6 2019-07-12 stsp if (err)
6409 39cd0ff6 2019-07-12 stsp goto done;
6410 39cd0ff6 2019-07-12 stsp
6411 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6412 39cd0ff6 2019-07-12 stsp if (err)
6413 39cd0ff6 2019-07-12 stsp goto done;
6414 39cd0ff6 2019-07-12 stsp
6415 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6416 39cd0ff6 2019-07-12 stsp if (err)
6417 39cd0ff6 2019-07-12 stsp goto done;
6418 39cd0ff6 2019-07-12 stsp
6419 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6420 39cd0ff6 2019-07-12 stsp if (err)
6421 39cd0ff6 2019-07-12 stsp goto done;
6422 39cd0ff6 2019-07-12 stsp
6423 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6424 5f8a88c6 2019-08-03 stsp &have_staged_files);
6425 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6426 5f8a88c6 2019-08-03 stsp goto done;
6427 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6428 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6429 5f8a88c6 2019-08-03 stsp if (err)
6430 5f8a88c6 2019-08-03 stsp goto done;
6431 5f8a88c6 2019-08-03 stsp }
6432 5f8a88c6 2019-08-03 stsp
6433 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6434 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6435 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6436 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6437 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6438 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6439 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6440 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6441 2a47b1e5 2022-11-01 stsp if (show_diff) {
6442 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6443 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6444 2a47b1e5 2022-11-01 stsp if (err)
6445 2a47b1e5 2022-11-01 stsp goto done;
6446 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6447 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6448 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6449 2a47b1e5 2022-11-01 stsp goto done;
6450 2a47b1e5 2022-11-01 stsp }
6451 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6452 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6453 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6454 2a47b1e5 2022-11-01 stsp goto done;
6455 2a47b1e5 2022-11-01 stsp }
6456 2a47b1e5 2022-11-01 stsp }
6457 2a47b1e5 2022-11-01 stsp
6458 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6459 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6460 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6461 5c1e53bc 2019-07-28 stsp if (err)
6462 5c1e53bc 2019-07-28 stsp goto done;
6463 5c1e53bc 2019-07-28 stsp }
6464 39cd0ff6 2019-07-12 stsp
6465 2a47b1e5 2022-11-01 stsp if (show_diff) {
6466 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6467 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6468 2a47b1e5 2022-11-01 stsp goto done;
6469 2a47b1e5 2022-11-01 stsp }
6470 2a47b1e5 2022-11-01 stsp }
6471 2a47b1e5 2022-11-01 stsp
6472 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6473 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6474 39cd0ff6 2019-07-12 stsp goto done;
6475 5c1e53bc 2019-07-28 stsp }
6476 5c1e53bc 2019-07-28 stsp
6477 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6478 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6479 5c1e53bc 2019-07-28 stsp if (err)
6480 5c1e53bc 2019-07-28 stsp goto done;
6481 39cd0ff6 2019-07-12 stsp }
6482 f0b75401 2019-08-03 stsp
6483 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6484 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6485 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6486 f0b75401 2019-08-03 stsp
6487 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6488 f0b75401 2019-08-03 stsp ct_path++;
6489 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6490 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6491 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6492 b50cabdf 2019-07-12 stsp if (err)
6493 b50cabdf 2019-07-12 stsp goto done;
6494 f0b75401 2019-08-03 stsp
6495 b50cabdf 2019-07-12 stsp }
6496 b50cabdf 2019-07-12 stsp
6497 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6498 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6499 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6500 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6501 39cd0ff6 2019-07-12 stsp if (err)
6502 39cd0ff6 2019-07-12 stsp goto done;
6503 39cd0ff6 2019-07-12 stsp
6504 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6505 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6506 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6507 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6508 39cd0ff6 2019-07-12 stsp err = sync_err;
6509 39cd0ff6 2019-07-12 stsp done:
6510 39cd0ff6 2019-07-12 stsp if (fileindex)
6511 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6512 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6513 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6514 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6515 39cd0ff6 2019-07-12 stsp err = unlockerr;
6516 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6517 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6518 d8bacb93 2023-01-10 mark
6519 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6520 39cd0ff6 2019-07-12 stsp }
6521 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6522 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6523 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6524 2a47b1e5 2022-11-01 stsp free(diff_path);
6525 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6526 2a47b1e5 2022-11-01 stsp err == NULL)
6527 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6528 c4296144 2019-05-09 stsp return err;
6529 8656d6c4 2019-05-20 stsp }
6530 8656d6c4 2019-05-20 stsp
6531 8656d6c4 2019-05-20 stsp const char *
6532 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6533 8656d6c4 2019-05-20 stsp {
6534 8656d6c4 2019-05-20 stsp return ct->path;
6535 8656d6c4 2019-05-20 stsp }
6536 8656d6c4 2019-05-20 stsp
6537 8656d6c4 2019-05-20 stsp unsigned int
6538 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6539 8656d6c4 2019-05-20 stsp {
6540 8656d6c4 2019-05-20 stsp return ct->status;
6541 818c7501 2019-07-11 stsp }
6542 818c7501 2019-07-11 stsp
6543 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6544 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6545 818c7501 2019-07-11 stsp struct got_repository *repo;
6546 818c7501 2019-07-11 stsp };
6547 818c7501 2019-07-11 stsp
6548 818c7501 2019-07-11 stsp static const struct got_error *
6549 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6550 818c7501 2019-07-11 stsp {
6551 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6552 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6553 818c7501 2019-07-11 stsp unsigned char status;
6554 818c7501 2019-07-11 stsp struct stat sb;
6555 818c7501 2019-07-11 stsp char *ondisk_path;
6556 818c7501 2019-07-11 stsp
6557 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6558 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6559 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6560 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6561 818c7501 2019-07-11 stsp
6562 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6563 818c7501 2019-07-11 stsp == -1)
6564 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6565 818c7501 2019-07-11 stsp
6566 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6567 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6568 818c7501 2019-07-11 stsp free(ondisk_path);
6569 818c7501 2019-07-11 stsp if (err)
6570 818c7501 2019-07-11 stsp return err;
6571 818c7501 2019-07-11 stsp
6572 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6573 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6574 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6575 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6576 818c7501 2019-07-11 stsp
6577 818c7501 2019-07-11 stsp return NULL;
6578 818c7501 2019-07-11 stsp }
6579 818c7501 2019-07-11 stsp
6580 818c7501 2019-07-11 stsp const struct got_error *
6581 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6582 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6583 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6584 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6585 818c7501 2019-07-11 stsp {
6586 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6587 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6588 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6589 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6590 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6591 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6592 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6593 818c7501 2019-07-11 stsp
6594 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6595 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6596 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6597 818c7501 2019-07-11 stsp
6598 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6599 818c7501 2019-07-11 stsp if (err)
6600 818c7501 2019-07-11 stsp return err;
6601 818c7501 2019-07-11 stsp
6602 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6603 818c7501 2019-07-11 stsp if (err)
6604 818c7501 2019-07-11 stsp goto done;
6605 818c7501 2019-07-11 stsp
6606 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6607 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6608 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6609 818c7501 2019-07-11 stsp &ok_arg);
6610 818c7501 2019-07-11 stsp if (err)
6611 818c7501 2019-07-11 stsp goto done;
6612 818c7501 2019-07-11 stsp
6613 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6614 818c7501 2019-07-11 stsp if (err)
6615 818c7501 2019-07-11 stsp goto done;
6616 818c7501 2019-07-11 stsp
6617 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6618 818c7501 2019-07-11 stsp if (err)
6619 818c7501 2019-07-11 stsp goto done;
6620 818c7501 2019-07-11 stsp
6621 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6622 818c7501 2019-07-11 stsp if (err)
6623 818c7501 2019-07-11 stsp goto done;
6624 818c7501 2019-07-11 stsp
6625 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6626 818c7501 2019-07-11 stsp 0);
6627 e51d7b55 2020-01-04 stsp if (err)
6628 e51d7b55 2020-01-04 stsp goto done;
6629 e51d7b55 2020-01-04 stsp
6630 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6631 818c7501 2019-07-11 stsp if (err)
6632 818c7501 2019-07-11 stsp goto done;
6633 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6634 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6635 e51d7b55 2020-01-04 stsp goto done;
6636 e51d7b55 2020-01-04 stsp }
6637 818c7501 2019-07-11 stsp
6638 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6639 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6640 818c7501 2019-07-11 stsp if (err)
6641 818c7501 2019-07-11 stsp goto done;
6642 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6643 818c7501 2019-07-11 stsp if (err)
6644 818c7501 2019-07-11 stsp goto done;
6645 818c7501 2019-07-11 stsp
6646 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6647 818c7501 2019-07-11 stsp
6648 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6649 818c7501 2019-07-11 stsp if (err)
6650 818c7501 2019-07-11 stsp goto done;
6651 818c7501 2019-07-11 stsp
6652 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6653 818c7501 2019-07-11 stsp if (err)
6654 818c7501 2019-07-11 stsp goto done;
6655 818c7501 2019-07-11 stsp
6656 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6657 818c7501 2019-07-11 stsp worktree->base_commit_id);
6658 818c7501 2019-07-11 stsp if (err)
6659 818c7501 2019-07-11 stsp goto done;
6660 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6661 818c7501 2019-07-11 stsp if (err)
6662 818c7501 2019-07-11 stsp goto done;
6663 818c7501 2019-07-11 stsp
6664 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6665 818c7501 2019-07-11 stsp if (err)
6666 818c7501 2019-07-11 stsp goto done;
6667 818c7501 2019-07-11 stsp done:
6668 818c7501 2019-07-11 stsp free(fileindex_path);
6669 818c7501 2019-07-11 stsp free(tmp_branch_name);
6670 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6671 818c7501 2019-07-11 stsp free(branch_ref_name);
6672 818c7501 2019-07-11 stsp if (branch_ref)
6673 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6674 818c7501 2019-07-11 stsp if (wt_branch)
6675 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6676 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6677 818c7501 2019-07-11 stsp if (err) {
6678 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6679 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6680 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6681 818c7501 2019-07-11 stsp }
6682 818c7501 2019-07-11 stsp if (*tmp_branch) {
6683 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6684 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6685 818c7501 2019-07-11 stsp }
6686 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6687 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6688 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6689 3e3a69f1 2019-07-25 stsp }
6690 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6691 818c7501 2019-07-11 stsp }
6692 818c7501 2019-07-11 stsp return err;
6693 818c7501 2019-07-11 stsp }
6694 818c7501 2019-07-11 stsp
6695 818c7501 2019-07-11 stsp const struct got_error *
6696 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6697 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6698 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6699 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6700 818c7501 2019-07-11 stsp {
6701 818c7501 2019-07-11 stsp const struct got_error *err;
6702 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6703 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6704 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6705 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6706 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6707 818c7501 2019-07-11 stsp
6708 818c7501 2019-07-11 stsp *commit_id = NULL;
6709 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6710 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6711 3e3a69f1 2019-07-25 stsp *branch = NULL;
6712 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6713 3e3a69f1 2019-07-25 stsp
6714 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6715 3e3a69f1 2019-07-25 stsp if (err)
6716 3e3a69f1 2019-07-25 stsp return err;
6717 818c7501 2019-07-11 stsp
6718 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6719 3e3a69f1 2019-07-25 stsp if (err)
6720 f032f1f7 2019-08-04 stsp goto done;
6721 f032f1f7 2019-08-04 stsp
6722 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6723 f032f1f7 2019-08-04 stsp &have_staged_files);
6724 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6725 f032f1f7 2019-08-04 stsp goto done;
6726 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6727 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6728 3e3a69f1 2019-07-25 stsp goto done;
6729 f032f1f7 2019-08-04 stsp }
6730 3e3a69f1 2019-07-25 stsp
6731 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6732 818c7501 2019-07-11 stsp if (err)
6733 f032f1f7 2019-08-04 stsp goto done;
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6736 818c7501 2019-07-11 stsp if (err)
6737 818c7501 2019-07-11 stsp goto done;
6738 818c7501 2019-07-11 stsp
6739 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6740 818c7501 2019-07-11 stsp if (err)
6741 818c7501 2019-07-11 stsp goto done;
6742 818c7501 2019-07-11 stsp
6743 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6744 818c7501 2019-07-11 stsp if (err)
6745 818c7501 2019-07-11 stsp goto done;
6746 818c7501 2019-07-11 stsp
6747 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6748 818c7501 2019-07-11 stsp if (err)
6749 818c7501 2019-07-11 stsp goto done;
6750 818c7501 2019-07-11 stsp
6751 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6752 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6753 818c7501 2019-07-11 stsp if (err)
6754 818c7501 2019-07-11 stsp goto done;
6755 818c7501 2019-07-11 stsp
6756 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6757 818c7501 2019-07-11 stsp if (err)
6758 818c7501 2019-07-11 stsp goto done;
6759 818c7501 2019-07-11 stsp
6760 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6761 818c7501 2019-07-11 stsp if (err)
6762 818c7501 2019-07-11 stsp goto done;
6763 818c7501 2019-07-11 stsp
6764 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6765 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6766 818c7501 2019-07-11 stsp if (err)
6767 818c7501 2019-07-11 stsp goto done;
6768 818c7501 2019-07-11 stsp
6769 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6770 818c7501 2019-07-11 stsp if (err)
6771 818c7501 2019-07-11 stsp goto done;
6772 818c7501 2019-07-11 stsp done:
6773 818c7501 2019-07-11 stsp free(commit_ref_name);
6774 818c7501 2019-07-11 stsp free(branch_ref_name);
6775 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6776 818c7501 2019-07-11 stsp if (commit_ref)
6777 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6778 818c7501 2019-07-11 stsp if (branch_ref)
6779 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6780 818c7501 2019-07-11 stsp if (err) {
6781 818c7501 2019-07-11 stsp free(*commit_id);
6782 818c7501 2019-07-11 stsp *commit_id = NULL;
6783 818c7501 2019-07-11 stsp if (*tmp_branch) {
6784 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6785 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6786 818c7501 2019-07-11 stsp }
6787 818c7501 2019-07-11 stsp if (*new_base_branch) {
6788 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6789 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6790 818c7501 2019-07-11 stsp }
6791 818c7501 2019-07-11 stsp if (*branch) {
6792 818c7501 2019-07-11 stsp got_ref_close(*branch);
6793 818c7501 2019-07-11 stsp *branch = NULL;
6794 818c7501 2019-07-11 stsp }
6795 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6796 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6797 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6798 3e3a69f1 2019-07-25 stsp }
6799 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6800 818c7501 2019-07-11 stsp }
6801 818c7501 2019-07-11 stsp return err;
6802 c4296144 2019-05-09 stsp }
6803 818c7501 2019-07-11 stsp
6804 818c7501 2019-07-11 stsp const struct got_error *
6805 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6806 818c7501 2019-07-11 stsp {
6807 818c7501 2019-07-11 stsp const struct got_error *err;
6808 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6809 818c7501 2019-07-11 stsp
6810 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6811 818c7501 2019-07-11 stsp if (err)
6812 818c7501 2019-07-11 stsp return err;
6813 818c7501 2019-07-11 stsp
6814 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6815 818c7501 2019-07-11 stsp free(tmp_branch_name);
6816 818c7501 2019-07-11 stsp return NULL;
6817 818c7501 2019-07-11 stsp }
6818 818c7501 2019-07-11 stsp
6819 818c7501 2019-07-11 stsp static const struct got_error *
6820 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6821 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6822 818c7501 2019-07-11 stsp {
6823 0ebf8283 2019-07-24 stsp *logmsg = arg;
6824 818c7501 2019-07-11 stsp return NULL;
6825 818c7501 2019-07-11 stsp }
6826 818c7501 2019-07-11 stsp
6827 818c7501 2019-07-11 stsp static const struct got_error *
6828 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6829 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6830 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6831 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6832 818c7501 2019-07-11 stsp {
6833 818c7501 2019-07-11 stsp return NULL;
6834 01757395 2019-07-12 stsp }
6835 01757395 2019-07-12 stsp
6836 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6837 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6838 01757395 2019-07-12 stsp void *progress_arg;
6839 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6840 01757395 2019-07-12 stsp };
6841 01757395 2019-07-12 stsp
6842 01757395 2019-07-12 stsp static const struct got_error *
6843 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6844 01757395 2019-07-12 stsp {
6845 01757395 2019-07-12 stsp const struct got_error *err;
6846 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6847 01757395 2019-07-12 stsp char *p;
6848 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6849 01757395 2019-07-12 stsp
6850 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6851 01757395 2019-07-12 stsp if (err)
6852 01757395 2019-07-12 stsp return err;
6853 01757395 2019-07-12 stsp
6854 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6855 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6856 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6857 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6858 01757395 2019-07-12 stsp return NULL;
6859 01757395 2019-07-12 stsp
6860 01757395 2019-07-12 stsp p = strdup(path);
6861 01757395 2019-07-12 stsp if (p == NULL)
6862 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6863 01757395 2019-07-12 stsp
6864 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6865 01757395 2019-07-12 stsp if (err || new == NULL)
6866 01757395 2019-07-12 stsp free(p);
6867 01757395 2019-07-12 stsp return err;
6868 818c7501 2019-07-11 stsp }
6869 818c7501 2019-07-11 stsp
6870 0ebf8283 2019-07-24 stsp static const struct got_error *
6871 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6872 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6873 818c7501 2019-07-11 stsp {
6874 818c7501 2019-07-11 stsp const struct got_error *err;
6875 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6876 818c7501 2019-07-11 stsp
6877 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6878 818c7501 2019-07-11 stsp if (err) {
6879 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6880 818c7501 2019-07-11 stsp goto done;
6881 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6882 818c7501 2019-07-11 stsp if (err)
6883 818c7501 2019-07-11 stsp goto done;
6884 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6885 818c7501 2019-07-11 stsp if (err)
6886 818c7501 2019-07-11 stsp goto done;
6887 de05890f 2020-03-05 stsp } else if (is_rebase) {
6888 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6889 818c7501 2019-07-11 stsp int cmp;
6890 818c7501 2019-07-11 stsp
6891 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6892 818c7501 2019-07-11 stsp if (err)
6893 818c7501 2019-07-11 stsp goto done;
6894 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6895 818c7501 2019-07-11 stsp free(stored_id);
6896 818c7501 2019-07-11 stsp if (cmp != 0) {
6897 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6898 818c7501 2019-07-11 stsp goto done;
6899 818c7501 2019-07-11 stsp }
6900 818c7501 2019-07-11 stsp }
6901 0ebf8283 2019-07-24 stsp done:
6902 0ebf8283 2019-07-24 stsp if (commit_ref)
6903 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6904 0ebf8283 2019-07-24 stsp return err;
6905 0ebf8283 2019-07-24 stsp }
6906 0ebf8283 2019-07-24 stsp
6907 0ebf8283 2019-07-24 stsp static const struct got_error *
6908 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6909 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6910 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6911 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6912 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6913 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6914 0ebf8283 2019-07-24 stsp {
6915 0ebf8283 2019-07-24 stsp const struct got_error *err;
6916 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6917 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6918 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6919 818c7501 2019-07-11 stsp
6920 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6921 0ebf8283 2019-07-24 stsp
6922 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6923 0ebf8283 2019-07-24 stsp if (err)
6924 0ebf8283 2019-07-24 stsp return err;
6925 0ebf8283 2019-07-24 stsp
6926 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6927 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6928 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6929 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6930 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6931 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6932 818c7501 2019-07-11 stsp if (commit_ref)
6933 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6934 818c7501 2019-07-11 stsp return err;
6935 818c7501 2019-07-11 stsp }
6936 818c7501 2019-07-11 stsp
6937 818c7501 2019-07-11 stsp const struct got_error *
6938 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6939 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6940 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6941 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6942 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6943 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6944 818c7501 2019-07-11 stsp {
6945 0ebf8283 2019-07-24 stsp const struct got_error *err;
6946 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6949 0ebf8283 2019-07-24 stsp if (err)
6950 0ebf8283 2019-07-24 stsp return err;
6951 0ebf8283 2019-07-24 stsp
6952 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6953 0ebf8283 2019-07-24 stsp if (err)
6954 0ebf8283 2019-07-24 stsp goto done;
6955 0ebf8283 2019-07-24 stsp
6956 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6957 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6958 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6959 0ebf8283 2019-07-24 stsp done:
6960 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6961 0ebf8283 2019-07-24 stsp return err;
6962 0ebf8283 2019-07-24 stsp }
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp const struct got_error *
6965 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6966 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6967 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6968 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6969 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6970 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6971 0ebf8283 2019-07-24 stsp {
6972 0ebf8283 2019-07-24 stsp const struct got_error *err;
6973 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6976 0ebf8283 2019-07-24 stsp if (err)
6977 0ebf8283 2019-07-24 stsp return err;
6978 0ebf8283 2019-07-24 stsp
6979 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp goto done;
6982 0ebf8283 2019-07-24 stsp
6983 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6984 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6985 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6986 0ebf8283 2019-07-24 stsp done:
6987 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6988 0ebf8283 2019-07-24 stsp return err;
6989 0ebf8283 2019-07-24 stsp }
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp static const struct got_error *
6992 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6993 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6994 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6995 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6996 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6997 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6998 0ebf8283 2019-07-24 stsp {
6999 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
7000 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
7001 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
7002 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7003 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
7004 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
7005 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
7006 818c7501 2019-07-11 stsp
7007 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7008 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
7009 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
7010 a0e95631 2019-07-12 stsp
7011 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
7012 818c7501 2019-07-11 stsp
7013 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7014 a0e95631 2019-07-12 stsp if (err)
7015 3e3a69f1 2019-07-25 stsp return err;
7016 a0e95631 2019-07-12 stsp
7017 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
7018 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
7019 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
7020 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
7021 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7022 01757395 2019-07-12 stsp /*
7023 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
7024 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
7025 6c13b005 2021-09-02 stsp *
7026 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
7027 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
7028 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
7029 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
7030 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
7031 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
7032 01757395 2019-07-12 stsp */
7033 01757395 2019-07-12 stsp if (merged_paths) {
7034 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
7035 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
7036 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
7037 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7038 f2a9dc41 2019-12-13 tracey 0);
7039 01757395 2019-07-12 stsp if (err)
7040 01757395 2019-07-12 stsp goto done;
7041 01757395 2019-07-12 stsp }
7042 01757395 2019-07-12 stsp } else {
7043 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7044 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7045 01757395 2019-07-12 stsp if (err)
7046 01757395 2019-07-12 stsp goto done;
7047 01757395 2019-07-12 stsp }
7048 a0e95631 2019-07-12 stsp
7049 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7050 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
7051 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7052 a0e95631 2019-07-12 stsp if (err)
7053 a0e95631 2019-07-12 stsp goto done;
7054 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7055 a0e95631 2019-07-12 stsp goto done;
7056 ff0d2220 2019-07-11 stsp }
7057 818c7501 2019-07-11 stsp
7058 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7059 edd02c5e 2019-07-11 stsp if (err)
7060 edd02c5e 2019-07-11 stsp goto done;
7061 edd02c5e 2019-07-11 stsp
7062 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7063 818c7501 2019-07-11 stsp if (err)
7064 818c7501 2019-07-11 stsp goto done;
7065 818c7501 2019-07-11 stsp
7066 5943eee2 2019-08-13 stsp if (new_logmsg) {
7067 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
7068 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
7069 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7070 5943eee2 2019-08-13 stsp goto done;
7071 5943eee2 2019-08-13 stsp }
7072 5943eee2 2019-08-13 stsp } else {
7073 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7074 5943eee2 2019-08-13 stsp if (err)
7075 5943eee2 2019-08-13 stsp goto done;
7076 5943eee2 2019-08-13 stsp }
7077 0ebf8283 2019-07-24 stsp
7078 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
7079 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7080 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
7081 50e7a649 2022-07-22 stsp committer ? committer :
7082 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
7083 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7084 a0e95631 2019-07-12 stsp if (err)
7085 a0e95631 2019-07-12 stsp goto done;
7086 a0e95631 2019-07-12 stsp
7087 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
7088 a0e95631 2019-07-12 stsp if (err)
7089 a0e95631 2019-07-12 stsp goto done;
7090 a0e95631 2019-07-12 stsp
7091 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
7092 a0e95631 2019-07-12 stsp if (err)
7093 a0e95631 2019-07-12 stsp goto done;
7094 a0e95631 2019-07-12 stsp
7095 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
7096 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
7097 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7098 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
7099 a0e95631 2019-07-12 stsp err = sync_err;
7100 818c7501 2019-07-11 stsp done:
7101 a0e95631 2019-07-12 stsp free(fileindex_path);
7102 a0e95631 2019-07-12 stsp free(head_commit_id);
7103 a0e95631 2019-07-12 stsp if (head_ref)
7104 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
7105 818c7501 2019-07-11 stsp if (err) {
7106 818c7501 2019-07-11 stsp free(*new_commit_id);
7107 818c7501 2019-07-11 stsp *new_commit_id = NULL;
7108 818c7501 2019-07-11 stsp }
7109 818c7501 2019-07-11 stsp return err;
7110 818c7501 2019-07-11 stsp }
7111 818c7501 2019-07-11 stsp
7112 818c7501 2019-07-11 stsp const struct got_error *
7113 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7114 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7115 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7116 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7117 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
7118 12383673 2023-02-18 mark struct got_repository *repo)
7119 0ebf8283 2019-07-24 stsp {
7120 0ebf8283 2019-07-24 stsp const struct got_error *err;
7121 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7122 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7123 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7124 0ebf8283 2019-07-24 stsp
7125 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7126 0ebf8283 2019-07-24 stsp if (err)
7127 0ebf8283 2019-07-24 stsp return err;
7128 0ebf8283 2019-07-24 stsp
7129 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7130 0ebf8283 2019-07-24 stsp if (err)
7131 0ebf8283 2019-07-24 stsp goto done;
7132 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
7133 0ebf8283 2019-07-24 stsp if (err)
7134 0ebf8283 2019-07-24 stsp goto done;
7135 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7136 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
7137 0ebf8283 2019-07-24 stsp goto done;
7138 0ebf8283 2019-07-24 stsp }
7139 0ebf8283 2019-07-24 stsp
7140 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7141 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7142 12383673 2023-02-18 mark NULL, allow_conflict, repo);
7143 0ebf8283 2019-07-24 stsp done:
7144 0ebf8283 2019-07-24 stsp if (commit_ref)
7145 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7146 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7147 0ebf8283 2019-07-24 stsp free(commit_id);
7148 0ebf8283 2019-07-24 stsp return err;
7149 0ebf8283 2019-07-24 stsp }
7150 0ebf8283 2019-07-24 stsp
7151 0ebf8283 2019-07-24 stsp const struct got_error *
7152 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7153 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7154 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7155 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7156 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7157 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7158 0ebf8283 2019-07-24 stsp {
7159 0ebf8283 2019-07-24 stsp const struct got_error *err;
7160 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7161 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7162 0ebf8283 2019-07-24 stsp
7163 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7164 0ebf8283 2019-07-24 stsp if (err)
7165 0ebf8283 2019-07-24 stsp return err;
7166 0ebf8283 2019-07-24 stsp
7167 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7168 0ebf8283 2019-07-24 stsp if (err)
7169 0ebf8283 2019-07-24 stsp goto done;
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7172 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7173 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7174 0ebf8283 2019-07-24 stsp done:
7175 0ebf8283 2019-07-24 stsp if (commit_ref)
7176 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7177 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7178 0ebf8283 2019-07-24 stsp return err;
7179 0ebf8283 2019-07-24 stsp }
7180 0ebf8283 2019-07-24 stsp
7181 0ebf8283 2019-07-24 stsp const struct got_error *
7182 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7183 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7184 818c7501 2019-07-11 stsp {
7185 3e3a69f1 2019-07-25 stsp if (fileindex)
7186 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7187 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7188 69844fba 2019-07-11 stsp }
7189 69844fba 2019-07-11 stsp
7190 69844fba 2019-07-11 stsp static const struct got_error *
7191 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7192 69844fba 2019-07-11 stsp {
7193 69844fba 2019-07-11 stsp const struct got_error *err;
7194 69844fba 2019-07-11 stsp struct got_reference *ref;
7195 69844fba 2019-07-11 stsp
7196 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7197 69844fba 2019-07-11 stsp if (err) {
7198 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7199 69844fba 2019-07-11 stsp return NULL;
7200 69844fba 2019-07-11 stsp return err;
7201 69844fba 2019-07-11 stsp }
7202 69844fba 2019-07-11 stsp
7203 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7204 69844fba 2019-07-11 stsp got_ref_close(ref);
7205 69844fba 2019-07-11 stsp return err;
7206 818c7501 2019-07-11 stsp }
7207 818c7501 2019-07-11 stsp
7208 69844fba 2019-07-11 stsp static const struct got_error *
7209 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7210 69844fba 2019-07-11 stsp {
7211 69844fba 2019-07-11 stsp const struct got_error *err;
7212 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7213 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7214 69844fba 2019-07-11 stsp
7215 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7216 69844fba 2019-07-11 stsp if (err)
7217 69844fba 2019-07-11 stsp goto done;
7218 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7219 69844fba 2019-07-11 stsp if (err)
7220 69844fba 2019-07-11 stsp goto done;
7221 69844fba 2019-07-11 stsp
7222 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7223 69844fba 2019-07-11 stsp if (err)
7224 69844fba 2019-07-11 stsp goto done;
7225 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7226 69844fba 2019-07-11 stsp if (err)
7227 69844fba 2019-07-11 stsp goto done;
7228 69844fba 2019-07-11 stsp
7229 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7230 69844fba 2019-07-11 stsp if (err)
7231 69844fba 2019-07-11 stsp goto done;
7232 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7233 69844fba 2019-07-11 stsp if (err)
7234 69844fba 2019-07-11 stsp goto done;
7235 69844fba 2019-07-11 stsp
7236 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7237 69844fba 2019-07-11 stsp if (err)
7238 69844fba 2019-07-11 stsp goto done;
7239 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7240 69844fba 2019-07-11 stsp if (err)
7241 69844fba 2019-07-11 stsp goto done;
7242 69844fba 2019-07-11 stsp
7243 69844fba 2019-07-11 stsp done:
7244 69844fba 2019-07-11 stsp free(tmp_branch_name);
7245 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7246 69844fba 2019-07-11 stsp free(branch_ref_name);
7247 69844fba 2019-07-11 stsp free(commit_ref_name);
7248 e600f124 2021-03-21 stsp return err;
7249 e600f124 2021-03-21 stsp }
7250 e600f124 2021-03-21 stsp
7251 336075a4 2022-06-25 op static const struct got_error *
7252 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7253 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7254 e600f124 2021-03-21 stsp {
7255 e600f124 2021-03-21 stsp const struct got_error *err;
7256 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7257 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7258 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7259 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7260 e600f124 2021-03-21 stsp char *refname = NULL;
7261 e600f124 2021-03-21 stsp
7262 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7263 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7264 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7265 e600f124 2021-03-21 stsp branch_name += 11;
7266 e600f124 2021-03-21 stsp
7267 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7268 e600f124 2021-03-21 stsp if (err)
7269 e600f124 2021-03-21 stsp return err;
7270 e600f124 2021-03-21 stsp
7271 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7272 e600f124 2021-03-21 stsp new_id_str) == -1) {
7273 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7274 e600f124 2021-03-21 stsp goto done;
7275 e600f124 2021-03-21 stsp }
7276 e600f124 2021-03-21 stsp
7277 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7278 e600f124 2021-03-21 stsp if (err)
7279 e600f124 2021-03-21 stsp goto done;
7280 e600f124 2021-03-21 stsp
7281 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7282 e600f124 2021-03-21 stsp if (err)
7283 e600f124 2021-03-21 stsp goto done;
7284 e600f124 2021-03-21 stsp
7285 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7286 e600f124 2021-03-21 stsp done:
7287 e600f124 2021-03-21 stsp free(new_id_str);
7288 e600f124 2021-03-21 stsp free(refname);
7289 e600f124 2021-03-21 stsp free(old_commit_id);
7290 e600f124 2021-03-21 stsp if (ref)
7291 e600f124 2021-03-21 stsp got_ref_close(ref);
7292 69844fba 2019-07-11 stsp return err;
7293 69844fba 2019-07-11 stsp }
7294 69844fba 2019-07-11 stsp
7295 818c7501 2019-07-11 stsp const struct got_error *
7296 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7297 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7298 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7299 ef85a376 2023-02-01 mark int create_backup)
7300 818c7501 2019-07-11 stsp {
7301 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7302 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7303 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7304 818c7501 2019-07-11 stsp
7305 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7306 818c7501 2019-07-11 stsp if (err)
7307 818c7501 2019-07-11 stsp return err;
7308 e600f124 2021-03-21 stsp
7309 e600f124 2021-03-21 stsp if (create_backup) {
7310 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7311 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7312 e600f124 2021-03-21 stsp if (err)
7313 e600f124 2021-03-21 stsp goto done;
7314 e600f124 2021-03-21 stsp }
7315 818c7501 2019-07-11 stsp
7316 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7317 818c7501 2019-07-11 stsp if (err)
7318 818c7501 2019-07-11 stsp goto done;
7319 818c7501 2019-07-11 stsp
7320 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7321 818c7501 2019-07-11 stsp if (err)
7322 818c7501 2019-07-11 stsp goto done;
7323 818c7501 2019-07-11 stsp
7324 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7325 818c7501 2019-07-11 stsp if (err)
7326 818c7501 2019-07-11 stsp goto done;
7327 818c7501 2019-07-11 stsp
7328 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7329 a615e0e7 2020-12-16 stsp if (err)
7330 a615e0e7 2020-12-16 stsp goto done;
7331 a615e0e7 2020-12-16 stsp
7332 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7333 a615e0e7 2020-12-16 stsp if (err)
7334 a615e0e7 2020-12-16 stsp goto done;
7335 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7336 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7337 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7338 a615e0e7 2020-12-16 stsp err = sync_err;
7339 818c7501 2019-07-11 stsp done:
7340 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7341 a615e0e7 2020-12-16 stsp free(fileindex_path);
7342 818c7501 2019-07-11 stsp free(new_head_commit_id);
7343 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7344 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7345 818c7501 2019-07-11 stsp err = unlockerr;
7346 818c7501 2019-07-11 stsp return err;
7347 818c7501 2019-07-11 stsp }
7348 af179be7 2023-04-14 stsp
7349 af179be7 2023-04-14 stsp static const struct got_error *
7350 af179be7 2023-04-14 stsp get_paths_changed_between_commits(struct got_pathlist_head *paths,
7351 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7352 af179be7 2023-04-14 stsp struct got_repository *repo)
7353 af179be7 2023-04-14 stsp {
7354 af179be7 2023-04-14 stsp const struct got_error *err;
7355 af179be7 2023-04-14 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7356 af179be7 2023-04-14 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7357 af179be7 2023-04-14 stsp
7358 af179be7 2023-04-14 stsp if (id1) {
7359 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit1, repo, id1);
7360 af179be7 2023-04-14 stsp if (err)
7361 af179be7 2023-04-14 stsp goto done;
7362 af179be7 2023-04-14 stsp
7363 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree1, repo,
7364 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit1));
7365 af179be7 2023-04-14 stsp if (err)
7366 af179be7 2023-04-14 stsp goto done;
7367 af179be7 2023-04-14 stsp }
7368 818c7501 2019-07-11 stsp
7369 af179be7 2023-04-14 stsp if (id2) {
7370 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit2, repo, id2);
7371 af179be7 2023-04-14 stsp if (err)
7372 af179be7 2023-04-14 stsp goto done;
7373 af179be7 2023-04-14 stsp
7374 af179be7 2023-04-14 stsp err = got_object_open_as_tree(&tree2, repo,
7375 af179be7 2023-04-14 stsp got_object_commit_get_tree_id(commit2));
7376 af179be7 2023-04-14 stsp if (err)
7377 af179be7 2023-04-14 stsp goto done;
7378 af179be7 2023-04-14 stsp }
7379 af179be7 2023-04-14 stsp
7380 af179be7 2023-04-14 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7381 af179be7 2023-04-14 stsp got_diff_tree_collect_changed_paths, paths, 0);
7382 af179be7 2023-04-14 stsp if (err)
7383 af179be7 2023-04-14 stsp goto done;
7384 af179be7 2023-04-14 stsp done:
7385 af179be7 2023-04-14 stsp if (commit1)
7386 af179be7 2023-04-14 stsp got_object_commit_close(commit1);
7387 af179be7 2023-04-14 stsp if (commit2)
7388 af179be7 2023-04-14 stsp got_object_commit_close(commit2);
7389 af179be7 2023-04-14 stsp if (tree1)
7390 af179be7 2023-04-14 stsp got_object_tree_close(tree1);
7391 af179be7 2023-04-14 stsp if (tree2)
7392 af179be7 2023-04-14 stsp got_object_tree_close(tree2);
7393 af179be7 2023-04-14 stsp return err;
7394 af179be7 2023-04-14 stsp }
7395 af179be7 2023-04-14 stsp
7396 af179be7 2023-04-14 stsp static const struct got_error *
7397 af179be7 2023-04-14 stsp get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7398 af179be7 2023-04-14 stsp struct got_object_id *id1, struct got_object_id *id2,
7399 af179be7 2023-04-14 stsp const char *path_prefix, struct got_repository *repo)
7400 af179be7 2023-04-14 stsp {
7401 af179be7 2023-04-14 stsp const struct got_error *err;
7402 af179be7 2023-04-14 stsp struct got_pathlist_head merged_paths;
7403 af179be7 2023-04-14 stsp struct got_pathlist_entry *pe;
7404 af179be7 2023-04-14 stsp char *abspath = NULL, *wt_path = NULL;
7405 af179be7 2023-04-14 stsp
7406 af179be7 2023-04-14 stsp TAILQ_INIT(&merged_paths);
7407 af179be7 2023-04-14 stsp
7408 af179be7 2023-04-14 stsp err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7409 af179be7 2023-04-14 stsp if (err)
7410 af179be7 2023-04-14 stsp goto done;
7411 af179be7 2023-04-14 stsp
7412 af179be7 2023-04-14 stsp TAILQ_FOREACH(pe, &merged_paths, entry) {
7413 af179be7 2023-04-14 stsp struct got_diff_changed_path *change = pe->data;
7414 af179be7 2023-04-14 stsp
7415 af179be7 2023-04-14 stsp if (change->status != GOT_STATUS_ADD)
7416 af179be7 2023-04-14 stsp continue;
7417 af179be7 2023-04-14 stsp
7418 af179be7 2023-04-14 stsp if (got_path_is_root_dir(path_prefix)) {
7419 af179be7 2023-04-14 stsp wt_path = strdup(pe->path);
7420 af179be7 2023-04-14 stsp if (wt_path == NULL) {
7421 af179be7 2023-04-14 stsp err = got_error_from_errno("strdup");
7422 af179be7 2023-04-14 stsp goto done;
7423 af179be7 2023-04-14 stsp }
7424 af179be7 2023-04-14 stsp } else {
7425 af179be7 2023-04-14 stsp if (asprintf(&abspath, "/%s", pe->path) == -1) {
7426 af179be7 2023-04-14 stsp err = got_error_from_errno("asprintf");
7427 af179be7 2023-04-14 stsp goto done;
7428 af179be7 2023-04-14 stsp }
7429 af179be7 2023-04-14 stsp
7430 af179be7 2023-04-14 stsp err = got_path_skip_common_ancestor(&wt_path,
7431 af179be7 2023-04-14 stsp path_prefix, abspath);
7432 af179be7 2023-04-14 stsp if (err)
7433 af179be7 2023-04-14 stsp goto done;
7434 af179be7 2023-04-14 stsp free(abspath);
7435 af179be7 2023-04-14 stsp abspath = NULL;
7436 af179be7 2023-04-14 stsp }
7437 af179be7 2023-04-14 stsp
7438 af179be7 2023-04-14 stsp err = got_pathlist_append(added_paths, wt_path, NULL);
7439 af179be7 2023-04-14 stsp if (err)
7440 af179be7 2023-04-14 stsp goto done;
7441 af179be7 2023-04-14 stsp wt_path = NULL;
7442 af179be7 2023-04-14 stsp }
7443 af179be7 2023-04-14 stsp
7444 af179be7 2023-04-14 stsp done:
7445 af179be7 2023-04-14 stsp got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7446 af179be7 2023-04-14 stsp free(abspath);
7447 af179be7 2023-04-14 stsp free(wt_path);
7448 af179be7 2023-04-14 stsp return err;
7449 af179be7 2023-04-14 stsp }
7450 af179be7 2023-04-14 stsp
7451 af179be7 2023-04-14 stsp static const struct got_error *
7452 af179be7 2023-04-14 stsp get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7453 af179be7 2023-04-14 stsp struct got_object_id *id, const char *path_prefix,
7454 af179be7 2023-04-14 stsp struct got_repository *repo)
7455 af179be7 2023-04-14 stsp {
7456 af179be7 2023-04-14 stsp const struct got_error *err;
7457 af179be7 2023-04-14 stsp struct got_commit_object *commit = NULL;
7458 af179be7 2023-04-14 stsp struct got_object_qid *pid;
7459 af179be7 2023-04-14 stsp
7460 af179be7 2023-04-14 stsp err = got_object_open_as_commit(&commit, repo, id);
7461 af179be7 2023-04-14 stsp if (err)
7462 af179be7 2023-04-14 stsp goto done;
7463 af179be7 2023-04-14 stsp
7464 af179be7 2023-04-14 stsp pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7465 af179be7 2023-04-14 stsp
7466 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(added_paths,
7467 af179be7 2023-04-14 stsp pid ? &pid->id : NULL, id, path_prefix, repo);
7468 af179be7 2023-04-14 stsp if (err)
7469 af179be7 2023-04-14 stsp goto done;
7470 af179be7 2023-04-14 stsp done:
7471 af179be7 2023-04-14 stsp if (commit)
7472 af179be7 2023-04-14 stsp got_object_commit_close(commit);
7473 af179be7 2023-04-14 stsp return err;
7474 af179be7 2023-04-14 stsp }
7475 af179be7 2023-04-14 stsp
7476 818c7501 2019-07-11 stsp const struct got_error *
7477 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7478 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7479 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7480 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7481 818c7501 2019-07-11 stsp {
7482 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7483 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7484 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7485 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7486 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7487 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7488 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7489 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7490 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7491 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7492 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7493 818c7501 2019-07-11 stsp
7494 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7495 af179be7 2023-04-14 stsp
7496 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7497 818c7501 2019-07-11 stsp if (err)
7498 818c7501 2019-07-11 stsp return err;
7499 af179be7 2023-04-14 stsp
7500 af179be7 2023-04-14 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7501 af179be7 2023-04-14 stsp if (err)
7502 af179be7 2023-04-14 stsp goto done;
7503 af179be7 2023-04-14 stsp
7504 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7505 af179be7 2023-04-14 stsp if (err)
7506 af179be7 2023-04-14 stsp goto done;
7507 af179be7 2023-04-14 stsp
7508 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7509 af179be7 2023-04-14 stsp if (err)
7510 af179be7 2023-04-14 stsp goto done;
7511 a44927cc 2022-04-07 stsp
7512 af179be7 2023-04-14 stsp /*
7513 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7514 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7515 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
7516 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
7517 af179be7 2023-04-14 stsp */
7518 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7519 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7520 af179be7 2023-04-14 stsp if (err)
7521 af179be7 2023-04-14 stsp goto done;
7522 af179be7 2023-04-14 stsp
7523 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7524 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7525 818c7501 2019-07-11 stsp if (err)
7526 818c7501 2019-07-11 stsp goto done;
7527 818c7501 2019-07-11 stsp
7528 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7529 818c7501 2019-07-11 stsp if (err)
7530 818c7501 2019-07-11 stsp goto done;
7531 818c7501 2019-07-11 stsp
7532 818c7501 2019-07-11 stsp /*
7533 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7534 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7535 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7536 818c7501 2019-07-11 stsp */
7537 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7538 818c7501 2019-07-11 stsp if (err)
7539 818c7501 2019-07-11 stsp goto done;
7540 818c7501 2019-07-11 stsp
7541 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7542 1b093d84 2023-04-12 stsp if (err)
7543 1b093d84 2023-04-12 stsp goto done;
7544 1b093d84 2023-04-12 stsp
7545 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7546 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7547 818c7501 2019-07-11 stsp if (err)
7548 818c7501 2019-07-11 stsp goto done;
7549 818c7501 2019-07-11 stsp
7550 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7551 a44927cc 2022-04-07 stsp worktree->path_prefix);
7552 ca355955 2019-07-12 stsp if (err)
7553 ca355955 2019-07-12 stsp goto done;
7554 ca355955 2019-07-12 stsp
7555 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7556 ca355955 2019-07-12 stsp if (err)
7557 ca355955 2019-07-12 stsp goto done;
7558 ca355955 2019-07-12 stsp
7559 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7560 818c7501 2019-07-11 stsp if (err)
7561 818c7501 2019-07-11 stsp goto done;
7562 818c7501 2019-07-11 stsp
7563 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7564 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7565 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7566 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7567 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7568 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7569 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7570 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7571 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7572 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7573 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7574 55bd499d 2019-07-12 stsp if (err)
7575 1f1abb7e 2019-08-08 stsp goto sync;
7576 55bd499d 2019-07-12 stsp
7577 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7578 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7579 ca355955 2019-07-12 stsp sync:
7580 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7581 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7582 ca355955 2019-07-12 stsp err = sync_err;
7583 818c7501 2019-07-11 stsp done:
7584 af179be7 2023-04-14 stsp got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7585 818c7501 2019-07-11 stsp got_ref_close(resolved);
7586 a3a2faf2 2019-07-12 stsp free(tree_id);
7587 818c7501 2019-07-11 stsp free(commit_id);
7588 af179be7 2023-04-14 stsp free(merged_commit_id);
7589 a44927cc 2022-04-07 stsp if (commit)
7590 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7591 0ebf8283 2019-07-24 stsp if (fileindex)
7592 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7593 0ebf8283 2019-07-24 stsp free(fileindex_path);
7594 af179be7 2023-04-14 stsp free(commit_ref_name);
7595 af179be7 2023-04-14 stsp if (commit_ref)
7596 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7597 0ebf8283 2019-07-24 stsp
7598 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7599 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7600 0ebf8283 2019-07-24 stsp err = unlockerr;
7601 0ebf8283 2019-07-24 stsp return err;
7602 0ebf8283 2019-07-24 stsp }
7603 0ebf8283 2019-07-24 stsp
7604 0ebf8283 2019-07-24 stsp const struct got_error *
7605 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7606 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7607 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7608 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7609 0ebf8283 2019-07-24 stsp {
7610 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7611 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7612 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7613 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7614 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7615 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7616 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7617 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7618 0ebf8283 2019-07-24 stsp
7619 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7620 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7621 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7622 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7623 0ebf8283 2019-07-24 stsp
7624 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7625 0ebf8283 2019-07-24 stsp if (err)
7626 0ebf8283 2019-07-24 stsp return err;
7627 0ebf8283 2019-07-24 stsp
7628 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7629 0ebf8283 2019-07-24 stsp if (err)
7630 0ebf8283 2019-07-24 stsp goto done;
7631 0ebf8283 2019-07-24 stsp
7632 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7633 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7634 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7635 0ebf8283 2019-07-24 stsp &ok_arg);
7636 0ebf8283 2019-07-24 stsp if (err)
7637 0ebf8283 2019-07-24 stsp goto done;
7638 0ebf8283 2019-07-24 stsp
7639 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7640 0ebf8283 2019-07-24 stsp if (err)
7641 0ebf8283 2019-07-24 stsp goto done;
7642 0ebf8283 2019-07-24 stsp
7643 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7644 0ebf8283 2019-07-24 stsp if (err)
7645 0ebf8283 2019-07-24 stsp goto done;
7646 0ebf8283 2019-07-24 stsp
7647 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7648 0ebf8283 2019-07-24 stsp worktree);
7649 0ebf8283 2019-07-24 stsp if (err)
7650 0ebf8283 2019-07-24 stsp goto done;
7651 0ebf8283 2019-07-24 stsp
7652 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7653 0ebf8283 2019-07-24 stsp 0);
7654 0ebf8283 2019-07-24 stsp if (err)
7655 0ebf8283 2019-07-24 stsp goto done;
7656 0ebf8283 2019-07-24 stsp
7657 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7658 0ebf8283 2019-07-24 stsp if (err)
7659 0ebf8283 2019-07-24 stsp goto done;
7660 0ebf8283 2019-07-24 stsp
7661 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7662 0ebf8283 2019-07-24 stsp if (err)
7663 0ebf8283 2019-07-24 stsp goto done;
7664 0ebf8283 2019-07-24 stsp
7665 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7666 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7667 0ebf8283 2019-07-24 stsp if (err)
7668 0ebf8283 2019-07-24 stsp goto done;
7669 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7670 0ebf8283 2019-07-24 stsp if (err)
7671 0ebf8283 2019-07-24 stsp goto done;
7672 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7673 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7674 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7675 0ebf8283 2019-07-24 stsp goto done;
7676 0ebf8283 2019-07-24 stsp }
7677 0ebf8283 2019-07-24 stsp
7678 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7679 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7680 0ebf8283 2019-07-24 stsp if (err)
7681 0ebf8283 2019-07-24 stsp goto done;
7682 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7683 0ebf8283 2019-07-24 stsp if (err)
7684 0ebf8283 2019-07-24 stsp goto done;
7685 0ebf8283 2019-07-24 stsp
7686 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7687 0ebf8283 2019-07-24 stsp if (err)
7688 0ebf8283 2019-07-24 stsp goto done;
7689 0ebf8283 2019-07-24 stsp done:
7690 0ebf8283 2019-07-24 stsp free(fileindex_path);
7691 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7692 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7693 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7694 0ebf8283 2019-07-24 stsp if (wt_branch)
7695 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7696 0ebf8283 2019-07-24 stsp if (err) {
7697 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7698 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7699 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7700 0ebf8283 2019-07-24 stsp }
7701 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7702 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7703 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7704 0ebf8283 2019-07-24 stsp }
7705 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7706 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7707 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7708 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7709 3e3a69f1 2019-07-25 stsp }
7710 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7711 0ebf8283 2019-07-24 stsp }
7712 0ebf8283 2019-07-24 stsp return err;
7713 0ebf8283 2019-07-24 stsp }
7714 0ebf8283 2019-07-24 stsp
7715 0ebf8283 2019-07-24 stsp const struct got_error *
7716 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7717 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7718 0ebf8283 2019-07-24 stsp {
7719 3e3a69f1 2019-07-25 stsp if (fileindex)
7720 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7721 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7722 0ebf8283 2019-07-24 stsp }
7723 0ebf8283 2019-07-24 stsp
7724 0ebf8283 2019-07-24 stsp const struct got_error *
7725 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7726 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7727 0ebf8283 2019-07-24 stsp {
7728 0ebf8283 2019-07-24 stsp const struct got_error *err;
7729 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7730 0ebf8283 2019-07-24 stsp
7731 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7732 0ebf8283 2019-07-24 stsp if (err)
7733 0ebf8283 2019-07-24 stsp return err;
7734 0ebf8283 2019-07-24 stsp
7735 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7736 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7737 0ebf8283 2019-07-24 stsp return NULL;
7738 0ebf8283 2019-07-24 stsp }
7739 0ebf8283 2019-07-24 stsp
7740 0ebf8283 2019-07-24 stsp const struct got_error *
7741 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7742 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7743 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7744 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7745 0ebf8283 2019-07-24 stsp {
7746 0ebf8283 2019-07-24 stsp const struct got_error *err;
7747 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7748 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7749 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7750 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7751 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7752 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7753 0ebf8283 2019-07-24 stsp
7754 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7755 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7756 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7757 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7758 0ebf8283 2019-07-24 stsp
7759 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7760 3e3a69f1 2019-07-25 stsp if (err)
7761 3e3a69f1 2019-07-25 stsp return err;
7762 3e3a69f1 2019-07-25 stsp
7763 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7764 3e3a69f1 2019-07-25 stsp if (err)
7765 f032f1f7 2019-08-04 stsp goto done;
7766 f032f1f7 2019-08-04 stsp
7767 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7768 f032f1f7 2019-08-04 stsp &have_staged_files);
7769 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7770 f032f1f7 2019-08-04 stsp goto done;
7771 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7772 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7773 3e3a69f1 2019-07-25 stsp goto done;
7774 f032f1f7 2019-08-04 stsp }
7775 3e3a69f1 2019-07-25 stsp
7776 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7777 0ebf8283 2019-07-24 stsp if (err)
7778 f032f1f7 2019-08-04 stsp goto done;
7779 0ebf8283 2019-07-24 stsp
7780 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7781 0ebf8283 2019-07-24 stsp if (err)
7782 0ebf8283 2019-07-24 stsp goto done;
7783 0ebf8283 2019-07-24 stsp
7784 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7785 0ebf8283 2019-07-24 stsp if (err)
7786 0ebf8283 2019-07-24 stsp goto done;
7787 0ebf8283 2019-07-24 stsp
7788 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7789 0ebf8283 2019-07-24 stsp worktree);
7790 0ebf8283 2019-07-24 stsp if (err)
7791 0ebf8283 2019-07-24 stsp goto done;
7792 0ebf8283 2019-07-24 stsp
7793 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7794 0ebf8283 2019-07-24 stsp if (err)
7795 0ebf8283 2019-07-24 stsp goto done;
7796 0ebf8283 2019-07-24 stsp
7797 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7798 0ebf8283 2019-07-24 stsp if (err)
7799 0ebf8283 2019-07-24 stsp goto done;
7800 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7801 0ebf8283 2019-07-24 stsp if (err)
7802 0ebf8283 2019-07-24 stsp goto done;
7803 0ebf8283 2019-07-24 stsp
7804 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7805 0ebf8283 2019-07-24 stsp if (err)
7806 0ebf8283 2019-07-24 stsp goto done;
7807 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7808 0ebf8283 2019-07-24 stsp if (err)
7809 0ebf8283 2019-07-24 stsp goto done;
7810 0ebf8283 2019-07-24 stsp
7811 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7812 0ebf8283 2019-07-24 stsp if (err)
7813 0ebf8283 2019-07-24 stsp goto done;
7814 0ebf8283 2019-07-24 stsp done:
7815 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7816 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7817 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7818 0ebf8283 2019-07-24 stsp if (commit_ref)
7819 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7820 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7821 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7822 0ebf8283 2019-07-24 stsp if (err) {
7823 0ebf8283 2019-07-24 stsp free(*commit_id);
7824 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7825 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7826 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7827 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7828 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7829 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7830 0ebf8283 2019-07-24 stsp }
7831 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7832 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7833 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7834 3e3a69f1 2019-07-25 stsp }
7835 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7836 0ebf8283 2019-07-24 stsp }
7837 0ebf8283 2019-07-24 stsp return err;
7838 0ebf8283 2019-07-24 stsp }
7839 0ebf8283 2019-07-24 stsp
7840 0ebf8283 2019-07-24 stsp static const struct got_error *
7841 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7842 0ebf8283 2019-07-24 stsp {
7843 0ebf8283 2019-07-24 stsp const struct got_error *err;
7844 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7845 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7846 0ebf8283 2019-07-24 stsp
7847 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7848 0ebf8283 2019-07-24 stsp if (err)
7849 0ebf8283 2019-07-24 stsp goto done;
7850 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7851 0ebf8283 2019-07-24 stsp if (err)
7852 0ebf8283 2019-07-24 stsp goto done;
7853 0ebf8283 2019-07-24 stsp
7854 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7855 0ebf8283 2019-07-24 stsp worktree);
7856 0ebf8283 2019-07-24 stsp if (err)
7857 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7859 0ebf8283 2019-07-24 stsp if (err)
7860 0ebf8283 2019-07-24 stsp goto done;
7861 0ebf8283 2019-07-24 stsp
7862 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7863 0ebf8283 2019-07-24 stsp if (err)
7864 0ebf8283 2019-07-24 stsp goto done;
7865 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7866 0ebf8283 2019-07-24 stsp if (err)
7867 0ebf8283 2019-07-24 stsp goto done;
7868 0ebf8283 2019-07-24 stsp
7869 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7870 0ebf8283 2019-07-24 stsp if (err)
7871 0ebf8283 2019-07-24 stsp goto done;
7872 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7873 0ebf8283 2019-07-24 stsp if (err)
7874 0ebf8283 2019-07-24 stsp goto done;
7875 0ebf8283 2019-07-24 stsp done:
7876 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7877 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7878 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7879 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7880 0ebf8283 2019-07-24 stsp return err;
7881 0ebf8283 2019-07-24 stsp }
7882 0ebf8283 2019-07-24 stsp
7883 0ebf8283 2019-07-24 stsp const struct got_error *
7884 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7885 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7886 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7887 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7888 0ebf8283 2019-07-24 stsp {
7889 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7890 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7891 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7892 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
7893 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7894 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
7895 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
7896 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7897 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7898 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
7899 0ebf8283 2019-07-24 stsp
7900 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
7901 af179be7 2023-04-14 stsp
7902 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7903 0ebf8283 2019-07-24 stsp if (err)
7904 0ebf8283 2019-07-24 stsp return err;
7905 a44927cc 2022-04-07 stsp
7906 af179be7 2023-04-14 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7907 af179be7 2023-04-14 stsp if (err)
7908 af179be7 2023-04-14 stsp goto done;
7909 af179be7 2023-04-14 stsp
7910 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7911 af179be7 2023-04-14 stsp if (err) {
7912 af179be7 2023-04-14 stsp if (err->code != GOT_ERR_NOT_REF)
7913 af179be7 2023-04-14 stsp goto done;
7914 af179be7 2023-04-14 stsp /* Can happen on early abort due to invalid histedit script. */
7915 af179be7 2023-04-14 stsp commit_ref = NULL;
7916 af179be7 2023-04-14 stsp }
7917 af179be7 2023-04-14 stsp
7918 af179be7 2023-04-14 stsp if (commit_ref) {
7919 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7920 af179be7 2023-04-14 stsp if (err)
7921 af179be7 2023-04-14 stsp goto done;
7922 af179be7 2023-04-14 stsp
7923 af179be7 2023-04-14 stsp /*
7924 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
7925 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
7926 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files added by the
7927 af179be7 2023-04-14 stsp * user during conflict resolution or during histedit -e.
7928 af179be7 2023-04-14 stsp */
7929 af179be7 2023-04-14 stsp err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7930 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
7931 af179be7 2023-04-14 stsp if (err)
7932 af179be7 2023-04-14 stsp goto done;
7933 af179be7 2023-04-14 stsp }
7934 af179be7 2023-04-14 stsp
7935 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7936 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7937 0ebf8283 2019-07-24 stsp if (err)
7938 0ebf8283 2019-07-24 stsp goto done;
7939 0ebf8283 2019-07-24 stsp
7940 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7941 0ebf8283 2019-07-24 stsp if (err)
7942 0ebf8283 2019-07-24 stsp goto done;
7943 0ebf8283 2019-07-24 stsp
7944 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7945 0ebf8283 2019-07-24 stsp if (err)
7946 0ebf8283 2019-07-24 stsp goto done;
7947 0ebf8283 2019-07-24 stsp
7948 1b093d84 2023-04-12 stsp err = got_object_open_as_commit(&commit, repo,
7949 1b093d84 2023-04-12 stsp worktree->base_commit_id);
7950 1b093d84 2023-04-12 stsp if (err)
7951 1b093d84 2023-04-12 stsp goto done;
7952 1b093d84 2023-04-12 stsp
7953 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7954 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7955 0ebf8283 2019-07-24 stsp if (err)
7956 0ebf8283 2019-07-24 stsp goto done;
7957 0ebf8283 2019-07-24 stsp
7958 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7959 0ebf8283 2019-07-24 stsp if (err)
7960 0ebf8283 2019-07-24 stsp goto done;
7961 0ebf8283 2019-07-24 stsp
7962 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7963 0ebf8283 2019-07-24 stsp if (err)
7964 0ebf8283 2019-07-24 stsp goto done;
7965 0ebf8283 2019-07-24 stsp
7966 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7967 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7968 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7969 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7970 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7971 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7972 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7973 af179be7 2023-04-14 stsp rfa.unlink_added_files = 1;
7974 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
7975 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7976 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7977 0ebf8283 2019-07-24 stsp if (err)
7978 1f1abb7e 2019-08-08 stsp goto sync;
7979 0ebf8283 2019-07-24 stsp
7980 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7981 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7982 0ebf8283 2019-07-24 stsp sync:
7983 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7984 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7985 0ebf8283 2019-07-24 stsp err = sync_err;
7986 0ebf8283 2019-07-24 stsp done:
7987 af179be7 2023-04-14 stsp if (resolved)
7988 af179be7 2023-04-14 stsp got_ref_close(resolved);
7989 af179be7 2023-04-14 stsp if (commit_ref)
7990 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
7991 af179be7 2023-04-14 stsp free(merged_commit_id);
7992 0ebf8283 2019-07-24 stsp free(tree_id);
7993 818c7501 2019-07-11 stsp free(fileindex_path);
7994 af179be7 2023-04-14 stsp free(commit_ref_name);
7995 818c7501 2019-07-11 stsp
7996 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7997 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7998 818c7501 2019-07-11 stsp err = unlockerr;
7999 818c7501 2019-07-11 stsp return err;
8000 818c7501 2019-07-11 stsp }
8001 0ebf8283 2019-07-24 stsp
8002 0ebf8283 2019-07-24 stsp const struct got_error *
8003 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
8004 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8005 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
8006 0ebf8283 2019-07-24 stsp {
8007 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
8008 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
8009 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
8010 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
8011 0ebf8283 2019-07-24 stsp
8012 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8013 0ebf8283 2019-07-24 stsp if (err)
8014 0ebf8283 2019-07-24 stsp return err;
8015 0ebf8283 2019-07-24 stsp
8016 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
8017 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
8018 e600f124 2021-03-21 stsp if (err)
8019 e600f124 2021-03-21 stsp goto done;
8020 e600f124 2021-03-21 stsp
8021 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8022 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
8023 0ebf8283 2019-07-24 stsp if (err)
8024 0ebf8283 2019-07-24 stsp goto done;
8025 0ebf8283 2019-07-24 stsp
8026 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
8027 0ebf8283 2019-07-24 stsp if (err)
8028 0ebf8283 2019-07-24 stsp goto done;
8029 0ebf8283 2019-07-24 stsp
8030 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
8031 0ebf8283 2019-07-24 stsp if (err)
8032 0ebf8283 2019-07-24 stsp goto done;
8033 0ebf8283 2019-07-24 stsp
8034 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
8035 0ebf8283 2019-07-24 stsp if (err)
8036 0ebf8283 2019-07-24 stsp goto done;
8037 0ebf8283 2019-07-24 stsp
8038 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
8039 a615e0e7 2020-12-16 stsp if (err)
8040 a615e0e7 2020-12-16 stsp goto done;
8041 a615e0e7 2020-12-16 stsp
8042 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
8043 a615e0e7 2020-12-16 stsp if (err)
8044 a615e0e7 2020-12-16 stsp goto done;
8045 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8046 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8047 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
8048 a615e0e7 2020-12-16 stsp err = sync_err;
8049 0ebf8283 2019-07-24 stsp done:
8050 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
8051 a615e0e7 2020-12-16 stsp free(fileindex_path);
8052 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
8053 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8054 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
8055 0ebf8283 2019-07-24 stsp err = unlockerr;
8056 0ebf8283 2019-07-24 stsp return err;
8057 0ebf8283 2019-07-24 stsp }
8058 0ebf8283 2019-07-24 stsp
8059 0ebf8283 2019-07-24 stsp const struct got_error *
8060 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8061 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8062 0ebf8283 2019-07-24 stsp {
8063 0ebf8283 2019-07-24 stsp const struct got_error *err;
8064 0ebf8283 2019-07-24 stsp char *commit_ref_name;
8065 0ebf8283 2019-07-24 stsp
8066 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8067 0ebf8283 2019-07-24 stsp if (err)
8068 0ebf8283 2019-07-24 stsp return err;
8069 0ebf8283 2019-07-24 stsp
8070 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8071 0ebf8283 2019-07-24 stsp if (err)
8072 0ebf8283 2019-07-24 stsp goto done;
8073 0ebf8283 2019-07-24 stsp
8074 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
8075 0ebf8283 2019-07-24 stsp done:
8076 0ebf8283 2019-07-24 stsp free(commit_ref_name);
8077 2822a352 2019-10-15 stsp return err;
8078 2822a352 2019-10-15 stsp }
8079 2822a352 2019-10-15 stsp
8080 2822a352 2019-10-15 stsp const struct got_error *
8081 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8082 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8083 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
8084 2822a352 2019-10-15 stsp struct got_repository *repo)
8085 2822a352 2019-10-15 stsp {
8086 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
8087 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8088 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
8089 2822a352 2019-10-15 stsp
8090 2822a352 2019-10-15 stsp *fileindex = NULL;
8091 2822a352 2019-10-15 stsp *branch_ref = NULL;
8092 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8093 2822a352 2019-10-15 stsp
8094 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
8095 2822a352 2019-10-15 stsp if (err)
8096 2822a352 2019-10-15 stsp return err;
8097 2822a352 2019-10-15 stsp
8098 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8099 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
8100 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
8101 2822a352 2019-10-15 stsp "update -b or different branch name required");
8102 2822a352 2019-10-15 stsp goto done;
8103 2822a352 2019-10-15 stsp }
8104 2822a352 2019-10-15 stsp
8105 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8106 2822a352 2019-10-15 stsp if (err)
8107 2822a352 2019-10-15 stsp goto done;
8108 2822a352 2019-10-15 stsp
8109 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
8110 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
8111 2822a352 2019-10-15 stsp ok_arg.repo = repo;
8112 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8113 2822a352 2019-10-15 stsp &ok_arg);
8114 2822a352 2019-10-15 stsp if (err)
8115 2822a352 2019-10-15 stsp goto done;
8116 2822a352 2019-10-15 stsp
8117 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
8118 2822a352 2019-10-15 stsp if (err)
8119 2822a352 2019-10-15 stsp goto done;
8120 2822a352 2019-10-15 stsp
8121 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
8122 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
8123 2822a352 2019-10-15 stsp done:
8124 2822a352 2019-10-15 stsp if (err) {
8125 2822a352 2019-10-15 stsp if (*branch_ref) {
8126 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
8127 2822a352 2019-10-15 stsp *branch_ref = NULL;
8128 2822a352 2019-10-15 stsp }
8129 2822a352 2019-10-15 stsp if (*base_branch_ref) {
8130 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
8131 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
8132 2822a352 2019-10-15 stsp }
8133 2822a352 2019-10-15 stsp if (*fileindex) {
8134 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
8135 2822a352 2019-10-15 stsp *fileindex = NULL;
8136 2822a352 2019-10-15 stsp }
8137 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
8138 2822a352 2019-10-15 stsp }
8139 0cb83759 2019-08-03 stsp return err;
8140 0cb83759 2019-08-03 stsp }
8141 0cb83759 2019-08-03 stsp
8142 2822a352 2019-10-15 stsp const struct got_error *
8143 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
8144 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8145 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8146 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8147 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8148 2822a352 2019-10-15 stsp {
8149 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8150 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
8151 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
8152 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8153 2822a352 2019-10-15 stsp
8154 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
8155 2822a352 2019-10-15 stsp if (err)
8156 2822a352 2019-10-15 stsp goto done;
8157 2822a352 2019-10-15 stsp
8158 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
8159 2822a352 2019-10-15 stsp if (err)
8160 2822a352 2019-10-15 stsp goto done;
8161 2822a352 2019-10-15 stsp
8162 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8163 a44927cc 2022-04-07 stsp if (err)
8164 a44927cc 2022-04-07 stsp goto done;
8165 a44927cc 2022-04-07 stsp
8166 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8167 2822a352 2019-10-15 stsp worktree->path_prefix);
8168 2822a352 2019-10-15 stsp if (err)
8169 2822a352 2019-10-15 stsp goto done;
8170 2822a352 2019-10-15 stsp
8171 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8172 2822a352 2019-10-15 stsp if (err)
8173 2822a352 2019-10-15 stsp goto done;
8174 2822a352 2019-10-15 stsp
8175 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8176 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
8177 2822a352 2019-10-15 stsp if (err)
8178 2822a352 2019-10-15 stsp goto sync;
8179 2822a352 2019-10-15 stsp
8180 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
8181 2822a352 2019-10-15 stsp if (err)
8182 2822a352 2019-10-15 stsp goto sync;
8183 2822a352 2019-10-15 stsp
8184 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
8185 6e210706 2021-01-22 stsp if (err)
8186 6e210706 2021-01-22 stsp goto sync;
8187 6e210706 2021-01-22 stsp
8188 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8189 2822a352 2019-10-15 stsp sync:
8190 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8191 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
8192 2822a352 2019-10-15 stsp err = sync_err;
8193 2822a352 2019-10-15 stsp
8194 2822a352 2019-10-15 stsp done:
8195 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
8196 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8197 2822a352 2019-10-15 stsp err = unlockerr;
8198 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8199 2822a352 2019-10-15 stsp
8200 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
8201 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8202 2822a352 2019-10-15 stsp err = unlockerr;
8203 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8204 2822a352 2019-10-15 stsp
8205 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
8206 2822a352 2019-10-15 stsp free(fileindex_path);
8207 2822a352 2019-10-15 stsp free(tree_id);
8208 a44927cc 2022-04-07 stsp if (commit)
8209 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8210 2822a352 2019-10-15 stsp
8211 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8212 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
8213 2822a352 2019-10-15 stsp err = unlockerr;
8214 2822a352 2019-10-15 stsp return err;
8215 2822a352 2019-10-15 stsp }
8216 2822a352 2019-10-15 stsp
8217 2822a352 2019-10-15 stsp const struct got_error *
8218 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
8219 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8220 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8221 2822a352 2019-10-15 stsp {
8222 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
8223 8b692cd0 2019-10-21 stsp
8224 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
8225 8b692cd0 2019-10-21 stsp
8226 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
8227 8b692cd0 2019-10-21 stsp
8228 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
8229 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8230 8b692cd0 2019-10-21 stsp err = unlockerr;
8231 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
8232 8b692cd0 2019-10-21 stsp
8233 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
8234 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
8235 8b692cd0 2019-10-21 stsp err = unlockerr;
8236 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
8237 f259c4c1 2021-09-24 stsp
8238 f259c4c1 2021-09-24 stsp return err;
8239 f259c4c1 2021-09-24 stsp }
8240 f259c4c1 2021-09-24 stsp
8241 f259c4c1 2021-09-24 stsp const struct got_error *
8242 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
8243 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
8244 f259c4c1 2021-09-24 stsp {
8245 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
8246 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8247 f259c4c1 2021-09-24 stsp
8248 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8249 f259c4c1 2021-09-24 stsp if (err)
8250 f259c4c1 2021-09-24 stsp goto done;
8251 8b692cd0 2019-10-21 stsp
8252 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8253 f259c4c1 2021-09-24 stsp
8254 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
8255 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8256 f259c4c1 2021-09-24 stsp err = sync_err;
8257 f259c4c1 2021-09-24 stsp done:
8258 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8259 f259c4c1 2021-09-24 stsp free(fileindex_path);
8260 8b692cd0 2019-10-21 stsp return err;
8261 2822a352 2019-10-15 stsp }
8262 2822a352 2019-10-15 stsp
8263 f259c4c1 2021-09-24 stsp static const struct got_error *
8264 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8265 f259c4c1 2021-09-24 stsp {
8266 f259c4c1 2021-09-24 stsp const struct got_error *err;
8267 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8268 f259c4c1 2021-09-24 stsp
8269 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8270 f259c4c1 2021-09-24 stsp if (err)
8271 f259c4c1 2021-09-24 stsp goto done;
8272 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
8273 f259c4c1 2021-09-24 stsp if (err)
8274 f259c4c1 2021-09-24 stsp goto done;
8275 f259c4c1 2021-09-24 stsp
8276 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8277 f259c4c1 2021-09-24 stsp if (err)
8278 f259c4c1 2021-09-24 stsp goto done;
8279 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
8280 f259c4c1 2021-09-24 stsp if (err)
8281 f259c4c1 2021-09-24 stsp goto done;
8282 f259c4c1 2021-09-24 stsp
8283 f259c4c1 2021-09-24 stsp done:
8284 f259c4c1 2021-09-24 stsp free(branch_refname);
8285 f259c4c1 2021-09-24 stsp free(commit_refname);
8286 f259c4c1 2021-09-24 stsp return err;
8287 f259c4c1 2021-09-24 stsp }
8288 f259c4c1 2021-09-24 stsp
8289 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
8290 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
8291 f259c4c1 2021-09-24 stsp const char *branch_name;
8292 f259c4c1 2021-09-24 stsp };
8293 f259c4c1 2021-09-24 stsp
8294 f259c4c1 2021-09-24 stsp static const struct got_error *
8295 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8296 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
8297 f259c4c1 2021-09-24 stsp {
8298 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
8299 f259c4c1 2021-09-24 stsp
8300 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8301 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
8302 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
8303 f259c4c1 2021-09-24 stsp
8304 f259c4c1 2021-09-24 stsp return NULL;
8305 f259c4c1 2021-09-24 stsp }
8306 f259c4c1 2021-09-24 stsp
8307 f259c4c1 2021-09-24 stsp
8308 f259c4c1 2021-09-24 stsp const struct got_error *
8309 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
8310 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
8311 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
8312 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
8313 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8314 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8315 f259c4c1 2021-09-24 stsp {
8316 f259c4c1 2021-09-24 stsp const struct got_error *err;
8317 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8318 f259c4c1 2021-09-24 stsp
8319 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8320 f259c4c1 2021-09-24 stsp if (err)
8321 f259c4c1 2021-09-24 stsp goto done;
8322 f259c4c1 2021-09-24 stsp
8323 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8324 f259c4c1 2021-09-24 stsp worktree);
8325 f259c4c1 2021-09-24 stsp if (err)
8326 f259c4c1 2021-09-24 stsp goto done;
8327 f259c4c1 2021-09-24 stsp
8328 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8329 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
8330 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
8331 f259c4c1 2021-09-24 stsp done:
8332 f259c4c1 2021-09-24 stsp free(fileindex_path);
8333 f259c4c1 2021-09-24 stsp return err;
8334 f259c4c1 2021-09-24 stsp }
8335 f259c4c1 2021-09-24 stsp
8336 f259c4c1 2021-09-24 stsp const struct got_error *
8337 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
8338 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8339 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
8340 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
8341 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
8342 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
8343 0ff8d236 2021-09-28 stsp
8344 f259c4c1 2021-09-24 stsp {
8345 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
8346 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
8347 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
8348 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8349 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8350 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8351 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8352 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8353 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8354 f259c4c1 2021-09-24 stsp
8355 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8356 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8357 f259c4c1 2021-09-24 stsp
8358 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8359 f259c4c1 2021-09-24 stsp
8360 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8361 f259c4c1 2021-09-24 stsp if (err)
8362 f259c4c1 2021-09-24 stsp goto done;
8363 f259c4c1 2021-09-24 stsp
8364 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8365 f259c4c1 2021-09-24 stsp if (err)
8366 f259c4c1 2021-09-24 stsp goto done;
8367 f259c4c1 2021-09-24 stsp
8368 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8369 f259c4c1 2021-09-24 stsp if (err)
8370 f259c4c1 2021-09-24 stsp goto done;
8371 f259c4c1 2021-09-24 stsp
8372 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8373 f259c4c1 2021-09-24 stsp &have_staged_files);
8374 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8375 f259c4c1 2021-09-24 stsp goto done;
8376 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8377 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8378 f259c4c1 2021-09-24 stsp goto done;
8379 f259c4c1 2021-09-24 stsp }
8380 f259c4c1 2021-09-24 stsp
8381 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8382 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8383 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8384 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8385 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8386 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8387 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8388 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8389 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8390 f259c4c1 2021-09-24 stsp if (err)
8391 f259c4c1 2021-09-24 stsp goto done;
8392 f259c4c1 2021-09-24 stsp
8393 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8394 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8395 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
8396 f259c4c1 2021-09-24 stsp
8397 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
8398 f259c4c1 2021-09-24 stsp ct_path++;
8399 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
8400 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8401 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8402 f259c4c1 2021-09-24 stsp if (err)
8403 f259c4c1 2021-09-24 stsp goto done;
8404 f259c4c1 2021-09-24 stsp
8405 f259c4c1 2021-09-24 stsp }
8406 f259c4c1 2021-09-24 stsp
8407 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8408 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8409 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8410 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8411 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8412 f259c4c1 2021-09-24 stsp if (err)
8413 f259c4c1 2021-09-24 stsp goto done;
8414 f259c4c1 2021-09-24 stsp
8415 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8416 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8417 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8418 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8419 f259c4c1 2021-09-24 stsp err = sync_err;
8420 f259c4c1 2021-09-24 stsp done:
8421 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8422 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8423 d8bacb93 2023-01-10 mark
8424 f259c4c1 2021-09-24 stsp free_commitable(ct);
8425 f259c4c1 2021-09-24 stsp }
8426 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8427 f259c4c1 2021-09-24 stsp free(fileindex_path);
8428 f259c4c1 2021-09-24 stsp return err;
8429 f259c4c1 2021-09-24 stsp }
8430 f259c4c1 2021-09-24 stsp
8431 f259c4c1 2021-09-24 stsp const struct got_error *
8432 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8433 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8434 f259c4c1 2021-09-24 stsp {
8435 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8436 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8437 f259c4c1 2021-09-24 stsp
8438 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8439 f259c4c1 2021-09-24 stsp if (err)
8440 f259c4c1 2021-09-24 stsp goto done;
8441 f259c4c1 2021-09-24 stsp
8442 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8443 f259c4c1 2021-09-24 stsp if (err)
8444 f259c4c1 2021-09-24 stsp goto done;
8445 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8446 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8447 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8448 f259c4c1 2021-09-24 stsp err = sync_err;
8449 f259c4c1 2021-09-24 stsp done:
8450 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8451 f259c4c1 2021-09-24 stsp free(fileindex_path);
8452 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8453 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8454 f259c4c1 2021-09-24 stsp err = unlockerr;
8455 f259c4c1 2021-09-24 stsp return err;
8456 f259c4c1 2021-09-24 stsp }
8457 f259c4c1 2021-09-24 stsp
8458 f259c4c1 2021-09-24 stsp const struct got_error *
8459 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8460 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8461 f259c4c1 2021-09-24 stsp {
8462 f259c4c1 2021-09-24 stsp const struct got_error *err;
8463 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8464 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8465 f259c4c1 2021-09-24 stsp
8466 f259c4c1 2021-09-24 stsp *in_progress = 0;
8467 f259c4c1 2021-09-24 stsp
8468 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8469 f259c4c1 2021-09-24 stsp if (err)
8470 f259c4c1 2021-09-24 stsp return err;
8471 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8472 793fcac3 2021-09-24 stsp free(branch_refname);
8473 f259c4c1 2021-09-24 stsp if (err) {
8474 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8475 f259c4c1 2021-09-24 stsp return err;
8476 f259c4c1 2021-09-24 stsp } else
8477 f259c4c1 2021-09-24 stsp *in_progress = 1;
8478 f259c4c1 2021-09-24 stsp
8479 f259c4c1 2021-09-24 stsp return NULL;
8480 f259c4c1 2021-09-24 stsp }
8481 f259c4c1 2021-09-24 stsp
8482 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8483 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8484 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8485 f259c4c1 2021-09-24 stsp {
8486 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8487 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8488 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8489 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8490 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8491 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8492 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8493 f259c4c1 2021-09-24 stsp
8494 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8495 f259c4c1 2021-09-24 stsp
8496 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8497 f259c4c1 2021-09-24 stsp if (err)
8498 f259c4c1 2021-09-24 stsp return err;
8499 f259c4c1 2021-09-24 stsp
8500 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8501 f259c4c1 2021-09-24 stsp if (err)
8502 f259c4c1 2021-09-24 stsp goto done;
8503 f259c4c1 2021-09-24 stsp
8504 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8505 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8506 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8507 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8508 f259c4c1 2021-09-24 stsp &ok_arg);
8509 f259c4c1 2021-09-24 stsp if (err)
8510 f259c4c1 2021-09-24 stsp goto done;
8511 f259c4c1 2021-09-24 stsp
8512 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8513 f259c4c1 2021-09-24 stsp if (err)
8514 f259c4c1 2021-09-24 stsp return err;
8515 f259c4c1 2021-09-24 stsp
8516 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8517 f259c4c1 2021-09-24 stsp if (err)
8518 f259c4c1 2021-09-24 stsp return err;
8519 f259c4c1 2021-09-24 stsp
8520 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8521 f259c4c1 2021-09-24 stsp 0);
8522 f259c4c1 2021-09-24 stsp if (err)
8523 f259c4c1 2021-09-24 stsp goto done;
8524 f259c4c1 2021-09-24 stsp
8525 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8526 f259c4c1 2021-09-24 stsp if (err)
8527 f259c4c1 2021-09-24 stsp goto done;
8528 f259c4c1 2021-09-24 stsp
8529 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8530 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8531 f259c4c1 2021-09-24 stsp goto done;
8532 f259c4c1 2021-09-24 stsp }
8533 f259c4c1 2021-09-24 stsp
8534 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8535 f259c4c1 2021-09-24 stsp if (err)
8536 f259c4c1 2021-09-24 stsp goto done;
8537 f259c4c1 2021-09-24 stsp
8538 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8539 f259c4c1 2021-09-24 stsp if (err)
8540 f259c4c1 2021-09-24 stsp goto done;
8541 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8542 f259c4c1 2021-09-24 stsp if (err)
8543 f259c4c1 2021-09-24 stsp goto done;
8544 f259c4c1 2021-09-24 stsp
8545 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8546 f259c4c1 2021-09-24 stsp if (err)
8547 f259c4c1 2021-09-24 stsp goto done;
8548 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8549 f259c4c1 2021-09-24 stsp if (err)
8550 f259c4c1 2021-09-24 stsp goto done;
8551 f259c4c1 2021-09-24 stsp
8552 f259c4c1 2021-09-24 stsp done:
8553 f259c4c1 2021-09-24 stsp free(branch_refname);
8554 f259c4c1 2021-09-24 stsp free(commit_refname);
8555 f259c4c1 2021-09-24 stsp free(fileindex_path);
8556 f259c4c1 2021-09-24 stsp if (branch_ref)
8557 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8558 f259c4c1 2021-09-24 stsp if (commit_ref)
8559 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8560 f259c4c1 2021-09-24 stsp if (wt_branch)
8561 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8562 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8563 f259c4c1 2021-09-24 stsp if (err) {
8564 f259c4c1 2021-09-24 stsp if (*fileindex) {
8565 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8566 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8567 f259c4c1 2021-09-24 stsp }
8568 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8569 f259c4c1 2021-09-24 stsp }
8570 f259c4c1 2021-09-24 stsp return err;
8571 f259c4c1 2021-09-24 stsp }
8572 f259c4c1 2021-09-24 stsp
8573 f259c4c1 2021-09-24 stsp const struct got_error *
8574 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8575 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8576 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8577 f259c4c1 2021-09-24 stsp {
8578 f259c4c1 2021-09-24 stsp const struct got_error *err;
8579 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8580 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8581 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8582 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8583 f259c4c1 2021-09-24 stsp
8584 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8585 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8586 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8587 f259c4c1 2021-09-24 stsp
8588 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8589 f259c4c1 2021-09-24 stsp if (err)
8590 f259c4c1 2021-09-24 stsp return err;
8591 f259c4c1 2021-09-24 stsp
8592 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8593 f259c4c1 2021-09-24 stsp if (err)
8594 f259c4c1 2021-09-24 stsp goto done;
8595 f259c4c1 2021-09-24 stsp
8596 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8597 f259c4c1 2021-09-24 stsp &have_staged_files);
8598 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8599 f259c4c1 2021-09-24 stsp goto done;
8600 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8601 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8602 f259c4c1 2021-09-24 stsp goto done;
8603 f259c4c1 2021-09-24 stsp }
8604 f259c4c1 2021-09-24 stsp
8605 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8606 f259c4c1 2021-09-24 stsp if (err)
8607 f259c4c1 2021-09-24 stsp goto done;
8608 f259c4c1 2021-09-24 stsp
8609 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8610 f259c4c1 2021-09-24 stsp if (err)
8611 f259c4c1 2021-09-24 stsp goto done;
8612 f259c4c1 2021-09-24 stsp
8613 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8614 f259c4c1 2021-09-24 stsp if (err)
8615 f259c4c1 2021-09-24 stsp goto done;
8616 f259c4c1 2021-09-24 stsp
8617 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8618 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8619 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8620 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8621 f259c4c1 2021-09-24 stsp goto done;
8622 f259c4c1 2021-09-24 stsp }
8623 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8624 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8625 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8626 f259c4c1 2021-09-24 stsp goto done;
8627 f259c4c1 2021-09-24 stsp }
8628 f259c4c1 2021-09-24 stsp
8629 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8630 f259c4c1 2021-09-24 stsp if (err)
8631 f259c4c1 2021-09-24 stsp goto done;
8632 f259c4c1 2021-09-24 stsp
8633 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8634 f259c4c1 2021-09-24 stsp if (err)
8635 f259c4c1 2021-09-24 stsp goto done;
8636 f259c4c1 2021-09-24 stsp done:
8637 f259c4c1 2021-09-24 stsp free(commit_refname);
8638 f259c4c1 2021-09-24 stsp free(branch_refname);
8639 f259c4c1 2021-09-24 stsp free(fileindex_path);
8640 f259c4c1 2021-09-24 stsp if (commit_ref)
8641 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8642 f259c4c1 2021-09-24 stsp if (branch_ref)
8643 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8644 f259c4c1 2021-09-24 stsp if (err) {
8645 f259c4c1 2021-09-24 stsp if (*branch_name) {
8646 f259c4c1 2021-09-24 stsp free(*branch_name);
8647 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8648 f259c4c1 2021-09-24 stsp }
8649 f259c4c1 2021-09-24 stsp free(*branch_tip);
8650 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8651 f259c4c1 2021-09-24 stsp if (*fileindex) {
8652 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8653 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8654 f259c4c1 2021-09-24 stsp }
8655 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8656 f259c4c1 2021-09-24 stsp }
8657 f259c4c1 2021-09-24 stsp return err;
8658 f259c4c1 2021-09-24 stsp }
8659 f259c4c1 2021-09-24 stsp
8660 f259c4c1 2021-09-24 stsp const struct got_error *
8661 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8662 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8663 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8664 f259c4c1 2021-09-24 stsp {
8665 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8666 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8667 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8668 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8669 af179be7 2023-04-14 stsp char *commit_ref_name = NULL;
8670 af179be7 2023-04-14 stsp struct got_reference *commit_ref = NULL;
8671 af179be7 2023-04-14 stsp struct got_object_id *merged_commit_id = NULL;
8672 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8673 af179be7 2023-04-14 stsp struct got_pathlist_head added_paths;
8674 af179be7 2023-04-14 stsp
8675 af179be7 2023-04-14 stsp TAILQ_INIT(&added_paths);
8676 af179be7 2023-04-14 stsp
8677 af179be7 2023-04-14 stsp err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8678 af179be7 2023-04-14 stsp if (err)
8679 af179be7 2023-04-14 stsp goto done;
8680 af179be7 2023-04-14 stsp
8681 af179be7 2023-04-14 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8682 af179be7 2023-04-14 stsp if (err)
8683 af179be7 2023-04-14 stsp goto done;
8684 af179be7 2023-04-14 stsp
8685 af179be7 2023-04-14 stsp err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8686 af179be7 2023-04-14 stsp if (err)
8687 af179be7 2023-04-14 stsp goto done;
8688 af179be7 2023-04-14 stsp
8689 af179be7 2023-04-14 stsp /*
8690 af179be7 2023-04-14 stsp * Determine which files in added status can be safely removed
8691 af179be7 2023-04-14 stsp * from disk while reverting changes in the work tree.
8692 af179be7 2023-04-14 stsp * We want to avoid deleting unrelated files which were added by
8693 af179be7 2023-04-14 stsp * the user for conflict resolution purposes.
8694 af179be7 2023-04-14 stsp */
8695 af179be7 2023-04-14 stsp err = get_paths_added_between_commits(&added_paths,
8696 af179be7 2023-04-14 stsp got_worktree_get_base_commit_id(worktree), merged_commit_id,
8697 af179be7 2023-04-14 stsp got_worktree_get_path_prefix(worktree), repo);
8698 af179be7 2023-04-14 stsp if (err)
8699 af179be7 2023-04-14 stsp goto done;
8700 f259c4c1 2021-09-24 stsp
8701 af179be7 2023-04-14 stsp
8702 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8703 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8704 a44927cc 2022-04-07 stsp if (err)
8705 a44927cc 2022-04-07 stsp goto done;
8706 a44927cc 2022-04-07 stsp
8707 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8708 a44927cc 2022-04-07 stsp worktree->path_prefix);
8709 f259c4c1 2021-09-24 stsp if (err)
8710 f259c4c1 2021-09-24 stsp goto done;
8711 f259c4c1 2021-09-24 stsp
8712 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8713 f259c4c1 2021-09-24 stsp if (err)
8714 f259c4c1 2021-09-24 stsp goto done;
8715 f259c4c1 2021-09-24 stsp
8716 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8717 f259c4c1 2021-09-24 stsp if (err)
8718 f259c4c1 2021-09-24 stsp goto done;
8719 f259c4c1 2021-09-24 stsp
8720 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8721 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8722 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8723 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8724 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8725 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8726 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8727 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8728 af179be7 2023-04-14 stsp rfa.added_files_to_unlink = &added_paths;
8729 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8730 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8731 f259c4c1 2021-09-24 stsp if (err)
8732 f259c4c1 2021-09-24 stsp goto sync;
8733 f259c4c1 2021-09-24 stsp
8734 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8735 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8736 f259c4c1 2021-09-24 stsp sync:
8737 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8738 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8739 f259c4c1 2021-09-24 stsp err = sync_err;
8740 f259c4c1 2021-09-24 stsp done:
8741 f259c4c1 2021-09-24 stsp free(tree_id);
8742 af179be7 2023-04-14 stsp free(merged_commit_id);
8743 a44927cc 2022-04-07 stsp if (commit)
8744 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8745 f259c4c1 2021-09-24 stsp if (fileindex)
8746 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8747 f259c4c1 2021-09-24 stsp free(fileindex_path);
8748 af179be7 2023-04-14 stsp if (commit_ref)
8749 af179be7 2023-04-14 stsp got_ref_close(commit_ref);
8750 af179be7 2023-04-14 stsp free(commit_ref_name);
8751 f259c4c1 2021-09-24 stsp
8752 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8753 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8754 f259c4c1 2021-09-24 stsp err = unlockerr;
8755 f259c4c1 2021-09-24 stsp return err;
8756 f259c4c1 2021-09-24 stsp }
8757 f259c4c1 2021-09-24 stsp
8758 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8759 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8760 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8761 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8762 2db2652d 2019-08-07 stsp struct got_repository *repo;
8763 2db2652d 2019-08-07 stsp int have_changes;
8764 2db2652d 2019-08-07 stsp };
8765 2db2652d 2019-08-07 stsp
8766 336075a4 2022-06-25 op static const struct got_error *
8767 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8768 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8769 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8770 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8771 735ef5ac 2019-08-03 stsp {
8772 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8773 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8774 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8775 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8776 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8777 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8778 8b13ce36 2019-08-08 stsp
8779 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8780 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8781 8b13ce36 2019-08-08 stsp return NULL;
8782 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8783 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8784 735ef5ac 2019-08-03 stsp
8785 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8786 735ef5ac 2019-08-03 stsp if (ie == NULL)
8787 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8788 735ef5ac 2019-08-03 stsp
8789 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8790 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8791 735ef5ac 2019-08-03 stsp relpath) == -1)
8792 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8793 735ef5ac 2019-08-03 stsp
8794 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8795 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8796 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8797 735ef5ac 2019-08-03 stsp }
8798 735ef5ac 2019-08-03 stsp
8799 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8800 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8801 3aa5969e 2019-08-06 stsp goto done;
8802 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8803 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8804 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8805 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8806 735ef5ac 2019-08-03 stsp goto done;
8807 3aa5969e 2019-08-06 stsp }
8808 735ef5ac 2019-08-03 stsp
8809 2db2652d 2019-08-07 stsp a->have_changes = 1;
8810 2db2652d 2019-08-07 stsp
8811 735ef5ac 2019-08-03 stsp p = in_repo_path;
8812 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8813 735ef5ac 2019-08-03 stsp p++;
8814 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8815 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8816 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8817 735ef5ac 2019-08-03 stsp done:
8818 735ef5ac 2019-08-03 stsp free(in_repo_path);
8819 dc424a06 2019-08-07 stsp return err;
8820 dc424a06 2019-08-07 stsp }
8821 dc424a06 2019-08-07 stsp
8822 2db2652d 2019-08-07 stsp struct stage_path_arg {
8823 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8824 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8825 2db2652d 2019-08-07 stsp struct got_repository *repo;
8826 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8827 2db2652d 2019-08-07 stsp void *status_arg;
8828 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8829 2db2652d 2019-08-07 stsp void *patch_arg;
8830 7b5dc508 2019-10-28 stsp int staged_something;
8831 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8832 2db2652d 2019-08-07 stsp };
8833 2db2652d 2019-08-07 stsp
8834 2db2652d 2019-08-07 stsp static const struct got_error *
8835 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8836 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8837 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8838 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8839 0cb83759 2019-08-03 stsp {
8840 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8841 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8842 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8843 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8844 0cb83759 2019-08-03 stsp uint32_t stage;
8845 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8846 0aeb8099 2020-07-23 stsp struct stat sb;
8847 8b13ce36 2019-08-08 stsp
8848 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8849 8b13ce36 2019-08-08 stsp return NULL;
8850 0cb83759 2019-08-03 stsp
8851 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8852 d3e7c587 2019-08-03 stsp if (ie == NULL)
8853 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8854 0cb83759 2019-08-03 stsp
8855 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8856 2db2652d 2019-08-07 stsp relpath)== -1)
8857 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8858 0cb83759 2019-08-03 stsp
8859 0cb83759 2019-08-03 stsp switch (status) {
8860 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8861 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8862 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8863 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8864 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8865 0aeb8099 2020-07-23 stsp break;
8866 0aeb8099 2020-07-23 stsp }
8867 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8868 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8869 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8870 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8871 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8872 dc424a06 2019-08-07 stsp if (err)
8873 dc424a06 2019-08-07 stsp break;
8874 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8875 dc424a06 2019-08-07 stsp break;
8876 dc424a06 2019-08-07 stsp } else {
8877 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8878 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8879 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8880 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8881 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8882 dc424a06 2019-08-07 stsp break;
8883 dc424a06 2019-08-07 stsp }
8884 dc424a06 2019-08-07 stsp }
8885 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8886 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8887 0cb83759 2019-08-03 stsp if (err)
8888 dc424a06 2019-08-07 stsp break;
8889 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8890 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8891 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8892 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8893 0cb83759 2019-08-03 stsp else
8894 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8895 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8896 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8897 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8898 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8899 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8900 35213c7c 2020-07-23 stsp ssize_t target_len;
8901 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8902 35213c7c 2020-07-23 stsp sizeof(target_path));
8903 35213c7c 2020-07-23 stsp if (target_len == -1) {
8904 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8905 35213c7c 2020-07-23 stsp ondisk_path);
8906 35213c7c 2020-07-23 stsp break;
8907 35213c7c 2020-07-23 stsp }
8908 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8909 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8910 35213c7c 2020-07-23 stsp a->worktree->root_path);
8911 35213c7c 2020-07-23 stsp if (err)
8912 35213c7c 2020-07-23 stsp break;
8913 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8914 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8915 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8916 35213c7c 2020-07-23 stsp break;
8917 35213c7c 2020-07-23 stsp }
8918 35213c7c 2020-07-23 stsp }
8919 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8920 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8921 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8922 35213c7c 2020-07-23 stsp else
8923 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8924 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8925 0aeb8099 2020-07-23 stsp } else {
8926 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8927 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8928 0aeb8099 2020-07-23 stsp }
8929 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8930 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8931 dc424a06 2019-08-07 stsp break;
8932 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8933 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8934 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8935 9fdde394 2022-06-04 op if (err)
8936 9fdde394 2022-06-04 op break;
8937 9fdde394 2022-06-04 op /*
8938 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8939 9fdde394 2022-06-04 op * implicitly unstage the file.
8940 9fdde394 2022-06-04 op */
8941 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8942 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8943 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8944 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8945 9fdde394 2022-06-04 op }
8946 0cb83759 2019-08-03 stsp break;
8947 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8948 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8949 d3e7c587 2019-08-03 stsp break;
8950 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8951 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8952 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8953 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8954 dc424a06 2019-08-07 stsp if (err)
8955 dc424a06 2019-08-07 stsp break;
8956 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8957 88f33a19 2019-08-08 stsp break;
8958 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8959 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8960 dc424a06 2019-08-07 stsp break;
8961 88f33a19 2019-08-08 stsp }
8962 dc424a06 2019-08-07 stsp }
8963 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8964 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8965 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8966 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8967 dc424a06 2019-08-07 stsp break;
8968 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8969 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8970 12463d8b 2019-12-13 stsp de_name);
8971 0cb83759 2019-08-03 stsp break;
8972 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8973 d3e7c587 2019-08-03 stsp break;
8974 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8975 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8976 ebf48fd5 2019-08-03 stsp break;
8977 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8978 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8979 2a06fe5f 2019-08-24 stsp break;
8980 0cb83759 2019-08-03 stsp default:
8981 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8982 537ac44b 2019-08-03 stsp break;
8983 0cb83759 2019-08-03 stsp }
8984 dc424a06 2019-08-07 stsp
8985 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8986 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8987 dc424a06 2019-08-07 stsp free(path_content);
8988 2db2652d 2019-08-07 stsp free(ondisk_path);
8989 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8990 0ebf8283 2019-07-24 stsp return err;
8991 0ebf8283 2019-07-24 stsp }
8992 0cb83759 2019-08-03 stsp
8993 0cb83759 2019-08-03 stsp const struct got_error *
8994 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8995 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8996 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8997 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8998 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8999 0cb83759 2019-08-03 stsp {
9000 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9001 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
9002 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9003 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
9004 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
9005 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
9006 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
9007 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
9008 0cb83759 2019-08-03 stsp
9009 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9010 0cb83759 2019-08-03 stsp if (err)
9011 0cb83759 2019-08-03 stsp return err;
9012 0cb83759 2019-08-03 stsp
9013 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
9014 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
9015 735ef5ac 2019-08-03 stsp if (err)
9016 735ef5ac 2019-08-03 stsp goto done;
9017 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
9018 735ef5ac 2019-08-03 stsp if (err)
9019 735ef5ac 2019-08-03 stsp goto done;
9020 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9021 0cb83759 2019-08-03 stsp if (err)
9022 0cb83759 2019-08-03 stsp goto done;
9023 0cb83759 2019-08-03 stsp
9024 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
9025 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
9026 2db2652d 2019-08-07 stsp oka.worktree = worktree;
9027 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
9028 2db2652d 2019-08-07 stsp oka.repo = repo;
9029 2db2652d 2019-08-07 stsp oka.have_changes = 0;
9030 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9031 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9032 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
9033 735ef5ac 2019-08-03 stsp if (err)
9034 735ef5ac 2019-08-03 stsp goto done;
9035 735ef5ac 2019-08-03 stsp }
9036 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
9037 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9038 2db2652d 2019-08-07 stsp goto done;
9039 2db2652d 2019-08-07 stsp }
9040 735ef5ac 2019-08-03 stsp
9041 2db2652d 2019-08-07 stsp spa.worktree = worktree;
9042 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
9043 2db2652d 2019-08-07 stsp spa.repo = repo;
9044 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
9045 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
9046 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
9047 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
9048 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
9049 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
9050 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9051 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9052 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
9053 42005733 2019-08-03 stsp if (err)
9054 2db2652d 2019-08-07 stsp goto done;
9055 7b5dc508 2019-10-28 stsp }
9056 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
9057 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9058 7b5dc508 2019-10-28 stsp goto done;
9059 0cb83759 2019-08-03 stsp }
9060 0cb83759 2019-08-03 stsp
9061 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9062 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
9063 0cb83759 2019-08-03 stsp err = sync_err;
9064 0cb83759 2019-08-03 stsp done:
9065 735ef5ac 2019-08-03 stsp if (head_ref)
9066 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
9067 735ef5ac 2019-08-03 stsp free(head_commit_id);
9068 0cb83759 2019-08-03 stsp free(fileindex_path);
9069 0cb83759 2019-08-03 stsp if (fileindex)
9070 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
9071 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9072 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
9073 0cb83759 2019-08-03 stsp err = unlockerr;
9074 0cb83759 2019-08-03 stsp return err;
9075 0cb83759 2019-08-03 stsp }
9076 ad493afc 2019-08-03 stsp
9077 ad493afc 2019-08-03 stsp struct unstage_path_arg {
9078 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
9079 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
9080 ad493afc 2019-08-03 stsp struct got_repository *repo;
9081 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
9082 ad493afc 2019-08-03 stsp void *progress_arg;
9083 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
9084 2e1f37b0 2019-08-08 stsp void *patch_arg;
9085 ad493afc 2019-08-03 stsp };
9086 2e1f37b0 2019-08-08 stsp
9087 2e1f37b0 2019-08-08 stsp static const struct got_error *
9088 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
9089 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
9090 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
9091 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
9092 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
9093 2e1f37b0 2019-08-08 stsp {
9094 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
9095 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
9096 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9097 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9098 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
9099 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9100 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9101 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9102 2e1f37b0 2019-08-08 stsp
9103 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9104 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9105 2e1f37b0 2019-08-08 stsp
9106 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
9107 2e1f37b0 2019-08-08 stsp if (err)
9108 2e1f37b0 2019-08-08 stsp return err;
9109 eb81bc23 2022-06-28 tracey
9110 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9111 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9112 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9113 eb81bc23 2022-06-28 tracey goto done;
9114 eb81bc23 2022-06-28 tracey }
9115 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9116 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9117 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9118 eb81bc23 2022-06-28 tracey goto done;
9119 eb81bc23 2022-06-28 tracey }
9120 eb81bc23 2022-06-28 tracey
9121 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9122 2e1f37b0 2019-08-08 stsp if (err)
9123 2e1f37b0 2019-08-08 stsp goto done;
9124 2e1f37b0 2019-08-08 stsp
9125 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9126 2e1f37b0 2019-08-08 stsp if (err)
9127 2e1f37b0 2019-08-08 stsp goto done;
9128 2e1f37b0 2019-08-08 stsp
9129 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9130 2e1f37b0 2019-08-08 stsp if (err)
9131 2e1f37b0 2019-08-08 stsp goto done;
9132 2e1f37b0 2019-08-08 stsp
9133 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9134 eb81bc23 2022-06-28 tracey fd2);
9135 2e1f37b0 2019-08-08 stsp if (err)
9136 2e1f37b0 2019-08-08 stsp goto done;
9137 2e1f37b0 2019-08-08 stsp
9138 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9139 2e1f37b0 2019-08-08 stsp if (err)
9140 2e1f37b0 2019-08-08 stsp goto done;
9141 ad493afc 2019-08-03 stsp
9142 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9143 2e1f37b0 2019-08-08 stsp if (err)
9144 2e1f37b0 2019-08-08 stsp goto done;
9145 2e1f37b0 2019-08-08 stsp
9146 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9147 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9148 2e1f37b0 2019-08-08 stsp if (err)
9149 2e1f37b0 2019-08-08 stsp goto done;
9150 2e1f37b0 2019-08-08 stsp
9151 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
9152 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
9153 2e1f37b0 2019-08-08 stsp if (err)
9154 2e1f37b0 2019-08-08 stsp goto done;
9155 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
9156 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
9157 2e1f37b0 2019-08-08 stsp if (err)
9158 2e1f37b0 2019-08-08 stsp goto done;
9159 2e1f37b0 2019-08-08 stsp
9160 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
9161 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
9162 2e1f37b0 2019-08-08 stsp goto done;
9163 2e1f37b0 2019-08-08 stsp }
9164 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
9165 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
9166 2e1f37b0 2019-08-08 stsp goto done;
9167 2e1f37b0 2019-08-08 stsp }
9168 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
9169 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9170 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
9171 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
9172 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
9173 fe621944 2020-11-10 stsp nchanges++;
9174 fe621944 2020-11-10 stsp }
9175 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9176 2e1f37b0 2019-08-08 stsp int choice;
9177 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
9178 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
9179 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
9180 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9181 2e1f37b0 2019-08-08 stsp if (err)
9182 2e1f37b0 2019-08-08 stsp goto done;
9183 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
9184 2e1f37b0 2019-08-08 stsp have_content = 1;
9185 19e4b907 2019-08-08 stsp else
9186 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
9187 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
9188 2e1f37b0 2019-08-08 stsp break;
9189 2e1f37b0 2019-08-08 stsp }
9190 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
9191 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9192 f1e81a05 2019-08-10 stsp outfile, rejectfile);
9193 2e1f37b0 2019-08-08 stsp done:
9194 2e1f37b0 2019-08-08 stsp free(label1);
9195 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9196 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9197 2e1f37b0 2019-08-08 stsp if (blob)
9198 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
9199 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9200 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9201 2e1f37b0 2019-08-08 stsp if (staged_blob)
9202 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
9203 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
9204 fe621944 2020-11-10 stsp if (free_err && err == NULL)
9205 fe621944 2020-11-10 stsp err = free_err;
9206 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
9207 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
9208 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
9209 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
9210 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
9211 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
9212 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9213 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
9214 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
9215 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
9216 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
9217 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
9218 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
9219 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
9220 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
9221 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9222 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
9223 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
9224 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
9225 2e1f37b0 2019-08-08 stsp }
9226 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
9227 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
9228 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
9229 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
9230 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
9231 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
9232 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
9233 2e1f37b0 2019-08-08 stsp }
9234 2e1f37b0 2019-08-08 stsp free(path1);
9235 2e1f37b0 2019-08-08 stsp free(path2);
9236 fda8017d 2020-07-23 stsp return err;
9237 fda8017d 2020-07-23 stsp }
9238 fda8017d 2020-07-23 stsp
9239 fda8017d 2020-07-23 stsp static const struct got_error *
9240 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
9241 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
9242 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9243 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
9244 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
9245 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9246 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
9247 fda8017d 2020-07-23 stsp {
9248 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
9249 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
9250 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
9251 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
9252 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
9253 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
9254 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9255 fda8017d 2020-07-23 stsp struct stat sb;
9256 fda8017d 2020-07-23 stsp
9257 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
9258 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
9259 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
9260 fda8017d 2020-07-23 stsp if (err)
9261 fda8017d 2020-07-23 stsp return err;
9262 fda8017d 2020-07-23 stsp
9263 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
9264 fda8017d 2020-07-23 stsp return NULL;
9265 fda8017d 2020-07-23 stsp
9266 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
9267 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
9268 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
9269 fda8017d 2020-07-23 stsp if (err)
9270 fda8017d 2020-07-23 stsp goto done;
9271 fda8017d 2020-07-23 stsp }
9272 fda8017d 2020-07-23 stsp
9273 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
9274 fda8017d 2020-07-23 stsp if (f == NULL) {
9275 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
9276 fda8017d 2020-07-23 stsp path_unstaged_content);
9277 fda8017d 2020-07-23 stsp goto done;
9278 fda8017d 2020-07-23 stsp }
9279 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
9280 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
9281 fda8017d 2020-07-23 stsp goto done;
9282 fda8017d 2020-07-23 stsp }
9283 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
9284 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9285 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
9286 fda8017d 2020-07-23 stsp size_t r;
9287 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
9288 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
9289 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
9290 fda8017d 2020-07-23 stsp goto done;
9291 fda8017d 2020-07-23 stsp }
9292 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
9293 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
9294 fda8017d 2020-07-23 stsp goto done;
9295 fda8017d 2020-07-23 stsp }
9296 fda8017d 2020-07-23 stsp link_target[r] = '\0';
9297 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
9298 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
9299 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
9300 fda8017d 2020-07-23 stsp progress_arg);
9301 fda8017d 2020-07-23 stsp } else {
9302 fda8017d 2020-07-23 stsp int local_changes_subsumed;
9303 67a66647 2021-05-31 stsp
9304 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
9305 67a66647 2021-05-31 stsp if (err)
9306 67a66647 2021-05-31 stsp return err;
9307 67a66647 2021-05-31 stsp
9308 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9309 67a66647 2021-05-31 stsp parent) == -1) {
9310 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
9311 67a66647 2021-05-31 stsp base_path = NULL;
9312 67a66647 2021-05-31 stsp goto done;
9313 67a66647 2021-05-31 stsp }
9314 67a66647 2021-05-31 stsp
9315 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
9316 b90054ed 2022-11-01 stsp base_path, "");
9317 67a66647 2021-05-31 stsp if (err)
9318 67a66647 2021-05-31 stsp goto done;
9319 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9320 67a66647 2021-05-31 stsp blob_base);
9321 67a66647 2021-05-31 stsp if (err)
9322 67a66647 2021-05-31 stsp goto done;
9323 67a66647 2021-05-31 stsp
9324 5e91dae4 2022-08-30 stsp /*
9325 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
9326 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
9327 eec2f5a9 2021-06-03 stsp */
9328 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9329 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
9330 eec2f5a9 2021-06-03 stsp ondisk_path);
9331 eec2f5a9 2021-06-03 stsp if (err)
9332 eec2f5a9 2021-06-03 stsp goto done;
9333 eec2f5a9 2021-06-03 stsp } else {
9334 eec2f5a9 2021-06-03 stsp int fd;
9335 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
9336 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9337 eec2f5a9 2021-06-03 stsp if (fd == -1) {
9338 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
9339 eec2f5a9 2021-06-03 stsp goto done;
9340 eec2f5a9 2021-06-03 stsp }
9341 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
9342 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
9343 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
9344 eec2f5a9 2021-06-03 stsp close(fd);
9345 eec2f5a9 2021-06-03 stsp goto done;
9346 eec2f5a9 2021-06-03 stsp }
9347 eec2f5a9 2021-06-03 stsp }
9348 eec2f5a9 2021-06-03 stsp
9349 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
9350 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
9351 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
9352 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9353 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
9354 fda8017d 2020-07-23 stsp }
9355 fda8017d 2020-07-23 stsp if (err)
9356 fda8017d 2020-07-23 stsp goto done;
9357 fda8017d 2020-07-23 stsp
9358 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
9359 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9360 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
9361 2ac8aa02 2020-11-09 stsp } else {
9362 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9363 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9364 2ac8aa02 2020-11-09 stsp }
9365 fda8017d 2020-07-23 stsp done:
9366 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
9367 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
9368 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
9369 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
9370 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
9371 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
9372 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
9373 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9374 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
9375 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
9376 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9377 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9378 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9379 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9380 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9381 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9382 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9383 67a66647 2021-05-31 stsp free(blob_base_path);
9384 67a66647 2021-05-31 stsp free(parent);
9385 67a66647 2021-05-31 stsp free(base_path);
9386 2e1f37b0 2019-08-08 stsp return err;
9387 2e1f37b0 2019-08-08 stsp }
9388 2e1f37b0 2019-08-08 stsp
9389 ad493afc 2019-08-03 stsp static const struct got_error *
9390 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9391 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9392 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9393 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9394 ad493afc 2019-08-03 stsp {
9395 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9396 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9397 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9398 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9399 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9400 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9401 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9402 9bc94a15 2019-08-03 stsp struct stat sb;
9403 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9404 ad493afc 2019-08-03 stsp
9405 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9406 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9407 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9408 2e1f37b0 2019-08-08 stsp return NULL;
9409 2e1f37b0 2019-08-08 stsp
9410 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9411 ad493afc 2019-08-03 stsp if (ie == NULL)
9412 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9413 9bc94a15 2019-08-03 stsp
9414 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9415 9bc94a15 2019-08-03 stsp == -1)
9416 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9417 ad493afc 2019-08-03 stsp
9418 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9419 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9420 f69721c3 2019-10-21 stsp if (err)
9421 f69721c3 2019-10-21 stsp goto done;
9422 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9423 f69721c3 2019-10-21 stsp id_str) == -1) {
9424 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9425 eb81bc23 2022-06-28 tracey goto done;
9426 eb81bc23 2022-06-28 tracey }
9427 eb81bc23 2022-06-28 tracey
9428 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9429 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9430 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9431 eb81bc23 2022-06-28 tracey goto done;
9432 eb81bc23 2022-06-28 tracey }
9433 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9434 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9435 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9436 f69721c3 2019-10-21 stsp goto done;
9437 f69721c3 2019-10-21 stsp }
9438 f69721c3 2019-10-21 stsp
9439 ad493afc 2019-08-03 stsp switch (staged_status) {
9440 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9441 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9442 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9443 ad493afc 2019-08-03 stsp if (err)
9444 ad493afc 2019-08-03 stsp break;
9445 ad493afc 2019-08-03 stsp /* fall through */
9446 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9447 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9448 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9449 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9450 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9451 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9452 2e1f37b0 2019-08-08 stsp if (err)
9453 2e1f37b0 2019-08-08 stsp break;
9454 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9455 2e1f37b0 2019-08-08 stsp break;
9456 2e1f37b0 2019-08-08 stsp } else {
9457 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9458 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9459 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9460 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9461 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9462 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9463 2e1f37b0 2019-08-08 stsp }
9464 2e1f37b0 2019-08-08 stsp }
9465 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9466 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9467 ad493afc 2019-08-03 stsp if (err)
9468 ad493afc 2019-08-03 stsp break;
9469 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9470 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9471 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9472 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9473 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9474 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9475 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9476 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9477 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9478 ea7786be 2020-07-23 stsp break;
9479 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9480 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9481 36bf999c 2020-07-23 stsp char *staged_target;
9482 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9483 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9484 36bf999c 2020-07-23 stsp if (err)
9485 36bf999c 2020-07-23 stsp goto done;
9486 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9487 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9488 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9489 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9490 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9491 36bf999c 2020-07-23 stsp free(staged_target);
9492 dfe9fba0 2020-07-23 stsp } else {
9493 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9494 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9495 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9496 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9497 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9498 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9499 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9500 dfe9fba0 2020-07-23 stsp }
9501 ea7786be 2020-07-23 stsp break;
9502 ea7786be 2020-07-23 stsp default:
9503 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9504 ea7786be 2020-07-23 stsp break;
9505 ea7786be 2020-07-23 stsp }
9506 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9507 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9508 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9509 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9510 2ac8aa02 2020-11-09 stsp }
9511 ad493afc 2019-08-03 stsp break;
9512 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9513 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9514 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9515 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9516 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9517 2e1f37b0 2019-08-08 stsp if (err)
9518 2e1f37b0 2019-08-08 stsp break;
9519 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9520 2e1f37b0 2019-08-08 stsp break;
9521 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9522 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9523 2e1f37b0 2019-08-08 stsp break;
9524 2e1f37b0 2019-08-08 stsp }
9525 2e1f37b0 2019-08-08 stsp }
9526 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9527 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9528 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9529 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9530 9bc94a15 2019-08-03 stsp if (err)
9531 9bc94a15 2019-08-03 stsp break;
9532 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9533 ad493afc 2019-08-03 stsp break;
9534 ad493afc 2019-08-03 stsp }
9535 f69721c3 2019-10-21 stsp done:
9536 ad493afc 2019-08-03 stsp free(ondisk_path);
9537 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9538 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9539 ad493afc 2019-08-03 stsp if (blob_base)
9540 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9541 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9542 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9543 ad493afc 2019-08-03 stsp if (blob_staged)
9544 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9545 f69721c3 2019-10-21 stsp free(id_str);
9546 f69721c3 2019-10-21 stsp free(label_orig);
9547 ad493afc 2019-08-03 stsp return err;
9548 ad493afc 2019-08-03 stsp }
9549 ad493afc 2019-08-03 stsp
9550 ad493afc 2019-08-03 stsp const struct got_error *
9551 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9552 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9553 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9554 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9555 ad493afc 2019-08-03 stsp struct got_repository *repo)
9556 ad493afc 2019-08-03 stsp {
9557 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9558 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9559 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9560 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9561 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9562 ad493afc 2019-08-03 stsp
9563 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9564 ad493afc 2019-08-03 stsp if (err)
9565 ad493afc 2019-08-03 stsp return err;
9566 ad493afc 2019-08-03 stsp
9567 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9568 ad493afc 2019-08-03 stsp if (err)
9569 ad493afc 2019-08-03 stsp goto done;
9570 ad493afc 2019-08-03 stsp
9571 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9572 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9573 ad493afc 2019-08-03 stsp upa.repo = repo;
9574 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9575 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9576 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9577 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9578 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9579 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9580 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9581 ad493afc 2019-08-03 stsp if (err)
9582 ad493afc 2019-08-03 stsp goto done;
9583 ad493afc 2019-08-03 stsp }
9584 ad493afc 2019-08-03 stsp
9585 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9586 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9587 ad493afc 2019-08-03 stsp err = sync_err;
9588 ad493afc 2019-08-03 stsp done:
9589 ad493afc 2019-08-03 stsp free(fileindex_path);
9590 ad493afc 2019-08-03 stsp if (fileindex)
9591 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9592 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9593 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9594 ad493afc 2019-08-03 stsp err = unlockerr;
9595 ad493afc 2019-08-03 stsp return err;
9596 ad493afc 2019-08-03 stsp }
9597 b2118c49 2020-07-28 stsp
9598 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9599 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9600 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9601 b2118c49 2020-07-28 stsp void *info_arg;
9602 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9603 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9604 b2118c49 2020-07-28 stsp void *cancel_arg;
9605 b2118c49 2020-07-28 stsp };
9606 b2118c49 2020-07-28 stsp
9607 b2118c49 2020-07-28 stsp static const struct got_error *
9608 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9609 b2118c49 2020-07-28 stsp {
9610 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9611 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9612 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9613 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9614 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9615 b2118c49 2020-07-28 stsp int stage;
9616 b2118c49 2020-07-28 stsp
9617 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9618 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9619 b2118c49 2020-07-28 stsp
9620 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9621 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9622 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9623 b2118c49 2020-07-28 stsp break;
9624 b2118c49 2020-07-28 stsp }
9625 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9626 b2118c49 2020-07-28 stsp return NULL;
9627 b2118c49 2020-07-28 stsp
9628 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9629 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9630 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9631 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9632 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9633 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9634 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9635 b2118c49 2020-07-28 stsp }
9636 b2118c49 2020-07-28 stsp
9637 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9638 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9639 b2118c49 2020-07-28 stsp
9640 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9641 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9642 b2118c49 2020-07-28 stsp }
9643 b2118c49 2020-07-28 stsp
9644 b2118c49 2020-07-28 stsp const struct got_error *
9645 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9646 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9647 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9648 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9649 b2118c49 2020-07-28 stsp
9650 b2118c49 2020-07-28 stsp {
9651 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9652 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9653 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9654 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9655 b2118c49 2020-07-28 stsp
9656 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9657 b2118c49 2020-07-28 stsp if (err)
9658 b2118c49 2020-07-28 stsp return err;
9659 b2118c49 2020-07-28 stsp
9660 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9661 b2118c49 2020-07-28 stsp if (err)
9662 b2118c49 2020-07-28 stsp goto done;
9663 b2118c49 2020-07-28 stsp
9664 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9665 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9666 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9667 b2118c49 2020-07-28 stsp arg.paths = paths;
9668 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9669 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9670 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9671 b2118c49 2020-07-28 stsp &arg);
9672 b2118c49 2020-07-28 stsp done:
9673 b2118c49 2020-07-28 stsp free(fileindex_path);
9674 b2118c49 2020-07-28 stsp if (fileindex)
9675 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9676 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9677 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9678 b2118c49 2020-07-28 stsp err = unlockerr;
9679 b2118c49 2020-07-28 stsp return err;
9680 b2118c49 2020-07-28 stsp }
9681 78f5ac24 2022-03-19 op
9682 78f5ac24 2022-03-19 op static const struct got_error *
9683 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9684 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9685 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9686 78f5ac24 2022-03-19 op {
9687 78f5ac24 2022-03-19 op const struct got_error *err;
9688 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9689 78f5ac24 2022-03-19 op struct stat sb;
9690 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9691 78f5ac24 2022-03-19 op
9692 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9693 78f5ac24 2022-03-19 op if (err)
9694 78f5ac24 2022-03-19 op return err;
9695 78f5ac24 2022-03-19 op
9696 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9697 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9698 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9699 78f5ac24 2022-03-19 op
9700 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9701 78f5ac24 2022-03-19 op if (ie) {
9702 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9703 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9704 a05fb460 2022-04-23 op repo);
9705 78f5ac24 2022-03-19 op if (err)
9706 78f5ac24 2022-03-19 op goto done;
9707 78f5ac24 2022-03-19 op } else {
9708 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9709 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9710 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9711 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9712 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9713 78f5ac24 2022-03-19 op ondisk_path);
9714 78f5ac24 2022-03-19 op goto done;
9715 78f5ac24 2022-03-19 op }
9716 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9717 78f5ac24 2022-03-19 op }
9718 78f5ac24 2022-03-19 op }
9719 78f5ac24 2022-03-19 op
9720 78f5ac24 2022-03-19 op done:
9721 78f5ac24 2022-03-19 op free(ondisk_path);
9722 78f5ac24 2022-03-19 op return err;
9723 78f5ac24 2022-03-19 op }
9724 78f5ac24 2022-03-19 op
9725 78f5ac24 2022-03-19 op static const struct got_error *
9726 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9727 78f5ac24 2022-03-19 op unsigned char staged_status)
9728 78f5ac24 2022-03-19 op {
9729 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9730 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9731 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9732 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9733 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9734 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9735 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9736 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9737 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9738 78f5ac24 2022-03-19 op return NULL;
9739 78f5ac24 2022-03-19 op }
9740 78f5ac24 2022-03-19 op
9741 78f5ac24 2022-03-19 op static const struct got_error *
9742 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9743 78f5ac24 2022-03-19 op {
9744 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9745 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9746 78f5ac24 2022-03-19 op return NULL;
9747 78f5ac24 2022-03-19 op }
9748 78f5ac24 2022-03-19 op
9749 78f5ac24 2022-03-19 op static const struct got_error *
9750 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9751 78f5ac24 2022-03-19 op unsigned char staged_status)
9752 78f5ac24 2022-03-19 op {
9753 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9754 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9755 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9756 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9757 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9758 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9759 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9760 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 78f5ac24 2022-03-19 op return NULL;
9762 78f5ac24 2022-03-19 op }
9763 78f5ac24 2022-03-19 op
9764 78f5ac24 2022-03-19 op const struct got_error *
9765 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9766 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9767 78f5ac24 2022-03-19 op {
9768 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9769 78f5ac24 2022-03-19 op }
9770 78f5ac24 2022-03-19 op
9771 78f5ac24 2022-03-19 op const struct got_error *
9772 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9773 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9774 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9775 78f5ac24 2022-03-19 op {
9776 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9777 78f5ac24 2022-03-19 op int file_renamed = 0;
9778 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9779 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9780 78f5ac24 2022-03-19 op
9781 78f5ac24 2022-03-19 op *oldpath = NULL;
9782 78f5ac24 2022-03-19 op *newpath = NULL;
9783 78f5ac24 2022-03-19 op
9784 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9785 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9786 78f5ac24 2022-03-19 op if (err)
9787 78f5ac24 2022-03-19 op goto done;
9788 78f5ac24 2022-03-19 op
9789 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9790 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9791 78f5ac24 2022-03-19 op if (err)
9792 78f5ac24 2022-03-19 op goto done;
9793 78f5ac24 2022-03-19 op
9794 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9795 78f5ac24 2022-03-19 op file_renamed = 1;
9796 78f5ac24 2022-03-19 op
9797 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9798 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9799 78f5ac24 2022-03-19 op else if (file_renamed) {
9800 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9801 78f5ac24 2022-03-19 op if (err == NULL)
9802 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9803 78f5ac24 2022-03-19 op } else if (old == NULL)
9804 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9805 78f5ac24 2022-03-19 op else
9806 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9807 78f5ac24 2022-03-19 op
9808 78f5ac24 2022-03-19 op done:
9809 78f5ac24 2022-03-19 op if (err) {
9810 78f5ac24 2022-03-19 op free(*oldpath);
9811 78f5ac24 2022-03-19 op *oldpath = NULL;
9812 78f5ac24 2022-03-19 op free(*newpath);
9813 78f5ac24 2022-03-19 op *newpath = NULL;
9814 78f5ac24 2022-03-19 op }
9815 78f5ac24 2022-03-19 op return err;
9816 78f5ac24 2022-03-19 op }
9817 78f5ac24 2022-03-19 op
9818 78f5ac24 2022-03-19 op const struct got_error *
9819 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9820 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9821 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9822 78f5ac24 2022-03-19 op {
9823 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9824 f2dd7807 2022-05-17 op
9825 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9826 f2dd7807 2022-05-17 op saa.worktree = worktree;
9827 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9828 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9829 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9830 f2dd7807 2022-05-17 op saa.repo = repo;
9831 f2dd7807 2022-05-17 op
9832 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9833 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9834 78f5ac24 2022-03-19 op }
9835 f2dd7807 2022-05-17 op
9836 f2dd7807 2022-05-17 op const struct got_error *
9837 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9838 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9839 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9840 f2dd7807 2022-05-17 op {
9841 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9842 f2dd7807 2022-05-17 op
9843 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9844 f2dd7807 2022-05-17 op sda.worktree = worktree;
9845 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9846 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9847 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9848 f2dd7807 2022-05-17 op sda.repo = repo;
9849 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9850 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9851 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9852 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9853 f2dd7807 2022-05-17 op
9854 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9855 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9856 f2dd7807 2022-05-17 op }
9857 f2dd7807 2022-05-17 op
9858 f2dd7807 2022-05-17 op const struct got_error *
9859 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9860 4bcdc895 2022-05-17 op const char *fileindex_path)
9861 f2dd7807 2022-05-17 op {
9862 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9863 f2dd7807 2022-05-17 op
9864 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9865 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9866 f2dd7807 2022-05-17 op
9867 f2dd7807 2022-05-17 op return err;
9868 f2dd7807 2022-05-17 op }