Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 b2b3fce1 2022-10-29 op
67 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
68 86c3caaf 2018-03-09 stsp
69 99724ed4 2018-03-10 stsp static const struct got_error *
70 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
71 99724ed4 2018-03-10 stsp {
72 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 99724ed4 2018-03-10 stsp char *path;
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
76 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
77 99724ed4 2018-03-10 stsp
78 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
79 99724ed4 2018-03-10 stsp free(path);
80 507dc3bb 2018-12-29 stsp return err;
81 507dc3bb 2018-12-29 stsp }
82 507dc3bb 2018-12-29 stsp
83 507dc3bb 2018-12-29 stsp static const struct got_error *
84 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
85 507dc3bb 2018-12-29 stsp {
86 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
87 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
88 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
89 507dc3bb 2018-12-29 stsp char *path = NULL;
90 507dc3bb 2018-12-29 stsp
91 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
92 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
93 507dc3bb 2018-12-29 stsp path = NULL;
94 507dc3bb 2018-12-29 stsp goto done;
95 507dc3bb 2018-12-29 stsp }
96 507dc3bb 2018-12-29 stsp
97 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
98 507dc3bb 2018-12-29 stsp if (err)
99 507dc3bb 2018-12-29 stsp goto done;
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp if (content) {
102 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
103 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
105 507dc3bb 2018-12-29 stsp goto done;
106 507dc3bb 2018-12-29 stsp }
107 507dc3bb 2018-12-29 stsp }
108 507dc3bb 2018-12-29 stsp
109 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
111 2a57020b 2019-02-20 stsp unlink(tmppath);
112 507dc3bb 2018-12-29 stsp goto done;
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp
115 507dc3bb 2018-12-29 stsp done:
116 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
117 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
118 230a42bd 2019-05-11 jcs free(tmppath);
119 09fe317a 2018-03-11 stsp return err;
120 09fe317a 2018-03-11 stsp }
121 09fe317a 2018-03-11 stsp
122 024e9686 2019-05-14 stsp static const struct got_error *
123 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
124 024e9686 2019-05-14 stsp {
125 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
126 024e9686 2019-05-14 stsp char *refstr = NULL;
127 024e9686 2019-05-14 stsp
128 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
129 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
130 024e9686 2019-05-14 stsp if (refstr == NULL)
131 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
132 024e9686 2019-05-14 stsp } else {
133 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
134 024e9686 2019-05-14 stsp if (refstr == NULL)
135 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
136 024e9686 2019-05-14 stsp }
137 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
138 024e9686 2019-05-14 stsp free(refstr);
139 024e9686 2019-05-14 stsp return err;
140 024e9686 2019-05-14 stsp }
141 024e9686 2019-05-14 stsp
142 86c3caaf 2018-03-09 stsp const struct got_error *
143 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
144 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
145 86c3caaf 2018-03-09 stsp {
146 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
147 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
148 ec22038e 2019-03-10 stsp uuid_t uuid;
149 ec22038e 2019-03-10 stsp uint32_t uuid_status;
150 65596e15 2018-12-24 stsp int obj_type;
151 7ac97322 2018-03-11 stsp char *path_got = NULL;
152 1451e70d 2018-03-10 stsp char *formatstr = NULL;
153 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
154 65596e15 2018-12-24 stsp char *basestr = NULL;
155 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
156 65596e15 2018-12-24 stsp
157 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
158 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
159 0c48fee2 2019-03-11 stsp goto done;
160 0c48fee2 2019-03-11 stsp }
161 0c48fee2 2019-03-11 stsp
162 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
163 65596e15 2018-12-24 stsp if (err)
164 65596e15 2018-12-24 stsp return err;
165 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
166 65596e15 2018-12-24 stsp if (err)
167 65596e15 2018-12-24 stsp return err;
168 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
170 86c3caaf 2018-03-09 stsp
171 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
172 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
174 0bb8a95e 2018-03-12 stsp }
175 577ec78f 2018-03-11 stsp
176 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
177 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
178 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
179 86c3caaf 2018-03-09 stsp goto done;
180 86c3caaf 2018-03-09 stsp }
181 86c3caaf 2018-03-09 stsp
182 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
183 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
185 86c3caaf 2018-03-09 stsp goto done;
186 86c3caaf 2018-03-09 stsp }
187 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
188 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
189 86c3caaf 2018-03-09 stsp goto done;
190 86c3caaf 2018-03-09 stsp }
191 86c3caaf 2018-03-09 stsp
192 056e7441 2018-03-11 stsp /* Create an empty lock file. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
194 056e7441 2018-03-11 stsp if (err)
195 056e7441 2018-03-11 stsp goto done;
196 056e7441 2018-03-11 stsp
197 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
198 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
199 99724ed4 2018-03-10 stsp if (err)
200 86c3caaf 2018-03-09 stsp goto done;
201 86c3caaf 2018-03-09 stsp
202 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
203 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
204 65596e15 2018-12-24 stsp if (err)
205 65596e15 2018-12-24 stsp goto done;
206 65596e15 2018-12-24 stsp
207 65596e15 2018-12-24 stsp /* Record our base commit. */
208 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
209 65596e15 2018-12-24 stsp if (err)
210 65596e15 2018-12-24 stsp goto done;
211 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 99724ed4 2018-03-10 stsp if (err)
213 86c3caaf 2018-03-09 stsp goto done;
214 86c3caaf 2018-03-09 stsp
215 1451e70d 2018-03-10 stsp /* Store path to repository. */
216 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
217 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
218 99724ed4 2018-03-10 stsp if (err)
219 86c3caaf 2018-03-09 stsp goto done;
220 86c3caaf 2018-03-09 stsp
221 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
222 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
223 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
224 ec22038e 2019-03-10 stsp if (err)
225 ec22038e 2019-03-10 stsp goto done;
226 ec22038e 2019-03-10 stsp
227 ec22038e 2019-03-10 stsp /* Generate UUID. */
228 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
229 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
230 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
231 ec22038e 2019-03-10 stsp goto done;
232 ec22038e 2019-03-10 stsp }
233 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
234 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
235 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
236 ec22038e 2019-03-10 stsp goto done;
237 ec22038e 2019-03-10 stsp }
238 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
239 577ec78f 2018-03-11 stsp if (err)
240 577ec78f 2018-03-11 stsp goto done;
241 577ec78f 2018-03-11 stsp
242 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
243 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 1451e70d 2018-03-10 stsp goto done;
246 1451e70d 2018-03-10 stsp }
247 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
248 99724ed4 2018-03-10 stsp if (err)
249 1451e70d 2018-03-10 stsp goto done;
250 1451e70d 2018-03-10 stsp
251 86c3caaf 2018-03-09 stsp done:
252 65596e15 2018-12-24 stsp free(commit_id);
253 7ac97322 2018-03-11 stsp free(path_got);
254 1451e70d 2018-03-10 stsp free(formatstr);
255 0bb8a95e 2018-03-12 stsp free(absprefix);
256 65596e15 2018-12-24 stsp free(basestr);
257 ec22038e 2019-03-10 stsp free(uuidstr);
258 86c3caaf 2018-03-09 stsp return err;
259 86c3caaf 2018-03-09 stsp }
260 86c3caaf 2018-03-09 stsp
261 247140b2 2019-02-05 stsp const struct got_error *
262 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
263 e5dc7198 2018-12-29 stsp const char *path_prefix)
264 e5dc7198 2018-12-29 stsp {
265 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
266 e5dc7198 2018-12-29 stsp
267 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
268 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
269 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
270 e5dc7198 2018-12-29 stsp }
271 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
272 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
273 e5dc7198 2018-12-29 stsp free(absprefix);
274 e5dc7198 2018-12-29 stsp return NULL;
275 86c3caaf 2018-03-09 stsp }
276 86c3caaf 2018-03-09 stsp
277 bc70eb79 2019-05-09 stsp const char *
278 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
279 86c3caaf 2018-03-09 stsp {
280 36a38700 2019-05-10 stsp return worktree->head_ref_name;
281 024e9686 2019-05-14 stsp }
282 024e9686 2019-05-14 stsp
283 024e9686 2019-05-14 stsp const struct got_error *
284 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
285 024e9686 2019-05-14 stsp struct got_reference *head_ref)
286 024e9686 2019-05-14 stsp {
287 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
288 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
289 024e9686 2019-05-14 stsp
290 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
291 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
292 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
293 024e9686 2019-05-14 stsp path_got = NULL;
294 024e9686 2019-05-14 stsp goto done;
295 024e9686 2019-05-14 stsp }
296 024e9686 2019-05-14 stsp
297 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
298 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
299 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
300 024e9686 2019-05-14 stsp goto done;
301 024e9686 2019-05-14 stsp }
302 024e9686 2019-05-14 stsp
303 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
304 024e9686 2019-05-14 stsp if (err)
305 024e9686 2019-05-14 stsp goto done;
306 024e9686 2019-05-14 stsp
307 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
308 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
309 024e9686 2019-05-14 stsp done:
310 024e9686 2019-05-14 stsp free(path_got);
311 024e9686 2019-05-14 stsp if (err)
312 024e9686 2019-05-14 stsp free(head_ref_name);
313 024e9686 2019-05-14 stsp return err;
314 507dc3bb 2018-12-29 stsp }
315 507dc3bb 2018-12-29 stsp
316 b72f483a 2019-02-05 stsp struct got_object_id *
317 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
318 507dc3bb 2018-12-29 stsp {
319 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 507dc3bb 2018-12-29 stsp const struct got_error *
323 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
324 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
325 507dc3bb 2018-12-29 stsp {
326 507dc3bb 2018-12-29 stsp const struct got_error *err;
327 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
328 507dc3bb 2018-12-29 stsp char *id_str = NULL;
329 507dc3bb 2018-12-29 stsp char *path_got = NULL;
330 507dc3bb 2018-12-29 stsp
331 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
332 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
333 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
334 507dc3bb 2018-12-29 stsp path_got = NULL;
335 507dc3bb 2018-12-29 stsp goto done;
336 507dc3bb 2018-12-29 stsp }
337 507dc3bb 2018-12-29 stsp
338 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
339 507dc3bb 2018-12-29 stsp if (err)
340 507dc3bb 2018-12-29 stsp return err;
341 507dc3bb 2018-12-29 stsp
342 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
343 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
344 507dc3bb 2018-12-29 stsp goto done;
345 507dc3bb 2018-12-29 stsp }
346 507dc3bb 2018-12-29 stsp
347 507dc3bb 2018-12-29 stsp /* Record our base commit. */
348 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
349 507dc3bb 2018-12-29 stsp if (err)
350 507dc3bb 2018-12-29 stsp goto done;
351 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
352 507dc3bb 2018-12-29 stsp if (err)
353 507dc3bb 2018-12-29 stsp goto done;
354 507dc3bb 2018-12-29 stsp
355 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
356 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
357 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
359 507dc3bb 2018-12-29 stsp goto done;
360 507dc3bb 2018-12-29 stsp }
361 507dc3bb 2018-12-29 stsp done:
362 507dc3bb 2018-12-29 stsp if (obj)
363 507dc3bb 2018-12-29 stsp got_object_close(obj);
364 507dc3bb 2018-12-29 stsp free(id_str);
365 507dc3bb 2018-12-29 stsp free(path_got);
366 507dc3bb 2018-12-29 stsp return err;
367 50b0790e 2020-09-11 stsp }
368 50b0790e 2020-09-11 stsp
369 50b0790e 2020-09-11 stsp const struct got_gotconfig *
370 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
371 50b0790e 2020-09-11 stsp {
372 50b0790e 2020-09-11 stsp return worktree->gotconfig;
373 507dc3bb 2018-12-29 stsp }
374 507dc3bb 2018-12-29 stsp
375 9d31a1d8 2018-03-11 stsp static const struct got_error *
376 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
377 86c3caaf 2018-03-09 stsp {
378 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
379 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
380 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
381 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
382 86c3caaf 2018-03-09 stsp return NULL;
383 21908da4 2019-01-13 stsp }
384 21908da4 2019-01-13 stsp
385 21908da4 2019-01-13 stsp static const struct got_error *
386 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
387 4a1ddfc2 2019-01-12 stsp {
388 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
389 4a1ddfc2 2019-01-12 stsp char *abspath;
390 4a1ddfc2 2019-01-12 stsp
391 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
392 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
393 4a1ddfc2 2019-01-12 stsp
394 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
395 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
396 ddcd8544 2019-03-11 stsp struct stat sb;
397 ddcd8544 2019-03-11 stsp err = NULL;
398 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
400 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
401 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
402 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 ddcd8544 2019-03-11 stsp }
404 ddcd8544 2019-03-11 stsp }
405 4a1ddfc2 2019-01-12 stsp free(abspath);
406 68c76935 2019-02-19 stsp return err;
407 68c76935 2019-02-19 stsp }
408 68c76935 2019-02-19 stsp
409 68c76935 2019-02-19 stsp static const struct got_error *
410 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
411 68c76935 2019-02-19 stsp {
412 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
413 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
414 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
415 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
416 68c76935 2019-02-19 stsp
417 68c76935 2019-02-19 stsp *same = 1;
418 68c76935 2019-02-19 stsp
419 230a42bd 2019-05-11 jcs for (;;) {
420 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
421 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
423 68c76935 2019-02-19 stsp break;
424 68c76935 2019-02-19 stsp }
425 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
426 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
427 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
428 68c76935 2019-02-19 stsp break;
429 68c76935 2019-02-19 stsp }
430 68c76935 2019-02-19 stsp if (flen1 == 0) {
431 68c76935 2019-02-19 stsp if (flen2 != 0)
432 68c76935 2019-02-19 stsp *same = 0;
433 68c76935 2019-02-19 stsp break;
434 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
435 68c76935 2019-02-19 stsp if (flen1 != 0)
436 68c76935 2019-02-19 stsp *same = 0;
437 68c76935 2019-02-19 stsp break;
438 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
439 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
440 68c76935 2019-02-19 stsp *same = 0;
441 68c76935 2019-02-19 stsp break;
442 68c76935 2019-02-19 stsp }
443 68c76935 2019-02-19 stsp } else {
444 68c76935 2019-02-19 stsp *same = 0;
445 68c76935 2019-02-19 stsp break;
446 68c76935 2019-02-19 stsp }
447 68c76935 2019-02-19 stsp }
448 68c76935 2019-02-19 stsp
449 68c76935 2019-02-19 stsp return err;
450 68c76935 2019-02-19 stsp }
451 68c76935 2019-02-19 stsp
452 68c76935 2019-02-19 stsp static const struct got_error *
453 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
454 68c76935 2019-02-19 stsp {
455 68c76935 2019-02-19 stsp struct stat sb;
456 68c76935 2019-02-19 stsp size_t size1, size2;
457 68c76935 2019-02-19 stsp
458 68c76935 2019-02-19 stsp *same = 1;
459 68c76935 2019-02-19 stsp
460 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
461 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
462 68c76935 2019-02-19 stsp size1 = sb.st_size;
463 68c76935 2019-02-19 stsp
464 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
465 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
466 68c76935 2019-02-19 stsp size2 = sb.st_size;
467 68c76935 2019-02-19 stsp
468 68c76935 2019-02-19 stsp if (size1 != size2) {
469 68c76935 2019-02-19 stsp *same = 0;
470 68c76935 2019-02-19 stsp return NULL;
471 68c76935 2019-02-19 stsp }
472 68c76935 2019-02-19 stsp
473 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
474 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
475 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
476 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
477 68c76935 2019-02-19 stsp
478 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
479 0e039681 2021-11-15 stsp }
480 0e039681 2021-11-15 stsp
481 0e039681 2021-11-15 stsp static const struct got_error *
482 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
483 0e039681 2021-11-15 stsp {
484 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
485 0e039681 2021-11-15 stsp size_t flen;
486 0e039681 2021-11-15 stsp ssize_t outlen;
487 0e039681 2021-11-15 stsp
488 0e039681 2021-11-15 stsp *outsize = 0;
489 0e039681 2021-11-15 stsp
490 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
491 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
492 0e039681 2021-11-15 stsp
493 0e039681 2021-11-15 stsp for (;;) {
494 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
495 0e039681 2021-11-15 stsp if (flen == 0) {
496 0e039681 2021-11-15 stsp if (ferror(f))
497 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
498 0e039681 2021-11-15 stsp if (feof(f))
499 0e039681 2021-11-15 stsp break;
500 0e039681 2021-11-15 stsp }
501 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
502 0e039681 2021-11-15 stsp if (outlen == -1)
503 0e039681 2021-11-15 stsp return got_error_from_errno("write");
504 0e039681 2021-11-15 stsp if (outlen != flen)
505 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
506 0e039681 2021-11-15 stsp *outsize += outlen;
507 0e039681 2021-11-15 stsp }
508 0e039681 2021-11-15 stsp
509 0e039681 2021-11-15 stsp return NULL;
510 0e039681 2021-11-15 stsp }
511 0e039681 2021-11-15 stsp
512 0e039681 2021-11-15 stsp static const struct got_error *
513 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
514 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
515 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
516 0e039681 2021-11-15 stsp const char *ondisk_path)
517 0e039681 2021-11-15 stsp {
518 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
519 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
520 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
521 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
522 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
523 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
524 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
525 0e039681 2021-11-15 stsp
526 0e039681 2021-11-15 stsp *overlapcnt = 0;
527 0e039681 2021-11-15 stsp
528 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
529 0e039681 2021-11-15 stsp if (err)
530 0e039681 2021-11-15 stsp return err;
531 0e039681 2021-11-15 stsp
532 0e039681 2021-11-15 stsp if (same_content)
533 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
534 0e039681 2021-11-15 stsp
535 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
536 0e039681 2021-11-15 stsp if (err)
537 0e039681 2021-11-15 stsp return err;
538 0e039681 2021-11-15 stsp changed_deriv = !same_content;
539 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
540 0e039681 2021-11-15 stsp if (err)
541 0e039681 2021-11-15 stsp return err;
542 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
543 0e039681 2021-11-15 stsp
544 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
545 0e039681 2021-11-15 stsp *overlapcnt = 1;
546 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
547 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
548 0e039681 2021-11-15 stsp goto done;
549 0e039681 2021-11-15 stsp }
550 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
551 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
552 0e039681 2021-11-15 stsp goto done;
553 0e039681 2021-11-15 stsp }
554 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
555 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
556 0e039681 2021-11-15 stsp goto done;
557 0e039681 2021-11-15 stsp }
558 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
559 b90054ed 2022-11-01 stsp base_path_orig, "");
560 0e039681 2021-11-15 stsp if (err)
561 0e039681 2021-11-15 stsp goto done;
562 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
563 b90054ed 2022-11-01 stsp base_path_deriv, "");
564 0e039681 2021-11-15 stsp if (err)
565 0e039681 2021-11-15 stsp goto done;
566 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
567 b90054ed 2022-11-01 stsp base_path_deriv2, "");
568 0e039681 2021-11-15 stsp if (err)
569 0e039681 2021-11-15 stsp goto done;
570 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
571 0e039681 2021-11-15 stsp if (err)
572 0e039681 2021-11-15 stsp goto done;
573 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
574 0e039681 2021-11-15 stsp if (err)
575 0e039681 2021-11-15 stsp goto done;
576 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
577 0e039681 2021-11-15 stsp if (err)
578 0e039681 2021-11-15 stsp goto done;
579 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
580 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
581 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
582 0e039681 2021-11-15 stsp goto done;
583 0e039681 2021-11-15 stsp }
584 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
585 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
586 0e039681 2021-11-15 stsp label_deriv ? " " : "",
587 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
588 0e039681 2021-11-15 stsp path_deriv) < 0) {
589 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
590 0e039681 2021-11-15 stsp goto done;
591 0e039681 2021-11-15 stsp }
592 0e039681 2021-11-15 stsp if (size_orig > 0) {
593 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
594 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
595 0e039681 2021-11-15 stsp label_orig ? " " : "",
596 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
597 0e039681 2021-11-15 stsp path_orig) < 0) {
598 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
599 0e039681 2021-11-15 stsp goto done;
600 0e039681 2021-11-15 stsp }
601 0e039681 2021-11-15 stsp }
602 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
603 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
604 0e039681 2021-11-15 stsp path_deriv2,
605 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
606 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
607 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
608 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
609 0e039681 2021-11-15 stsp goto done;
610 0e039681 2021-11-15 stsp }
611 0e039681 2021-11-15 stsp } else if (changed_deriv)
612 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
613 0e039681 2021-11-15 stsp else if (changed_deriv2)
614 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
615 0e039681 2021-11-15 stsp done:
616 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
617 0e039681 2021-11-15 stsp err == NULL)
618 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
619 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
620 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
621 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
622 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
623 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
624 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
625 0e039681 2021-11-15 stsp free(path_orig);
626 0e039681 2021-11-15 stsp free(path_deriv);
627 0e039681 2021-11-15 stsp free(path_deriv2);
628 0e039681 2021-11-15 stsp free(base_path_orig);
629 0e039681 2021-11-15 stsp free(base_path_deriv);
630 0e039681 2021-11-15 stsp free(base_path_deriv2);
631 0e039681 2021-11-15 stsp return err;
632 6353ad76 2019-02-08 stsp }
633 6353ad76 2019-02-08 stsp
634 6353ad76 2019-02-08 stsp /*
635 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
636 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
637 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
638 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
639 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
640 6353ad76 2019-02-08 stsp */
641 6353ad76 2019-02-08 stsp static const struct got_error *
642 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
643 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
644 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
645 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
646 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
647 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
648 6353ad76 2019-02-08 stsp {
649 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
650 6353ad76 2019-02-08 stsp int merged_fd = -1;
651 db590691 2021-06-02 stsp FILE *f_merged = NULL;
652 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
653 234035bc 2019-06-01 stsp int overlapcnt = 0;
654 3524bbf9 2020-10-20 stsp char *parent = NULL;
655 6353ad76 2019-02-08 stsp
656 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
657 234035bc 2019-06-01 stsp
658 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
659 3524bbf9 2020-10-20 stsp if (err)
660 3524bbf9 2020-10-20 stsp return err;
661 af54ae4a 2019-02-19 stsp
662 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
663 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
664 3524bbf9 2020-10-20 stsp goto done;
665 3524bbf9 2020-10-20 stsp }
666 af54ae4a 2019-02-19 stsp
667 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
668 6353ad76 2019-02-08 stsp if (err)
669 6353ad76 2019-02-08 stsp goto done;
670 3b9f0f87 2020-07-23 stsp
671 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
672 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
673 0e039681 2021-11-15 stsp if (err) {
674 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
675 0e039681 2021-11-15 stsp goto done;
676 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
677 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
678 0e039681 2021-11-15 stsp ondisk_path);
679 0e039681 2021-11-15 stsp if (err)
680 0e039681 2021-11-15 stsp goto done;
681 0e039681 2021-11-15 stsp }
682 6353ad76 2019-02-08 stsp
683 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
684 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
685 1ee397ad 2019-07-12 stsp if (err)
686 1ee397ad 2019-07-12 stsp goto done;
687 6353ad76 2019-02-08 stsp
688 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
690 816dc654 2019-02-16 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp
693 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
694 db590691 2021-06-02 stsp if (f_merged == NULL) {
695 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
696 db590691 2021-06-02 stsp goto done;
697 db590691 2021-06-02 stsp }
698 db590691 2021-06-02 stsp merged_fd = -1;
699 db590691 2021-06-02 stsp
700 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
701 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
702 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
703 db590691 2021-06-02 stsp f_merged);
704 68c76935 2019-02-19 stsp if (err)
705 68c76935 2019-02-19 stsp goto done;
706 816dc654 2019-02-16 stsp }
707 6353ad76 2019-02-08 stsp
708 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
709 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
710 70a0c8ec 2019-02-20 stsp goto done;
711 70a0c8ec 2019-02-20 stsp }
712 70a0c8ec 2019-02-20 stsp
713 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
714 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
715 230a42bd 2019-05-11 jcs ondisk_path);
716 6353ad76 2019-02-08 stsp goto done;
717 6353ad76 2019-02-08 stsp }
718 6353ad76 2019-02-08 stsp done:
719 fdcb7daf 2019-12-15 stsp if (err) {
720 fdcb7daf 2019-12-15 stsp if (merged_path)
721 fdcb7daf 2019-12-15 stsp unlink(merged_path);
722 3b9f0f87 2020-07-23 stsp }
723 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
724 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
725 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
726 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
727 6353ad76 2019-02-08 stsp free(merged_path);
728 af54ae4a 2019-02-19 stsp free(base_path);
729 3524bbf9 2020-10-20 stsp free(parent);
730 af57b12a 2020-07-23 stsp return err;
731 af57b12a 2020-07-23 stsp }
732 af57b12a 2020-07-23 stsp
733 af57b12a 2020-07-23 stsp static const struct got_error *
734 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
735 af57b12a 2020-07-23 stsp size_t target_len)
736 af57b12a 2020-07-23 stsp {
737 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
738 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
739 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
740 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
741 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
742 af57b12a 2020-07-23 stsp ondisk_path);
743 af57b12a 2020-07-23 stsp return NULL;
744 af57b12a 2020-07-23 stsp }
745 af57b12a 2020-07-23 stsp
746 af57b12a 2020-07-23 stsp /*
747 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
748 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
749 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
750 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
751 993e2a1b 2020-07-23 stsp *
752 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
753 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
754 993e2a1b 2020-07-23 stsp *
755 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
756 993e2a1b 2020-07-23 stsp * has deleted this symlink.
757 11cc08c1 2020-07-23 stsp */
758 11cc08c1 2020-07-23 stsp static const struct got_error *
759 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
760 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
761 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
762 11cc08c1 2020-07-23 stsp {
763 11cc08c1 2020-07-23 stsp const struct got_error *err;
764 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
765 11cc08c1 2020-07-23 stsp FILE *f = NULL;
766 11cc08c1 2020-07-23 stsp
767 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
768 11cc08c1 2020-07-23 stsp if (err)
769 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
770 11cc08c1 2020-07-23 stsp
771 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
772 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
773 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
774 11cc08c1 2020-07-23 stsp goto done;
775 11cc08c1 2020-07-23 stsp }
776 11cc08c1 2020-07-23 stsp
777 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
778 11cc08c1 2020-07-23 stsp if (err)
779 3818e3c4 2020-11-01 naddy goto done;
780 3818e3c4 2020-11-01 naddy
781 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
782 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
783 11cc08c1 2020-07-23 stsp goto done;
784 3818e3c4 2020-11-01 naddy }
785 11cc08c1 2020-07-23 stsp
786 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
787 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
788 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
789 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
790 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
791 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
792 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
793 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
794 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
795 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
796 11cc08c1 2020-07-23 stsp goto done;
797 11cc08c1 2020-07-23 stsp }
798 11cc08c1 2020-07-23 stsp
799 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
800 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
801 11cc08c1 2020-07-23 stsp goto done;
802 11cc08c1 2020-07-23 stsp }
803 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
804 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
805 11cc08c1 2020-07-23 stsp goto done;
806 11cc08c1 2020-07-23 stsp }
807 11cc08c1 2020-07-23 stsp done:
808 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
809 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
810 11cc08c1 2020-07-23 stsp free(path);
811 11cc08c1 2020-07-23 stsp free(id_str);
812 11cc08c1 2020-07-23 stsp free(label_deriv);
813 11cc08c1 2020-07-23 stsp return err;
814 11cc08c1 2020-07-23 stsp }
815 d219f183 2020-07-23 stsp
816 d219f183 2020-07-23 stsp /* forward declaration */
817 d219f183 2020-07-23 stsp static const struct got_error *
818 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
819 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
820 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
821 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
822 11cc08c1 2020-07-23 stsp
823 11cc08c1 2020-07-23 stsp /*
824 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
825 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
826 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
827 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
828 af57b12a 2020-07-23 stsp */
829 af57b12a 2020-07-23 stsp static const struct got_error *
830 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
831 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
832 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
833 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
834 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
835 af57b12a 2020-07-23 stsp {
836 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
837 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
838 af57b12a 2020-07-23 stsp struct stat sb;
839 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
840 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
841 11cc08c1 2020-07-23 stsp int have_local_change = 0;
842 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
843 af57b12a 2020-07-23 stsp
844 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
845 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
846 af57b12a 2020-07-23 stsp
847 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
848 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
849 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
850 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
851 af57b12a 2020-07-23 stsp ondisk_path);
852 af57b12a 2020-07-23 stsp goto done;
853 af57b12a 2020-07-23 stsp }
854 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
855 af57b12a 2020-07-23 stsp
856 526a746f 2020-07-23 stsp if (blob_orig) {
857 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
858 526a746f 2020-07-23 stsp if (err)
859 526a746f 2020-07-23 stsp goto done;
860 526a746f 2020-07-23 stsp }
861 af57b12a 2020-07-23 stsp
862 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
863 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
864 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
865 11cc08c1 2020-07-23 stsp have_local_change = 1;
866 11cc08c1 2020-07-23 stsp
867 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
868 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
869 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
870 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
871 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
872 11cc08c1 2020-07-23 stsp
873 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
874 11cc08c1 2020-07-23 stsp if (ancestor_target) {
875 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
876 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 11cc08c1 2020-07-23 stsp path);
878 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
879 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
880 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
881 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
882 11cc08c1 2020-07-23 stsp path);
883 11cc08c1 2020-07-23 stsp } else {
884 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
885 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
886 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
887 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
888 11cc08c1 2020-07-23 stsp if (err)
889 11cc08c1 2020-07-23 stsp goto done;
890 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
891 11cc08c1 2020-07-23 stsp path);
892 11cc08c1 2020-07-23 stsp }
893 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
894 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
895 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
896 af57b12a 2020-07-23 stsp strlen(deriv_target));
897 af57b12a 2020-07-23 stsp if (err)
898 af57b12a 2020-07-23 stsp goto done;
899 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
900 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
901 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
902 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
903 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
904 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
905 ea7786be 2020-07-23 stsp path);
906 ea7786be 2020-07-23 stsp } else {
907 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
908 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
909 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
910 ea7786be 2020-07-23 stsp if (err)
911 ea7786be 2020-07-23 stsp goto done;
912 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
913 ea7786be 2020-07-23 stsp path);
914 ea7786be 2020-07-23 stsp }
915 6353ad76 2019-02-08 stsp }
916 11cc08c1 2020-07-23 stsp
917 af57b12a 2020-07-23 stsp done:
918 af57b12a 2020-07-23 stsp free(ancestor_target);
919 eec2f5a9 2021-06-03 stsp return err;
920 eec2f5a9 2021-06-03 stsp }
921 eec2f5a9 2021-06-03 stsp
922 eec2f5a9 2021-06-03 stsp static const struct got_error *
923 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
924 eec2f5a9 2021-06-03 stsp {
925 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
926 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
927 eec2f5a9 2021-06-03 stsp ssize_t target_len;
928 eec2f5a9 2021-06-03 stsp size_t n;
929 eec2f5a9 2021-06-03 stsp FILE *f;
930 eec2f5a9 2021-06-03 stsp
931 eec2f5a9 2021-06-03 stsp *outfile = NULL;
932 eec2f5a9 2021-06-03 stsp
933 eec2f5a9 2021-06-03 stsp f = got_opentemp();
934 eec2f5a9 2021-06-03 stsp if (f == NULL)
935 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
936 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
937 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
938 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
939 eec2f5a9 2021-06-03 stsp goto done;
940 eec2f5a9 2021-06-03 stsp }
941 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
942 eec2f5a9 2021-06-03 stsp if (n != target_len) {
943 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
944 eec2f5a9 2021-06-03 stsp goto done;
945 eec2f5a9 2021-06-03 stsp }
946 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
947 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
948 eec2f5a9 2021-06-03 stsp goto done;
949 eec2f5a9 2021-06-03 stsp }
950 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
951 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
952 eec2f5a9 2021-06-03 stsp goto done;
953 eec2f5a9 2021-06-03 stsp }
954 eec2f5a9 2021-06-03 stsp done:
955 eec2f5a9 2021-06-03 stsp if (err)
956 eec2f5a9 2021-06-03 stsp fclose(f);
957 eec2f5a9 2021-06-03 stsp else
958 eec2f5a9 2021-06-03 stsp *outfile = f;
959 14c901f1 2019-08-08 stsp return err;
960 14c901f1 2019-08-08 stsp }
961 14c901f1 2019-08-08 stsp
962 14c901f1 2019-08-08 stsp /*
963 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
964 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
965 14c901f1 2019-08-08 stsp * acts as the second derived version.
966 14c901f1 2019-08-08 stsp */
967 14c901f1 2019-08-08 stsp static const struct got_error *
968 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
969 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
970 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
971 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
972 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
973 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
974 14c901f1 2019-08-08 stsp {
975 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
976 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
977 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
978 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
979 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
980 14c901f1 2019-08-08 stsp
981 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
982 14c901f1 2019-08-08 stsp
983 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
984 ed6b5030 2020-10-20 stsp if (err)
985 ed6b5030 2020-10-20 stsp return err;
986 14c901f1 2019-08-08 stsp
987 67a66647 2021-05-31 stsp if (blob_orig) {
988 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
989 67a66647 2021-05-31 stsp parent) == -1) {
990 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
991 67a66647 2021-05-31 stsp base_path = NULL;
992 67a66647 2021-05-31 stsp goto done;
993 67a66647 2021-05-31 stsp }
994 67a66647 2021-05-31 stsp
995 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
996 b90054ed 2022-11-01 stsp base_path, "");
997 67a66647 2021-05-31 stsp if (err)
998 67a66647 2021-05-31 stsp goto done;
999 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1000 67a66647 2021-05-31 stsp blob_orig);
1001 67a66647 2021-05-31 stsp if (err)
1002 67a66647 2021-05-31 stsp goto done;
1003 67a66647 2021-05-31 stsp free(base_path);
1004 db590691 2021-06-02 stsp } else {
1005 db590691 2021-06-02 stsp /*
1006 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1007 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1008 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1009 db590691 2021-06-02 stsp */
1010 db590691 2021-06-02 stsp f_orig = got_opentemp();
1011 db590691 2021-06-02 stsp if (f_orig == NULL) {
1012 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1013 db590691 2021-06-02 stsp goto done;
1014 db590691 2021-06-02 stsp }
1015 67a66647 2021-05-31 stsp }
1016 67a66647 2021-05-31 stsp
1017 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1018 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1019 14c901f1 2019-08-08 stsp base_path = NULL;
1020 14c901f1 2019-08-08 stsp goto done;
1021 14c901f1 2019-08-08 stsp }
1022 14c901f1 2019-08-08 stsp
1023 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1024 14c901f1 2019-08-08 stsp if (err)
1025 14c901f1 2019-08-08 stsp goto done;
1026 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1027 14c901f1 2019-08-08 stsp blob_deriv);
1028 14c901f1 2019-08-08 stsp if (err)
1029 14c901f1 2019-08-08 stsp goto done;
1030 14c901f1 2019-08-08 stsp
1031 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1032 14c901f1 2019-08-08 stsp if (err)
1033 14c901f1 2019-08-08 stsp goto done;
1034 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1035 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1036 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1037 14c901f1 2019-08-08 stsp goto done;
1038 eec2f5a9 2021-06-03 stsp }
1039 eec2f5a9 2021-06-03 stsp
1040 5e91dae4 2022-08-30 stsp /*
1041 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1042 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1043 eec2f5a9 2021-06-03 stsp */
1044 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1045 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1046 eec2f5a9 2021-06-03 stsp if (err)
1047 eec2f5a9 2021-06-03 stsp goto done;
1048 eec2f5a9 2021-06-03 stsp } else {
1049 eec2f5a9 2021-06-03 stsp int fd;
1050 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1051 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1052 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1053 eec2f5a9 2021-06-03 stsp goto done;
1054 eec2f5a9 2021-06-03 stsp }
1055 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1056 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1057 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1058 eec2f5a9 2021-06-03 stsp close(fd);
1059 eec2f5a9 2021-06-03 stsp goto done;
1060 eec2f5a9 2021-06-03 stsp }
1061 14c901f1 2019-08-08 stsp }
1062 14c901f1 2019-08-08 stsp
1063 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1064 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1065 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1066 14c901f1 2019-08-08 stsp done:
1067 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1068 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1069 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1070 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1071 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1072 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1073 14c901f1 2019-08-08 stsp free(base_path);
1074 67a66647 2021-05-31 stsp if (blob_orig_path) {
1075 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1076 67a66647 2021-05-31 stsp free(blob_orig_path);
1077 67a66647 2021-05-31 stsp }
1078 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1079 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1080 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1081 14c901f1 2019-08-08 stsp }
1082 6353ad76 2019-02-08 stsp free(id_str);
1083 818c7501 2019-07-11 stsp free(label_deriv);
1084 ed6b5030 2020-10-20 stsp free(parent);
1085 4a1ddfc2 2019-01-12 stsp return err;
1086 4a1ddfc2 2019-01-12 stsp }
1087 4a1ddfc2 2019-01-12 stsp
1088 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1089 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1090 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1091 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1092 13d9040b 2019-03-27 stsp {
1093 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1094 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1095 13d9040b 2019-03-27 stsp
1096 65b05cec 2020-07-23 stsp *new_iep = NULL;
1097 65b05cec 2020-07-23 stsp
1098 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1099 054041d0 2020-06-24 stsp if (err)
1100 054041d0 2020-06-24 stsp return err;
1101 054041d0 2020-06-24 stsp
1102 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1103 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1104 054041d0 2020-06-24 stsp if (err)
1105 054041d0 2020-06-24 stsp goto done;
1106 054041d0 2020-06-24 stsp
1107 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1108 054041d0 2020-06-24 stsp done:
1109 054041d0 2020-06-24 stsp if (err)
1110 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1111 65b05cec 2020-07-23 stsp else
1112 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1113 13d9040b 2019-03-27 stsp return err;
1114 1ebedb77 2019-10-19 stsp }
1115 1ebedb77 2019-10-19 stsp
1116 1ebedb77 2019-10-19 stsp static mode_t
1117 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1118 1ebedb77 2019-10-19 stsp {
1119 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1120 1ebedb77 2019-10-19 stsp
1121 1ebedb77 2019-10-19 stsp if (executable) {
1122 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1123 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1124 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1125 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1126 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1127 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1128 1ebedb77 2019-10-19 stsp }
1129 1ebedb77 2019-10-19 stsp
1130 b2b3fce1 2022-10-29 op return st_mode;
1131 b2b3fce1 2022-10-29 op }
1132 b2b3fce1 2022-10-29 op
1133 b2b3fce1 2022-10-29 op static mode_t
1134 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1135 b2b3fce1 2022-10-29 op {
1136 b2b3fce1 2022-10-29 op mode_t um;
1137 b2b3fce1 2022-10-29 op
1138 b2b3fce1 2022-10-29 op um = umask(000);
1139 b2b3fce1 2022-10-29 op umask(um);
1140 b2b3fce1 2022-10-29 op return mode & ~um;
1141 13d9040b 2019-03-27 stsp }
1142 13d9040b 2019-03-27 stsp
1143 8ba819a3 2020-07-23 stsp /* forward declaration */
1144 13d9040b 2019-03-27 stsp static const struct got_error *
1145 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1146 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1147 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1148 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1149 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1150 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 8ba819a3 2020-07-23 stsp
1152 5a1fbc73 2020-07-23 stsp /*
1153 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1154 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1155 5a1fbc73 2020-07-23 stsp */
1156 8ba819a3 2020-07-23 stsp static const struct got_error *
1157 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1158 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1159 5a1fbc73 2020-07-23 stsp {
1160 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1161 5a1fbc73 2020-07-23 stsp ssize_t elen;
1162 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1163 5a1fbc73 2020-07-23 stsp int fd;
1164 5a1fbc73 2020-07-23 stsp
1165 c6e8a826 2021-04-05 stsp *did_something = 0;
1166 c6e8a826 2021-04-05 stsp
1167 5a1fbc73 2020-07-23 stsp /*
1168 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1169 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1170 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1171 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1172 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1173 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1174 5a1fbc73 2020-07-23 stsp */
1175 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1176 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1177 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1178 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1179 5a1fbc73 2020-07-23 stsp
1180 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1181 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1182 5a1fbc73 2020-07-23 stsp if (elen == -1)
1183 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1184 5a1fbc73 2020-07-23 stsp
1185 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1186 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1187 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1188 5a1fbc73 2020-07-23 stsp }
1189 5a1fbc73 2020-07-23 stsp
1190 c6e8a826 2021-04-05 stsp *did_something = 1;
1191 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1192 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1193 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1194 5a1fbc73 2020-07-23 stsp return err;
1195 3c1ec782 2020-07-23 stsp }
1196 3c1ec782 2020-07-23 stsp
1197 3c1ec782 2020-07-23 stsp static const struct got_error *
1198 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1199 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1200 3c1ec782 2020-07-23 stsp {
1201 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1202 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1203 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1204 3c1ec782 2020-07-23 stsp
1205 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1206 3c1ec782 2020-07-23 stsp
1207 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1209 3c1ec782 2020-07-23 stsp return NULL;
1210 3c1ec782 2020-07-23 stsp }
1211 3c1ec782 2020-07-23 stsp
1212 3c1ec782 2020-07-23 stsp /*
1213 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1214 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1215 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1216 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1217 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1218 3c1ec782 2020-07-23 stsp */
1219 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1220 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1221 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1222 ce031e9e 2020-10-20 stsp if (err)
1223 ce031e9e 2020-10-20 stsp return err;
1224 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1225 ce031e9e 2020-10-20 stsp free(parent);
1226 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1227 ce031e9e 2020-10-20 stsp }
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1230 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1231 3c1ec782 2020-07-23 stsp free(abspath);
1232 3c1ec782 2020-07-23 stsp return err;
1233 3c1ec782 2020-07-23 stsp }
1234 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1235 3c1ec782 2020-07-23 stsp free(abspath);
1236 3c1ec782 2020-07-23 stsp if (err)
1237 3c1ec782 2020-07-23 stsp return err;
1238 3c1ec782 2020-07-23 stsp } else {
1239 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1240 3c1ec782 2020-07-23 stsp if (err)
1241 3c1ec782 2020-07-23 stsp return err;
1242 3c1ec782 2020-07-23 stsp }
1243 3c1ec782 2020-07-23 stsp
1244 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1245 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1246 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1247 3c1ec782 2020-07-23 stsp return NULL;
1248 3c1ec782 2020-07-23 stsp }
1249 3c1ec782 2020-07-23 stsp
1250 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1251 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1252 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1253 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1254 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1256 3c1ec782 2020-07-23 stsp
1257 3c1ec782 2020-07-23 stsp free(path_got);
1258 3c1ec782 2020-07-23 stsp return NULL;
1259 5a1fbc73 2020-07-23 stsp }
1260 5a1fbc73 2020-07-23 stsp
1261 5a1fbc73 2020-07-23 stsp static const struct got_error *
1262 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1263 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1264 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1265 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1266 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1267 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1268 9d31a1d8 2018-03-11 stsp {
1269 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1270 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1271 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1272 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1273 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1274 8ba819a3 2020-07-23 stsp
1275 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1276 2e1fa222 2020-07-23 stsp
1277 3c29341b 2022-07-21 florian /*
1278 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1279 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1280 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1281 8ba819a3 2020-07-23 stsp * in the blob object.
1282 8ba819a3 2020-07-23 stsp */
1283 8ba819a3 2020-07-23 stsp do {
1284 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1285 538b6881 2022-07-21 florian if (err)
1286 538b6881 2022-07-21 florian return err;
1287 538b6881 2022-07-21 florian
1288 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1289 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1290 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1291 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1292 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1293 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1294 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1295 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1296 3b9f0f87 2020-07-23 stsp progress_arg);
1297 8ba819a3 2020-07-23 stsp }
1298 8ba819a3 2020-07-23 stsp if (len > 0) {
1299 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1300 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1301 8ba819a3 2020-07-23 stsp len - hdrlen);
1302 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1303 8ba819a3 2020-07-23 stsp hdrlen = 0;
1304 8ba819a3 2020-07-23 stsp }
1305 8ba819a3 2020-07-23 stsp } while (len != 0);
1306 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1307 8ba819a3 2020-07-23 stsp
1308 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1309 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1310 3c1ec782 2020-07-23 stsp if (err)
1311 3c1ec782 2020-07-23 stsp return err;
1312 8ba819a3 2020-07-23 stsp
1313 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1314 906c123b 2020-07-23 stsp /* install as a regular file */
1315 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1316 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1317 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1319 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1320 3c29341b 2022-07-21 florian return err;
1321 906c123b 2020-07-23 stsp }
1322 906c123b 2020-07-23 stsp
1323 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1324 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1325 c6e8a826 2021-04-05 stsp int symlink_replaced;
1326 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1327 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1328 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1329 3c29341b 2022-07-21 florian return err;
1330 c90c8ce3 2020-07-23 stsp }
1331 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1332 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1333 5a1fbc73 2020-07-23 stsp if (err)
1334 3c29341b 2022-07-21 florian return err;
1335 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1336 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1337 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1338 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1339 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1341 c6e8a826 2021-04-05 stsp } else {
1342 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1344 c6e8a826 2021-04-05 stsp }
1345 f35fa46a 2020-07-23 stsp }
1346 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1347 f35fa46a 2020-07-23 stsp }
1348 f35fa46a 2020-07-23 stsp
1349 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1350 f4994adc 2020-10-20 stsp char *parent;
1351 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1352 f4994adc 2020-10-20 stsp if (err)
1353 3c29341b 2022-07-21 florian return err;
1354 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1355 f4994adc 2020-10-20 stsp free(parent);
1356 f35fa46a 2020-07-23 stsp if (err)
1357 3c29341b 2022-07-21 florian return err;
1358 f35fa46a 2020-07-23 stsp /*
1359 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1360 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1361 f35fa46a 2020-07-23 stsp */
1362 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1363 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1364 3c29341b 2022-07-21 florian return err;
1365 f35fa46a 2020-07-23 stsp }
1366 f35fa46a 2020-07-23 stsp }
1367 f35fa46a 2020-07-23 stsp
1368 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1369 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1370 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1371 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1372 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1373 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1374 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1376 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1377 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1378 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1379 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1380 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1381 8ba819a3 2020-07-23 stsp } else {
1382 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1383 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1384 8ba819a3 2020-07-23 stsp }
1385 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1386 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1387 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1388 8ba819a3 2020-07-23 stsp return err;
1389 8ba819a3 2020-07-23 stsp }
1390 8ba819a3 2020-07-23 stsp
1391 8ba819a3 2020-07-23 stsp static const struct got_error *
1392 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1393 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1394 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1395 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1396 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1397 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1398 8ba819a3 2020-07-23 stsp {
1399 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1400 507dc3bb 2018-12-29 stsp int fd = -1;
1401 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1402 507dc3bb 2018-12-29 stsp int update = 0;
1403 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1404 b2b3fce1 2022-10-29 op mode_t mode;
1405 8ba819a3 2020-07-23 stsp
1406 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1407 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1408 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1409 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1410 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1411 f5375317 2020-10-20 stsp char *parent;
1412 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1413 f5375317 2020-10-20 stsp if (err)
1414 f5375317 2020-10-20 stsp return err;
1415 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1416 f5375317 2020-10-20 stsp free(parent);
1417 21908da4 2019-01-13 stsp if (err)
1418 21908da4 2019-01-13 stsp return err;
1419 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1420 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 b2b3fce1 2022-10-29 op mode);
1422 21908da4 2019-01-13 stsp if (fd == -1)
1423 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1424 230a42bd 2019-05-11 jcs ondisk_path);
1425 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1426 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1427 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1428 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1429 3b9f0f87 2020-07-23 stsp goto done;
1430 3b9f0f87 2020-07-23 stsp }
1431 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1434 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1435 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1436 507dc3bb 2018-12-29 stsp goto done;
1437 d70b8e30 2018-12-27 stsp } else {
1438 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1439 b90054ed 2022-11-01 stsp ondisk_path, "");
1440 507dc3bb 2018-12-29 stsp if (err)
1441 507dc3bb 2018-12-29 stsp goto done;
1442 507dc3bb 2018-12-29 stsp update = 1;
1443 b2b3fce1 2022-10-29 op
1444 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1445 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1446 b2b3fce1 2022-10-29 op tmppath);
1447 b2b3fce1 2022-10-29 op goto done;
1448 b2b3fce1 2022-10-29 op }
1449 9d31a1d8 2018-03-11 stsp }
1450 507dc3bb 2018-12-29 stsp } else
1451 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1452 3818e3c4 2020-11-01 naddy }
1453 3818e3c4 2020-11-01 naddy
1454 bd6aa359 2020-07-23 stsp if (progress_cb) {
1455 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1456 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 bd6aa359 2020-07-23 stsp path);
1458 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1463 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 bd6aa359 2020-07-23 stsp if (err)
1465 bd6aa359 2020-07-23 stsp goto done;
1466 bd6aa359 2020-07-23 stsp }
1467 d7b62c98 2018-12-27 stsp
1468 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1469 9d31a1d8 2018-03-11 stsp do {
1470 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1472 9d31a1d8 2018-03-11 stsp if (err)
1473 9d31a1d8 2018-03-11 stsp break;
1474 9d31a1d8 2018-03-11 stsp if (len > 0) {
1475 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1476 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1478 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1479 b87c6f83 2018-12-24 stsp goto done;
1480 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1481 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1482 b87c6f83 2018-12-24 stsp goto done;
1483 9d31a1d8 2018-03-11 stsp }
1484 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1485 9d31a1d8 2018-03-11 stsp }
1486 9d31a1d8 2018-03-11 stsp } while (len != 0);
1487 9d31a1d8 2018-03-11 stsp
1488 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1490 816dc654 2019-02-16 stsp goto done;
1491 816dc654 2019-02-16 stsp }
1492 9d31a1d8 2018-03-11 stsp
1493 507dc3bb 2018-12-29 stsp if (update) {
1494 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1496 f6d8c0ac 2020-11-09 stsp goto done;
1497 f6d8c0ac 2020-11-09 stsp }
1498 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 68ed9ba5 2019-02-10 stsp goto done;
1502 68ed9ba5 2019-02-10 stsp }
1503 63df146d 2020-11-09 stsp free(tmppath);
1504 63df146d 2020-11-09 stsp tmppath = NULL;
1505 507dc3bb 2018-12-29 stsp }
1506 507dc3bb 2018-12-29 stsp
1507 9d31a1d8 2018-03-11 stsp done:
1508 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1510 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1512 507dc3bb 2018-12-29 stsp free(tmppath);
1513 6353ad76 2019-02-08 stsp return err;
1514 6353ad76 2019-02-08 stsp }
1515 d952957d 2023-02-19 mark
1516 d952957d 2023-02-19 mark static const struct got_error *skip_one_line(FILE *);
1517 6353ad76 2019-02-08 stsp
1518 12383673 2023-02-18 mark /*
1519 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1520 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1521 12383673 2023-02-18 mark */
1522 6353ad76 2019-02-08 stsp static const struct got_error *
1523 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1524 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1525 12383673 2023-02-18 mark FILE *ondisk_file)
1526 7154f6ce 2019-03-27 stsp {
1527 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1528 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1530 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1531 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1532 7154f6ce 2019-03-27 stsp };
1533 d952957d 2023-02-19 mark FILE *f1 = NULL;
1534 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1535 d952957d 2023-02-19 mark struct diff_result *r;
1536 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1537 9bdd68dd 2021-01-02 naddy char *line = NULL;
1538 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1539 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1540 7154f6ce 2019-03-27 stsp
1541 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1542 d952957d 2023-02-19 mark return NULL;
1543 12383673 2023-02-18 mark
1544 d952957d 2023-02-19 mark f1 = got_opentemp();
1545 d952957d 2023-02-19 mark if (f1 == NULL)
1546 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1547 12383673 2023-02-18 mark
1548 d952957d 2023-02-19 mark if (blob) {
1549 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1550 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1551 12383673 2023-02-18 mark if (err)
1552 12383673 2023-02-18 mark goto done;
1553 d952957d 2023-02-19 mark }
1554 12383673 2023-02-18 mark
1555 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1556 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1557 d952957d 2023-02-19 mark if (err)
1558 d952957d 2023-02-19 mark goto done;
1559 d952957d 2023-02-19 mark
1560 d952957d 2023-02-19 mark if (fseek(ondisk_file, 0L, SEEK_SET) == -1) {
1561 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1562 d952957d 2023-02-19 mark goto done;
1563 12383673 2023-02-18 mark }
1564 12383673 2023-02-18 mark
1565 d952957d 2023-02-19 mark r = diffreg_result->result;
1566 d952957d 2023-02-19 mark
1567 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1568 d952957d 2023-02-19 mark struct diff_chunk *c;
1569 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1570 d952957d 2023-02-19 mark int clc, crc;
1571 d952957d 2023-02-19 mark
1572 d952957d 2023-02-19 mark /*
1573 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1574 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1575 d952957d 2023-02-19 mark */
1576 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1577 d952957d 2023-02-19 mark clc = diff_chunk_get_left_count(c);
1578 d952957d 2023-02-19 mark crc = diff_chunk_get_right_count(c);
1579 d952957d 2023-02-19 mark
1580 d952957d 2023-02-19 mark if (!crc && clc) {
1581 d952957d 2023-02-19 mark nchunks_parsed = 1;
1582 d952957d 2023-02-19 mark continue; /* removed lines */
1583 7154f6ce 2019-03-27 stsp }
1584 7154f6ce 2019-03-27 stsp
1585 d952957d 2023-02-19 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 d952957d 2023-02-19 mark
1587 d952957d 2023-02-19 mark while (ln < cc.right.start) {
1588 d952957d 2023-02-19 mark err = skip_one_line(ondisk_file);
1589 d952957d 2023-02-19 mark if (err)
1590 d952957d 2023-02-19 mark goto done;
1591 d952957d 2023-02-19 mark ++ln;
1592 7154f6ce 2019-03-27 stsp }
1593 d952957d 2023-02-19 mark
1594 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1595 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1596 d952957d 2023-02-19 mark if (linelen == -1) {
1597 d952957d 2023-02-19 mark if (feof(ondisk_file))
1598 d952957d 2023-02-19 mark break;
1599 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1600 d952957d 2023-02-19 mark break;
1601 d952957d 2023-02-19 mark }
1602 d952957d 2023-02-19 mark
1603 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1604 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1605 d952957d 2023-02-19 mark if (strcmp(markers[i],
1606 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1607 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1608 d952957d 2023-02-19 mark goto done;
1609 d952957d 2023-02-19 mark } else
1610 d952957d 2023-02-19 mark i++;
1611 d952957d 2023-02-19 mark }
1612 d952957d 2023-02-19 mark ++ln;
1613 d952957d 2023-02-19 mark }
1614 7154f6ce 2019-03-27 stsp }
1615 12383673 2023-02-18 mark
1616 12383673 2023-02-18 mark done:
1617 9bdd68dd 2021-01-02 naddy free(line);
1618 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1619 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1620 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1621 d952957d 2023-02-19 mark if (err == NULL)
1622 d952957d 2023-02-19 mark err = free_err;
1623 7154f6ce 2019-03-27 stsp
1624 7154f6ce 2019-03-27 stsp return err;
1625 e2b1e152 2019-07-13 stsp }
1626 e2b1e152 2019-07-13 stsp
1627 e2b1e152 2019-07-13 stsp static int
1628 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1629 1ebedb77 2019-10-19 stsp {
1630 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1631 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1632 1ebedb77 2019-10-19 stsp }
1633 1ebedb77 2019-10-19 stsp
1634 1ebedb77 2019-10-19 stsp static int
1635 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1636 e2b1e152 2019-07-13 stsp {
1637 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1638 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1639 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1640 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1641 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1642 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1643 c363b2c1 2019-08-03 stsp }
1644 c363b2c1 2019-08-03 stsp
1645 c363b2c1 2019-08-03 stsp static unsigned char
1646 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1647 c363b2c1 2019-08-03 stsp {
1648 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1649 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1650 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1651 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1652 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1653 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1654 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1655 c363b2c1 2019-08-03 stsp default:
1656 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1657 a919d5c4 2020-07-23 stsp }
1658 a919d5c4 2020-07-23 stsp }
1659 a919d5c4 2020-07-23 stsp
1660 a919d5c4 2020-07-23 stsp static const struct got_error *
1661 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1662 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1663 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1664 a919d5c4 2020-07-23 stsp {
1665 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1666 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1667 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1668 a919d5c4 2020-07-23 stsp ssize_t elen;
1669 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1670 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1671 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1672 a919d5c4 2020-07-23 stsp
1673 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1674 a919d5c4 2020-07-23 stsp
1675 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1676 a919d5c4 2020-07-23 stsp do {
1677 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1678 a919d5c4 2020-07-23 stsp if (err)
1679 a919d5c4 2020-07-23 stsp return err;
1680 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1681 a919d5c4 2020-07-23 stsp /*
1682 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1683 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1684 a919d5c4 2020-07-23 stsp */
1685 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1686 a919d5c4 2020-07-23 stsp }
1687 a919d5c4 2020-07-23 stsp if (len > 0) {
1688 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1689 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1690 a919d5c4 2020-07-23 stsp len - hdrlen);
1691 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1692 a919d5c4 2020-07-23 stsp hdrlen = 0;
1693 a919d5c4 2020-07-23 stsp }
1694 a919d5c4 2020-07-23 stsp } while (len != 0);
1695 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1696 a919d5c4 2020-07-23 stsp
1697 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1698 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1699 a919d5c4 2020-07-23 stsp if (elen == -1)
1700 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1701 a919d5c4 2020-07-23 stsp } else {
1702 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1703 a919d5c4 2020-07-23 stsp if (elen == -1)
1704 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1705 c363b2c1 2019-08-03 stsp }
1706 a919d5c4 2020-07-23 stsp
1707 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1708 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1709 a919d5c4 2020-07-23 stsp
1710 a919d5c4 2020-07-23 stsp return NULL;
1711 7154f6ce 2019-03-27 stsp }
1712 7154f6ce 2019-03-27 stsp
1713 7154f6ce 2019-03-27 stsp static const struct got_error *
1714 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1715 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1716 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1717 6353ad76 2019-02-08 stsp {
1718 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1719 6353ad76 2019-02-08 stsp struct got_object_id id;
1720 6353ad76 2019-02-08 stsp size_t hdrlen;
1721 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1722 6353ad76 2019-02-08 stsp FILE *f = NULL;
1723 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1724 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1725 6353ad76 2019-02-08 stsp size_t flen, blen;
1726 2c4740ad 2023-02-12 mark unsigned char staged_status;
1727 6353ad76 2019-02-08 stsp
1728 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1729 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1730 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1731 6353ad76 2019-02-08 stsp
1732 7f91a133 2019-12-13 stsp /*
1733 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1734 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1735 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1736 7f91a133 2019-12-13 stsp */
1737 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1738 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1739 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1740 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1741 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1742 882ef1b9 2019-12-13 stsp else
1743 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1744 882ef1b9 2019-12-13 stsp goto done;
1745 882ef1b9 2019-12-13 stsp }
1746 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1747 3d35a492 2019-12-13 stsp goto done;
1748 3d35a492 2019-12-13 stsp }
1749 7f91a133 2019-12-13 stsp } else {
1750 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1751 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1752 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1753 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1754 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1755 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1756 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1757 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1758 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1759 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1760 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1761 3d35a492 2019-12-13 stsp else
1762 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1763 3d35a492 2019-12-13 stsp goto done;
1764 3d35a492 2019-12-13 stsp }
1765 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1766 1338848f 2019-12-13 stsp goto done;
1767 a378724f 2019-02-10 stsp }
1768 a378724f 2019-02-10 stsp }
1769 6353ad76 2019-02-08 stsp
1770 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1771 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1772 1338848f 2019-12-13 stsp goto done;
1773 3f148bc6 2019-07-27 stsp }
1774 efdd40df 2019-07-27 stsp
1775 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1776 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1777 1338848f 2019-12-13 stsp goto done;
1778 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1779 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1780 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1781 1338848f 2019-12-13 stsp goto done;
1782 d00136be 2019-03-26 stsp }
1783 b8f41171 2019-02-10 stsp
1784 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1785 f179e66d 2020-07-23 stsp goto done;
1786 f179e66d 2020-07-23 stsp
1787 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1788 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1789 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1790 1338848f 2019-12-13 stsp goto done;
1791 f179e66d 2020-07-23 stsp }
1792 6353ad76 2019-02-08 stsp
1793 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1794 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1795 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1796 c363b2c1 2019-08-03 stsp else
1797 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1798 c363b2c1 2019-08-03 stsp
1799 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1800 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1801 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1802 eb81bc23 2022-06-28 tracey goto done;
1803 eb81bc23 2022-06-28 tracey }
1804 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1805 6353ad76 2019-02-08 stsp if (err)
1806 1338848f 2019-12-13 stsp goto done;
1807 6353ad76 2019-02-08 stsp
1808 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1809 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1810 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1811 a919d5c4 2020-07-23 stsp goto done;
1812 a919d5c4 2020-07-23 stsp }
1813 a919d5c4 2020-07-23 stsp
1814 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1815 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1816 ab0d4361 2019-12-13 stsp if (fd == -1) {
1817 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1818 ab0d4361 2019-12-13 stsp goto done;
1819 ab0d4361 2019-12-13 stsp }
1820 3d35a492 2019-12-13 stsp }
1821 3d35a492 2019-12-13 stsp
1822 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1823 6353ad76 2019-02-08 stsp if (f == NULL) {
1824 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1825 6353ad76 2019-02-08 stsp goto done;
1826 6353ad76 2019-02-08 stsp }
1827 1338848f 2019-12-13 stsp fd = -1;
1828 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1829 656b1f76 2019-05-11 jcs for (;;) {
1830 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1831 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1832 6353ad76 2019-02-08 stsp if (err)
1833 7154f6ce 2019-03-27 stsp goto done;
1834 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1835 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1836 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1837 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1838 7154f6ce 2019-03-27 stsp goto done;
1839 80c5c120 2019-02-19 stsp }
1840 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1841 6353ad76 2019-02-08 stsp if (flen != 0)
1842 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1843 6353ad76 2019-02-08 stsp break;
1844 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1845 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1846 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1847 6353ad76 2019-02-08 stsp break;
1848 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1849 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1850 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1851 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1852 6353ad76 2019-02-08 stsp break;
1853 6353ad76 2019-02-08 stsp }
1854 6353ad76 2019-02-08 stsp } else {
1855 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1856 6353ad76 2019-02-08 stsp break;
1857 6353ad76 2019-02-08 stsp }
1858 6353ad76 2019-02-08 stsp hdrlen = 0;
1859 6353ad76 2019-02-08 stsp }
1860 7154f6ce 2019-03-27 stsp
1861 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1862 7154f6ce 2019-03-27 stsp rewind(f);
1863 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1864 12383673 2023-02-18 mark sb, f);
1865 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1866 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1867 6353ad76 2019-02-08 stsp done:
1868 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1869 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1870 6353ad76 2019-02-08 stsp if (blob)
1871 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1872 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1873 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1874 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1875 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1876 9d31a1d8 2018-03-11 stsp return err;
1877 e2b1e152 2019-07-13 stsp }
1878 e2b1e152 2019-07-13 stsp
1879 e2b1e152 2019-07-13 stsp /*
1880 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1881 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1882 e2b1e152 2019-07-13 stsp */
1883 e2b1e152 2019-07-13 stsp static const struct got_error *
1884 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1885 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1886 e2b1e152 2019-07-13 stsp {
1887 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1888 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1889 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1890 e2b1e152 2019-07-13 stsp
1891 e2b1e152 2019-07-13 stsp return NULL;
1892 9d31a1d8 2018-03-11 stsp }
1893 9d31a1d8 2018-03-11 stsp
1894 9d31a1d8 2018-03-11 stsp static const struct got_error *
1895 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1896 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1897 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1898 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1899 0584f854 2019-04-06 stsp void *progress_arg)
1900 9d31a1d8 2018-03-11 stsp {
1901 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1902 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1903 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1904 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1905 b8f41171 2019-02-10 stsp struct stat sb;
1906 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1907 9d31a1d8 2018-03-11 stsp
1908 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1909 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1910 6353ad76 2019-02-08 stsp
1911 abb4604f 2019-07-27 stsp if (ie) {
1912 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1913 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1914 a76c42e6 2019-08-03 stsp goto done;
1915 a76c42e6 2019-08-03 stsp }
1916 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1917 7f91a133 2019-12-13 stsp repo);
1918 abb4604f 2019-07-27 stsp if (err)
1919 abb4604f 2019-07-27 stsp goto done;
1920 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1921 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1922 c90c8ce3 2020-07-23 stsp } else {
1923 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1924 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1925 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1926 f6764181 2021-09-24 stsp ondisk_path);
1927 f6764181 2021-09-24 stsp goto done;
1928 f6764181 2021-09-24 stsp }
1929 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1930 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1931 f6764181 2021-09-24 stsp } else {
1932 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1933 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1934 f6764181 2021-09-24 stsp else
1935 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1936 f6764181 2021-09-24 stsp }
1937 c90c8ce3 2020-07-23 stsp }
1938 6353ad76 2019-02-08 stsp
1939 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1940 2c41dce7 2021-06-27 stsp if (ie)
1941 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1942 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1943 b8f41171 2019-02-10 stsp goto done;
1944 b8f41171 2019-02-10 stsp }
1945 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1946 a769b60b 2021-06-27 stsp if (ie)
1947 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1948 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1949 5036ab18 2020-04-18 stsp path);
1950 5036ab18 2020-04-18 stsp goto done;
1951 5036ab18 2020-04-18 stsp }
1952 b8f41171 2019-02-10 stsp
1953 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1954 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1955 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1956 c6e8a826 2021-04-05 stsp /*
1957 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1958 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1959 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1960 c6e8a826 2021-04-05 stsp * updating contents of this file.
1961 c6e8a826 2021-04-05 stsp */
1962 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1963 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1964 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1965 c6e8a826 2021-04-05 stsp /* Same commit. */
1966 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1967 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1968 e2b1e152 2019-07-13 stsp if (err)
1969 e2b1e152 2019-07-13 stsp goto done;
1970 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1971 b8f41171 2019-02-10 stsp path);
1972 6353ad76 2019-02-08 stsp goto done;
1973 a378724f 2019-02-10 stsp }
1974 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1975 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1976 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1977 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1978 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1979 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1980 0f58026f 2021-04-05 stsp if (err)
1981 0f58026f 2021-04-05 stsp goto done;
1982 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1983 0f58026f 2021-04-05 stsp path);
1984 b8f41171 2019-02-10 stsp goto done;
1985 e2b1e152 2019-07-13 stsp }
1986 9d31a1d8 2018-03-11 stsp }
1987 9d31a1d8 2018-03-11 stsp
1988 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1989 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1990 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1991 eb81bc23 2022-06-28 tracey goto done;
1992 eb81bc23 2022-06-28 tracey }
1993 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1994 8da9e5f4 2019-01-12 stsp if (err)
1995 6353ad76 2019-02-08 stsp goto done;
1996 512f0d0e 2019-01-02 stsp
1997 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1998 234035bc 2019-06-01 stsp int update_timestamps;
1999 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2000 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2001 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2002 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
2003 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
2004 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
2005 eb81bc23 2022-06-28 tracey goto done;
2006 eb81bc23 2022-06-28 tracey }
2007 234035bc 2019-06-01 stsp struct got_object_id id2;
2008 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2009 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2010 eb81bc23 2022-06-28 tracey fd2);
2011 234035bc 2019-06-01 stsp if (err)
2012 f69721c3 2019-10-21 stsp goto done;
2013 f69721c3 2019-10-21 stsp }
2014 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2015 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2016 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2017 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2018 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2019 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2020 f69721c3 2019-10-21 stsp goto done;
2021 f69721c3 2019-10-21 stsp }
2022 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2023 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2024 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2025 234035bc 2019-06-01 stsp goto done;
2026 f69721c3 2019-10-21 stsp }
2027 234035bc 2019-06-01 stsp }
2028 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2029 36bf999c 2020-07-23 stsp char *link_target;
2030 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2031 36bf999c 2020-07-23 stsp if (err)
2032 36bf999c 2020-07-23 stsp goto done;
2033 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2034 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2035 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2036 36bf999c 2020-07-23 stsp free(link_target);
2037 993e2a1b 2020-07-23 stsp } else {
2038 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2039 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2040 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2041 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2042 993e2a1b 2020-07-23 stsp }
2043 f69721c3 2019-10-21 stsp free(label_orig);
2044 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2045 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2046 eb81bc23 2022-06-28 tracey goto done;
2047 eb81bc23 2022-06-28 tracey }
2048 234035bc 2019-06-01 stsp if (blob2)
2049 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2050 909d120e 2019-09-22 stsp if (err)
2051 909d120e 2019-09-22 stsp goto done;
2052 234035bc 2019-06-01 stsp /*
2053 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2054 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2055 234035bc 2019-06-01 stsp * unmodified files again.
2056 234035bc 2019-06-01 stsp */
2057 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2058 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2059 234035bc 2019-06-01 stsp update_timestamps);
2060 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2061 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2062 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2063 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2064 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2065 1ee397ad 2019-07-12 stsp if (err)
2066 1ee397ad 2019-07-12 stsp goto done;
2067 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2068 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2069 13d9040b 2019-03-27 stsp if (err)
2070 13d9040b 2019-03-27 stsp goto done;
2071 13d9040b 2019-03-27 stsp } else {
2072 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2073 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2074 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2075 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2076 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2077 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2078 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2079 2e1fa222 2020-07-23 stsp } else {
2080 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2081 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2082 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2083 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2084 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2085 2e1fa222 2020-07-23 stsp }
2086 13d9040b 2019-03-27 stsp if (err)
2087 13d9040b 2019-03-27 stsp goto done;
2088 65b05cec 2020-07-23 stsp
2089 054041d0 2020-06-24 stsp if (ie) {
2090 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2091 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2092 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2093 054041d0 2020-06-24 stsp } else {
2094 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2095 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2096 054041d0 2020-06-24 stsp &blob->id);
2097 054041d0 2020-06-24 stsp }
2098 13d9040b 2019-03-27 stsp if (err)
2099 13d9040b 2019-03-27 stsp goto done;
2100 65b05cec 2020-07-23 stsp
2101 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2102 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2103 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2104 2e1fa222 2020-07-23 stsp }
2105 13d9040b 2019-03-27 stsp }
2106 eb81bc23 2022-06-28 tracey
2107 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2108 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2109 eb81bc23 2022-06-28 tracey goto done;
2110 eb81bc23 2022-06-28 tracey }
2111 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2112 6353ad76 2019-02-08 stsp done:
2113 6353ad76 2019-02-08 stsp free(ondisk_path);
2114 8da9e5f4 2019-01-12 stsp return err;
2115 90285c3b 2019-01-08 stsp }
2116 90285c3b 2019-01-08 stsp
2117 90285c3b 2019-01-08 stsp static const struct got_error *
2118 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2119 90285c3b 2019-01-08 stsp {
2120 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2121 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2122 90285c3b 2019-01-08 stsp
2123 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2124 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2125 90285c3b 2019-01-08 stsp
2126 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2127 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2128 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2129 c97665ae 2019-01-13 stsp } else {
2130 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2131 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2132 1c4cdd89 2021-06-20 stsp if (err)
2133 1c4cdd89 2021-06-20 stsp goto done;
2134 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2135 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2136 fddefe3b 2020-10-20 stsp free(ondisk_path);
2137 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2138 1c4cdd89 2021-06-20 stsp parent = NULL;
2139 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2140 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2141 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2142 fddefe3b 2020-10-20 stsp ondisk_path);
2143 90285c3b 2019-01-08 stsp break;
2144 90285c3b 2019-01-08 stsp }
2145 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2146 1c4cdd89 2021-06-20 stsp if (err)
2147 1c4cdd89 2021-06-20 stsp break;
2148 1c4cdd89 2021-06-20 stsp }
2149 90285c3b 2019-01-08 stsp }
2150 1c4cdd89 2021-06-20 stsp done:
2151 90285c3b 2019-01-08 stsp free(ondisk_path);
2152 1c4cdd89 2021-06-20 stsp free(parent);
2153 90285c3b 2019-01-08 stsp return err;
2154 512f0d0e 2019-01-02 stsp }
2155 512f0d0e 2019-01-02 stsp
2156 708d8e67 2019-03-27 stsp static const struct got_error *
2157 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2158 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2159 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2160 708d8e67 2019-03-27 stsp {
2161 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2162 708d8e67 2019-03-27 stsp unsigned char status;
2163 708d8e67 2019-03-27 stsp struct stat sb;
2164 708d8e67 2019-03-27 stsp char *ondisk_path;
2165 708d8e67 2019-03-27 stsp
2166 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2167 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2168 a76c42e6 2019-08-03 stsp
2169 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2170 708d8e67 2019-03-27 stsp == -1)
2171 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2172 708d8e67 2019-03-27 stsp
2173 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2174 708d8e67 2019-03-27 stsp if (err)
2175 5a58a424 2020-06-23 stsp goto done;
2176 708d8e67 2019-03-27 stsp
2177 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2178 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2179 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2180 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2181 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2182 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2183 993e2a1b 2020-07-23 stsp goto done;
2184 993e2a1b 2020-07-23 stsp }
2185 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2186 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2187 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2188 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2189 993e2a1b 2020-07-23 stsp if (err)
2190 993e2a1b 2020-07-23 stsp goto done;
2191 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2192 993e2a1b 2020-07-23 stsp ie->path);
2193 993e2a1b 2020-07-23 stsp goto done;
2194 993e2a1b 2020-07-23 stsp }
2195 993e2a1b 2020-07-23 stsp
2196 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2197 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2198 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2199 1ee397ad 2019-07-12 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 fc6346c4 2019-03-27 stsp /*
2202 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2203 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2204 fc6346c4 2019-03-27 stsp */
2205 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2206 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2207 fc6346c4 2019-03-27 stsp } else {
2208 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2209 1ee397ad 2019-07-12 stsp if (err)
2210 5a58a424 2020-06-23 stsp goto done;
2211 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2212 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2213 fc6346c4 2019-03-27 stsp if (err)
2214 5a58a424 2020-06-23 stsp goto done;
2215 fc6346c4 2019-03-27 stsp }
2216 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2217 708d8e67 2019-03-27 stsp }
2218 5a58a424 2020-06-23 stsp done:
2219 5a58a424 2020-06-23 stsp free(ondisk_path);
2220 fc6346c4 2019-03-27 stsp return err;
2221 708d8e67 2019-03-27 stsp }
2222 708d8e67 2019-03-27 stsp
2223 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2224 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2225 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2226 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2227 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2228 8da9e5f4 2019-01-12 stsp void *progress_arg;
2229 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2230 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2231 8da9e5f4 2019-01-12 stsp };
2232 8da9e5f4 2019-01-12 stsp
2233 512f0d0e 2019-01-02 stsp static const struct got_error *
2234 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2235 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2236 512f0d0e 2019-01-02 stsp {
2237 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2238 0584f854 2019-04-06 stsp
2239 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2241 512f0d0e 2019-01-02 stsp
2242 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2243 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2244 8da9e5f4 2019-01-12 stsp }
2245 512f0d0e 2019-01-02 stsp
2246 8da9e5f4 2019-01-12 stsp static const struct got_error *
2247 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2248 8da9e5f4 2019-01-12 stsp {
2249 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2250 7a9df742 2019-01-08 stsp
2251 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2252 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2253 0584f854 2019-04-06 stsp
2254 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2255 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2256 9d31a1d8 2018-03-11 stsp }
2257 9d31a1d8 2018-03-11 stsp
2258 9d31a1d8 2018-03-11 stsp static const struct got_error *
2259 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2260 9d31a1d8 2018-03-11 stsp {
2261 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2262 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2263 8da9e5f4 2019-01-12 stsp char *path;
2264 9d31a1d8 2018-03-11 stsp
2265 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2266 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2267 0584f854 2019-04-06 stsp
2268 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2269 63c5ca5d 2019-08-24 stsp return NULL;
2270 63c5ca5d 2019-08-24 stsp
2271 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2272 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2273 8da9e5f4 2019-01-12 stsp == -1)
2274 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2275 9d31a1d8 2018-03-11 stsp
2276 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2277 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2278 8da9e5f4 2019-01-12 stsp else
2279 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2280 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2281 9d31a1d8 2018-03-11 stsp
2282 8da9e5f4 2019-01-12 stsp free(path);
2283 0cd1c46a 2019-03-11 stsp return err;
2284 0cd1c46a 2019-03-11 stsp }
2285 0cd1c46a 2019-03-11 stsp
2286 b2118c49 2020-07-28 stsp const struct got_error *
2287 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2288 b2118c49 2020-07-28 stsp {
2289 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2290 b2118c49 2020-07-28 stsp
2291 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2292 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2293 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2294 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2295 b2118c49 2020-07-28 stsp }
2296 b2118c49 2020-07-28 stsp
2297 b2118c49 2020-07-28 stsp return NULL;
2298 b2118c49 2020-07-28 stsp }
2299 b2118c49 2020-07-28 stsp
2300 818c7501 2019-07-11 stsp static const struct got_error *
2301 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2302 0cd1c46a 2019-03-11 stsp {
2303 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2304 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2305 0cd1c46a 2019-03-11 stsp
2306 517bab73 2019-03-11 stsp *refname = NULL;
2307 517bab73 2019-03-11 stsp
2308 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2309 b2118c49 2020-07-28 stsp if (err)
2310 b2118c49 2020-07-28 stsp return err;
2311 0cd1c46a 2019-03-11 stsp
2312 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2313 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2314 0647c563 2019-03-11 stsp *refname = NULL;
2315 0cd1c46a 2019-03-11 stsp }
2316 517bab73 2019-03-11 stsp free(uuidstr);
2317 517bab73 2019-03-11 stsp return err;
2318 517bab73 2019-03-11 stsp }
2319 517bab73 2019-03-11 stsp
2320 818c7501 2019-07-11 stsp const struct got_error *
2321 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2322 9587e6cc 2023-01-28 mark const char *prefix)
2323 9587e6cc 2023-01-28 mark {
2324 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2325 9587e6cc 2023-01-28 mark }
2326 9587e6cc 2023-01-28 mark
2327 9587e6cc 2023-01-28 mark const struct got_error *
2328 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2329 818c7501 2019-07-11 stsp {
2330 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2331 818c7501 2019-07-11 stsp }
2332 818c7501 2019-07-11 stsp
2333 818c7501 2019-07-11 stsp static const struct got_error *
2334 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2335 818c7501 2019-07-11 stsp {
2336 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2337 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2338 818c7501 2019-07-11 stsp }
2339 818c7501 2019-07-11 stsp
2340 818c7501 2019-07-11 stsp static const struct got_error *
2341 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2342 818c7501 2019-07-11 stsp {
2343 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2344 818c7501 2019-07-11 stsp }
2345 818c7501 2019-07-11 stsp
2346 818c7501 2019-07-11 stsp static const struct got_error *
2347 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2348 818c7501 2019-07-11 stsp {
2349 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2350 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2351 818c7501 2019-07-11 stsp }
2352 818c7501 2019-07-11 stsp
2353 818c7501 2019-07-11 stsp static const struct got_error *
2354 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2355 818c7501 2019-07-11 stsp {
2356 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2357 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2358 0ebf8283 2019-07-24 stsp }
2359 0ebf8283 2019-07-24 stsp
2360 0ebf8283 2019-07-24 stsp static const struct got_error *
2361 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2362 0ebf8283 2019-07-24 stsp {
2363 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2364 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2365 0ebf8283 2019-07-24 stsp }
2366 0ebf8283 2019-07-24 stsp
2367 0ebf8283 2019-07-24 stsp static const struct got_error *
2368 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2369 0ebf8283 2019-07-24 stsp {
2370 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2371 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2372 0ebf8283 2019-07-24 stsp }
2373 0ebf8283 2019-07-24 stsp
2374 0ebf8283 2019-07-24 stsp static const struct got_error *
2375 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2376 0ebf8283 2019-07-24 stsp {
2377 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2378 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2379 818c7501 2019-07-11 stsp }
2380 818c7501 2019-07-11 stsp
2381 0ebf8283 2019-07-24 stsp static const struct got_error *
2382 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2383 0ebf8283 2019-07-24 stsp {
2384 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2385 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2386 0ebf8283 2019-07-24 stsp }
2387 818c7501 2019-07-11 stsp
2388 0ebf8283 2019-07-24 stsp const struct got_error *
2389 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2390 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2391 0ebf8283 2019-07-24 stsp {
2392 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2393 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2394 0ebf8283 2019-07-24 stsp *path = NULL;
2395 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2396 0ebf8283 2019-07-24 stsp }
2397 0ebf8283 2019-07-24 stsp return NULL;
2398 f259c4c1 2021-09-24 stsp }
2399 f259c4c1 2021-09-24 stsp
2400 f259c4c1 2021-09-24 stsp static const struct got_error *
2401 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2402 f259c4c1 2021-09-24 stsp {
2403 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2404 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2405 0ebf8283 2019-07-24 stsp }
2406 0ebf8283 2019-07-24 stsp
2407 f259c4c1 2021-09-24 stsp static const struct got_error *
2408 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2409 f259c4c1 2021-09-24 stsp {
2410 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2411 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2412 f259c4c1 2021-09-24 stsp }
2413 f259c4c1 2021-09-24 stsp
2414 517bab73 2019-03-11 stsp /*
2415 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2416 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2417 517bab73 2019-03-11 stsp */
2418 517bab73 2019-03-11 stsp static const struct got_error *
2419 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2420 517bab73 2019-03-11 stsp {
2421 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2422 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2423 517bab73 2019-03-11 stsp char *refname;
2424 517bab73 2019-03-11 stsp
2425 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2426 517bab73 2019-03-11 stsp if (err)
2427 517bab73 2019-03-11 stsp return err;
2428 0cd1c46a 2019-03-11 stsp
2429 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2430 0cd1c46a 2019-03-11 stsp if (err)
2431 0cd1c46a 2019-03-11 stsp goto done;
2432 0cd1c46a 2019-03-11 stsp
2433 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2434 0cd1c46a 2019-03-11 stsp done:
2435 0cd1c46a 2019-03-11 stsp free(refname);
2436 0cd1c46a 2019-03-11 stsp if (ref)
2437 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2438 3e3a69f1 2019-07-25 stsp return err;
2439 3e3a69f1 2019-07-25 stsp }
2440 3e3a69f1 2019-07-25 stsp
2441 3e3a69f1 2019-07-25 stsp static const struct got_error *
2442 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2443 3e3a69f1 2019-07-25 stsp {
2444 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2445 3e3a69f1 2019-07-25 stsp
2446 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2447 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2448 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2449 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2450 3e3a69f1 2019-07-25 stsp }
2451 9d31a1d8 2018-03-11 stsp return err;
2452 9d31a1d8 2018-03-11 stsp }
2453 9d31a1d8 2018-03-11 stsp
2454 3e3a69f1 2019-07-25 stsp
2455 ebf99748 2019-05-09 stsp static const struct got_error *
2456 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2457 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2458 ebf99748 2019-05-09 stsp {
2459 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2460 ebf99748 2019-05-09 stsp FILE *index = NULL;
2461 0cd1c46a 2019-03-11 stsp
2462 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2463 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2464 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2465 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2466 ebf99748 2019-05-09 stsp
2467 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2468 3e3a69f1 2019-07-25 stsp if (err)
2469 ebf99748 2019-05-09 stsp goto done;
2470 ebf99748 2019-05-09 stsp
2471 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2472 ebf99748 2019-05-09 stsp if (index == NULL) {
2473 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2474 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2475 ebf99748 2019-05-09 stsp } else {
2476 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2477 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2478 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2479 ebf99748 2019-05-09 stsp }
2480 ebf99748 2019-05-09 stsp done:
2481 ebf99748 2019-05-09 stsp if (err) {
2482 ebf99748 2019-05-09 stsp free(*fileindex_path);
2483 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2484 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2485 ebf99748 2019-05-09 stsp *fileindex = NULL;
2486 ebf99748 2019-05-09 stsp }
2487 ebf99748 2019-05-09 stsp return err;
2488 ebf99748 2019-05-09 stsp }
2489 c932eeeb 2019-05-22 stsp
2490 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2491 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2492 c932eeeb 2019-05-22 stsp const char *path;
2493 c932eeeb 2019-05-22 stsp size_t path_len;
2494 c932eeeb 2019-05-22 stsp const char *entry_name;
2495 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2496 a484d721 2019-06-10 stsp void *progress_arg;
2497 c932eeeb 2019-05-22 stsp };
2498 ebf99748 2019-05-09 stsp
2499 c932eeeb 2019-05-22 stsp static const struct got_error *
2500 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2501 c932eeeb 2019-05-22 stsp {
2502 1ee397ad 2019-07-12 stsp const struct got_error *err;
2503 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2504 c932eeeb 2019-05-22 stsp
2505 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2506 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2507 c932eeeb 2019-05-22 stsp return NULL;
2508 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2509 102ff934 2019-06-11 stsp return NULL;
2510 102ff934 2019-06-11 stsp
2511 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2512 a769b60b 2021-06-27 stsp return NULL;
2513 a769b60b 2021-06-27 stsp
2514 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2515 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2516 c932eeeb 2019-05-22 stsp return NULL;
2517 c932eeeb 2019-05-22 stsp
2518 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2519 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2520 edd02c5e 2019-07-11 stsp ie->path);
2521 1ee397ad 2019-07-12 stsp if (err)
2522 1ee397ad 2019-07-12 stsp return err;
2523 1ee397ad 2019-07-12 stsp }
2524 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2525 c932eeeb 2019-05-22 stsp return NULL;
2526 a615e0e7 2020-12-16 stsp }
2527 a615e0e7 2020-12-16 stsp
2528 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2529 a615e0e7 2020-12-16 stsp static const struct got_error *
2530 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2531 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2532 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2533 a615e0e7 2020-12-16 stsp {
2534 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2535 a615e0e7 2020-12-16 stsp
2536 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2537 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2538 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2539 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2540 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2541 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2542 a615e0e7 2020-12-16 stsp
2543 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2544 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2545 9c6338c4 2019-06-02 stsp }
2546 9c6338c4 2019-06-02 stsp
2547 9c6338c4 2019-06-02 stsp static const struct got_error *
2548 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2549 9c6338c4 2019-06-02 stsp {
2550 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2551 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2552 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2553 867630bb 2020-01-17 stsp struct timespec timeout;
2554 9c6338c4 2019-06-02 stsp
2555 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2556 b90054ed 2022-11-01 stsp fileindex_path, "");
2557 9c6338c4 2019-06-02 stsp if (err)
2558 9c6338c4 2019-06-02 stsp goto done;
2559 9c6338c4 2019-06-02 stsp
2560 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2561 9c6338c4 2019-06-02 stsp if (err)
2562 9c6338c4 2019-06-02 stsp goto done;
2563 9c6338c4 2019-06-02 stsp
2564 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2565 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2566 9c6338c4 2019-06-02 stsp fileindex_path);
2567 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2568 9c6338c4 2019-06-02 stsp }
2569 867630bb 2020-01-17 stsp
2570 867630bb 2020-01-17 stsp /*
2571 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2572 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2573 867630bb 2020-01-17 stsp * was recorded in the file index.
2574 867630bb 2020-01-17 stsp */
2575 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2576 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2577 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2578 9c6338c4 2019-06-02 stsp done:
2579 9c6338c4 2019-06-02 stsp if (new_index)
2580 9c6338c4 2019-06-02 stsp fclose(new_index);
2581 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2582 9c6338c4 2019-06-02 stsp return err;
2583 c932eeeb 2019-05-22 stsp }
2584 6ced4176 2019-07-12 stsp
2585 6ced4176 2019-07-12 stsp static const struct got_error *
2586 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2587 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2588 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2589 a44927cc 2022-04-07 stsp struct got_repository *repo)
2590 6ced4176 2019-07-12 stsp {
2591 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2592 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2593 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2594 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2595 6ced4176 2019-07-12 stsp
2596 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2597 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2598 6ced4176 2019-07-12 stsp *tree_id = NULL;
2599 6ced4176 2019-07-12 stsp
2600 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2601 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2602 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2603 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2604 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2605 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2606 6ced4176 2019-07-12 stsp goto done;
2607 6ced4176 2019-07-12 stsp }
2608 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2609 a44927cc 2022-04-07 stsp worktree->path_prefix);
2610 6ced4176 2019-07-12 stsp if (err)
2611 6ced4176 2019-07-12 stsp goto done;
2612 6ced4176 2019-07-12 stsp return NULL;
2613 6ced4176 2019-07-12 stsp }
2614 6ced4176 2019-07-12 stsp
2615 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2616 6ced4176 2019-07-12 stsp
2617 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2618 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2619 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2620 6ced4176 2019-07-12 stsp goto done;
2621 6ced4176 2019-07-12 stsp }
2622 6ced4176 2019-07-12 stsp
2623 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2624 6ced4176 2019-07-12 stsp if (err)
2625 6ced4176 2019-07-12 stsp goto done;
2626 6ced4176 2019-07-12 stsp
2627 6ced4176 2019-07-12 stsp free(in_repo_path);
2628 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2629 6ced4176 2019-07-12 stsp
2630 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2631 6ced4176 2019-07-12 stsp if (err)
2632 6ced4176 2019-07-12 stsp goto done;
2633 c932eeeb 2019-05-22 stsp
2634 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2635 6ced4176 2019-07-12 stsp /* Check out a single file. */
2636 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2637 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2638 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2639 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2640 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2641 6ced4176 2019-07-12 stsp goto done;
2642 6ced4176 2019-07-12 stsp }
2643 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2644 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2645 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2646 6ced4176 2019-07-12 stsp goto done;
2647 6ced4176 2019-07-12 stsp }
2648 6ced4176 2019-07-12 stsp } else {
2649 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2650 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2651 6ced4176 2019-07-12 stsp if (err)
2652 6ced4176 2019-07-12 stsp return err;
2653 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2654 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2655 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2656 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2657 6ced4176 2019-07-12 stsp goto done;
2658 6ced4176 2019-07-12 stsp }
2659 6ced4176 2019-07-12 stsp }
2660 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2661 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2662 6ced4176 2019-07-12 stsp } else {
2663 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2664 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2665 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2670 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2671 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2672 6ced4176 2019-07-12 stsp goto done;
2673 6ced4176 2019-07-12 stsp }
2674 6ced4176 2019-07-12 stsp }
2675 6ced4176 2019-07-12 stsp done:
2676 6ced4176 2019-07-12 stsp free(id);
2677 6ced4176 2019-07-12 stsp free(in_repo_path);
2678 6ced4176 2019-07-12 stsp if (err) {
2679 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2680 6ced4176 2019-07-12 stsp free(*tree_relpath);
2681 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2682 6ced4176 2019-07-12 stsp free(*tree_id);
2683 6ced4176 2019-07-12 stsp *tree_id = NULL;
2684 6ced4176 2019-07-12 stsp }
2685 07ed472d 2019-07-12 stsp return err;
2686 07ed472d 2019-07-12 stsp }
2687 07ed472d 2019-07-12 stsp
2688 07ed472d 2019-07-12 stsp static const struct got_error *
2689 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2690 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2691 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2692 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2693 07ed472d 2019-07-12 stsp {
2694 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2695 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2696 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2697 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2698 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2699 07ed472d 2019-07-12 stsp
2700 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2701 7f47418f 2019-12-20 stsp if (err) {
2702 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2703 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2704 7f47418f 2019-12-20 stsp goto done;
2705 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2706 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2707 7f47418f 2019-12-20 stsp if (err)
2708 7f47418f 2019-12-20 stsp return err;
2709 7f47418f 2019-12-20 stsp }
2710 07ed472d 2019-07-12 stsp
2711 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2712 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2713 07ed472d 2019-07-12 stsp if (err)
2714 07ed472d 2019-07-12 stsp goto done;
2715 07ed472d 2019-07-12 stsp
2716 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2717 07ed472d 2019-07-12 stsp if (err)
2718 07ed472d 2019-07-12 stsp goto done;
2719 07ed472d 2019-07-12 stsp
2720 07ed472d 2019-07-12 stsp if (entry_name &&
2721 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2722 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2723 07ed472d 2019-07-12 stsp goto done;
2724 07ed472d 2019-07-12 stsp }
2725 07ed472d 2019-07-12 stsp
2726 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2727 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2728 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2729 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2730 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2731 07ed472d 2019-07-12 stsp arg.repo = repo;
2732 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2733 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2734 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2735 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2736 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2737 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2738 07ed472d 2019-07-12 stsp done:
2739 07ed472d 2019-07-12 stsp if (tree)
2740 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2741 07ed472d 2019-07-12 stsp if (commit)
2742 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2743 6ced4176 2019-07-12 stsp return err;
2744 6ced4176 2019-07-12 stsp }
2745 6ced4176 2019-07-12 stsp
2746 9d31a1d8 2018-03-11 stsp const struct got_error *
2747 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2748 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2749 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2750 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2751 9d31a1d8 2018-03-11 stsp {
2752 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2753 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2754 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2755 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2756 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2757 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2758 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2759 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2760 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2761 f2ea84fa 2019-07-27 stsp int entry_type;
2762 f2ea84fa 2019-07-27 stsp char *relpath;
2763 f2ea84fa 2019-07-27 stsp char *entry_name;
2764 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2765 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2766 9d31a1d8 2018-03-11 stsp
2767 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2768 f2ea84fa 2019-07-27 stsp
2769 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2770 9d31a1d8 2018-03-11 stsp if (err)
2771 9d31a1d8 2018-03-11 stsp return err;
2772 a44927cc 2022-04-07 stsp
2773 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2774 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2775 a44927cc 2022-04-07 stsp if (err)
2776 a44927cc 2022-04-07 stsp goto done;
2777 93a30277 2018-12-24 stsp
2778 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2779 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2780 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2781 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2782 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2783 f2ea84fa 2019-07-27 stsp goto done;
2784 f2ea84fa 2019-07-27 stsp }
2785 6ced4176 2019-07-12 stsp
2786 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2787 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2788 a44927cc 2022-04-07 stsp worktree, repo);
2789 f2ea84fa 2019-07-27 stsp if (err) {
2790 f2ea84fa 2019-07-27 stsp free(tpd);
2791 6ced4176 2019-07-12 stsp goto done;
2792 6ced4176 2019-07-12 stsp }
2793 f2ea84fa 2019-07-27 stsp
2794 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2795 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2796 f2ea84fa 2019-07-27 stsp if (err) {
2797 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2798 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2799 f2ea84fa 2019-07-27 stsp free(tpd);
2800 f2ea84fa 2019-07-27 stsp goto done;
2801 f2ea84fa 2019-07-27 stsp }
2802 f2ea84fa 2019-07-27 stsp } else
2803 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2804 f2ea84fa 2019-07-27 stsp
2805 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2806 6ced4176 2019-07-12 stsp }
2807 6ced4176 2019-07-12 stsp
2808 51514078 2018-12-25 stsp /*
2809 51514078 2018-12-25 stsp * Read the file index.
2810 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2811 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2812 51514078 2018-12-25 stsp */
2813 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2814 8da9e5f4 2019-01-12 stsp if (err)
2815 c4cdcb68 2019-04-03 stsp goto done;
2816 c4cdcb68 2019-04-03 stsp
2817 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2818 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2819 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2820 f2ea84fa 2019-07-27 stsp
2821 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2822 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2823 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2824 f2ea84fa 2019-07-27 stsp if (err)
2825 f2ea84fa 2019-07-27 stsp break;
2826 f2ea84fa 2019-07-27 stsp
2827 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2828 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2829 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2830 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2831 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2832 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2833 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2834 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2835 f2ea84fa 2019-07-27 stsp if (err)
2836 f2ea84fa 2019-07-27 stsp break;
2837 f2ea84fa 2019-07-27 stsp
2838 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2839 711d9cd9 2019-07-12 stsp }
2840 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2841 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2842 af12c6b9 2019-06-04 stsp err = sync_err;
2843 9d31a1d8 2018-03-11 stsp done:
2844 9c6338c4 2019-06-02 stsp free(fileindex_path);
2845 edfa7d7f 2018-09-11 stsp if (tree)
2846 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2847 9d31a1d8 2018-03-11 stsp if (commit)
2848 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2849 5ade8233 2019-07-12 stsp if (fileindex)
2850 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2851 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2852 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2853 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2854 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2855 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2856 f2ea84fa 2019-07-27 stsp free(tpd);
2857 f2ea84fa 2019-07-27 stsp }
2858 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2859 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2860 9d31a1d8 2018-03-11 stsp err = unlockerr;
2861 f8d1f275 2019-02-04 stsp return err;
2862 f8d1f275 2019-02-04 stsp }
2863 f8d1f275 2019-02-04 stsp
2864 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2865 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2866 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2867 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2868 234035bc 2019-06-01 stsp void *progress_arg;
2869 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2870 234035bc 2019-06-01 stsp void *cancel_arg;
2871 f69721c3 2019-10-21 stsp const char *label_orig;
2872 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2873 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2874 234035bc 2019-06-01 stsp };
2875 234035bc 2019-06-01 stsp
2876 234035bc 2019-06-01 stsp static const struct got_error *
2877 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2878 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2879 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2880 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2881 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2882 234035bc 2019-06-01 stsp {
2883 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2884 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2885 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2886 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2887 234035bc 2019-06-01 stsp struct stat sb;
2888 234035bc 2019-06-01 stsp unsigned char status;
2889 234035bc 2019-06-01 stsp int local_changes_subsumed;
2890 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2891 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2892 234035bc 2019-06-01 stsp
2893 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2894 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2895 d6c87207 2019-08-02 stsp strlen(path2));
2896 1ee397ad 2019-07-12 stsp if (ie == NULL)
2897 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2898 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2899 234035bc 2019-06-01 stsp
2900 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2901 234035bc 2019-06-01 stsp path2) == -1)
2902 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2903 234035bc 2019-06-01 stsp
2904 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2905 7f91a133 2019-12-13 stsp repo);
2906 234035bc 2019-06-01 stsp if (err)
2907 234035bc 2019-06-01 stsp goto done;
2908 234035bc 2019-06-01 stsp
2909 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2910 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2911 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2912 234035bc 2019-06-01 stsp goto done;
2913 234035bc 2019-06-01 stsp }
2914 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2915 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2916 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2917 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2918 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2919 234035bc 2019-06-01 stsp goto done;
2920 234035bc 2019-06-01 stsp }
2921 234035bc 2019-06-01 stsp
2922 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2923 36bf999c 2020-07-23 stsp char *link_target2;
2924 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2925 36bf999c 2020-07-23 stsp if (err)
2926 36bf999c 2020-07-23 stsp goto done;
2927 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2928 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2929 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2930 36bf999c 2020-07-23 stsp free(link_target2);
2931 af57b12a 2020-07-23 stsp } else {
2932 1af628f4 2021-06-03 stsp int fd;
2933 1af628f4 2021-06-03 stsp
2934 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2935 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2936 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2937 1af628f4 2021-06-03 stsp goto done;
2938 1af628f4 2021-06-03 stsp }
2939 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2940 1af628f4 2021-06-03 stsp f_orig, blob1);
2941 1af628f4 2021-06-03 stsp if (err)
2942 1af628f4 2021-06-03 stsp goto done;
2943 1af628f4 2021-06-03 stsp
2944 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2945 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2946 1af628f4 2021-06-03 stsp goto done;
2947 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2948 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2949 1af628f4 2021-06-03 stsp if (err)
2950 1af628f4 2021-06-03 stsp goto done;
2951 1af628f4 2021-06-03 stsp
2952 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2953 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2954 1af628f4 2021-06-03 stsp if (fd == -1) {
2955 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2956 1af628f4 2021-06-03 stsp ondisk_path);
2957 1af628f4 2021-06-03 stsp goto done;
2958 1af628f4 2021-06-03 stsp }
2959 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2960 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2961 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2962 1af628f4 2021-06-03 stsp ondisk_path);
2963 1af628f4 2021-06-03 stsp close(fd);
2964 1af628f4 2021-06-03 stsp goto done;
2965 1af628f4 2021-06-03 stsp }
2966 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2967 1af628f4 2021-06-03 stsp if (err)
2968 1af628f4 2021-06-03 stsp goto done;
2969 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2970 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2971 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2972 1af628f4 2021-06-03 stsp goto done;
2973 1af628f4 2021-06-03 stsp }
2974 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2975 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2976 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
2977 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2978 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2979 af57b12a 2020-07-23 stsp }
2980 234035bc 2019-06-01 stsp } else if (blob1) {
2981 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2982 d6c87207 2019-08-02 stsp strlen(path1));
2983 1ee397ad 2019-07-12 stsp if (ie == NULL)
2984 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2985 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2986 2b92fad7 2019-06-02 stsp
2987 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2988 2b92fad7 2019-06-02 stsp path1) == -1)
2989 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2990 2b92fad7 2019-06-02 stsp
2991 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2992 7f91a133 2019-12-13 stsp repo);
2993 2b92fad7 2019-06-02 stsp if (err)
2994 2b92fad7 2019-06-02 stsp goto done;
2995 2b92fad7 2019-06-02 stsp
2996 2b92fad7 2019-06-02 stsp switch (status) {
2997 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2998 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2999 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3000 1ee397ad 2019-07-12 stsp if (err)
3001 1ee397ad 2019-07-12 stsp goto done;
3002 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3003 2b92fad7 2019-06-02 stsp if (err)
3004 2b92fad7 2019-06-02 stsp goto done;
3005 2b92fad7 2019-06-02 stsp if (ie)
3006 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3007 2b92fad7 2019-06-02 stsp break;
3008 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3009 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3010 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3011 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3012 1ee397ad 2019-07-12 stsp if (err)
3013 1ee397ad 2019-07-12 stsp goto done;
3014 2b92fad7 2019-06-02 stsp if (ie)
3015 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3016 2b92fad7 2019-06-02 stsp break;
3017 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3018 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3019 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3020 72840534 2022-01-19 stsp off_t blob1_size;
3021 0a22ca1a 2020-09-23 stsp /*
3022 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3023 0a22ca1a 2020-09-23 stsp * exists in the repository.
3024 0a22ca1a 2020-09-23 stsp */
3025 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3026 72840534 2022-01-19 stsp &blob1_size, path1);
3027 0a22ca1a 2020-09-23 stsp if (err)
3028 0a22ca1a 2020-09-23 stsp goto done;
3029 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3030 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3031 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3032 0a22ca1a 2020-09-23 stsp if (err)
3033 0a22ca1a 2020-09-23 stsp goto done;
3034 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3035 0a22ca1a 2020-09-23 stsp path1);
3036 0a22ca1a 2020-09-23 stsp if (err)
3037 0a22ca1a 2020-09-23 stsp goto done;
3038 0a22ca1a 2020-09-23 stsp if (ie)
3039 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3040 0a22ca1a 2020-09-23 stsp ie);
3041 0a22ca1a 2020-09-23 stsp } else {
3042 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3043 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3044 0a22ca1a 2020-09-23 stsp }
3045 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3046 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3047 0a22ca1a 2020-09-23 stsp free(id);
3048 0a22ca1a 2020-09-23 stsp if (err)
3049 0a22ca1a 2020-09-23 stsp goto done;
3050 0a22ca1a 2020-09-23 stsp break;
3051 0a22ca1a 2020-09-23 stsp }
3052 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3053 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3054 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3055 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3056 1ee397ad 2019-07-12 stsp if (err)
3057 1ee397ad 2019-07-12 stsp goto done;
3058 2b92fad7 2019-06-02 stsp break;
3059 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3060 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3061 1ee397ad 2019-07-12 stsp if (err)
3062 1ee397ad 2019-07-12 stsp goto done;
3063 2b92fad7 2019-06-02 stsp break;
3064 2b92fad7 2019-06-02 stsp default:
3065 2b92fad7 2019-06-02 stsp break;
3066 2b92fad7 2019-06-02 stsp }
3067 234035bc 2019-06-01 stsp } else if (blob2) {
3068 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3069 234035bc 2019-06-01 stsp path2) == -1)
3070 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3071 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3072 d6c87207 2019-08-02 stsp strlen(path2));
3073 234035bc 2019-06-01 stsp if (ie) {
3074 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3075 7f91a133 2019-12-13 stsp -1, NULL, repo);
3076 234035bc 2019-06-01 stsp if (err)
3077 234035bc 2019-06-01 stsp goto done;
3078 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3079 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3080 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3081 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3082 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3083 1ee397ad 2019-07-12 stsp status, path2);
3084 234035bc 2019-06-01 stsp goto done;
3085 234035bc 2019-06-01 stsp }
3086 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3087 36bf999c 2020-07-23 stsp char *link_target2;
3088 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3089 36bf999c 2020-07-23 stsp blob2);
3090 36bf999c 2020-07-23 stsp if (err)
3091 36bf999c 2020-07-23 stsp goto done;
3092 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3093 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3094 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3095 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3096 36bf999c 2020-07-23 stsp free(link_target2);
3097 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3098 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3099 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3100 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3101 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3102 526a746f 2020-07-23 stsp a->progress_arg);
3103 dfe9fba0 2020-07-23 stsp } else {
3104 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3105 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3106 526a746f 2020-07-23 stsp }
3107 dfe9fba0 2020-07-23 stsp if (err)
3108 dfe9fba0 2020-07-23 stsp goto done;
3109 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3110 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3111 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3112 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3113 234035bc 2019-06-01 stsp if (err)
3114 234035bc 2019-06-01 stsp goto done;
3115 234035bc 2019-06-01 stsp }
3116 234035bc 2019-06-01 stsp } else {
3117 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3118 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3119 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3120 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3121 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3122 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3123 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3124 2e1fa222 2020-07-23 stsp } else {
3125 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3126 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3127 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3128 2e1fa222 2020-07-23 stsp }
3129 234035bc 2019-06-01 stsp if (err)
3130 234035bc 2019-06-01 stsp goto done;
3131 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3132 234035bc 2019-06-01 stsp if (err)
3133 234035bc 2019-06-01 stsp goto done;
3134 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3135 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3136 3969253a 2020-03-07 stsp if (err) {
3137 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3138 3969253a 2020-03-07 stsp goto done;
3139 3969253a 2020-03-07 stsp }
3140 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3141 2b92fad7 2019-06-02 stsp if (err) {
3142 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3143 2b92fad7 2019-06-02 stsp goto done;
3144 2b92fad7 2019-06-02 stsp }
3145 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3146 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3147 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3148 2e1fa222 2020-07-23 stsp }
3149 234035bc 2019-06-01 stsp }
3150 234035bc 2019-06-01 stsp }
3151 234035bc 2019-06-01 stsp done:
3152 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3153 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3154 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3155 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3156 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3157 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3158 1af628f4 2021-06-03 stsp free(id_str);
3159 54d5be07 2021-06-03 stsp free(label_deriv2);
3160 234035bc 2019-06-01 stsp free(ondisk_path);
3161 234035bc 2019-06-01 stsp return err;
3162 234035bc 2019-06-01 stsp }
3163 234035bc 2019-06-01 stsp
3164 69de9dd4 2021-09-03 stsp static const struct got_error *
3165 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3166 69de9dd4 2021-09-03 stsp {
3167 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3168 69de9dd4 2021-09-03 stsp
3169 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3170 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3171 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3172 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3173 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3174 69de9dd4 2021-09-03 stsp
3175 69de9dd4 2021-09-03 stsp return NULL;
3176 69de9dd4 2021-09-03 stsp }
3177 69de9dd4 2021-09-03 stsp
3178 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3179 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3180 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3181 234035bc 2019-06-01 stsp struct got_repository *repo;
3182 234035bc 2019-06-01 stsp };
3183 234035bc 2019-06-01 stsp
3184 234035bc 2019-06-01 stsp static const struct got_error *
3185 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3186 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3187 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3188 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3189 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3190 234035bc 2019-06-01 stsp {
3191 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3192 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3193 234035bc 2019-06-01 stsp unsigned char status;
3194 234035bc 2019-06-01 stsp struct stat sb;
3195 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3196 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3197 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3198 234035bc 2019-06-01 stsp char *ondisk_path;
3199 234035bc 2019-06-01 stsp
3200 69de9dd4 2021-09-03 stsp if (id == NULL)
3201 69de9dd4 2021-09-03 stsp return NULL;
3202 234035bc 2019-06-01 stsp
3203 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3204 69de9dd4 2021-09-03 stsp if (ie == NULL)
3205 69de9dd4 2021-09-03 stsp return NULL;
3206 69de9dd4 2021-09-03 stsp
3207 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3208 234035bc 2019-06-01 stsp == -1)
3209 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3210 234035bc 2019-06-01 stsp
3211 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3212 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3213 5546d466 2021-09-02 stsp free(ondisk_path);
3214 234035bc 2019-06-01 stsp if (err)
3215 234035bc 2019-06-01 stsp return err;
3216 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3217 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3218 234035bc 2019-06-01 stsp
3219 234035bc 2019-06-01 stsp return NULL;
3220 234035bc 2019-06-01 stsp }
3221 234035bc 2019-06-01 stsp
3222 818c7501 2019-07-11 stsp static const struct got_error *
3223 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3224 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3225 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3226 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3227 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3228 234035bc 2019-06-01 stsp {
3229 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3230 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3231 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3232 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3233 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3234 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3235 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3236 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3237 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3238 234035bc 2019-06-01 stsp
3239 03415a1a 2019-06-02 stsp if (commit_id1) {
3240 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3241 a44927cc 2022-04-07 stsp if (err)
3242 a44927cc 2022-04-07 stsp goto done;
3243 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3244 03415a1a 2019-06-02 stsp worktree->path_prefix);
3245 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3246 03415a1a 2019-06-02 stsp goto done;
3247 69d57f3d 2020-07-31 stsp }
3248 69d57f3d 2020-07-31 stsp if (tree_id1) {
3249 69d57f3d 2020-07-31 stsp char *id_str;
3250 03415a1a 2019-06-02 stsp
3251 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3252 03415a1a 2019-06-02 stsp if (err)
3253 03415a1a 2019-06-02 stsp goto done;
3254 f69721c3 2019-10-21 stsp
3255 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3256 f69721c3 2019-10-21 stsp if (err)
3257 f69721c3 2019-10-21 stsp goto done;
3258 f69721c3 2019-10-21 stsp
3259 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3260 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3261 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3262 f69721c3 2019-10-21 stsp free(id_str);
3263 f69721c3 2019-10-21 stsp goto done;
3264 f69721c3 2019-10-21 stsp }
3265 f69721c3 2019-10-21 stsp free(id_str);
3266 b72706c3 2022-06-01 stsp
3267 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3268 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3269 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3270 b72706c3 2022-06-01 stsp goto done;
3271 b72706c3 2022-06-01 stsp }
3272 03415a1a 2019-06-02 stsp }
3273 234035bc 2019-06-01 stsp
3274 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3275 a44927cc 2022-04-07 stsp if (err)
3276 a44927cc 2022-04-07 stsp goto done;
3277 a44927cc 2022-04-07 stsp
3278 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3279 234035bc 2019-06-01 stsp worktree->path_prefix);
3280 234035bc 2019-06-01 stsp if (err)
3281 234035bc 2019-06-01 stsp goto done;
3282 234035bc 2019-06-01 stsp
3283 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3284 234035bc 2019-06-01 stsp if (err)
3285 234035bc 2019-06-01 stsp goto done;
3286 234035bc 2019-06-01 stsp
3287 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3288 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3289 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3290 f9d37699 2022-06-28 stsp goto done;
3291 f9d37699 2022-06-28 stsp }
3292 f9d37699 2022-06-28 stsp
3293 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3294 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3295 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3296 b72706c3 2022-06-01 stsp goto done;
3297 b72706c3 2022-06-01 stsp }
3298 b72706c3 2022-06-01 stsp
3299 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3300 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3301 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3302 f9d37699 2022-06-28 stsp goto done;
3303 f9d37699 2022-06-28 stsp }
3304 f9d37699 2022-06-28 stsp
3305 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3306 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3307 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3308 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3309 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3310 69de9dd4 2021-09-03 stsp if (err)
3311 69de9dd4 2021-09-03 stsp goto done;
3312 69de9dd4 2021-09-03 stsp
3313 234035bc 2019-06-01 stsp arg.worktree = worktree;
3314 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3315 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3316 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3317 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3318 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3319 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3320 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3321 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3322 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3323 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3324 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3325 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3326 af12c6b9 2019-06-04 stsp err = sync_err;
3327 234035bc 2019-06-01 stsp done:
3328 a44927cc 2022-04-07 stsp if (commit1)
3329 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3330 a44927cc 2022-04-07 stsp if (commit2)
3331 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3332 234035bc 2019-06-01 stsp if (tree1)
3333 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3334 234035bc 2019-06-01 stsp if (tree2)
3335 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3336 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3337 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3338 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3339 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3340 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3341 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3342 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3343 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3344 f69721c3 2019-10-21 stsp free(label_orig);
3345 818c7501 2019-07-11 stsp return err;
3346 818c7501 2019-07-11 stsp }
3347 234035bc 2019-06-01 stsp
3348 818c7501 2019-07-11 stsp const struct got_error *
3349 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3350 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3351 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3352 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3353 818c7501 2019-07-11 stsp {
3354 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3355 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3356 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3357 818c7501 2019-07-11 stsp
3358 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3359 818c7501 2019-07-11 stsp if (err)
3360 818c7501 2019-07-11 stsp return err;
3361 818c7501 2019-07-11 stsp
3362 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3363 818c7501 2019-07-11 stsp if (err)
3364 818c7501 2019-07-11 stsp goto done;
3365 818c7501 2019-07-11 stsp
3366 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3367 69de9dd4 2021-09-03 stsp worktree);
3368 818c7501 2019-07-11 stsp if (err)
3369 818c7501 2019-07-11 stsp goto done;
3370 818c7501 2019-07-11 stsp
3371 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3372 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3373 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3374 818c7501 2019-07-11 stsp done:
3375 818c7501 2019-07-11 stsp if (fileindex)
3376 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3377 818c7501 2019-07-11 stsp free(fileindex_path);
3378 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3379 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3380 234035bc 2019-06-01 stsp err = unlockerr;
3381 234035bc 2019-06-01 stsp return err;
3382 234035bc 2019-06-01 stsp }
3383 234035bc 2019-06-01 stsp
3384 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3385 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3386 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3387 927df6b7 2019-02-10 stsp const char *status_path;
3388 927df6b7 2019-02-10 stsp size_t status_path_len;
3389 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3390 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3391 f8d1f275 2019-02-04 stsp void *status_arg;
3392 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3393 f8d1f275 2019-02-04 stsp void *cancel_arg;
3394 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3395 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3396 f2a9dc41 2019-12-13 tracey int report_unchanged;
3397 3143d852 2020-06-25 stsp int no_ignores;
3398 f8d1f275 2019-02-04 stsp };
3399 88d0e355 2019-08-03 stsp
3400 f8d1f275 2019-02-04 stsp static const struct got_error *
3401 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3402 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3403 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3404 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3405 927df6b7 2019-02-10 stsp {
3406 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3407 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3408 2c4740ad 2023-02-12 mark unsigned char staged_status;
3409 927df6b7 2019-02-10 stsp struct stat sb;
3410 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3411 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3412 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3413 927df6b7 2019-02-10 stsp
3414 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3415 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3416 98eaaa12 2019-08-03 stsp if (err)
3417 98eaaa12 2019-08-03 stsp return err;
3418 98eaaa12 2019-08-03 stsp
3419 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3420 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3421 98eaaa12 2019-08-03 stsp return NULL;
3422 98eaaa12 2019-08-03 stsp
3423 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3424 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3425 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3426 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3427 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3428 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3429 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3430 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3431 98eaaa12 2019-08-03 stsp }
3432 98eaaa12 2019-08-03 stsp
3433 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3434 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3435 927df6b7 2019-02-10 stsp }
3436 927df6b7 2019-02-10 stsp
3437 927df6b7 2019-02-10 stsp static const struct got_error *
3438 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3439 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3440 f8d1f275 2019-02-04 stsp {
3441 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3442 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3443 f8d1f275 2019-02-04 stsp char *abspath;
3444 f8d1f275 2019-02-04 stsp
3445 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3446 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3447 0584f854 2019-04-06 stsp
3448 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3449 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3450 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3451 927df6b7 2019-02-10 stsp return NULL;
3452 927df6b7 2019-02-10 stsp
3453 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3454 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3455 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3456 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3457 f8d1f275 2019-02-04 stsp } else {
3458 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3459 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3460 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3461 f8d1f275 2019-02-04 stsp }
3462 f8d1f275 2019-02-04 stsp
3463 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3464 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3465 f8d1f275 2019-02-04 stsp free(abspath);
3466 9d31a1d8 2018-03-11 stsp return err;
3467 9d31a1d8 2018-03-11 stsp }
3468 f8d1f275 2019-02-04 stsp
3469 f8d1f275 2019-02-04 stsp static const struct got_error *
3470 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3471 f8d1f275 2019-02-04 stsp {
3472 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3473 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3474 2ec1f75b 2019-03-26 stsp unsigned char status;
3475 927df6b7 2019-02-10 stsp
3476 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3477 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3478 0584f854 2019-04-06 stsp
3479 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3480 927df6b7 2019-02-10 stsp return NULL;
3481 927df6b7 2019-02-10 stsp
3482 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3483 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3484 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3485 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3486 2ec1f75b 2019-03-26 stsp else
3487 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3488 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3489 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3490 6841da00 2019-08-08 stsp }
3491 6841da00 2019-08-08 stsp
3492 336075a4 2022-06-25 op static void
3493 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3494 6841da00 2019-08-08 stsp {
3495 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3496 6841da00 2019-08-08 stsp
3497 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3498 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3499 d8bacb93 2023-01-10 mark
3500 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3501 6841da00 2019-08-08 stsp }
3502 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3503 6841da00 2019-08-08 stsp }
3504 6841da00 2019-08-08 stsp
3505 6841da00 2019-08-08 stsp static const struct got_error *
3506 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3507 6841da00 2019-08-08 stsp {
3508 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3509 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3510 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3511 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3512 6841da00 2019-08-08 stsp size_t linesize = 0;
3513 6841da00 2019-08-08 stsp ssize_t linelen;
3514 6841da00 2019-08-08 stsp
3515 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3516 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3517 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3518 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3519 6841da00 2019-08-08 stsp
3520 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3521 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3522 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3523 bd8de430 2019-10-04 stsp
3524 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3525 bd8de430 2019-10-04 stsp if (line[0] == '#')
3526 bd8de430 2019-10-04 stsp continue;
3527 bd8de430 2019-10-04 stsp
3528 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3529 bd8de430 2019-10-04 stsp if (line[0] == '!')
3530 bd8de430 2019-10-04 stsp continue;
3531 bd8de430 2019-10-04 stsp
3532 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3533 6841da00 2019-08-08 stsp line) == -1) {
3534 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3535 6841da00 2019-08-08 stsp goto done;
3536 6841da00 2019-08-08 stsp }
3537 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3538 6841da00 2019-08-08 stsp if (err)
3539 6841da00 2019-08-08 stsp goto done;
3540 6841da00 2019-08-08 stsp }
3541 6841da00 2019-08-08 stsp if (ferror(f)) {
3542 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3543 6841da00 2019-08-08 stsp goto done;
3544 6841da00 2019-08-08 stsp }
3545 6841da00 2019-08-08 stsp
3546 6841da00 2019-08-08 stsp dirpath = strdup(path);
3547 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3548 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3549 6841da00 2019-08-08 stsp goto done;
3550 6841da00 2019-08-08 stsp }
3551 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3552 6841da00 2019-08-08 stsp done:
3553 6841da00 2019-08-08 stsp free(line);
3554 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3555 6841da00 2019-08-08 stsp free(dirpath);
3556 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3557 6841da00 2019-08-08 stsp }
3558 6841da00 2019-08-08 stsp return err;
3559 6841da00 2019-08-08 stsp }
3560 6841da00 2019-08-08 stsp
3561 336075a4 2022-06-25 op static int
3562 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3563 6841da00 2019-08-08 stsp {
3564 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3565 bd8de430 2019-10-04 stsp
3566 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3567 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3568 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3569 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3570 bd8de430 2019-10-04 stsp
3571 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3572 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3573 6841da00 2019-08-08 stsp
3574 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3575 bd8de430 2019-10-04 stsp continue;
3576 bd8de430 2019-10-04 stsp pattern += 3;
3577 bd8de430 2019-10-04 stsp p = path;
3578 bd8de430 2019-10-04 stsp while (*p) {
3579 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3580 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3581 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3582 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3583 bd8de430 2019-10-04 stsp p++;
3584 bd8de430 2019-10-04 stsp while (*p == '/')
3585 bd8de430 2019-10-04 stsp p++;
3586 bd8de430 2019-10-04 stsp continue;
3587 bd8de430 2019-10-04 stsp }
3588 bd8de430 2019-10-04 stsp return 1;
3589 bd8de430 2019-10-04 stsp }
3590 bd8de430 2019-10-04 stsp }
3591 bd8de430 2019-10-04 stsp }
3592 bd8de430 2019-10-04 stsp
3593 6841da00 2019-08-08 stsp /*
3594 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3595 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3596 6841da00 2019-08-08 stsp * ignores backwards.
3597 6841da00 2019-08-08 stsp */
3598 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3599 6841da00 2019-08-08 stsp while (pe) {
3600 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3601 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3602 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3603 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3604 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3605 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3606 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3607 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3608 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3609 6841da00 2019-08-08 stsp continue;
3610 6841da00 2019-08-08 stsp return 1;
3611 6841da00 2019-08-08 stsp }
3612 6841da00 2019-08-08 stsp }
3613 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3614 6841da00 2019-08-08 stsp }
3615 6841da00 2019-08-08 stsp
3616 6841da00 2019-08-08 stsp return 0;
3617 6841da00 2019-08-08 stsp }
3618 6841da00 2019-08-08 stsp
3619 6841da00 2019-08-08 stsp static const struct got_error *
3620 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3621 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3622 6841da00 2019-08-08 stsp {
3623 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3624 6841da00 2019-08-08 stsp char *ignorespath;
3625 886cec17 2019-12-15 stsp int fd = -1;
3626 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3627 6841da00 2019-08-08 stsp
3628 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3629 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3630 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3631 6841da00 2019-08-08 stsp
3632 886cec17 2019-12-15 stsp if (dirfd != -1) {
3633 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3634 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3635 886cec17 2019-12-15 stsp if (fd == -1) {
3636 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3637 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3638 886cec17 2019-12-15 stsp ignorespath);
3639 886cec17 2019-12-15 stsp } else {
3640 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3641 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3642 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3643 886cec17 2019-12-15 stsp ignorespath);
3644 886cec17 2019-12-15 stsp else {
3645 886cec17 2019-12-15 stsp fd = -1;
3646 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3647 886cec17 2019-12-15 stsp }
3648 886cec17 2019-12-15 stsp }
3649 886cec17 2019-12-15 stsp } else {
3650 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3651 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3652 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3653 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3654 886cec17 2019-12-15 stsp ignorespath);
3655 886cec17 2019-12-15 stsp } else
3656 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3657 886cec17 2019-12-15 stsp }
3658 6841da00 2019-08-08 stsp
3659 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3660 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3661 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3662 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3663 6841da00 2019-08-08 stsp free(ignorespath);
3664 6841da00 2019-08-08 stsp return err;
3665 f8d1f275 2019-02-04 stsp }
3666 f8d1f275 2019-02-04 stsp
3667 f8d1f275 2019-02-04 stsp static const struct got_error *
3668 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3669 62da3196 2021-10-01 stsp int dirfd)
3670 f8d1f275 2019-02-04 stsp {
3671 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3672 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3673 f8d1f275 2019-02-04 stsp char *path = NULL;
3674 62da3196 2021-10-01 stsp
3675 62da3196 2021-10-01 stsp if (ignore != NULL)
3676 62da3196 2021-10-01 stsp *ignore = 0;
3677 f8d1f275 2019-02-04 stsp
3678 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3679 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3680 0584f854 2019-04-06 stsp
3681 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3682 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3683 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3684 f8d1f275 2019-02-04 stsp } else {
3685 f8d1f275 2019-02-04 stsp path = de->d_name;
3686 f8d1f275 2019-02-04 stsp }
3687 f8d1f275 2019-02-04 stsp
3688 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3689 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3690 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3691 62da3196 2021-10-01 stsp *ignore = 1;
3692 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3693 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3694 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3695 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3696 f8d1f275 2019-02-04 stsp if (parent_path[0])
3697 f8d1f275 2019-02-04 stsp free(path);
3698 b72f483a 2019-02-05 stsp return err;
3699 f8d1f275 2019-02-04 stsp }
3700 f8d1f275 2019-02-04 stsp
3701 347d1d3e 2019-07-12 stsp static const struct got_error *
3702 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3703 3143d852 2020-06-25 stsp {
3704 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3705 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3706 3143d852 2020-06-25 stsp
3707 3143d852 2020-06-25 stsp if (a->no_ignores)
3708 3143d852 2020-06-25 stsp return NULL;
3709 3143d852 2020-06-25 stsp
3710 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3711 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3712 3143d852 2020-06-25 stsp if (err)
3713 3143d852 2020-06-25 stsp return err;
3714 3143d852 2020-06-25 stsp
3715 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3716 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3717 3143d852 2020-06-25 stsp
3718 3143d852 2020-06-25 stsp return err;
3719 3143d852 2020-06-25 stsp }
3720 3143d852 2020-06-25 stsp
3721 3143d852 2020-06-25 stsp static const struct got_error *
3722 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3723 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3724 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3725 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3726 abb4604f 2019-07-27 stsp {
3727 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3728 abb4604f 2019-07-27 stsp struct stat sb;
3729 ff56836b 2021-07-08 stsp
3730 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3731 abb4604f 2019-07-27 stsp if (ie)
3732 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3733 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3734 abb4604f 2019-07-27 stsp
3735 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3736 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3737 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3738 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3739 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3740 abb4604f 2019-07-27 stsp }
3741 abb4604f 2019-07-27 stsp
3742 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3743 916237f3 2022-02-11 stsp return NULL;
3744 916237f3 2022-02-11 stsp
3745 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3746 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3747 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3748 abb4604f 2019-07-27 stsp
3749 abb4604f 2019-07-27 stsp return NULL;
3750 3143d852 2020-06-25 stsp }
3751 3143d852 2020-06-25 stsp
3752 3143d852 2020-06-25 stsp static const struct got_error *
3753 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3754 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3755 3143d852 2020-06-25 stsp {
3756 3143d852 2020-06-25 stsp const struct got_error *err;
3757 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3758 3143d852 2020-06-25 stsp
3759 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3760 3143d852 2020-06-25 stsp ".cvsignore");
3761 3143d852 2020-06-25 stsp if (err)
3762 3143d852 2020-06-25 stsp return err;
3763 3143d852 2020-06-25 stsp
3764 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3765 3143d852 2020-06-25 stsp ".gitignore");
3766 3143d852 2020-06-25 stsp if (err)
3767 3143d852 2020-06-25 stsp return err;
3768 3143d852 2020-06-25 stsp
3769 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3770 3143d852 2020-06-25 stsp if (err) {
3771 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3772 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3773 3143d852 2020-06-25 stsp return err;
3774 3143d852 2020-06-25 stsp }
3775 3143d852 2020-06-25 stsp for (;;) {
3776 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3777 3143d852 2020-06-25 stsp ".cvsignore");
3778 3143d852 2020-06-25 stsp if (err)
3779 3143d852 2020-06-25 stsp break;
3780 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3781 3143d852 2020-06-25 stsp ".gitignore");
3782 3143d852 2020-06-25 stsp if (err)
3783 3143d852 2020-06-25 stsp break;
3784 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3785 3143d852 2020-06-25 stsp if (err) {
3786 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3787 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3788 3143d852 2020-06-25 stsp break;
3789 3143d852 2020-06-25 stsp }
3790 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3791 ff56836b 2021-07-08 stsp break;
3792 b737c85a 2020-06-26 stsp free(parent_path);
3793 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3794 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3795 3143d852 2020-06-25 stsp }
3796 3143d852 2020-06-25 stsp
3797 b737c85a 2020-06-26 stsp free(parent_path);
3798 b737c85a 2020-06-26 stsp free(next_parent_path);
3799 3143d852 2020-06-25 stsp return err;
3800 abb4604f 2019-07-27 stsp }
3801 abb4604f 2019-07-27 stsp
3802 abb4604f 2019-07-27 stsp static const struct got_error *
3803 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3804 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3805 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3806 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3807 f2a9dc41 2019-12-13 tracey int report_unchanged)
3808 f8d1f275 2019-02-04 stsp {
3809 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3810 6fc93f37 2019-12-13 stsp int fd = -1;
3811 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3812 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3813 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3814 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3815 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3816 3143d852 2020-06-25 stsp
3817 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3818 f8d1f275 2019-02-04 stsp
3819 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3820 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3821 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3822 8dc303cc 2019-07-27 stsp
3823 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3824 a84c0d30 2022-03-12 stsp if (ie) {
3825 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3826 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3827 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3828 a84c0d30 2022-03-12 stsp goto done;
3829 a84c0d30 2022-03-12 stsp }
3830 a84c0d30 2022-03-12 stsp
3831 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3832 6fc93f37 2019-12-13 stsp if (fd == -1) {
3833 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3834 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3835 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3836 ff56836b 2021-07-08 stsp else {
3837 ff56836b 2021-07-08 stsp if (!no_ignores) {
3838 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3839 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3840 ff56836b 2021-07-08 stsp if (err)
3841 ff56836b 2021-07-08 stsp goto done;
3842 ff56836b 2021-07-08 stsp }
3843 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3844 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3845 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3846 ff56836b 2021-07-08 stsp }
3847 abb4604f 2019-07-27 stsp } else {
3848 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3849 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3850 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3851 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3852 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3853 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3854 abb4604f 2019-07-27 stsp arg.status_path = path;
3855 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3856 abb4604f 2019-07-27 stsp arg.repo = repo;
3857 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3858 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3859 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3860 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3861 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3862 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3863 022fae89 2019-12-06 tracey if (!no_ignores) {
3864 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3865 3143d852 2020-06-25 stsp worktree->root_path, path);
3866 3143d852 2020-06-25 stsp if (err)
3867 3143d852 2020-06-25 stsp goto done;
3868 022fae89 2019-12-06 tracey }
3869 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3870 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3871 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3872 927df6b7 2019-02-10 stsp }
3873 3143d852 2020-06-25 stsp done:
3874 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3875 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3876 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3877 927df6b7 2019-02-10 stsp free(ondisk_path);
3878 347d1d3e 2019-07-12 stsp return err;
3879 347d1d3e 2019-07-12 stsp }
3880 347d1d3e 2019-07-12 stsp
3881 347d1d3e 2019-07-12 stsp const struct got_error *
3882 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3883 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3884 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3885 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3886 347d1d3e 2019-07-12 stsp {
3887 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3888 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3889 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3890 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3891 347d1d3e 2019-07-12 stsp
3892 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3893 347d1d3e 2019-07-12 stsp if (err)
3894 347d1d3e 2019-07-12 stsp return err;
3895 347d1d3e 2019-07-12 stsp
3896 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3897 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3898 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3899 f6343036 2021-06-22 stsp no_ignores, 0);
3900 72ea6654 2019-07-27 stsp if (err)
3901 72ea6654 2019-07-27 stsp break;
3902 72ea6654 2019-07-27 stsp }
3903 f8d1f275 2019-02-04 stsp free(fileindex_path);
3904 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3905 f8d1f275 2019-02-04 stsp return err;
3906 f8d1f275 2019-02-04 stsp }
3907 6c7ab921 2019-03-18 stsp
3908 6c7ab921 2019-03-18 stsp const struct got_error *
3909 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3910 6c7ab921 2019-03-18 stsp const char *arg)
3911 6c7ab921 2019-03-18 stsp {
3912 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3913 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3914 6c7ab921 2019-03-18 stsp size_t len;
3915 00bb5ea0 2020-07-23 stsp struct stat sb;
3916 01740607 2020-11-04 stsp char *abspath = NULL;
3917 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3918 6c7ab921 2019-03-18 stsp
3919 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3920 6c7ab921 2019-03-18 stsp
3921 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3922 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3923 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3924 00bb5ea0 2020-07-23 stsp
3925 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3926 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3927 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3928 00bb5ea0 2020-07-23 stsp goto done;
3929 00bb5ea0 2020-07-23 stsp }
3930 727173c3 2020-11-06 stsp sb.st_mode = 0;
3931 00bb5ea0 2020-07-23 stsp }
3932 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3933 00bb5ea0 2020-07-23 stsp /*
3934 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3935 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3936 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3937 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3938 00bb5ea0 2020-07-23 stsp */
3939 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3940 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3941 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3942 00bb5ea0 2020-07-23 stsp goto done;
3943 00bb5ea0 2020-07-23 stsp }
3944 00bb5ea0 2020-07-23 stsp
3945 00bb5ea0 2020-07-23 stsp }
3946 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3947 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3948 00bb5ea0 2020-07-23 stsp if (err)
3949 d0710d08 2019-07-22 stsp goto done;
3950 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3951 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3952 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3953 00bb5ea0 2020-07-23 stsp goto done;
3954 00bb5ea0 2020-07-23 stsp }
3955 00bb5ea0 2020-07-23 stsp } else {
3956 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3957 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3958 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3959 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3960 00bb5ea0 2020-07-23 stsp goto done;
3961 00bb5ea0 2020-07-23 stsp }
3962 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3963 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3964 01740607 2020-11-04 stsp goto done;
3965 01740607 2020-11-04 stsp }
3966 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3967 01740607 2020-11-04 stsp sizeof(canonpath));
3968 01740607 2020-11-04 stsp if (err)
3969 00bb5ea0 2020-07-23 stsp goto done;
3970 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3971 01740607 2020-11-04 stsp if (resolved == NULL) {
3972 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3973 01740607 2020-11-04 stsp goto done;
3974 00bb5ea0 2020-07-23 stsp }
3975 d0710d08 2019-07-22 stsp }
3976 d0710d08 2019-07-22 stsp }
3977 6c7ab921 2019-03-18 stsp
3978 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3979 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3980 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3981 6c7ab921 2019-03-18 stsp goto done;
3982 6c7ab921 2019-03-18 stsp }
3983 6c7ab921 2019-03-18 stsp
3984 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3985 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3986 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3987 f6d88e1a 2019-05-29 stsp if (err)
3988 f6d88e1a 2019-05-29 stsp goto done;
3989 f6d88e1a 2019-05-29 stsp } else {
3990 f6d88e1a 2019-05-29 stsp path = strdup("");
3991 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3992 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3993 f6d88e1a 2019-05-29 stsp goto done;
3994 f6d88e1a 2019-05-29 stsp }
3995 6c7ab921 2019-03-18 stsp }
3996 6c7ab921 2019-03-18 stsp
3997 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3998 6c7ab921 2019-03-18 stsp len = strlen(path);
3999 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4000 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4001 6c7ab921 2019-03-18 stsp len--;
4002 6c7ab921 2019-03-18 stsp }
4003 6c7ab921 2019-03-18 stsp done:
4004 01740607 2020-11-04 stsp free(abspath);
4005 6c7ab921 2019-03-18 stsp free(resolved);
4006 d0710d08 2019-07-22 stsp free(cwd);
4007 6c7ab921 2019-03-18 stsp if (err == NULL)
4008 6c7ab921 2019-03-18 stsp *wt_path = path;
4009 6c7ab921 2019-03-18 stsp else
4010 6c7ab921 2019-03-18 stsp free(path);
4011 d00136be 2019-03-26 stsp return err;
4012 d00136be 2019-03-26 stsp }
4013 d00136be 2019-03-26 stsp
4014 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4015 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4016 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4017 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4018 4e68cba3 2019-11-23 stsp void *progress_arg;
4019 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4020 4e68cba3 2019-11-23 stsp };
4021 4e68cba3 2019-11-23 stsp
4022 4e68cba3 2019-11-23 stsp static const struct got_error *
4023 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4024 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4025 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4026 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4027 07f5b47a 2019-06-02 stsp {
4028 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4029 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4030 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4031 6d022e97 2019-08-04 stsp struct stat sb;
4032 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4033 07f5b47a 2019-06-02 stsp
4034 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4035 dbb83fbd 2019-12-12 stsp relpath) == -1)
4036 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4037 dbb83fbd 2019-12-12 stsp
4038 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4039 6d022e97 2019-08-04 stsp if (ie) {
4040 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4041 12463d8b 2019-12-13 stsp de_name, a->repo);
4042 6d022e97 2019-08-04 stsp if (err)
4043 dbb83fbd 2019-12-12 stsp goto done;
4044 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4045 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4046 dbb83fbd 2019-12-12 stsp goto done;
4047 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4048 dbb83fbd 2019-12-12 stsp if (err)
4049 dbb83fbd 2019-12-12 stsp goto done;
4050 6d022e97 2019-08-04 stsp }
4051 07f5b47a 2019-06-02 stsp
4052 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4053 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4054 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4055 9b4603c0 2022-01-31 stsp else
4056 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4057 dbb83fbd 2019-12-12 stsp goto done;
4058 4e68cba3 2019-11-23 stsp }
4059 07f5b47a 2019-06-02 stsp
4060 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4061 4e68cba3 2019-11-23 stsp if (err)
4062 4e68cba3 2019-11-23 stsp goto done;
4063 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4064 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4065 3969253a 2020-03-07 stsp if (err) {
4066 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4067 3969253a 2020-03-07 stsp goto done;
4068 3969253a 2020-03-07 stsp }
4069 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4070 07f5b47a 2019-06-02 stsp if (err) {
4071 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4072 4e68cba3 2019-11-23 stsp goto done;
4073 07f5b47a 2019-06-02 stsp }
4074 4e68cba3 2019-11-23 stsp done:
4075 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4076 dbb83fbd 2019-12-12 stsp if (err)
4077 dbb83fbd 2019-12-12 stsp return err;
4078 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4079 dbb83fbd 2019-12-12 stsp return NULL;
4080 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4081 07f5b47a 2019-06-02 stsp }
4082 07f5b47a 2019-06-02 stsp
4083 d00136be 2019-03-26 stsp const struct got_error *
4084 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4085 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4086 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4087 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4088 d00136be 2019-03-26 stsp {
4089 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4090 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4091 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4092 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4093 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4094 d00136be 2019-03-26 stsp
4095 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4096 d00136be 2019-03-26 stsp if (err)
4097 d00136be 2019-03-26 stsp return err;
4098 d00136be 2019-03-26 stsp
4099 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4100 d00136be 2019-03-26 stsp if (err)
4101 d00136be 2019-03-26 stsp goto done;
4102 d00136be 2019-03-26 stsp
4103 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4104 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4105 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4106 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4107 4e68cba3 2019-11-23 stsp saa.repo = repo;
4108 4e68cba3 2019-11-23 stsp
4109 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4110 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4111 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4112 1dd54920 2019-05-11 stsp if (err)
4113 af12c6b9 2019-06-04 stsp break;
4114 1dd54920 2019-05-11 stsp }
4115 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4116 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4117 af12c6b9 2019-06-04 stsp err = sync_err;
4118 d00136be 2019-03-26 stsp done:
4119 fb399478 2019-07-12 stsp free(fileindex_path);
4120 d00136be 2019-03-26 stsp if (fileindex)
4121 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4122 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4123 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4124 d00136be 2019-03-26 stsp err = unlockerr;
4125 6c7ab921 2019-03-18 stsp return err;
4126 6c7ab921 2019-03-18 stsp }
4127 17ed4618 2019-06-02 stsp
4128 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4129 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4130 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4131 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4132 f2a9dc41 2019-12-13 tracey void *progress_arg;
4133 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4134 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4135 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4136 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4137 766841c2 2020-08-13 stsp const char *status_codes;
4138 f2a9dc41 2019-12-13 tracey };
4139 f2a9dc41 2019-12-13 tracey
4140 f2a9dc41 2019-12-13 tracey static const struct got_error *
4141 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4142 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4143 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4144 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4145 17ed4618 2019-06-02 stsp {
4146 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4147 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4148 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4149 17ed4618 2019-06-02 stsp struct stat sb;
4150 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4151 4e12cd97 2022-01-25 stsp
4152 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4153 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4154 4e12cd97 2022-01-25 stsp return NULL;
4155 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4156 4e12cd97 2022-01-25 stsp }
4157 17ed4618 2019-06-02 stsp
4158 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4159 17ed4618 2019-06-02 stsp if (ie == NULL)
4160 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4161 2ec1f75b 2019-03-26 stsp
4162 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4163 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4164 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4165 9acbc4fa 2019-08-03 stsp return NULL;
4166 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4167 9acbc4fa 2019-08-03 stsp }
4168 9acbc4fa 2019-08-03 stsp
4169 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4170 f2a9dc41 2019-12-13 tracey relpath) == -1)
4171 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4172 f2a9dc41 2019-12-13 tracey
4173 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4174 12463d8b 2019-12-13 stsp a->repo);
4175 17ed4618 2019-06-02 stsp if (err)
4176 f2a9dc41 2019-12-13 tracey goto done;
4177 17ed4618 2019-06-02 stsp
4178 766841c2 2020-08-13 stsp if (a->status_codes) {
4179 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4180 766841c2 2020-08-13 stsp int i;
4181 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4182 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4183 766841c2 2020-08-13 stsp break;
4184 766841c2 2020-08-13 stsp }
4185 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4186 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4187 766841c2 2020-08-13 stsp free(ondisk_path);
4188 766841c2 2020-08-13 stsp return NULL;
4189 766841c2 2020-08-13 stsp }
4190 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4191 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4192 766841c2 2020-08-13 stsp static char msg[64];
4193 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4194 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4195 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4196 766841c2 2020-08-13 stsp goto done;
4197 766841c2 2020-08-13 stsp }
4198 766841c2 2020-08-13 stsp }
4199 766841c2 2020-08-13 stsp
4200 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4201 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4202 f2a9dc41 2019-12-13 tracey goto done;
4203 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4204 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4205 f2a9dc41 2019-12-13 tracey goto done;
4206 f2a9dc41 2019-12-13 tracey }
4207 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4208 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4209 4e12cd97 2022-01-25 stsp goto done;
4210 4e12cd97 2022-01-25 stsp }
4211 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4212 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4213 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4214 f2a9dc41 2019-12-13 tracey goto done;
4215 f2a9dc41 2019-12-13 tracey }
4216 17ed4618 2019-06-02 stsp }
4217 17ed4618 2019-06-02 stsp
4218 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4219 20a2ad1c 2020-10-20 stsp size_t root_len;
4220 20a2ad1c 2020-10-20 stsp
4221 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4222 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4223 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4224 12463d8b 2019-12-13 stsp ondisk_path);
4225 12463d8b 2019-12-13 stsp goto done;
4226 12463d8b 2019-12-13 stsp }
4227 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4228 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4229 12463d8b 2019-12-13 stsp goto done;
4230 15341bfd 2020-03-05 tracey }
4231 15341bfd 2020-03-05 tracey
4232 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4233 20a2ad1c 2020-10-20 stsp do {
4234 20a2ad1c 2020-10-20 stsp char *parent;
4235 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4236 20a2ad1c 2020-10-20 stsp if (err)
4237 2513f20a 2020-10-20 stsp goto done;
4238 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4239 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4240 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4241 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4242 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4243 20a2ad1c 2020-10-20 stsp ondisk_path);
4244 15341bfd 2020-03-05 tracey break;
4245 15341bfd 2020-03-05 tracey }
4246 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4247 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4248 f2a9dc41 2019-12-13 tracey }
4249 17ed4618 2019-06-02 stsp
4250 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4251 f2a9dc41 2019-12-13 tracey done:
4252 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4253 f2a9dc41 2019-12-13 tracey if (err)
4254 f2a9dc41 2019-12-13 tracey return err;
4255 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4256 f2a9dc41 2019-12-13 tracey return NULL;
4257 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4258 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4259 17ed4618 2019-06-02 stsp }
4260 17ed4618 2019-06-02 stsp
4261 2ec1f75b 2019-03-26 stsp const struct got_error *
4262 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4263 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4264 766841c2 2020-08-13 stsp const char *status_codes,
4265 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4266 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4267 2ec1f75b 2019-03-26 stsp {
4268 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4269 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4270 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4271 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4272 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4273 2ec1f75b 2019-03-26 stsp
4274 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4275 2ec1f75b 2019-03-26 stsp if (err)
4276 2ec1f75b 2019-03-26 stsp return err;
4277 2ec1f75b 2019-03-26 stsp
4278 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4279 2ec1f75b 2019-03-26 stsp if (err)
4280 2ec1f75b 2019-03-26 stsp goto done;
4281 f2a9dc41 2019-12-13 tracey
4282 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4283 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4284 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4285 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4286 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4287 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4288 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4289 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4290 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4291 2ec1f75b 2019-03-26 stsp
4292 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4293 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4294 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4295 17ed4618 2019-06-02 stsp if (err)
4296 af12c6b9 2019-06-04 stsp break;
4297 2ec1f75b 2019-03-26 stsp }
4298 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4299 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4300 af12c6b9 2019-06-04 stsp err = sync_err;
4301 2ec1f75b 2019-03-26 stsp done:
4302 fb399478 2019-07-12 stsp free(fileindex_path);
4303 2ec1f75b 2019-03-26 stsp if (fileindex)
4304 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4305 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4306 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4307 2ec1f75b 2019-03-26 stsp err = unlockerr;
4308 33aa809d 2019-08-08 stsp return err;
4309 33aa809d 2019-08-08 stsp }
4310 33aa809d 2019-08-08 stsp
4311 33aa809d 2019-08-08 stsp static const struct got_error *
4312 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4313 33aa809d 2019-08-08 stsp {
4314 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4315 33aa809d 2019-08-08 stsp char *line = NULL;
4316 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4317 33aa809d 2019-08-08 stsp ssize_t linelen;
4318 33aa809d 2019-08-08 stsp
4319 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4320 33aa809d 2019-08-08 stsp if (linelen == -1) {
4321 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4322 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4323 33aa809d 2019-08-08 stsp goto done;
4324 33aa809d 2019-08-08 stsp }
4325 33aa809d 2019-08-08 stsp return NULL;
4326 33aa809d 2019-08-08 stsp }
4327 33aa809d 2019-08-08 stsp if (outfile) {
4328 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4329 33aa809d 2019-08-08 stsp if (n != linelen) {
4330 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4331 33aa809d 2019-08-08 stsp goto done;
4332 33aa809d 2019-08-08 stsp }
4333 33aa809d 2019-08-08 stsp }
4334 33aa809d 2019-08-08 stsp if (rejectfile) {
4335 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4336 33aa809d 2019-08-08 stsp if (n != linelen)
4337 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4338 33aa809d 2019-08-08 stsp }
4339 33aa809d 2019-08-08 stsp done:
4340 33aa809d 2019-08-08 stsp free(line);
4341 2ec1f75b 2019-03-26 stsp return err;
4342 2ec1f75b 2019-03-26 stsp }
4343 1f1abb7e 2019-08-08 stsp
4344 33aa809d 2019-08-08 stsp static const struct got_error *
4345 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4346 33aa809d 2019-08-08 stsp {
4347 33aa809d 2019-08-08 stsp char *line = NULL;
4348 33aa809d 2019-08-08 stsp size_t linesize = 0;
4349 33aa809d 2019-08-08 stsp ssize_t linelen;
4350 33aa809d 2019-08-08 stsp
4351 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4352 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4353 500467ff 2019-09-25 hiltjo if (ferror(f))
4354 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4355 500467ff 2019-09-25 hiltjo return NULL;
4356 500467ff 2019-09-25 hiltjo }
4357 33aa809d 2019-08-08 stsp free(line);
4358 33aa809d 2019-08-08 stsp return NULL;
4359 33aa809d 2019-08-08 stsp }
4360 33aa809d 2019-08-08 stsp
4361 33aa809d 2019-08-08 stsp static const struct got_error *
4362 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4363 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4364 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4365 abc59930 2021-09-05 naddy {
4366 33aa809d 2019-08-08 stsp const struct got_error *err;
4367 33aa809d 2019-08-08 stsp
4368 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4369 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4370 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4371 33aa809d 2019-08-08 stsp if (err)
4372 33aa809d 2019-08-08 stsp return err;
4373 33aa809d 2019-08-08 stsp (*line_cur1)++;
4374 33aa809d 2019-08-08 stsp }
4375 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4376 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4377 33aa809d 2019-08-08 stsp if (rejectfile)
4378 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4379 33aa809d 2019-08-08 stsp else
4380 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4381 33aa809d 2019-08-08 stsp if (err)
4382 33aa809d 2019-08-08 stsp return err;
4383 33aa809d 2019-08-08 stsp (*line_cur2)++;
4384 33aa809d 2019-08-08 stsp }
4385 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4386 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4387 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4388 33aa809d 2019-08-08 stsp if (err)
4389 33aa809d 2019-08-08 stsp return err;
4390 33aa809d 2019-08-08 stsp (*line_cur2)++;
4391 33aa809d 2019-08-08 stsp }
4392 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4393 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4394 33aa809d 2019-08-08 stsp if (rejectfile)
4395 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4396 33aa809d 2019-08-08 stsp else
4397 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4398 33aa809d 2019-08-08 stsp if (err)
4399 33aa809d 2019-08-08 stsp return err;
4400 33aa809d 2019-08-08 stsp (*line_cur1)++;
4401 33aa809d 2019-08-08 stsp }
4402 f1e81a05 2019-08-10 stsp
4403 f1e81a05 2019-08-10 stsp return NULL;
4404 f1e81a05 2019-08-10 stsp }
4405 f1e81a05 2019-08-10 stsp
4406 f1e81a05 2019-08-10 stsp static const struct got_error *
4407 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4408 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4409 f1e81a05 2019-08-10 stsp {
4410 f1e81a05 2019-08-10 stsp const struct got_error *err;
4411 f1e81a05 2019-08-10 stsp
4412 f1e81a05 2019-08-10 stsp if (outfile) {
4413 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4414 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4415 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4416 f1e81a05 2019-08-10 stsp if (err)
4417 f1e81a05 2019-08-10 stsp return err;
4418 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4419 f1e81a05 2019-08-10 stsp }
4420 f1e81a05 2019-08-10 stsp }
4421 f1e81a05 2019-08-10 stsp if (rejectfile) {
4422 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4423 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4424 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4425 f1e81a05 2019-08-10 stsp if (err)
4426 f1e81a05 2019-08-10 stsp return err;
4427 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4428 f1e81a05 2019-08-10 stsp }
4429 33aa809d 2019-08-08 stsp }
4430 33aa809d 2019-08-08 stsp
4431 33aa809d 2019-08-08 stsp return NULL;
4432 33aa809d 2019-08-08 stsp }
4433 33aa809d 2019-08-08 stsp
4434 33aa809d 2019-08-08 stsp static const struct got_error *
4435 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4436 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4437 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4438 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4439 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4440 33aa809d 2019-08-08 stsp {
4441 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4442 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4443 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4444 33aa809d 2019-08-08 stsp FILE *hunkfile;
4445 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4446 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4447 fe621944 2020-11-10 stsp int rc;
4448 33aa809d 2019-08-08 stsp
4449 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4450 33aa809d 2019-08-08 stsp
4451 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4452 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4453 fe621944 2020-11-10 stsp start_old = cc.left.start;
4454 fe621944 2020-11-10 stsp end_old = cc.left.end;
4455 fe621944 2020-11-10 stsp start_new = cc.right.start;
4456 fe621944 2020-11-10 stsp end_new = cc.right.end;
4457 33aa809d 2019-08-08 stsp
4458 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4459 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4460 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4461 33aa809d 2019-08-08 stsp
4462 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4463 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4464 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4465 33aa809d 2019-08-08 stsp
4466 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4467 fe621944 2020-11-10 stsp if (diff_state == NULL)
4468 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4469 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4470 fe621944 2020-11-10 stsp
4471 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4472 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4473 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4474 33aa809d 2019-08-08 stsp goto done;
4475 33aa809d 2019-08-08 stsp }
4476 fe621944 2020-11-10 stsp
4477 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4478 fe621944 2020-11-10 stsp diff_result, &cc);
4479 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4480 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4481 33aa809d 2019-08-08 stsp goto done;
4482 33aa809d 2019-08-08 stsp }
4483 fe621944 2020-11-10 stsp
4484 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4485 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4486 33aa809d 2019-08-08 stsp goto done;
4487 33aa809d 2019-08-08 stsp }
4488 33aa809d 2019-08-08 stsp
4489 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4490 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4491 33aa809d 2019-08-08 stsp if (err)
4492 33aa809d 2019-08-08 stsp goto done;
4493 33aa809d 2019-08-08 stsp
4494 33aa809d 2019-08-08 stsp switch (*choice) {
4495 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4496 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4497 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4498 33aa809d 2019-08-08 stsp break;
4499 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4500 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4501 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4502 33aa809d 2019-08-08 stsp break;
4503 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4504 33aa809d 2019-08-08 stsp break;
4505 33aa809d 2019-08-08 stsp default:
4506 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4507 33aa809d 2019-08-08 stsp break;
4508 33aa809d 2019-08-08 stsp }
4509 33aa809d 2019-08-08 stsp done:
4510 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4511 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4512 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4513 33aa809d 2019-08-08 stsp return err;
4514 33aa809d 2019-08-08 stsp }
4515 33aa809d 2019-08-08 stsp
4516 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4517 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4518 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4519 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4520 1f1abb7e 2019-08-08 stsp void *progress_arg;
4521 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4522 33aa809d 2019-08-08 stsp void *patch_arg;
4523 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4524 f259c4c1 2021-09-24 stsp int unlink_added_files;
4525 1f1abb7e 2019-08-08 stsp };
4526 a129376b 2019-03-28 stsp
4527 e20a8b6f 2019-06-04 stsp static const struct got_error *
4528 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4529 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4530 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4531 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4532 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4533 33aa809d 2019-08-08 stsp {
4534 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4535 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4536 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4537 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4538 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4539 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4540 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4541 fe621944 2020-11-10 stsp struct stat sb2;
4542 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4543 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4544 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4545 33aa809d 2019-08-08 stsp
4546 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4547 33aa809d 2019-08-08 stsp
4548 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4549 33aa809d 2019-08-08 stsp if (err)
4550 33aa809d 2019-08-08 stsp return err;
4551 33aa809d 2019-08-08 stsp
4552 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4553 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4554 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4555 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4556 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4557 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4558 fa3cef63 2020-07-23 stsp goto done;
4559 fa3cef63 2020-07-23 stsp }
4560 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4561 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4562 c0df5966 2021-12-31 stsp if (link_len == -1) {
4563 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4564 c0df5966 2021-12-31 stsp path2);
4565 c0df5966 2021-12-31 stsp }
4566 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4567 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4568 12463d8b 2019-12-13 stsp }
4569 12463d8b 2019-12-13 stsp } else {
4570 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4571 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4572 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4573 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4574 fa3cef63 2020-07-23 stsp goto done;
4575 fa3cef63 2020-07-23 stsp }
4576 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4577 fa3cef63 2020-07-23 stsp sizeof(link_target));
4578 fa3cef63 2020-07-23 stsp if (link_len == -1)
4579 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4580 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4581 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4582 12463d8b 2019-12-13 stsp }
4583 1ebedb77 2019-10-19 stsp }
4584 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4585 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4586 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4587 fa3cef63 2020-07-23 stsp goto done;
4588 fa3cef63 2020-07-23 stsp }
4589 1ebedb77 2019-10-19 stsp
4590 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4591 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4592 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4593 fa3cef63 2020-07-23 stsp goto done;
4594 fa3cef63 2020-07-23 stsp }
4595 fa3cef63 2020-07-23 stsp fd2 = -1;
4596 fa3cef63 2020-07-23 stsp } else {
4597 fa3cef63 2020-07-23 stsp size_t n;
4598 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4599 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4600 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4601 fa3cef63 2020-07-23 stsp goto done;
4602 fa3cef63 2020-07-23 stsp }
4603 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4604 fa3cef63 2020-07-23 stsp if (n != link_len) {
4605 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4606 fa3cef63 2020-07-23 stsp goto done;
4607 fa3cef63 2020-07-23 stsp }
4608 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4609 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4610 fa3cef63 2020-07-23 stsp goto done;
4611 fa3cef63 2020-07-23 stsp }
4612 fa3cef63 2020-07-23 stsp rewind(f2);
4613 33aa809d 2019-08-08 stsp }
4614 33aa809d 2019-08-08 stsp
4615 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4616 eb81bc23 2022-06-28 tracey if (fd == -1) {
4617 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4618 eb81bc23 2022-06-28 tracey goto done;
4619 eb81bc23 2022-06-28 tracey }
4620 eb81bc23 2022-06-28 tracey
4621 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4622 33aa809d 2019-08-08 stsp if (err)
4623 33aa809d 2019-08-08 stsp goto done;
4624 33aa809d 2019-08-08 stsp
4625 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4626 33aa809d 2019-08-08 stsp if (err)
4627 33aa809d 2019-08-08 stsp goto done;
4628 33aa809d 2019-08-08 stsp
4629 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4630 33aa809d 2019-08-08 stsp if (err)
4631 33aa809d 2019-08-08 stsp goto done;
4632 33aa809d 2019-08-08 stsp
4633 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4634 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4635 33aa809d 2019-08-08 stsp if (err)
4636 33aa809d 2019-08-08 stsp goto done;
4637 33aa809d 2019-08-08 stsp
4638 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4639 b90054ed 2022-11-01 stsp "");
4640 33aa809d 2019-08-08 stsp if (err)
4641 33aa809d 2019-08-08 stsp goto done;
4642 33aa809d 2019-08-08 stsp
4643 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4644 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4645 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4646 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4647 fe621944 2020-11-10 stsp
4648 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4649 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4650 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4651 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4652 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4653 fe621944 2020-11-10 stsp nchanges++;
4654 fe621944 2020-11-10 stsp }
4655 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4656 33aa809d 2019-08-08 stsp int choice;
4657 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4658 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4659 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4660 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4661 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4662 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4663 33aa809d 2019-08-08 stsp if (err)
4664 33aa809d 2019-08-08 stsp goto done;
4665 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4666 33aa809d 2019-08-08 stsp have_content = 1;
4667 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4668 33aa809d 2019-08-08 stsp break;
4669 33aa809d 2019-08-08 stsp }
4670 1ebedb77 2019-10-19 stsp if (have_content) {
4671 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4672 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4673 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4674 1ebedb77 2019-10-19 stsp if (err)
4675 1ebedb77 2019-10-19 stsp goto done;
4676 1ebedb77 2019-10-19 stsp
4677 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4678 b2b3fce1 2022-10-29 op mode_t mode;
4679 b2b3fce1 2022-10-29 op
4680 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4681 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4682 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4683 fa3cef63 2020-07-23 stsp goto done;
4684 fa3cef63 2020-07-23 stsp }
4685 1ebedb77 2019-10-19 stsp }
4686 1ebedb77 2019-10-19 stsp }
4687 33aa809d 2019-08-08 stsp done:
4688 33aa809d 2019-08-08 stsp free(id_str);
4689 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4690 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4691 33aa809d 2019-08-08 stsp if (blob)
4692 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4693 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4694 fe621944 2020-11-10 stsp if (err == NULL)
4695 fe621944 2020-11-10 stsp err = free_err;
4696 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4697 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4698 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4699 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4700 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4701 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4702 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4703 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4704 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4705 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4706 33aa809d 2019-08-08 stsp if (err || !have_content) {
4707 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4708 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4709 33aa809d 2019-08-08 stsp free(*path_outfile);
4710 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4711 33aa809d 2019-08-08 stsp }
4712 33aa809d 2019-08-08 stsp free(path1);
4713 33aa809d 2019-08-08 stsp return err;
4714 33aa809d 2019-08-08 stsp }
4715 33aa809d 2019-08-08 stsp
4716 33aa809d 2019-08-08 stsp static const struct got_error *
4717 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4718 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4719 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4720 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4721 a129376b 2019-03-28 stsp {
4722 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4723 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4724 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4725 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4726 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4727 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4728 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4729 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4730 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4731 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4732 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4733 eb81bc23 2022-06-28 tracey int fd = -1;
4734 a129376b 2019-03-28 stsp
4735 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4736 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4737 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4738 d3bcc3d1 2019-08-08 stsp return NULL;
4739 3d69ad8d 2019-08-17 semarie
4740 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4741 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4742 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4743 d3bcc3d1 2019-08-08 stsp
4744 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4745 65084dad 2019-08-08 stsp if (ie == NULL)
4746 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4747 a129376b 2019-03-28 stsp
4748 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4749 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4750 a129376b 2019-03-28 stsp if (err) {
4751 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4752 a129376b 2019-03-28 stsp goto done;
4753 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4754 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4755 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4756 e20a8b6f 2019-06-04 stsp goto done;
4757 e20a8b6f 2019-06-04 stsp }
4758 a129376b 2019-03-28 stsp }
4759 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4760 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4761 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4762 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4763 a129376b 2019-03-28 stsp goto done;
4764 a129376b 2019-03-28 stsp }
4765 a129376b 2019-03-28 stsp } else {
4766 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4767 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4768 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4769 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4770 a129376b 2019-03-28 stsp goto done;
4771 a129376b 2019-03-28 stsp }
4772 a129376b 2019-03-28 stsp } else {
4773 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4774 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4775 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4776 a129376b 2019-03-28 stsp goto done;
4777 a129376b 2019-03-28 stsp }
4778 a129376b 2019-03-28 stsp }
4779 a129376b 2019-03-28 stsp }
4780 a129376b 2019-03-28 stsp
4781 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4782 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4783 a44927cc 2022-04-07 stsp if (err)
4784 a44927cc 2022-04-07 stsp goto done;
4785 a44927cc 2022-04-07 stsp
4786 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4787 a9fa2909 2019-07-27 stsp if (err) {
4788 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4789 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4790 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4791 a9fa2909 2019-07-27 stsp goto done;
4792 a9fa2909 2019-07-27 stsp } else {
4793 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4794 a9fa2909 2019-07-27 stsp if (err)
4795 a9fa2909 2019-07-27 stsp goto done;
4796 a9fa2909 2019-07-27 stsp
4797 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4798 1233e6b6 2020-10-19 stsp if (err)
4799 a9fa2909 2019-07-27 stsp goto done;
4800 a9fa2909 2019-07-27 stsp
4801 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4802 1233e6b6 2020-10-19 stsp free(te_name);
4803 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4804 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4805 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4806 a9fa2909 2019-07-27 stsp goto done;
4807 a9fa2909 2019-07-27 stsp }
4808 a129376b 2019-03-28 stsp }
4809 a129376b 2019-03-28 stsp
4810 a129376b 2019-03-28 stsp switch (status) {
4811 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4812 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4813 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4814 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4815 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4816 33aa809d 2019-08-08 stsp if (err)
4817 33aa809d 2019-08-08 stsp goto done;
4818 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4819 33aa809d 2019-08-08 stsp break;
4820 33aa809d 2019-08-08 stsp }
4821 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4822 1f1abb7e 2019-08-08 stsp ie->path);
4823 1ee397ad 2019-07-12 stsp if (err)
4824 1ee397ad 2019-07-12 stsp goto done;
4825 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4826 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4827 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4828 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4829 f259c4c1 2021-09-24 stsp relpath) == -1) {
4830 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4831 f259c4c1 2021-09-24 stsp goto done;
4832 f259c4c1 2021-09-24 stsp }
4833 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4834 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4835 f259c4c1 2021-09-24 stsp ondisk_path);
4836 f259c4c1 2021-09-24 stsp break;
4837 f259c4c1 2021-09-24 stsp }
4838 f259c4c1 2021-09-24 stsp }
4839 a129376b 2019-03-28 stsp break;
4840 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4841 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4842 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4843 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4844 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4845 33aa809d 2019-08-08 stsp if (err)
4846 33aa809d 2019-08-08 stsp goto done;
4847 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4848 33aa809d 2019-08-08 stsp break;
4849 33aa809d 2019-08-08 stsp }
4850 33aa809d 2019-08-08 stsp /* fall through */
4851 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4852 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4853 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4854 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4855 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4856 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4857 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
4858 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
4859 b4b2adf5 2023-02-09 op else
4860 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
4861 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4862 eb81bc23 2022-06-28 tracey if (fd == -1) {
4863 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4864 eb81bc23 2022-06-28 tracey goto done;
4865 eb81bc23 2022-06-28 tracey }
4866 eb81bc23 2022-06-28 tracey
4867 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4868 a129376b 2019-03-28 stsp if (err)
4869 65084dad 2019-08-08 stsp goto done;
4870 65084dad 2019-08-08 stsp
4871 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4872 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4873 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4874 a129376b 2019-03-28 stsp goto done;
4875 65084dad 2019-08-08 stsp }
4876 65084dad 2019-08-08 stsp
4877 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4878 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4879 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4880 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4881 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4882 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4883 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4884 33aa809d 2019-08-08 stsp break;
4885 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4886 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4887 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4888 369fd7e5 2020-07-23 stsp path_content);
4889 369fd7e5 2020-07-23 stsp break;
4890 369fd7e5 2020-07-23 stsp }
4891 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4892 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4893 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4894 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4895 369fd7e5 2020-07-23 stsp } else {
4896 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4897 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4898 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4899 369fd7e5 2020-07-23 stsp goto done;
4900 369fd7e5 2020-07-23 stsp }
4901 33aa809d 2019-08-08 stsp }
4902 33aa809d 2019-08-08 stsp } else {
4903 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4904 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4905 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4906 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4907 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4908 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4909 2e1fa222 2020-07-23 stsp } else {
4910 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4911 2e1fa222 2020-07-23 stsp ie->path,
4912 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4913 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4914 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4915 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4916 2e1fa222 2020-07-23 stsp }
4917 a129376b 2019-03-28 stsp if (err)
4918 a129376b 2019-03-28 stsp goto done;
4919 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4920 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4921 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4922 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4923 437adc9d 2020-12-10 yzhong blob->id.sha1,
4924 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4925 2e1fa222 2020-07-23 stsp if (err)
4926 2e1fa222 2020-07-23 stsp goto done;
4927 2e1fa222 2020-07-23 stsp }
4928 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4929 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4930 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4931 33aa809d 2019-08-08 stsp }
4932 a129376b 2019-03-28 stsp }
4933 a129376b 2019-03-28 stsp break;
4934 e20a8b6f 2019-06-04 stsp }
4935 a129376b 2019-03-28 stsp default:
4936 1f1abb7e 2019-08-08 stsp break;
4937 a129376b 2019-03-28 stsp }
4938 a129376b 2019-03-28 stsp done:
4939 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4940 33aa809d 2019-08-08 stsp free(path_content);
4941 e20a8b6f 2019-06-04 stsp free(parent_path);
4942 a129376b 2019-03-28 stsp free(tree_path);
4943 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4944 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4945 a129376b 2019-03-28 stsp if (blob)
4946 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4947 a129376b 2019-03-28 stsp if (tree)
4948 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4949 a129376b 2019-03-28 stsp free(tree_id);
4950 a44927cc 2022-04-07 stsp if (base_commit)
4951 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4952 e20a8b6f 2019-06-04 stsp return err;
4953 e20a8b6f 2019-06-04 stsp }
4954 e20a8b6f 2019-06-04 stsp
4955 e20a8b6f 2019-06-04 stsp const struct got_error *
4956 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4957 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4958 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4959 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4960 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4961 e20a8b6f 2019-06-04 stsp {
4962 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4963 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4964 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4965 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4966 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4967 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4968 e20a8b6f 2019-06-04 stsp
4969 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4970 e20a8b6f 2019-06-04 stsp if (err)
4971 e20a8b6f 2019-06-04 stsp return err;
4972 e20a8b6f 2019-06-04 stsp
4973 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4974 e20a8b6f 2019-06-04 stsp if (err)
4975 e20a8b6f 2019-06-04 stsp goto done;
4976 e20a8b6f 2019-06-04 stsp
4977 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4978 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4979 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4980 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4981 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4982 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4983 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4984 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4985 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4986 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4987 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4988 e20a8b6f 2019-06-04 stsp if (err)
4989 af12c6b9 2019-06-04 stsp break;
4990 e20a8b6f 2019-06-04 stsp }
4991 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4992 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4993 e20a8b6f 2019-06-04 stsp err = sync_err;
4994 af12c6b9 2019-06-04 stsp done:
4995 fb399478 2019-07-12 stsp free(fileindex_path);
4996 a129376b 2019-03-28 stsp if (fileindex)
4997 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4998 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4999 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5000 a129376b 2019-03-28 stsp err = unlockerr;
5001 c4296144 2019-05-09 stsp return err;
5002 c4296144 2019-05-09 stsp }
5003 c4296144 2019-05-09 stsp
5004 cf066bf8 2019-05-09 stsp static void
5005 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5006 cf066bf8 2019-05-09 stsp {
5007 24519714 2019-05-09 stsp free(ct->path);
5008 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5009 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5010 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5011 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5012 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5013 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5014 cf066bf8 2019-05-09 stsp free(ct);
5015 cf066bf8 2019-05-09 stsp }
5016 24519714 2019-05-09 stsp
5017 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5018 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5019 24519714 2019-05-09 stsp struct got_repository *repo;
5020 24519714 2019-05-09 stsp struct got_worktree *worktree;
5021 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5022 5f8a88c6 2019-08-03 stsp int have_staged_files;
5023 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5024 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5025 12383673 2023-02-18 mark int commit_conflicts;
5026 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5027 2a47b1e5 2022-11-01 stsp FILE *f1;
5028 2a47b1e5 2022-11-01 stsp FILE *f2;
5029 24519714 2019-05-09 stsp };
5030 2a47b1e5 2022-11-01 stsp
5031 2a47b1e5 2022-11-01 stsp /*
5032 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5033 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5034 2a47b1e5 2022-11-01 stsp */
5035 2a47b1e5 2022-11-01 stsp static const struct got_error *
5036 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5037 2a47b1e5 2022-11-01 stsp const char *abspath)
5038 2a47b1e5 2022-11-01 stsp {
5039 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5040 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5041 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5042 2a47b1e5 2022-11-01 stsp
5043 2a47b1e5 2022-11-01 stsp *fd = -1;
5044 2a47b1e5 2022-11-01 stsp
5045 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5046 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5047 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5048 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5049 2a47b1e5 2022-11-01 stsp } else {
5050 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5051 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5052 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5053 2a47b1e5 2022-11-01 stsp }
5054 2a47b1e5 2022-11-01 stsp
5055 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5056 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5057 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5058 2a47b1e5 2022-11-01 stsp
5059 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5060 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5061 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5062 2a47b1e5 2022-11-01 stsp goto done;
5063 2a47b1e5 2022-11-01 stsp }
5064 2a47b1e5 2022-11-01 stsp
5065 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5066 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5067 2a47b1e5 2022-11-01 stsp goto done;
5068 2a47b1e5 2022-11-01 stsp }
5069 2a47b1e5 2022-11-01 stsp done:
5070 2a47b1e5 2022-11-01 stsp if (err) {
5071 2a47b1e5 2022-11-01 stsp close(*fd);
5072 2a47b1e5 2022-11-01 stsp *fd = -1;
5073 2a47b1e5 2022-11-01 stsp }
5074 2a47b1e5 2022-11-01 stsp return err;
5075 2a47b1e5 2022-11-01 stsp }
5076 2a47b1e5 2022-11-01 stsp
5077 2a47b1e5 2022-11-01 stsp static const struct got_error *
5078 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5079 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5080 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5081 2a47b1e5 2022-11-01 stsp {
5082 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5083 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5084 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5085 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5086 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5087 2a47b1e5 2022-11-01 stsp struct stat sb;
5088 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5089 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5090 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5091 2a47b1e5 2022-11-01 stsp
5092 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5093 4ba5cca9 2022-11-01 stsp
5094 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5095 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5096 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5097 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5098 4ba5cca9 2022-11-01 stsp return NULL;
5099 4ba5cca9 2022-11-01 stsp } else {
5100 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5101 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5102 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5103 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5104 4ba5cca9 2022-11-01 stsp return NULL;
5105 4ba5cca9 2022-11-01 stsp }
5106 2a47b1e5 2022-11-01 stsp
5107 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5108 2a47b1e5 2022-11-01 stsp if (err)
5109 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5110 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5111 2a47b1e5 2022-11-01 stsp if (err)
5112 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5113 2a47b1e5 2022-11-01 stsp
5114 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5115 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5116 2a47b1e5 2022-11-01 stsp if (err)
5117 2a47b1e5 2022-11-01 stsp return err;
5118 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5119 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5120 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5121 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5122 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5123 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5124 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5125 2a47b1e5 2022-11-01 stsp }
5126 2a47b1e5 2022-11-01 stsp
5127 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5128 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5129 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5130 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5131 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5132 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5133 2a47b1e5 2022-11-01 stsp break;
5134 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5135 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5136 2a47b1e5 2022-11-01 stsp break;
5137 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5138 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5139 2a47b1e5 2022-11-01 stsp break;
5140 2a47b1e5 2022-11-01 stsp default:
5141 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5142 2a47b1e5 2022-11-01 stsp }
5143 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5144 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5145 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5146 2a47b1e5 2022-11-01 stsp goto done;
5147 2a47b1e5 2022-11-01 stsp }
5148 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5149 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5150 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5151 2a47b1e5 2022-11-01 stsp goto done;
5152 2a47b1e5 2022-11-01 stsp }
5153 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5154 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5155 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5156 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5157 2a47b1e5 2022-11-01 stsp goto done;
5158 2a47b1e5 2022-11-01 stsp }
5159 2a47b1e5 2022-11-01 stsp
5160 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5161 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5162 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5163 2a47b1e5 2022-11-01 stsp goto done;
5164 2a47b1e5 2022-11-01 stsp }
5165 2a47b1e5 2022-11-01 stsp
5166 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5167 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5168 2a47b1e5 2022-11-01 stsp 8192, fd1);
5169 2a47b1e5 2022-11-01 stsp if (err)
5170 2a47b1e5 2022-11-01 stsp goto done;
5171 2a47b1e5 2022-11-01 stsp }
5172 2a47b1e5 2022-11-01 stsp
5173 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5174 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5175 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5176 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5177 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5178 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5179 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5180 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5181 2a47b1e5 2022-11-01 stsp goto done;
5182 2a47b1e5 2022-11-01 stsp }
5183 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5184 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5185 2a47b1e5 2022-11-01 stsp if (err)
5186 2a47b1e5 2022-11-01 stsp goto done;
5187 2a47b1e5 2022-11-01 stsp }
5188 2a47b1e5 2022-11-01 stsp } else {
5189 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5190 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5191 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5192 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5193 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5194 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5195 2a47b1e5 2022-11-01 stsp goto done;
5196 2a47b1e5 2022-11-01 stsp }
5197 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5198 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5199 2a47b1e5 2022-11-01 stsp if (err)
5200 2a47b1e5 2022-11-01 stsp goto done;
5201 2a47b1e5 2022-11-01 stsp }
5202 2a47b1e5 2022-11-01 stsp }
5203 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5204 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5205 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5206 2a47b1e5 2022-11-01 stsp goto done;
5207 2a47b1e5 2022-11-01 stsp }
5208 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5209 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5210 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5211 2a47b1e5 2022-11-01 stsp goto done;
5212 2a47b1e5 2022-11-01 stsp }
5213 2a47b1e5 2022-11-01 stsp fd = -1;
5214 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5215 2a47b1e5 2022-11-01 stsp }
5216 2a47b1e5 2022-11-01 stsp
5217 2a47b1e5 2022-11-01 stsp if (blob1) {
5218 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5219 2a47b1e5 2022-11-01 stsp f1, blob1);
5220 2a47b1e5 2022-11-01 stsp if (err)
5221 2a47b1e5 2022-11-01 stsp goto done;
5222 2a47b1e5 2022-11-01 stsp }
5223 2a47b1e5 2022-11-01 stsp
5224 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5225 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5226 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5227 2a47b1e5 2022-11-01 stsp done:
5228 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5229 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5230 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5231 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5232 2a47b1e5 2022-11-01 stsp if (blob1)
5233 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5234 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5235 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5236 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5237 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5238 2a47b1e5 2022-11-01 stsp return err;
5239 2a47b1e5 2022-11-01 stsp }
5240 cf066bf8 2019-05-09 stsp
5241 c4296144 2019-05-09 stsp static const struct got_error *
5242 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5243 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5244 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5245 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5246 c4296144 2019-05-09 stsp {
5247 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5248 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5249 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5250 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5251 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5252 768aea60 2019-05-09 stsp struct stat sb;
5253 c4296144 2019-05-09 stsp
5254 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5255 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5256 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5257 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5258 5f8a88c6 2019-08-03 stsp return NULL;
5259 5f8a88c6 2019-08-03 stsp } else {
5260 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5261 12383673 2023-02-18 mark printf("C %s\n", relpath);
5262 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5263 12383673 2023-02-18 mark }
5264 c4296144 2019-05-09 stsp
5265 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5266 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5267 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5268 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5269 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5270 5f8a88c6 2019-08-03 stsp return NULL;
5271 5f8a88c6 2019-08-03 stsp }
5272 0b5cc0d6 2019-05-09 stsp
5273 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5274 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5275 036813ee 2019-05-09 stsp goto done;
5276 036813ee 2019-05-09 stsp }
5277 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5278 036813ee 2019-05-09 stsp parent_path = strdup("");
5279 69960a46 2019-05-09 stsp if (parent_path == NULL)
5280 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5281 69960a46 2019-05-09 stsp } else {
5282 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5283 69960a46 2019-05-09 stsp if (err)
5284 69960a46 2019-05-09 stsp return err;
5285 69960a46 2019-05-09 stsp }
5286 c4296144 2019-05-09 stsp
5287 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5288 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5289 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5290 c4296144 2019-05-09 stsp goto done;
5291 768aea60 2019-05-09 stsp }
5292 768aea60 2019-05-09 stsp
5293 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5294 768aea60 2019-05-09 stsp relpath) == -1) {
5295 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5296 768aea60 2019-05-09 stsp goto done;
5297 768aea60 2019-05-09 stsp }
5298 0aeb8099 2020-07-23 stsp
5299 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5300 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5301 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5302 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5303 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5304 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5305 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5306 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5307 0aeb8099 2020-07-23 stsp break;
5308 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5309 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5310 0aeb8099 2020-07-23 stsp break;
5311 0aeb8099 2020-07-23 stsp default:
5312 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5313 0aeb8099 2020-07-23 stsp goto done;
5314 0aeb8099 2020-07-23 stsp }
5315 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5316 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5317 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5318 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5319 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5320 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5321 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5322 12463d8b 2019-12-13 stsp ct->ondisk_path);
5323 12463d8b 2019-12-13 stsp goto done;
5324 12463d8b 2019-12-13 stsp }
5325 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5326 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5327 768aea60 2019-05-09 stsp goto done;
5328 768aea60 2019-05-09 stsp }
5329 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5330 c4296144 2019-05-09 stsp }
5331 c4296144 2019-05-09 stsp
5332 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5333 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5334 44d03001 2019-05-09 stsp relpath) == -1) {
5335 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5336 44d03001 2019-05-09 stsp goto done;
5337 44d03001 2019-05-09 stsp }
5338 44d03001 2019-05-09 stsp
5339 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5340 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5341 35213c7c 2020-07-23 stsp int is_bad_symlink;
5342 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5343 35213c7c 2020-07-23 stsp ssize_t target_len;
5344 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5345 35213c7c 2020-07-23 stsp sizeof(target_path));
5346 35213c7c 2020-07-23 stsp if (target_len == -1) {
5347 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5348 35213c7c 2020-07-23 stsp ct->ondisk_path);
5349 35213c7c 2020-07-23 stsp goto done;
5350 35213c7c 2020-07-23 stsp }
5351 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5352 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5353 35213c7c 2020-07-23 stsp if (err)
5354 35213c7c 2020-07-23 stsp goto done;
5355 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5356 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5357 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5358 35213c7c 2020-07-23 stsp goto done;
5359 35213c7c 2020-07-23 stsp }
5360 35213c7c 2020-07-23 stsp }
5361 35213c7c 2020-07-23 stsp
5362 35213c7c 2020-07-23 stsp
5363 cf066bf8 2019-05-09 stsp ct->status = status;
5364 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5365 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5366 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5367 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5368 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5369 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5370 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5371 b416585c 2019-05-13 stsp goto done;
5372 b416585c 2019-05-13 stsp }
5373 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5374 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5375 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5376 f0b75401 2019-08-03 stsp goto done;
5377 f0b75401 2019-08-03 stsp }
5378 f0b75401 2019-08-03 stsp }
5379 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5380 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5381 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5382 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5383 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5384 036813ee 2019-05-09 stsp goto done;
5385 036813ee 2019-05-09 stsp }
5386 ca2503ea 2019-05-09 stsp }
5387 24519714 2019-05-09 stsp ct->path = strdup(path);
5388 24519714 2019-05-09 stsp if (ct->path == NULL) {
5389 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5390 24519714 2019-05-09 stsp goto done;
5391 24519714 2019-05-09 stsp }
5392 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5393 2a47b1e5 2022-11-01 stsp if (err)
5394 2a47b1e5 2022-11-01 stsp goto done;
5395 2a47b1e5 2022-11-01 stsp
5396 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5397 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5398 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5399 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5400 2a47b1e5 2022-11-01 stsp if (err)
5401 2a47b1e5 2022-11-01 stsp goto done;
5402 2a47b1e5 2022-11-01 stsp }
5403 c4296144 2019-05-09 stsp done:
5404 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5405 ed175427 2019-05-09 stsp free_commitable(ct);
5406 24519714 2019-05-09 stsp free(parent_path);
5407 036813ee 2019-05-09 stsp free(path);
5408 a129376b 2019-03-28 stsp return err;
5409 a129376b 2019-03-28 stsp }
5410 c4296144 2019-05-09 stsp
5411 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5412 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5413 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5414 036813ee 2019-05-09 stsp struct got_repository *);
5415 ed175427 2019-05-09 stsp
5416 ed175427 2019-05-09 stsp static const struct got_error *
5417 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5418 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5419 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5420 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5421 afa376bf 2019-05-09 stsp struct got_repository *repo)
5422 ed175427 2019-05-09 stsp {
5423 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5424 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5425 036813ee 2019-05-09 stsp char *subpath;
5426 ed175427 2019-05-09 stsp
5427 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5428 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5429 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5430 ed175427 2019-05-09 stsp
5431 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5432 ed175427 2019-05-09 stsp if (err)
5433 ed175427 2019-05-09 stsp return err;
5434 ed175427 2019-05-09 stsp
5435 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5436 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5437 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5438 036813ee 2019-05-09 stsp free(subpath);
5439 036813ee 2019-05-09 stsp return err;
5440 036813ee 2019-05-09 stsp }
5441 ed175427 2019-05-09 stsp
5442 036813ee 2019-05-09 stsp static const struct got_error *
5443 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5444 036813ee 2019-05-09 stsp {
5445 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5446 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5447 ed175427 2019-05-09 stsp
5448 036813ee 2019-05-09 stsp *match = 0;
5449 ed175427 2019-05-09 stsp
5450 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5451 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5452 0f63689d 2019-05-10 stsp return NULL;
5453 036813ee 2019-05-09 stsp }
5454 0b5cc0d6 2019-05-09 stsp
5455 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5456 0f63689d 2019-05-10 stsp if (err)
5457 0f63689d 2019-05-10 stsp return err;
5458 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5459 036813ee 2019-05-09 stsp free(ct_parent_path);
5460 036813ee 2019-05-09 stsp return err;
5461 036813ee 2019-05-09 stsp }
5462 0b5cc0d6 2019-05-09 stsp
5463 768aea60 2019-05-09 stsp static mode_t
5464 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5465 768aea60 2019-05-09 stsp {
5466 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5467 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5468 3d9a4ec4 2020-07-23 stsp
5469 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5470 768aea60 2019-05-09 stsp }
5471 768aea60 2019-05-09 stsp
5472 036813ee 2019-05-09 stsp static const struct got_error *
5473 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5474 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5475 036813ee 2019-05-09 stsp {
5476 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5477 ca2503ea 2019-05-09 stsp
5478 036813ee 2019-05-09 stsp *new_te = NULL;
5479 0b5cc0d6 2019-05-09 stsp
5480 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5481 036813ee 2019-05-09 stsp if (err)
5482 036813ee 2019-05-09 stsp goto done;
5483 0b5cc0d6 2019-05-09 stsp
5484 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5485 036813ee 2019-05-09 stsp
5486 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5487 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5488 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5489 5f8a88c6 2019-08-03 stsp else
5490 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5491 036813ee 2019-05-09 stsp done:
5492 036813ee 2019-05-09 stsp if (err && *new_te) {
5493 56e0773d 2019-11-28 stsp free(*new_te);
5494 036813ee 2019-05-09 stsp *new_te = NULL;
5495 036813ee 2019-05-09 stsp }
5496 036813ee 2019-05-09 stsp return err;
5497 036813ee 2019-05-09 stsp }
5498 036813ee 2019-05-09 stsp
5499 036813ee 2019-05-09 stsp static const struct got_error *
5500 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5501 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5502 036813ee 2019-05-09 stsp {
5503 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5504 102b254e 2020-10-19 stsp char *ct_name = NULL;
5505 036813ee 2019-05-09 stsp
5506 036813ee 2019-05-09 stsp *new_te = NULL;
5507 036813ee 2019-05-09 stsp
5508 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5509 036813ee 2019-05-09 stsp if (*new_te == NULL)
5510 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5511 036813ee 2019-05-09 stsp
5512 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5513 102b254e 2020-10-19 stsp if (err)
5514 036813ee 2019-05-09 stsp goto done;
5515 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5516 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5517 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5518 036813ee 2019-05-09 stsp goto done;
5519 036813ee 2019-05-09 stsp }
5520 036813ee 2019-05-09 stsp
5521 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5522 036813ee 2019-05-09 stsp
5523 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5524 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5525 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5526 5f8a88c6 2019-08-03 stsp else
5527 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5528 036813ee 2019-05-09 stsp done:
5529 102b254e 2020-10-19 stsp free(ct_name);
5530 036813ee 2019-05-09 stsp if (err && *new_te) {
5531 56e0773d 2019-11-28 stsp free(*new_te);
5532 036813ee 2019-05-09 stsp *new_te = NULL;
5533 036813ee 2019-05-09 stsp }
5534 036813ee 2019-05-09 stsp return err;
5535 036813ee 2019-05-09 stsp }
5536 036813ee 2019-05-09 stsp
5537 036813ee 2019-05-09 stsp static const struct got_error *
5538 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5539 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5540 036813ee 2019-05-09 stsp {
5541 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5542 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5543 036813ee 2019-05-09 stsp
5544 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5545 036813ee 2019-05-09 stsp if (err)
5546 036813ee 2019-05-09 stsp return err;
5547 036813ee 2019-05-09 stsp if (new_pe == NULL)
5548 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5549 036813ee 2019-05-09 stsp return NULL;
5550 afa376bf 2019-05-09 stsp }
5551 afa376bf 2019-05-09 stsp
5552 afa376bf 2019-05-09 stsp static const struct got_error *
5553 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5554 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5555 afa376bf 2019-05-09 stsp {
5556 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5557 5f8a88c6 2019-08-03 stsp unsigned char status;
5558 0ff8d236 2021-09-28 stsp
5559 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5560 0ff8d236 2021-09-28 stsp return NULL;
5561 5f8a88c6 2019-08-03 stsp
5562 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5563 afa376bf 2019-05-09 stsp ct_path++;
5564 5f8a88c6 2019-08-03 stsp
5565 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5566 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5567 5f8a88c6 2019-08-03 stsp else
5568 5f8a88c6 2019-08-03 stsp status = ct->status;
5569 5f8a88c6 2019-08-03 stsp
5570 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5571 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5572 036813ee 2019-05-09 stsp }
5573 036813ee 2019-05-09 stsp
5574 036813ee 2019-05-09 stsp static const struct got_error *
5575 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5576 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5577 44d03001 2019-05-09 stsp {
5578 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5579 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5580 44d03001 2019-05-09 stsp char *te_path;
5581 44d03001 2019-05-09 stsp
5582 44d03001 2019-05-09 stsp *modified = 0;
5583 44d03001 2019-05-09 stsp
5584 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5585 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5586 44d03001 2019-05-09 stsp te->name) == -1)
5587 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5588 44d03001 2019-05-09 stsp
5589 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5590 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5591 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5592 44d03001 2019-05-09 stsp strlen(te_path));
5593 62d463ca 2020-10-20 naddy if (*modified)
5594 44d03001 2019-05-09 stsp break;
5595 44d03001 2019-05-09 stsp }
5596 44d03001 2019-05-09 stsp
5597 44d03001 2019-05-09 stsp free(te_path);
5598 44d03001 2019-05-09 stsp return err;
5599 44d03001 2019-05-09 stsp }
5600 44d03001 2019-05-09 stsp
5601 44d03001 2019-05-09 stsp static const struct got_error *
5602 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5603 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5604 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5605 036813ee 2019-05-09 stsp {
5606 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5607 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5608 036813ee 2019-05-09 stsp
5609 036813ee 2019-05-09 stsp *ctp = NULL;
5610 036813ee 2019-05-09 stsp
5611 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5612 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5613 036813ee 2019-05-09 stsp char *ct_name = NULL;
5614 036813ee 2019-05-09 stsp int path_matches;
5615 036813ee 2019-05-09 stsp
5616 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5617 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5618 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5619 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5620 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5621 5f8a88c6 2019-08-03 stsp continue;
5622 5f8a88c6 2019-08-03 stsp } else {
5623 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5624 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5625 5f8a88c6 2019-08-03 stsp continue;
5626 5f8a88c6 2019-08-03 stsp }
5627 036813ee 2019-05-09 stsp
5628 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5629 036813ee 2019-05-09 stsp continue;
5630 036813ee 2019-05-09 stsp
5631 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5632 62d463ca 2020-10-20 naddy if (err)
5633 036813ee 2019-05-09 stsp return err;
5634 036813ee 2019-05-09 stsp if (!path_matches)
5635 036813ee 2019-05-09 stsp continue;
5636 036813ee 2019-05-09 stsp
5637 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5638 d34b633e 2020-10-19 stsp if (err)
5639 d34b633e 2020-10-19 stsp return err;
5640 d34b633e 2020-10-19 stsp
5641 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5642 d34b633e 2020-10-19 stsp free(ct_name);
5643 036813ee 2019-05-09 stsp continue;
5644 d34b633e 2020-10-19 stsp }
5645 d34b633e 2020-10-19 stsp free(ct_name);
5646 036813ee 2019-05-09 stsp
5647 036813ee 2019-05-09 stsp *ctp = ct;
5648 036813ee 2019-05-09 stsp break;
5649 036813ee 2019-05-09 stsp }
5650 2b496619 2019-07-10 stsp
5651 2b496619 2019-07-10 stsp return err;
5652 2b496619 2019-07-10 stsp }
5653 2b496619 2019-07-10 stsp
5654 2b496619 2019-07-10 stsp static const struct got_error *
5655 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5656 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5657 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5658 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5659 2b496619 2019-07-10 stsp struct got_repository *repo)
5660 2b496619 2019-07-10 stsp {
5661 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5662 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5663 2b496619 2019-07-10 stsp char *subtree_path;
5664 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5665 ba580f68 2020-03-22 stsp int nentries;
5666 2b496619 2019-07-10 stsp
5667 2b496619 2019-07-10 stsp *new_tep = NULL;
5668 2b496619 2019-07-10 stsp
5669 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5670 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5671 2b496619 2019-07-10 stsp child_path) == -1)
5672 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5673 036813ee 2019-05-09 stsp
5674 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5675 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5676 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5677 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5678 56e0773d 2019-11-28 stsp
5679 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5680 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5681 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5682 2b496619 2019-07-10 stsp goto done;
5683 2b496619 2019-07-10 stsp }
5684 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5685 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5686 2b496619 2019-07-10 stsp if (err) {
5687 56e0773d 2019-11-28 stsp free(new_te);
5688 2b496619 2019-07-10 stsp goto done;
5689 2b496619 2019-07-10 stsp }
5690 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5691 2b496619 2019-07-10 stsp done:
5692 56e0773d 2019-11-28 stsp free(id);
5693 2b496619 2019-07-10 stsp free(subtree_path);
5694 2b496619 2019-07-10 stsp if (err == NULL)
5695 2b496619 2019-07-10 stsp *new_tep = new_te;
5696 036813ee 2019-05-09 stsp return err;
5697 036813ee 2019-05-09 stsp }
5698 036813ee 2019-05-09 stsp
5699 036813ee 2019-05-09 stsp static const struct got_error *
5700 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5701 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5702 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5703 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5704 036813ee 2019-05-09 stsp struct got_repository *repo)
5705 036813ee 2019-05-09 stsp {
5706 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5707 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5708 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5709 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5710 036813ee 2019-05-09 stsp
5711 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5712 ba580f68 2020-03-22 stsp *nentries = 0;
5713 036813ee 2019-05-09 stsp
5714 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5715 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5716 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5717 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5718 036813ee 2019-05-09 stsp
5719 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5720 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5721 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5722 036813ee 2019-05-09 stsp continue;
5723 036813ee 2019-05-09 stsp
5724 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5725 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5726 036813ee 2019-05-09 stsp continue;
5727 036813ee 2019-05-09 stsp
5728 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5729 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5730 036813ee 2019-05-09 stsp if (err)
5731 036813ee 2019-05-09 stsp goto done;
5732 036813ee 2019-05-09 stsp
5733 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5734 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5735 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5736 036813ee 2019-05-09 stsp if (err)
5737 036813ee 2019-05-09 stsp goto done;
5738 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5739 afa376bf 2019-05-09 stsp if (err)
5740 afa376bf 2019-05-09 stsp goto done;
5741 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5742 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5743 2b496619 2019-07-10 stsp if (err)
5744 2f51b5b3 2019-05-09 stsp goto done;
5745 ba580f68 2020-03-22 stsp (*nentries)++;
5746 2b496619 2019-07-10 stsp } else {
5747 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5748 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5749 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5750 2b496619 2019-07-10 stsp == NULL) {
5751 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5752 2b496619 2019-07-10 stsp child_path, path_base_tree,
5753 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5754 2b496619 2019-07-10 stsp repo);
5755 2b496619 2019-07-10 stsp if (err)
5756 2b496619 2019-07-10 stsp goto done;
5757 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5758 2b496619 2019-07-10 stsp if (err)
5759 2b496619 2019-07-10 stsp goto done;
5760 ba580f68 2020-03-22 stsp (*nentries)++;
5761 9ba0479c 2019-05-10 stsp }
5762 ed175427 2019-05-09 stsp }
5763 2f51b5b3 2019-05-09 stsp }
5764 2f51b5b3 2019-05-09 stsp
5765 2f51b5b3 2019-05-09 stsp if (base_tree) {
5766 56e0773d 2019-11-28 stsp int i, nbase_entries;
5767 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5768 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5769 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5770 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5771 63c5ca5d 2019-08-24 stsp
5772 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5773 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5774 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5775 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5776 63c5ca5d 2019-08-24 stsp if (err)
5777 63c5ca5d 2019-08-24 stsp goto done;
5778 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5779 63c5ca5d 2019-08-24 stsp if (err)
5780 63c5ca5d 2019-08-24 stsp goto done;
5781 ba580f68 2020-03-22 stsp (*nentries)++;
5782 63c5ca5d 2019-08-24 stsp continue;
5783 63c5ca5d 2019-08-24 stsp }
5784 2f51b5b3 2019-05-09 stsp
5785 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5786 44d03001 2019-05-09 stsp int modified;
5787 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5788 036813ee 2019-05-09 stsp if (err)
5789 036813ee 2019-05-09 stsp goto done;
5790 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5791 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5792 2f51b5b3 2019-05-09 stsp if (err)
5793 2f51b5b3 2019-05-09 stsp goto done;
5794 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5795 44d03001 2019-05-09 stsp if (modified) {
5796 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5797 ba580f68 2020-03-22 stsp int nsubentries;
5798 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5799 ba580f68 2020-03-22 stsp &nsubentries, te,
5800 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5801 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5802 44d03001 2019-05-09 stsp if (err)
5803 44d03001 2019-05-09 stsp goto done;
5804 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5805 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5806 ba580f68 2020-03-22 stsp free(new_id);
5807 ba580f68 2020-03-22 stsp continue;
5808 ba580f68 2020-03-22 stsp }
5809 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5810 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5811 56e0773d 2019-11-28 stsp free(new_id);
5812 44d03001 2019-05-09 stsp }
5813 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5814 036813ee 2019-05-09 stsp if (err)
5815 036813ee 2019-05-09 stsp goto done;
5816 ba580f68 2020-03-22 stsp (*nentries)++;
5817 2f51b5b3 2019-05-09 stsp continue;
5818 036813ee 2019-05-09 stsp }
5819 2f51b5b3 2019-05-09 stsp
5820 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5821 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5822 f66c734c 2019-09-22 stsp if (err)
5823 f66c734c 2019-09-22 stsp goto done;
5824 2f51b5b3 2019-05-09 stsp if (ct) {
5825 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5826 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5827 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5828 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
5829 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5830 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5831 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5832 2f51b5b3 2019-05-09 stsp if (err)
5833 2f51b5b3 2019-05-09 stsp goto done;
5834 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5835 2f51b5b3 2019-05-09 stsp if (err)
5836 2f51b5b3 2019-05-09 stsp goto done;
5837 ba580f68 2020-03-22 stsp (*nentries)++;
5838 2f51b5b3 2019-05-09 stsp }
5839 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5840 b416585c 2019-05-13 stsp status_arg);
5841 afa376bf 2019-05-09 stsp if (err)
5842 afa376bf 2019-05-09 stsp goto done;
5843 2f51b5b3 2019-05-09 stsp } else {
5844 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5845 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5846 2f51b5b3 2019-05-09 stsp if (err)
5847 2f51b5b3 2019-05-09 stsp goto done;
5848 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5849 2f51b5b3 2019-05-09 stsp if (err)
5850 2f51b5b3 2019-05-09 stsp goto done;
5851 ba580f68 2020-03-22 stsp (*nentries)++;
5852 2f51b5b3 2019-05-09 stsp }
5853 ca2503ea 2019-05-09 stsp }
5854 ed175427 2019-05-09 stsp }
5855 0b5cc0d6 2019-05-09 stsp
5856 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5857 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5858 ed175427 2019-05-09 stsp done:
5859 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5860 2e1fa222 2020-07-23 stsp return err;
5861 2e1fa222 2020-07-23 stsp }
5862 2e1fa222 2020-07-23 stsp
5863 2e1fa222 2020-07-23 stsp static const struct got_error *
5864 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5865 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5866 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5867 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5868 ebf99748 2019-05-09 stsp {
5869 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5870 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5871 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5872 ebf99748 2019-05-09 stsp
5873 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5874 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5875 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5876 ebf99748 2019-05-09 stsp
5877 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5878 437adc9d 2020-12-10 yzhong
5879 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5880 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5881 437adc9d 2020-12-10 yzhong if (err)
5882 437adc9d 2020-12-10 yzhong goto done;
5883 437adc9d 2020-12-10 yzhong
5884 ebf99748 2019-05-09 stsp if (ie) {
5885 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5886 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5887 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5888 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5889 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5890 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5891 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5892 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5893 437adc9d 2020-12-10 yzhong
5894 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5895 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5896 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5897 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5898 72fd46fa 2019-09-06 stsp !have_staged_files);
5899 ebf99748 2019-05-09 stsp } else
5900 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5901 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5902 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5903 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5904 72fd46fa 2019-09-06 stsp !have_staged_files);
5905 ebf99748 2019-05-09 stsp } else {
5906 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5907 ebf99748 2019-05-09 stsp if (err)
5908 437adc9d 2020-12-10 yzhong goto done;
5909 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5910 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5911 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5912 3969253a 2020-03-07 stsp if (err) {
5913 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5914 437adc9d 2020-12-10 yzhong goto done;
5915 3969253a 2020-03-07 stsp }
5916 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5917 3969253a 2020-03-07 stsp if (err) {
5918 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5919 437adc9d 2020-12-10 yzhong goto done;
5920 3969253a 2020-03-07 stsp }
5921 ebf99748 2019-05-09 stsp }
5922 437adc9d 2020-12-10 yzhong free(relpath);
5923 437adc9d 2020-12-10 yzhong relpath = NULL;
5924 ebf99748 2019-05-09 stsp }
5925 437adc9d 2020-12-10 yzhong done:
5926 437adc9d 2020-12-10 yzhong free(relpath);
5927 d56d26ce 2019-05-10 stsp return err;
5928 d56d26ce 2019-05-10 stsp }
5929 735ef5ac 2019-08-03 stsp
5930 d56d26ce 2019-05-10 stsp
5931 d56d26ce 2019-05-10 stsp static const struct got_error *
5932 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5933 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5934 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5935 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5936 735ef5ac 2019-08-03 stsp int ood_errcode)
5937 d56d26ce 2019-05-10 stsp {
5938 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5939 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5940 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5941 1a36436d 2019-06-10 stsp
5942 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5943 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5944 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5945 1a36436d 2019-06-10 stsp return NULL;
5946 9bead371 2019-07-28 stsp /*
5947 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5948 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5949 9bead371 2019-07-28 stsp */
5950 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5951 a44927cc 2022-04-07 stsp if (err)
5952 a44927cc 2022-04-07 stsp goto done;
5953 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5954 9bead371 2019-07-28 stsp if (err) {
5955 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5956 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5957 1a36436d 2019-06-10 stsp goto done;
5958 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5959 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5960 1a36436d 2019-06-10 stsp } else {
5961 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5962 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5963 a44927cc 2022-04-07 stsp if (err)
5964 a44927cc 2022-04-07 stsp goto done;
5965 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5966 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5967 1a36436d 2019-06-10 stsp goto done;
5968 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5969 1a36436d 2019-06-10 stsp }
5970 1a36436d 2019-06-10 stsp done:
5971 1a36436d 2019-06-10 stsp free(id);
5972 a44927cc 2022-04-07 stsp if (commit)
5973 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5974 1a36436d 2019-06-10 stsp return err;
5975 ebf99748 2019-05-09 stsp }
5976 ebf99748 2019-05-09 stsp
5977 336075a4 2022-06-25 op static const struct got_error *
5978 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5979 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5980 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5981 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5982 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5983 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5984 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5985 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5986 afa376bf 2019-05-09 stsp struct got_repository *repo)
5987 c4296144 2019-05-09 stsp {
5988 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5989 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5990 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5991 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5992 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5993 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5994 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5995 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5996 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5997 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5998 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5999 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6000 f8399b8f 2022-07-22 stsp time_t timestamp;
6001 c4296144 2019-05-09 stsp
6002 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6003 c4296144 2019-05-09 stsp
6004 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6005 675c7539 2019-05-09 stsp
6006 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6007 588edf97 2019-05-10 stsp if (err)
6008 588edf97 2019-05-10 stsp goto done;
6009 de18fc63 2019-05-09 stsp
6010 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6011 588edf97 2019-05-10 stsp if (err)
6012 588edf97 2019-05-10 stsp goto done;
6013 588edf97 2019-05-10 stsp
6014 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6015 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6016 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6017 33ad4cbe 2019-05-12 jcs if (err)
6018 33ad4cbe 2019-05-12 jcs goto done;
6019 33ad4cbe 2019-05-12 jcs }
6020 c4296144 2019-05-09 stsp
6021 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6022 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6023 33ad4cbe 2019-05-12 jcs goto done;
6024 33ad4cbe 2019-05-12 jcs }
6025 33ad4cbe 2019-05-12 jcs
6026 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6027 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6028 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6029 cf066bf8 2019-05-09 stsp char *ondisk_path;
6030 cf066bf8 2019-05-09 stsp
6031 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6032 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6033 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6034 5f8a88c6 2019-08-03 stsp continue;
6035 5f8a88c6 2019-08-03 stsp
6036 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6037 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6038 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6039 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6040 cf066bf8 2019-05-09 stsp continue;
6041 cf066bf8 2019-05-09 stsp
6042 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6043 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6044 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6045 cf066bf8 2019-05-09 stsp goto done;
6046 cf066bf8 2019-05-09 stsp }
6047 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6048 2e1fa222 2020-07-23 stsp free(ondisk_path);
6049 2e1fa222 2020-07-23 stsp if (err)
6050 cf066bf8 2019-05-09 stsp goto done;
6051 cf066bf8 2019-05-09 stsp }
6052 036813ee 2019-05-09 stsp
6053 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6054 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6055 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6056 036813ee 2019-05-09 stsp if (err)
6057 036813ee 2019-05-09 stsp goto done;
6058 036813ee 2019-05-09 stsp
6059 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6060 de18fc63 2019-05-09 stsp if (err)
6061 de18fc63 2019-05-09 stsp goto done;
6062 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6063 f259c4c1 2021-09-24 stsp nparents++;
6064 f259c4c1 2021-09-24 stsp if (parent_id2) {
6065 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6066 f259c4c1 2021-09-24 stsp if (err)
6067 f259c4c1 2021-09-24 stsp goto done;
6068 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6069 f259c4c1 2021-09-24 stsp nparents++;
6070 f259c4c1 2021-09-24 stsp }
6071 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6072 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6073 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6074 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6075 33ad4cbe 2019-05-12 jcs free(logmsg);
6076 9d40349a 2019-05-09 stsp if (err)
6077 9d40349a 2019-05-09 stsp goto done;
6078 9d40349a 2019-05-09 stsp
6079 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6080 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6081 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6082 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6083 09f5bd90 2019-05-09 stsp goto done;
6084 09f5bd90 2019-05-09 stsp }
6085 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6086 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6087 09f5bd90 2019-05-09 stsp if (err)
6088 09f5bd90 2019-05-09 stsp goto done;
6089 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6090 ebf99748 2019-05-09 stsp if (err)
6091 ebf99748 2019-05-09 stsp goto done;
6092 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6093 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6094 09f5bd90 2019-05-09 stsp goto done;
6095 09f5bd90 2019-05-09 stsp }
6096 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6097 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6098 f2c16586 2019-05-09 stsp if (err)
6099 f2c16586 2019-05-09 stsp goto done;
6100 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6101 f2c16586 2019-05-09 stsp if (err)
6102 f2c16586 2019-05-09 stsp goto done;
6103 f2c16586 2019-05-09 stsp
6104 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6105 09f5bd90 2019-05-09 stsp if (err)
6106 09f5bd90 2019-05-09 stsp goto done;
6107 09f5bd90 2019-05-09 stsp
6108 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6109 ebf99748 2019-05-09 stsp if (err)
6110 ebf99748 2019-05-09 stsp goto done;
6111 c4296144 2019-05-09 stsp done:
6112 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6113 588edf97 2019-05-10 stsp if (head_tree)
6114 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6115 588edf97 2019-05-10 stsp if (head_commit)
6116 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6117 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6118 2f17228e 2019-05-12 stsp if (head_ref2) {
6119 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6120 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6121 2f17228e 2019-05-12 stsp err = unlockerr;
6122 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6123 39cd0ff6 2019-07-12 stsp }
6124 39cd0ff6 2019-07-12 stsp return err;
6125 39cd0ff6 2019-07-12 stsp }
6126 5c1e53bc 2019-07-28 stsp
6127 5c1e53bc 2019-07-28 stsp static const struct got_error *
6128 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6129 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6130 5c1e53bc 2019-07-28 stsp {
6131 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6132 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6133 39cd0ff6 2019-07-12 stsp
6134 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6135 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6136 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6137 5c1e53bc 2019-07-28 stsp
6138 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6139 5c1e53bc 2019-07-28 stsp ct_path++;
6140 5c1e53bc 2019-07-28 stsp
6141 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6142 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6143 5c1e53bc 2019-07-28 stsp break;
6144 5c1e53bc 2019-07-28 stsp }
6145 5c1e53bc 2019-07-28 stsp
6146 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6147 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6148 5c1e53bc 2019-07-28 stsp
6149 5c1e53bc 2019-07-28 stsp return NULL;
6150 5c1e53bc 2019-07-28 stsp }
6151 5c1e53bc 2019-07-28 stsp
6152 f0b75401 2019-08-03 stsp static const struct got_error *
6153 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6154 f0b75401 2019-08-03 stsp {
6155 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6156 f0b75401 2019-08-03 stsp
6157 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6158 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6159 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6160 f0b75401 2019-08-03 stsp }
6161 f0b75401 2019-08-03 stsp
6162 f0b75401 2019-08-03 stsp return NULL;
6163 f0b75401 2019-08-03 stsp }
6164 f0b75401 2019-08-03 stsp
6165 5f8a88c6 2019-08-03 stsp static const struct got_error *
6166 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6167 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6168 5f8a88c6 2019-08-03 stsp {
6169 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6170 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6171 5f8a88c6 2019-08-03 stsp
6172 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6173 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6174 5f8a88c6 2019-08-03 stsp continue;
6175 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6176 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6177 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6178 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6179 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6180 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6181 5f8a88c6 2019-08-03 stsp }
6182 5f8a88c6 2019-08-03 stsp
6183 5f8a88c6 2019-08-03 stsp return NULL;
6184 5f8a88c6 2019-08-03 stsp }
6185 5f8a88c6 2019-08-03 stsp
6186 39cd0ff6 2019-07-12 stsp const struct got_error *
6187 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6188 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6189 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6190 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6191 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6192 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6193 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6194 39cd0ff6 2019-07-12 stsp {
6195 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6196 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6197 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6198 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6199 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6200 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6201 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6202 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6203 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6204 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6205 39cd0ff6 2019-07-12 stsp
6206 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6207 39cd0ff6 2019-07-12 stsp
6208 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6209 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6210 39cd0ff6 2019-07-12 stsp
6211 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6212 39cd0ff6 2019-07-12 stsp if (err)
6213 39cd0ff6 2019-07-12 stsp goto done;
6214 39cd0ff6 2019-07-12 stsp
6215 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6216 39cd0ff6 2019-07-12 stsp if (err)
6217 39cd0ff6 2019-07-12 stsp goto done;
6218 39cd0ff6 2019-07-12 stsp
6219 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6220 39cd0ff6 2019-07-12 stsp if (err)
6221 39cd0ff6 2019-07-12 stsp goto done;
6222 39cd0ff6 2019-07-12 stsp
6223 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6224 39cd0ff6 2019-07-12 stsp if (err)
6225 39cd0ff6 2019-07-12 stsp goto done;
6226 39cd0ff6 2019-07-12 stsp
6227 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6228 5f8a88c6 2019-08-03 stsp &have_staged_files);
6229 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6230 5f8a88c6 2019-08-03 stsp goto done;
6231 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6232 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6233 5f8a88c6 2019-08-03 stsp if (err)
6234 5f8a88c6 2019-08-03 stsp goto done;
6235 5f8a88c6 2019-08-03 stsp }
6236 5f8a88c6 2019-08-03 stsp
6237 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6238 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6239 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6240 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6241 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6242 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6243 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6244 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6245 2a47b1e5 2022-11-01 stsp if (show_diff) {
6246 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6247 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6248 2a47b1e5 2022-11-01 stsp if (err)
6249 2a47b1e5 2022-11-01 stsp goto done;
6250 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6251 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6252 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6253 2a47b1e5 2022-11-01 stsp goto done;
6254 2a47b1e5 2022-11-01 stsp }
6255 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6256 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6257 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6258 2a47b1e5 2022-11-01 stsp goto done;
6259 2a47b1e5 2022-11-01 stsp }
6260 2a47b1e5 2022-11-01 stsp }
6261 2a47b1e5 2022-11-01 stsp
6262 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6263 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6264 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6265 5c1e53bc 2019-07-28 stsp if (err)
6266 5c1e53bc 2019-07-28 stsp goto done;
6267 5c1e53bc 2019-07-28 stsp }
6268 39cd0ff6 2019-07-12 stsp
6269 2a47b1e5 2022-11-01 stsp if (show_diff) {
6270 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6271 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6272 2a47b1e5 2022-11-01 stsp goto done;
6273 2a47b1e5 2022-11-01 stsp }
6274 2a47b1e5 2022-11-01 stsp }
6275 2a47b1e5 2022-11-01 stsp
6276 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6277 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6278 39cd0ff6 2019-07-12 stsp goto done;
6279 5c1e53bc 2019-07-28 stsp }
6280 5c1e53bc 2019-07-28 stsp
6281 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6282 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6283 5c1e53bc 2019-07-28 stsp if (err)
6284 5c1e53bc 2019-07-28 stsp goto done;
6285 39cd0ff6 2019-07-12 stsp }
6286 f0b75401 2019-08-03 stsp
6287 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6288 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6289 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6290 f0b75401 2019-08-03 stsp
6291 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6292 f0b75401 2019-08-03 stsp ct_path++;
6293 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6294 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6295 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6296 b50cabdf 2019-07-12 stsp if (err)
6297 b50cabdf 2019-07-12 stsp goto done;
6298 f0b75401 2019-08-03 stsp
6299 b50cabdf 2019-07-12 stsp }
6300 b50cabdf 2019-07-12 stsp
6301 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6302 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6303 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6304 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6305 39cd0ff6 2019-07-12 stsp if (err)
6306 39cd0ff6 2019-07-12 stsp goto done;
6307 39cd0ff6 2019-07-12 stsp
6308 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6309 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6310 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6311 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6312 39cd0ff6 2019-07-12 stsp err = sync_err;
6313 39cd0ff6 2019-07-12 stsp done:
6314 39cd0ff6 2019-07-12 stsp if (fileindex)
6315 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6316 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6317 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6318 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6319 39cd0ff6 2019-07-12 stsp err = unlockerr;
6320 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6321 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6322 d8bacb93 2023-01-10 mark
6323 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6324 39cd0ff6 2019-07-12 stsp }
6325 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6326 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6327 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6328 2a47b1e5 2022-11-01 stsp free(diff_path);
6329 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6330 2a47b1e5 2022-11-01 stsp err == NULL)
6331 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6332 c4296144 2019-05-09 stsp return err;
6333 8656d6c4 2019-05-20 stsp }
6334 8656d6c4 2019-05-20 stsp
6335 8656d6c4 2019-05-20 stsp const char *
6336 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6337 8656d6c4 2019-05-20 stsp {
6338 8656d6c4 2019-05-20 stsp return ct->path;
6339 8656d6c4 2019-05-20 stsp }
6340 8656d6c4 2019-05-20 stsp
6341 8656d6c4 2019-05-20 stsp unsigned int
6342 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6343 8656d6c4 2019-05-20 stsp {
6344 8656d6c4 2019-05-20 stsp return ct->status;
6345 818c7501 2019-07-11 stsp }
6346 818c7501 2019-07-11 stsp
6347 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6348 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6349 818c7501 2019-07-11 stsp struct got_repository *repo;
6350 818c7501 2019-07-11 stsp };
6351 818c7501 2019-07-11 stsp
6352 818c7501 2019-07-11 stsp static const struct got_error *
6353 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6354 818c7501 2019-07-11 stsp {
6355 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6356 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6357 818c7501 2019-07-11 stsp unsigned char status;
6358 818c7501 2019-07-11 stsp struct stat sb;
6359 818c7501 2019-07-11 stsp char *ondisk_path;
6360 818c7501 2019-07-11 stsp
6361 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6362 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6363 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6364 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6365 818c7501 2019-07-11 stsp
6366 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6367 818c7501 2019-07-11 stsp == -1)
6368 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6369 818c7501 2019-07-11 stsp
6370 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6371 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6372 818c7501 2019-07-11 stsp free(ondisk_path);
6373 818c7501 2019-07-11 stsp if (err)
6374 818c7501 2019-07-11 stsp return err;
6375 818c7501 2019-07-11 stsp
6376 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6377 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6378 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6379 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6380 818c7501 2019-07-11 stsp
6381 818c7501 2019-07-11 stsp return NULL;
6382 818c7501 2019-07-11 stsp }
6383 818c7501 2019-07-11 stsp
6384 818c7501 2019-07-11 stsp const struct got_error *
6385 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6386 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6387 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6388 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6389 818c7501 2019-07-11 stsp {
6390 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6391 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6392 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6393 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6394 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6395 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6396 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6397 818c7501 2019-07-11 stsp
6398 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6399 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6400 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6401 818c7501 2019-07-11 stsp
6402 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6403 818c7501 2019-07-11 stsp if (err)
6404 818c7501 2019-07-11 stsp return err;
6405 818c7501 2019-07-11 stsp
6406 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6407 818c7501 2019-07-11 stsp if (err)
6408 818c7501 2019-07-11 stsp goto done;
6409 818c7501 2019-07-11 stsp
6410 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6411 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6412 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6413 818c7501 2019-07-11 stsp &ok_arg);
6414 818c7501 2019-07-11 stsp if (err)
6415 818c7501 2019-07-11 stsp goto done;
6416 818c7501 2019-07-11 stsp
6417 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6418 818c7501 2019-07-11 stsp if (err)
6419 818c7501 2019-07-11 stsp goto done;
6420 818c7501 2019-07-11 stsp
6421 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6422 818c7501 2019-07-11 stsp if (err)
6423 818c7501 2019-07-11 stsp goto done;
6424 818c7501 2019-07-11 stsp
6425 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6426 818c7501 2019-07-11 stsp if (err)
6427 818c7501 2019-07-11 stsp goto done;
6428 818c7501 2019-07-11 stsp
6429 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6430 818c7501 2019-07-11 stsp 0);
6431 e51d7b55 2020-01-04 stsp if (err)
6432 e51d7b55 2020-01-04 stsp goto done;
6433 e51d7b55 2020-01-04 stsp
6434 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6435 818c7501 2019-07-11 stsp if (err)
6436 818c7501 2019-07-11 stsp goto done;
6437 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6438 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6439 e51d7b55 2020-01-04 stsp goto done;
6440 e51d7b55 2020-01-04 stsp }
6441 818c7501 2019-07-11 stsp
6442 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6443 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6444 818c7501 2019-07-11 stsp if (err)
6445 818c7501 2019-07-11 stsp goto done;
6446 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6447 818c7501 2019-07-11 stsp if (err)
6448 818c7501 2019-07-11 stsp goto done;
6449 818c7501 2019-07-11 stsp
6450 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6451 818c7501 2019-07-11 stsp
6452 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6453 818c7501 2019-07-11 stsp if (err)
6454 818c7501 2019-07-11 stsp goto done;
6455 818c7501 2019-07-11 stsp
6456 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6457 818c7501 2019-07-11 stsp if (err)
6458 818c7501 2019-07-11 stsp goto done;
6459 818c7501 2019-07-11 stsp
6460 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6461 818c7501 2019-07-11 stsp worktree->base_commit_id);
6462 818c7501 2019-07-11 stsp if (err)
6463 818c7501 2019-07-11 stsp goto done;
6464 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6465 818c7501 2019-07-11 stsp if (err)
6466 818c7501 2019-07-11 stsp goto done;
6467 818c7501 2019-07-11 stsp
6468 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6469 818c7501 2019-07-11 stsp if (err)
6470 818c7501 2019-07-11 stsp goto done;
6471 818c7501 2019-07-11 stsp done:
6472 818c7501 2019-07-11 stsp free(fileindex_path);
6473 818c7501 2019-07-11 stsp free(tmp_branch_name);
6474 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6475 818c7501 2019-07-11 stsp free(branch_ref_name);
6476 818c7501 2019-07-11 stsp if (branch_ref)
6477 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6478 818c7501 2019-07-11 stsp if (wt_branch)
6479 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6480 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6481 818c7501 2019-07-11 stsp if (err) {
6482 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6483 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6484 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6485 818c7501 2019-07-11 stsp }
6486 818c7501 2019-07-11 stsp if (*tmp_branch) {
6487 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6488 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6489 818c7501 2019-07-11 stsp }
6490 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6491 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6492 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6493 3e3a69f1 2019-07-25 stsp }
6494 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6495 818c7501 2019-07-11 stsp }
6496 818c7501 2019-07-11 stsp return err;
6497 818c7501 2019-07-11 stsp }
6498 818c7501 2019-07-11 stsp
6499 818c7501 2019-07-11 stsp const struct got_error *
6500 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6501 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6502 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6503 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6504 818c7501 2019-07-11 stsp {
6505 818c7501 2019-07-11 stsp const struct got_error *err;
6506 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6507 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6508 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6509 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6510 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6511 818c7501 2019-07-11 stsp
6512 818c7501 2019-07-11 stsp *commit_id = NULL;
6513 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6514 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6515 3e3a69f1 2019-07-25 stsp *branch = NULL;
6516 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6517 3e3a69f1 2019-07-25 stsp
6518 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6519 3e3a69f1 2019-07-25 stsp if (err)
6520 3e3a69f1 2019-07-25 stsp return err;
6521 818c7501 2019-07-11 stsp
6522 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6523 3e3a69f1 2019-07-25 stsp if (err)
6524 f032f1f7 2019-08-04 stsp goto done;
6525 f032f1f7 2019-08-04 stsp
6526 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6527 f032f1f7 2019-08-04 stsp &have_staged_files);
6528 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6529 f032f1f7 2019-08-04 stsp goto done;
6530 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6531 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6532 3e3a69f1 2019-07-25 stsp goto done;
6533 f032f1f7 2019-08-04 stsp }
6534 3e3a69f1 2019-07-25 stsp
6535 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6536 818c7501 2019-07-11 stsp if (err)
6537 f032f1f7 2019-08-04 stsp goto done;
6538 818c7501 2019-07-11 stsp
6539 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6540 818c7501 2019-07-11 stsp if (err)
6541 818c7501 2019-07-11 stsp goto done;
6542 818c7501 2019-07-11 stsp
6543 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6544 818c7501 2019-07-11 stsp if (err)
6545 818c7501 2019-07-11 stsp goto done;
6546 818c7501 2019-07-11 stsp
6547 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6548 818c7501 2019-07-11 stsp if (err)
6549 818c7501 2019-07-11 stsp goto done;
6550 818c7501 2019-07-11 stsp
6551 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6552 818c7501 2019-07-11 stsp if (err)
6553 818c7501 2019-07-11 stsp goto done;
6554 818c7501 2019-07-11 stsp
6555 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6556 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6557 818c7501 2019-07-11 stsp if (err)
6558 818c7501 2019-07-11 stsp goto done;
6559 818c7501 2019-07-11 stsp
6560 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6561 818c7501 2019-07-11 stsp if (err)
6562 818c7501 2019-07-11 stsp goto done;
6563 818c7501 2019-07-11 stsp
6564 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6565 818c7501 2019-07-11 stsp if (err)
6566 818c7501 2019-07-11 stsp goto done;
6567 818c7501 2019-07-11 stsp
6568 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6569 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6570 818c7501 2019-07-11 stsp if (err)
6571 818c7501 2019-07-11 stsp goto done;
6572 818c7501 2019-07-11 stsp
6573 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6574 818c7501 2019-07-11 stsp if (err)
6575 818c7501 2019-07-11 stsp goto done;
6576 818c7501 2019-07-11 stsp done:
6577 818c7501 2019-07-11 stsp free(commit_ref_name);
6578 818c7501 2019-07-11 stsp free(branch_ref_name);
6579 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6580 818c7501 2019-07-11 stsp if (commit_ref)
6581 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6582 818c7501 2019-07-11 stsp if (branch_ref)
6583 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6584 818c7501 2019-07-11 stsp if (err) {
6585 818c7501 2019-07-11 stsp free(*commit_id);
6586 818c7501 2019-07-11 stsp *commit_id = NULL;
6587 818c7501 2019-07-11 stsp if (*tmp_branch) {
6588 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6589 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6590 818c7501 2019-07-11 stsp }
6591 818c7501 2019-07-11 stsp if (*new_base_branch) {
6592 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6593 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6594 818c7501 2019-07-11 stsp }
6595 818c7501 2019-07-11 stsp if (*branch) {
6596 818c7501 2019-07-11 stsp got_ref_close(*branch);
6597 818c7501 2019-07-11 stsp *branch = NULL;
6598 818c7501 2019-07-11 stsp }
6599 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6600 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6601 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6602 3e3a69f1 2019-07-25 stsp }
6603 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6604 818c7501 2019-07-11 stsp }
6605 818c7501 2019-07-11 stsp return err;
6606 c4296144 2019-05-09 stsp }
6607 818c7501 2019-07-11 stsp
6608 818c7501 2019-07-11 stsp const struct got_error *
6609 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6610 818c7501 2019-07-11 stsp {
6611 818c7501 2019-07-11 stsp const struct got_error *err;
6612 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6613 818c7501 2019-07-11 stsp
6614 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6615 818c7501 2019-07-11 stsp if (err)
6616 818c7501 2019-07-11 stsp return err;
6617 818c7501 2019-07-11 stsp
6618 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6619 818c7501 2019-07-11 stsp free(tmp_branch_name);
6620 818c7501 2019-07-11 stsp return NULL;
6621 818c7501 2019-07-11 stsp }
6622 818c7501 2019-07-11 stsp
6623 818c7501 2019-07-11 stsp static const struct got_error *
6624 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6625 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6626 818c7501 2019-07-11 stsp {
6627 0ebf8283 2019-07-24 stsp *logmsg = arg;
6628 818c7501 2019-07-11 stsp return NULL;
6629 818c7501 2019-07-11 stsp }
6630 818c7501 2019-07-11 stsp
6631 818c7501 2019-07-11 stsp static const struct got_error *
6632 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6633 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6634 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6635 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6636 818c7501 2019-07-11 stsp {
6637 818c7501 2019-07-11 stsp return NULL;
6638 01757395 2019-07-12 stsp }
6639 01757395 2019-07-12 stsp
6640 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6641 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6642 01757395 2019-07-12 stsp void *progress_arg;
6643 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6644 01757395 2019-07-12 stsp };
6645 01757395 2019-07-12 stsp
6646 01757395 2019-07-12 stsp static const struct got_error *
6647 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6648 01757395 2019-07-12 stsp {
6649 01757395 2019-07-12 stsp const struct got_error *err;
6650 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6651 01757395 2019-07-12 stsp char *p;
6652 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6653 01757395 2019-07-12 stsp
6654 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6655 01757395 2019-07-12 stsp if (err)
6656 01757395 2019-07-12 stsp return err;
6657 01757395 2019-07-12 stsp
6658 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6659 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6660 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6661 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6662 01757395 2019-07-12 stsp return NULL;
6663 01757395 2019-07-12 stsp
6664 01757395 2019-07-12 stsp p = strdup(path);
6665 01757395 2019-07-12 stsp if (p == NULL)
6666 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6667 01757395 2019-07-12 stsp
6668 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6669 01757395 2019-07-12 stsp if (err || new == NULL)
6670 01757395 2019-07-12 stsp free(p);
6671 01757395 2019-07-12 stsp return err;
6672 818c7501 2019-07-11 stsp }
6673 818c7501 2019-07-11 stsp
6674 0ebf8283 2019-07-24 stsp static const struct got_error *
6675 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6676 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6677 818c7501 2019-07-11 stsp {
6678 818c7501 2019-07-11 stsp const struct got_error *err;
6679 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6680 818c7501 2019-07-11 stsp
6681 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6682 818c7501 2019-07-11 stsp if (err) {
6683 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6684 818c7501 2019-07-11 stsp goto done;
6685 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6686 818c7501 2019-07-11 stsp if (err)
6687 818c7501 2019-07-11 stsp goto done;
6688 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6689 818c7501 2019-07-11 stsp if (err)
6690 818c7501 2019-07-11 stsp goto done;
6691 de05890f 2020-03-05 stsp } else if (is_rebase) {
6692 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6693 818c7501 2019-07-11 stsp int cmp;
6694 818c7501 2019-07-11 stsp
6695 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6696 818c7501 2019-07-11 stsp if (err)
6697 818c7501 2019-07-11 stsp goto done;
6698 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6699 818c7501 2019-07-11 stsp free(stored_id);
6700 818c7501 2019-07-11 stsp if (cmp != 0) {
6701 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6702 818c7501 2019-07-11 stsp goto done;
6703 818c7501 2019-07-11 stsp }
6704 818c7501 2019-07-11 stsp }
6705 0ebf8283 2019-07-24 stsp done:
6706 0ebf8283 2019-07-24 stsp if (commit_ref)
6707 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6708 0ebf8283 2019-07-24 stsp return err;
6709 0ebf8283 2019-07-24 stsp }
6710 0ebf8283 2019-07-24 stsp
6711 0ebf8283 2019-07-24 stsp static const struct got_error *
6712 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6713 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6714 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6715 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6716 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6717 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6718 0ebf8283 2019-07-24 stsp {
6719 0ebf8283 2019-07-24 stsp const struct got_error *err;
6720 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6721 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6722 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6723 818c7501 2019-07-11 stsp
6724 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6725 0ebf8283 2019-07-24 stsp
6726 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6727 0ebf8283 2019-07-24 stsp if (err)
6728 0ebf8283 2019-07-24 stsp return err;
6729 0ebf8283 2019-07-24 stsp
6730 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6731 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6732 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6733 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6734 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6735 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6736 818c7501 2019-07-11 stsp if (commit_ref)
6737 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6738 818c7501 2019-07-11 stsp return err;
6739 818c7501 2019-07-11 stsp }
6740 818c7501 2019-07-11 stsp
6741 818c7501 2019-07-11 stsp const struct got_error *
6742 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6743 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6744 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6745 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6746 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6747 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6748 818c7501 2019-07-11 stsp {
6749 0ebf8283 2019-07-24 stsp const struct got_error *err;
6750 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6751 0ebf8283 2019-07-24 stsp
6752 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6753 0ebf8283 2019-07-24 stsp if (err)
6754 0ebf8283 2019-07-24 stsp return err;
6755 0ebf8283 2019-07-24 stsp
6756 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6757 0ebf8283 2019-07-24 stsp if (err)
6758 0ebf8283 2019-07-24 stsp goto done;
6759 0ebf8283 2019-07-24 stsp
6760 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6761 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6762 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6763 0ebf8283 2019-07-24 stsp done:
6764 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6765 0ebf8283 2019-07-24 stsp return err;
6766 0ebf8283 2019-07-24 stsp }
6767 0ebf8283 2019-07-24 stsp
6768 0ebf8283 2019-07-24 stsp const struct got_error *
6769 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6770 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6771 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6772 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6773 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6774 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6775 0ebf8283 2019-07-24 stsp {
6776 0ebf8283 2019-07-24 stsp const struct got_error *err;
6777 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6778 0ebf8283 2019-07-24 stsp
6779 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6780 0ebf8283 2019-07-24 stsp if (err)
6781 0ebf8283 2019-07-24 stsp return err;
6782 0ebf8283 2019-07-24 stsp
6783 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6784 0ebf8283 2019-07-24 stsp if (err)
6785 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp
6787 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6788 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6789 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6790 0ebf8283 2019-07-24 stsp done:
6791 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6792 0ebf8283 2019-07-24 stsp return err;
6793 0ebf8283 2019-07-24 stsp }
6794 0ebf8283 2019-07-24 stsp
6795 0ebf8283 2019-07-24 stsp static const struct got_error *
6796 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6797 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6798 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6799 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6800 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6801 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6802 0ebf8283 2019-07-24 stsp {
6803 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6804 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6805 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6806 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6807 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6808 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6809 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6810 818c7501 2019-07-11 stsp
6811 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6812 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6813 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6814 a0e95631 2019-07-12 stsp
6815 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6816 818c7501 2019-07-11 stsp
6817 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6818 a0e95631 2019-07-12 stsp if (err)
6819 3e3a69f1 2019-07-25 stsp return err;
6820 a0e95631 2019-07-12 stsp
6821 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6822 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6823 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6824 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6825 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
6826 01757395 2019-07-12 stsp /*
6827 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6828 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6829 6c13b005 2021-09-02 stsp *
6830 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6831 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6832 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6833 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6834 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6835 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6836 01757395 2019-07-12 stsp */
6837 01757395 2019-07-12 stsp if (merged_paths) {
6838 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6839 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6840 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6841 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6842 f2a9dc41 2019-12-13 tracey 0);
6843 01757395 2019-07-12 stsp if (err)
6844 01757395 2019-07-12 stsp goto done;
6845 01757395 2019-07-12 stsp }
6846 01757395 2019-07-12 stsp } else {
6847 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6848 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6849 01757395 2019-07-12 stsp if (err)
6850 01757395 2019-07-12 stsp goto done;
6851 01757395 2019-07-12 stsp }
6852 a0e95631 2019-07-12 stsp
6853 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6854 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6855 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6856 a0e95631 2019-07-12 stsp if (err)
6857 a0e95631 2019-07-12 stsp goto done;
6858 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6859 a0e95631 2019-07-12 stsp goto done;
6860 ff0d2220 2019-07-11 stsp }
6861 818c7501 2019-07-11 stsp
6862 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6863 edd02c5e 2019-07-11 stsp if (err)
6864 edd02c5e 2019-07-11 stsp goto done;
6865 edd02c5e 2019-07-11 stsp
6866 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6867 818c7501 2019-07-11 stsp if (err)
6868 818c7501 2019-07-11 stsp goto done;
6869 818c7501 2019-07-11 stsp
6870 5943eee2 2019-08-13 stsp if (new_logmsg) {
6871 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6872 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6873 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6874 5943eee2 2019-08-13 stsp goto done;
6875 5943eee2 2019-08-13 stsp }
6876 5943eee2 2019-08-13 stsp } else {
6877 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6878 5943eee2 2019-08-13 stsp if (err)
6879 5943eee2 2019-08-13 stsp goto done;
6880 5943eee2 2019-08-13 stsp }
6881 0ebf8283 2019-07-24 stsp
6882 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6883 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6884 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6885 50e7a649 2022-07-22 stsp committer ? committer :
6886 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6887 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6888 a0e95631 2019-07-12 stsp if (err)
6889 a0e95631 2019-07-12 stsp goto done;
6890 a0e95631 2019-07-12 stsp
6891 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6892 a0e95631 2019-07-12 stsp if (err)
6893 a0e95631 2019-07-12 stsp goto done;
6894 a0e95631 2019-07-12 stsp
6895 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6896 a0e95631 2019-07-12 stsp if (err)
6897 a0e95631 2019-07-12 stsp goto done;
6898 a0e95631 2019-07-12 stsp
6899 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6900 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6901 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6902 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6903 a0e95631 2019-07-12 stsp err = sync_err;
6904 818c7501 2019-07-11 stsp done:
6905 a0e95631 2019-07-12 stsp free(fileindex_path);
6906 a0e95631 2019-07-12 stsp free(head_commit_id);
6907 a0e95631 2019-07-12 stsp if (head_ref)
6908 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6909 818c7501 2019-07-11 stsp if (err) {
6910 818c7501 2019-07-11 stsp free(*new_commit_id);
6911 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6912 818c7501 2019-07-11 stsp }
6913 818c7501 2019-07-11 stsp return err;
6914 818c7501 2019-07-11 stsp }
6915 818c7501 2019-07-11 stsp
6916 818c7501 2019-07-11 stsp const struct got_error *
6917 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6918 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6919 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6920 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6921 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
6922 12383673 2023-02-18 mark struct got_repository *repo)
6923 0ebf8283 2019-07-24 stsp {
6924 0ebf8283 2019-07-24 stsp const struct got_error *err;
6925 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6926 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6927 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6928 0ebf8283 2019-07-24 stsp
6929 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp return err;
6932 0ebf8283 2019-07-24 stsp
6933 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6934 0ebf8283 2019-07-24 stsp if (err)
6935 0ebf8283 2019-07-24 stsp goto done;
6936 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6937 0ebf8283 2019-07-24 stsp if (err)
6938 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6940 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6941 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp }
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6945 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6946 12383673 2023-02-18 mark NULL, allow_conflict, repo);
6947 0ebf8283 2019-07-24 stsp done:
6948 0ebf8283 2019-07-24 stsp if (commit_ref)
6949 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6950 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6951 0ebf8283 2019-07-24 stsp free(commit_id);
6952 0ebf8283 2019-07-24 stsp return err;
6953 0ebf8283 2019-07-24 stsp }
6954 0ebf8283 2019-07-24 stsp
6955 0ebf8283 2019-07-24 stsp const struct got_error *
6956 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6957 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6958 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6959 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6960 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6961 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6962 0ebf8283 2019-07-24 stsp {
6963 0ebf8283 2019-07-24 stsp const struct got_error *err;
6964 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6965 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6966 0ebf8283 2019-07-24 stsp
6967 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6968 0ebf8283 2019-07-24 stsp if (err)
6969 0ebf8283 2019-07-24 stsp return err;
6970 0ebf8283 2019-07-24 stsp
6971 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6972 0ebf8283 2019-07-24 stsp if (err)
6973 0ebf8283 2019-07-24 stsp goto done;
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6976 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6977 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
6978 0ebf8283 2019-07-24 stsp done:
6979 0ebf8283 2019-07-24 stsp if (commit_ref)
6980 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6981 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6982 0ebf8283 2019-07-24 stsp return err;
6983 0ebf8283 2019-07-24 stsp }
6984 0ebf8283 2019-07-24 stsp
6985 0ebf8283 2019-07-24 stsp const struct got_error *
6986 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6987 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6988 818c7501 2019-07-11 stsp {
6989 3e3a69f1 2019-07-25 stsp if (fileindex)
6990 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6991 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6992 69844fba 2019-07-11 stsp }
6993 69844fba 2019-07-11 stsp
6994 69844fba 2019-07-11 stsp static const struct got_error *
6995 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6996 69844fba 2019-07-11 stsp {
6997 69844fba 2019-07-11 stsp const struct got_error *err;
6998 69844fba 2019-07-11 stsp struct got_reference *ref;
6999 69844fba 2019-07-11 stsp
7000 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7001 69844fba 2019-07-11 stsp if (err) {
7002 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7003 69844fba 2019-07-11 stsp return NULL;
7004 69844fba 2019-07-11 stsp return err;
7005 69844fba 2019-07-11 stsp }
7006 69844fba 2019-07-11 stsp
7007 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7008 69844fba 2019-07-11 stsp got_ref_close(ref);
7009 69844fba 2019-07-11 stsp return err;
7010 818c7501 2019-07-11 stsp }
7011 818c7501 2019-07-11 stsp
7012 69844fba 2019-07-11 stsp static const struct got_error *
7013 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7014 69844fba 2019-07-11 stsp {
7015 69844fba 2019-07-11 stsp const struct got_error *err;
7016 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7017 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7018 69844fba 2019-07-11 stsp
7019 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7020 69844fba 2019-07-11 stsp if (err)
7021 69844fba 2019-07-11 stsp goto done;
7022 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7023 69844fba 2019-07-11 stsp if (err)
7024 69844fba 2019-07-11 stsp goto done;
7025 69844fba 2019-07-11 stsp
7026 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7027 69844fba 2019-07-11 stsp if (err)
7028 69844fba 2019-07-11 stsp goto done;
7029 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7030 69844fba 2019-07-11 stsp if (err)
7031 69844fba 2019-07-11 stsp goto done;
7032 69844fba 2019-07-11 stsp
7033 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7034 69844fba 2019-07-11 stsp if (err)
7035 69844fba 2019-07-11 stsp goto done;
7036 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7037 69844fba 2019-07-11 stsp if (err)
7038 69844fba 2019-07-11 stsp goto done;
7039 69844fba 2019-07-11 stsp
7040 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7041 69844fba 2019-07-11 stsp if (err)
7042 69844fba 2019-07-11 stsp goto done;
7043 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7044 69844fba 2019-07-11 stsp if (err)
7045 69844fba 2019-07-11 stsp goto done;
7046 69844fba 2019-07-11 stsp
7047 69844fba 2019-07-11 stsp done:
7048 69844fba 2019-07-11 stsp free(tmp_branch_name);
7049 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7050 69844fba 2019-07-11 stsp free(branch_ref_name);
7051 69844fba 2019-07-11 stsp free(commit_ref_name);
7052 e600f124 2021-03-21 stsp return err;
7053 e600f124 2021-03-21 stsp }
7054 e600f124 2021-03-21 stsp
7055 336075a4 2022-06-25 op static const struct got_error *
7056 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7057 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7058 e600f124 2021-03-21 stsp {
7059 e600f124 2021-03-21 stsp const struct got_error *err;
7060 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7061 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7062 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7063 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7064 e600f124 2021-03-21 stsp char *refname = NULL;
7065 e600f124 2021-03-21 stsp
7066 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7067 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7068 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7069 e600f124 2021-03-21 stsp branch_name += 11;
7070 e600f124 2021-03-21 stsp
7071 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7072 e600f124 2021-03-21 stsp if (err)
7073 e600f124 2021-03-21 stsp return err;
7074 e600f124 2021-03-21 stsp
7075 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7076 e600f124 2021-03-21 stsp new_id_str) == -1) {
7077 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7078 e600f124 2021-03-21 stsp goto done;
7079 e600f124 2021-03-21 stsp }
7080 e600f124 2021-03-21 stsp
7081 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7082 e600f124 2021-03-21 stsp if (err)
7083 e600f124 2021-03-21 stsp goto done;
7084 e600f124 2021-03-21 stsp
7085 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7086 e600f124 2021-03-21 stsp if (err)
7087 e600f124 2021-03-21 stsp goto done;
7088 e600f124 2021-03-21 stsp
7089 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7090 e600f124 2021-03-21 stsp done:
7091 e600f124 2021-03-21 stsp free(new_id_str);
7092 e600f124 2021-03-21 stsp free(refname);
7093 e600f124 2021-03-21 stsp free(old_commit_id);
7094 e600f124 2021-03-21 stsp if (ref)
7095 e600f124 2021-03-21 stsp got_ref_close(ref);
7096 69844fba 2019-07-11 stsp return err;
7097 69844fba 2019-07-11 stsp }
7098 69844fba 2019-07-11 stsp
7099 818c7501 2019-07-11 stsp const struct got_error *
7100 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7101 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7102 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7103 ef85a376 2023-02-01 mark int create_backup)
7104 818c7501 2019-07-11 stsp {
7105 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7106 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7107 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7108 818c7501 2019-07-11 stsp
7109 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7110 818c7501 2019-07-11 stsp if (err)
7111 818c7501 2019-07-11 stsp return err;
7112 e600f124 2021-03-21 stsp
7113 e600f124 2021-03-21 stsp if (create_backup) {
7114 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7115 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7116 e600f124 2021-03-21 stsp if (err)
7117 e600f124 2021-03-21 stsp goto done;
7118 e600f124 2021-03-21 stsp }
7119 818c7501 2019-07-11 stsp
7120 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7121 818c7501 2019-07-11 stsp if (err)
7122 818c7501 2019-07-11 stsp goto done;
7123 818c7501 2019-07-11 stsp
7124 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7125 818c7501 2019-07-11 stsp if (err)
7126 818c7501 2019-07-11 stsp goto done;
7127 818c7501 2019-07-11 stsp
7128 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7129 818c7501 2019-07-11 stsp if (err)
7130 818c7501 2019-07-11 stsp goto done;
7131 818c7501 2019-07-11 stsp
7132 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7133 a615e0e7 2020-12-16 stsp if (err)
7134 a615e0e7 2020-12-16 stsp goto done;
7135 a615e0e7 2020-12-16 stsp
7136 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7137 a615e0e7 2020-12-16 stsp if (err)
7138 a615e0e7 2020-12-16 stsp goto done;
7139 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7140 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7141 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7142 a615e0e7 2020-12-16 stsp err = sync_err;
7143 818c7501 2019-07-11 stsp done:
7144 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7145 a615e0e7 2020-12-16 stsp free(fileindex_path);
7146 818c7501 2019-07-11 stsp free(new_head_commit_id);
7147 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7148 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7149 818c7501 2019-07-11 stsp err = unlockerr;
7150 818c7501 2019-07-11 stsp return err;
7151 818c7501 2019-07-11 stsp }
7152 818c7501 2019-07-11 stsp
7153 818c7501 2019-07-11 stsp const struct got_error *
7154 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7155 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7156 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7157 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7158 818c7501 2019-07-11 stsp {
7159 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7160 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7161 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7162 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7163 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7164 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7165 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7166 818c7501 2019-07-11 stsp
7167 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7168 818c7501 2019-07-11 stsp if (err)
7169 818c7501 2019-07-11 stsp return err;
7170 818c7501 2019-07-11 stsp
7171 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7172 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7173 a44927cc 2022-04-07 stsp if (err)
7174 a44927cc 2022-04-07 stsp goto done;
7175 a44927cc 2022-04-07 stsp
7176 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7177 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7178 818c7501 2019-07-11 stsp if (err)
7179 818c7501 2019-07-11 stsp goto done;
7180 818c7501 2019-07-11 stsp
7181 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7182 818c7501 2019-07-11 stsp if (err)
7183 818c7501 2019-07-11 stsp goto done;
7184 818c7501 2019-07-11 stsp
7185 818c7501 2019-07-11 stsp /*
7186 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7187 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7188 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7189 818c7501 2019-07-11 stsp */
7190 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7191 818c7501 2019-07-11 stsp if (err)
7192 818c7501 2019-07-11 stsp goto done;
7193 818c7501 2019-07-11 stsp
7194 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7195 818c7501 2019-07-11 stsp if (err)
7196 818c7501 2019-07-11 stsp goto done;
7197 818c7501 2019-07-11 stsp
7198 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7199 a44927cc 2022-04-07 stsp worktree->path_prefix);
7200 ca355955 2019-07-12 stsp if (err)
7201 ca355955 2019-07-12 stsp goto done;
7202 ca355955 2019-07-12 stsp
7203 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7204 ca355955 2019-07-12 stsp if (err)
7205 ca355955 2019-07-12 stsp goto done;
7206 ca355955 2019-07-12 stsp
7207 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7208 818c7501 2019-07-11 stsp if (err)
7209 818c7501 2019-07-11 stsp goto done;
7210 818c7501 2019-07-11 stsp
7211 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7212 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7213 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7214 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7215 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7216 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7217 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7218 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7219 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7220 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7221 55bd499d 2019-07-12 stsp if (err)
7222 1f1abb7e 2019-08-08 stsp goto sync;
7223 55bd499d 2019-07-12 stsp
7224 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7225 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7226 ca355955 2019-07-12 stsp sync:
7227 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7228 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7229 ca355955 2019-07-12 stsp err = sync_err;
7230 818c7501 2019-07-11 stsp done:
7231 818c7501 2019-07-11 stsp got_ref_close(resolved);
7232 a3a2faf2 2019-07-12 stsp free(tree_id);
7233 818c7501 2019-07-11 stsp free(commit_id);
7234 a44927cc 2022-04-07 stsp if (commit)
7235 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7236 0ebf8283 2019-07-24 stsp if (fileindex)
7237 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7238 0ebf8283 2019-07-24 stsp free(fileindex_path);
7239 0ebf8283 2019-07-24 stsp
7240 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7241 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7242 0ebf8283 2019-07-24 stsp err = unlockerr;
7243 0ebf8283 2019-07-24 stsp return err;
7244 0ebf8283 2019-07-24 stsp }
7245 0ebf8283 2019-07-24 stsp
7246 0ebf8283 2019-07-24 stsp const struct got_error *
7247 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7248 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7249 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7250 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7251 0ebf8283 2019-07-24 stsp {
7252 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7253 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7254 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7255 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7256 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7257 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7258 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7259 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7260 0ebf8283 2019-07-24 stsp
7261 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7262 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7263 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7264 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7265 0ebf8283 2019-07-24 stsp
7266 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7267 0ebf8283 2019-07-24 stsp if (err)
7268 0ebf8283 2019-07-24 stsp return err;
7269 0ebf8283 2019-07-24 stsp
7270 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7271 0ebf8283 2019-07-24 stsp if (err)
7272 0ebf8283 2019-07-24 stsp goto done;
7273 0ebf8283 2019-07-24 stsp
7274 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7275 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7276 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7277 0ebf8283 2019-07-24 stsp &ok_arg);
7278 0ebf8283 2019-07-24 stsp if (err)
7279 0ebf8283 2019-07-24 stsp goto done;
7280 0ebf8283 2019-07-24 stsp
7281 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7282 0ebf8283 2019-07-24 stsp if (err)
7283 0ebf8283 2019-07-24 stsp goto done;
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7286 0ebf8283 2019-07-24 stsp if (err)
7287 0ebf8283 2019-07-24 stsp goto done;
7288 0ebf8283 2019-07-24 stsp
7289 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7290 0ebf8283 2019-07-24 stsp worktree);
7291 0ebf8283 2019-07-24 stsp if (err)
7292 0ebf8283 2019-07-24 stsp goto done;
7293 0ebf8283 2019-07-24 stsp
7294 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7295 0ebf8283 2019-07-24 stsp 0);
7296 0ebf8283 2019-07-24 stsp if (err)
7297 0ebf8283 2019-07-24 stsp goto done;
7298 0ebf8283 2019-07-24 stsp
7299 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7300 0ebf8283 2019-07-24 stsp if (err)
7301 0ebf8283 2019-07-24 stsp goto done;
7302 0ebf8283 2019-07-24 stsp
7303 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7304 0ebf8283 2019-07-24 stsp if (err)
7305 0ebf8283 2019-07-24 stsp goto done;
7306 0ebf8283 2019-07-24 stsp
7307 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7308 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7309 0ebf8283 2019-07-24 stsp if (err)
7310 0ebf8283 2019-07-24 stsp goto done;
7311 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7312 0ebf8283 2019-07-24 stsp if (err)
7313 0ebf8283 2019-07-24 stsp goto done;
7314 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7315 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7316 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7317 0ebf8283 2019-07-24 stsp goto done;
7318 0ebf8283 2019-07-24 stsp }
7319 0ebf8283 2019-07-24 stsp
7320 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7321 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7322 0ebf8283 2019-07-24 stsp if (err)
7323 0ebf8283 2019-07-24 stsp goto done;
7324 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7325 0ebf8283 2019-07-24 stsp if (err)
7326 0ebf8283 2019-07-24 stsp goto done;
7327 0ebf8283 2019-07-24 stsp
7328 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7329 0ebf8283 2019-07-24 stsp if (err)
7330 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp done:
7332 0ebf8283 2019-07-24 stsp free(fileindex_path);
7333 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7334 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7335 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7336 0ebf8283 2019-07-24 stsp if (wt_branch)
7337 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7338 0ebf8283 2019-07-24 stsp if (err) {
7339 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7340 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7341 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7342 0ebf8283 2019-07-24 stsp }
7343 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7344 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7345 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7346 0ebf8283 2019-07-24 stsp }
7347 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7348 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7349 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7350 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7351 3e3a69f1 2019-07-25 stsp }
7352 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7353 0ebf8283 2019-07-24 stsp }
7354 0ebf8283 2019-07-24 stsp return err;
7355 0ebf8283 2019-07-24 stsp }
7356 0ebf8283 2019-07-24 stsp
7357 0ebf8283 2019-07-24 stsp const struct got_error *
7358 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7359 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7360 0ebf8283 2019-07-24 stsp {
7361 3e3a69f1 2019-07-25 stsp if (fileindex)
7362 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7363 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7364 0ebf8283 2019-07-24 stsp }
7365 0ebf8283 2019-07-24 stsp
7366 0ebf8283 2019-07-24 stsp const struct got_error *
7367 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7368 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7369 0ebf8283 2019-07-24 stsp {
7370 0ebf8283 2019-07-24 stsp const struct got_error *err;
7371 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7372 0ebf8283 2019-07-24 stsp
7373 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7374 0ebf8283 2019-07-24 stsp if (err)
7375 0ebf8283 2019-07-24 stsp return err;
7376 0ebf8283 2019-07-24 stsp
7377 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7378 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7379 0ebf8283 2019-07-24 stsp return NULL;
7380 0ebf8283 2019-07-24 stsp }
7381 0ebf8283 2019-07-24 stsp
7382 0ebf8283 2019-07-24 stsp const struct got_error *
7383 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7384 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7385 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7386 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7387 0ebf8283 2019-07-24 stsp {
7388 0ebf8283 2019-07-24 stsp const struct got_error *err;
7389 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7390 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7391 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7392 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7393 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7394 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7395 0ebf8283 2019-07-24 stsp
7396 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7397 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7398 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7399 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7400 0ebf8283 2019-07-24 stsp
7401 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7402 3e3a69f1 2019-07-25 stsp if (err)
7403 3e3a69f1 2019-07-25 stsp return err;
7404 3e3a69f1 2019-07-25 stsp
7405 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7406 3e3a69f1 2019-07-25 stsp if (err)
7407 f032f1f7 2019-08-04 stsp goto done;
7408 f032f1f7 2019-08-04 stsp
7409 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7410 f032f1f7 2019-08-04 stsp &have_staged_files);
7411 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7412 f032f1f7 2019-08-04 stsp goto done;
7413 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7414 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7415 3e3a69f1 2019-07-25 stsp goto done;
7416 f032f1f7 2019-08-04 stsp }
7417 3e3a69f1 2019-07-25 stsp
7418 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7419 0ebf8283 2019-07-24 stsp if (err)
7420 f032f1f7 2019-08-04 stsp goto done;
7421 0ebf8283 2019-07-24 stsp
7422 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7423 0ebf8283 2019-07-24 stsp if (err)
7424 0ebf8283 2019-07-24 stsp goto done;
7425 0ebf8283 2019-07-24 stsp
7426 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7427 0ebf8283 2019-07-24 stsp if (err)
7428 0ebf8283 2019-07-24 stsp goto done;
7429 0ebf8283 2019-07-24 stsp
7430 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7431 0ebf8283 2019-07-24 stsp worktree);
7432 0ebf8283 2019-07-24 stsp if (err)
7433 0ebf8283 2019-07-24 stsp goto done;
7434 0ebf8283 2019-07-24 stsp
7435 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7436 0ebf8283 2019-07-24 stsp if (err)
7437 0ebf8283 2019-07-24 stsp goto done;
7438 0ebf8283 2019-07-24 stsp
7439 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7440 0ebf8283 2019-07-24 stsp if (err)
7441 0ebf8283 2019-07-24 stsp goto done;
7442 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7443 0ebf8283 2019-07-24 stsp if (err)
7444 0ebf8283 2019-07-24 stsp goto done;
7445 0ebf8283 2019-07-24 stsp
7446 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7447 0ebf8283 2019-07-24 stsp if (err)
7448 0ebf8283 2019-07-24 stsp goto done;
7449 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7450 0ebf8283 2019-07-24 stsp if (err)
7451 0ebf8283 2019-07-24 stsp goto done;
7452 0ebf8283 2019-07-24 stsp
7453 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7454 0ebf8283 2019-07-24 stsp if (err)
7455 0ebf8283 2019-07-24 stsp goto done;
7456 0ebf8283 2019-07-24 stsp done:
7457 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7458 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7459 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7460 0ebf8283 2019-07-24 stsp if (commit_ref)
7461 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7462 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7463 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7464 0ebf8283 2019-07-24 stsp if (err) {
7465 0ebf8283 2019-07-24 stsp free(*commit_id);
7466 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7467 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7468 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7469 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7470 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7471 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7472 0ebf8283 2019-07-24 stsp }
7473 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7474 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7475 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7476 3e3a69f1 2019-07-25 stsp }
7477 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7478 0ebf8283 2019-07-24 stsp }
7479 0ebf8283 2019-07-24 stsp return err;
7480 0ebf8283 2019-07-24 stsp }
7481 0ebf8283 2019-07-24 stsp
7482 0ebf8283 2019-07-24 stsp static const struct got_error *
7483 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7484 0ebf8283 2019-07-24 stsp {
7485 0ebf8283 2019-07-24 stsp const struct got_error *err;
7486 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7487 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7488 0ebf8283 2019-07-24 stsp
7489 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7490 0ebf8283 2019-07-24 stsp if (err)
7491 0ebf8283 2019-07-24 stsp goto done;
7492 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7493 0ebf8283 2019-07-24 stsp if (err)
7494 0ebf8283 2019-07-24 stsp goto done;
7495 0ebf8283 2019-07-24 stsp
7496 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7497 0ebf8283 2019-07-24 stsp worktree);
7498 0ebf8283 2019-07-24 stsp if (err)
7499 0ebf8283 2019-07-24 stsp goto done;
7500 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7501 0ebf8283 2019-07-24 stsp if (err)
7502 0ebf8283 2019-07-24 stsp goto done;
7503 0ebf8283 2019-07-24 stsp
7504 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7505 0ebf8283 2019-07-24 stsp if (err)
7506 0ebf8283 2019-07-24 stsp goto done;
7507 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7508 0ebf8283 2019-07-24 stsp if (err)
7509 0ebf8283 2019-07-24 stsp goto done;
7510 0ebf8283 2019-07-24 stsp
7511 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7512 0ebf8283 2019-07-24 stsp if (err)
7513 0ebf8283 2019-07-24 stsp goto done;
7514 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7515 0ebf8283 2019-07-24 stsp if (err)
7516 0ebf8283 2019-07-24 stsp goto done;
7517 0ebf8283 2019-07-24 stsp done:
7518 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7519 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7520 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7521 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7522 0ebf8283 2019-07-24 stsp return err;
7523 0ebf8283 2019-07-24 stsp }
7524 0ebf8283 2019-07-24 stsp
7525 0ebf8283 2019-07-24 stsp const struct got_error *
7526 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7527 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7528 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7529 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7530 0ebf8283 2019-07-24 stsp {
7531 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7532 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7533 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7534 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7535 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7536 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7537 0ebf8283 2019-07-24 stsp
7538 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7539 0ebf8283 2019-07-24 stsp if (err)
7540 0ebf8283 2019-07-24 stsp return err;
7541 0ebf8283 2019-07-24 stsp
7542 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7543 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7544 a44927cc 2022-04-07 stsp if (err)
7545 a44927cc 2022-04-07 stsp goto done;
7546 a44927cc 2022-04-07 stsp
7547 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7548 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7549 0ebf8283 2019-07-24 stsp if (err)
7550 0ebf8283 2019-07-24 stsp goto done;
7551 0ebf8283 2019-07-24 stsp
7552 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7553 0ebf8283 2019-07-24 stsp if (err)
7554 0ebf8283 2019-07-24 stsp goto done;
7555 0ebf8283 2019-07-24 stsp
7556 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7557 0ebf8283 2019-07-24 stsp if (err)
7558 0ebf8283 2019-07-24 stsp goto done;
7559 0ebf8283 2019-07-24 stsp
7560 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7561 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7562 0ebf8283 2019-07-24 stsp if (err)
7563 0ebf8283 2019-07-24 stsp goto done;
7564 0ebf8283 2019-07-24 stsp
7565 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7566 0ebf8283 2019-07-24 stsp if (err)
7567 0ebf8283 2019-07-24 stsp goto done;
7568 0ebf8283 2019-07-24 stsp
7569 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7570 0ebf8283 2019-07-24 stsp if (err)
7571 0ebf8283 2019-07-24 stsp goto done;
7572 0ebf8283 2019-07-24 stsp
7573 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7574 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7575 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7576 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7577 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7578 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7579 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7580 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7581 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7582 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7583 0ebf8283 2019-07-24 stsp if (err)
7584 1f1abb7e 2019-08-08 stsp goto sync;
7585 0ebf8283 2019-07-24 stsp
7586 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7587 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7588 0ebf8283 2019-07-24 stsp sync:
7589 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7590 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7591 0ebf8283 2019-07-24 stsp err = sync_err;
7592 0ebf8283 2019-07-24 stsp done:
7593 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7594 0ebf8283 2019-07-24 stsp free(tree_id);
7595 818c7501 2019-07-11 stsp free(fileindex_path);
7596 818c7501 2019-07-11 stsp
7597 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7598 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7599 818c7501 2019-07-11 stsp err = unlockerr;
7600 818c7501 2019-07-11 stsp return err;
7601 818c7501 2019-07-11 stsp }
7602 0ebf8283 2019-07-24 stsp
7603 0ebf8283 2019-07-24 stsp const struct got_error *
7604 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7605 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7606 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7607 0ebf8283 2019-07-24 stsp {
7608 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7609 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7610 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7611 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7612 0ebf8283 2019-07-24 stsp
7613 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7614 0ebf8283 2019-07-24 stsp if (err)
7615 0ebf8283 2019-07-24 stsp return err;
7616 0ebf8283 2019-07-24 stsp
7617 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7618 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7619 e600f124 2021-03-21 stsp if (err)
7620 e600f124 2021-03-21 stsp goto done;
7621 e600f124 2021-03-21 stsp
7622 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7623 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7624 0ebf8283 2019-07-24 stsp if (err)
7625 0ebf8283 2019-07-24 stsp goto done;
7626 0ebf8283 2019-07-24 stsp
7627 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7628 0ebf8283 2019-07-24 stsp if (err)
7629 0ebf8283 2019-07-24 stsp goto done;
7630 0ebf8283 2019-07-24 stsp
7631 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7632 0ebf8283 2019-07-24 stsp if (err)
7633 0ebf8283 2019-07-24 stsp goto done;
7634 0ebf8283 2019-07-24 stsp
7635 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
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 = delete_histedit_refs(worktree, repo);
7640 a615e0e7 2020-12-16 stsp if (err)
7641 a615e0e7 2020-12-16 stsp goto done;
7642 a615e0e7 2020-12-16 stsp
7643 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7644 a615e0e7 2020-12-16 stsp if (err)
7645 a615e0e7 2020-12-16 stsp goto done;
7646 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7647 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7648 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7649 a615e0e7 2020-12-16 stsp err = sync_err;
7650 0ebf8283 2019-07-24 stsp done:
7651 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7652 a615e0e7 2020-12-16 stsp free(fileindex_path);
7653 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7654 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7655 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7656 0ebf8283 2019-07-24 stsp err = unlockerr;
7657 0ebf8283 2019-07-24 stsp return err;
7658 0ebf8283 2019-07-24 stsp }
7659 0ebf8283 2019-07-24 stsp
7660 0ebf8283 2019-07-24 stsp const struct got_error *
7661 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7662 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7663 0ebf8283 2019-07-24 stsp {
7664 0ebf8283 2019-07-24 stsp const struct got_error *err;
7665 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7666 0ebf8283 2019-07-24 stsp
7667 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7668 0ebf8283 2019-07-24 stsp if (err)
7669 0ebf8283 2019-07-24 stsp return err;
7670 0ebf8283 2019-07-24 stsp
7671 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7672 0ebf8283 2019-07-24 stsp if (err)
7673 0ebf8283 2019-07-24 stsp goto done;
7674 0ebf8283 2019-07-24 stsp
7675 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7676 0ebf8283 2019-07-24 stsp done:
7677 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7678 2822a352 2019-10-15 stsp return err;
7679 2822a352 2019-10-15 stsp }
7680 2822a352 2019-10-15 stsp
7681 2822a352 2019-10-15 stsp const struct got_error *
7682 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7683 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7684 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7685 2822a352 2019-10-15 stsp struct got_repository *repo)
7686 2822a352 2019-10-15 stsp {
7687 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7688 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7689 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7690 2822a352 2019-10-15 stsp
7691 2822a352 2019-10-15 stsp *fileindex = NULL;
7692 2822a352 2019-10-15 stsp *branch_ref = NULL;
7693 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7694 2822a352 2019-10-15 stsp
7695 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7696 2822a352 2019-10-15 stsp if (err)
7697 2822a352 2019-10-15 stsp return err;
7698 2822a352 2019-10-15 stsp
7699 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7700 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7701 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7702 2822a352 2019-10-15 stsp "update -b or different branch name required");
7703 2822a352 2019-10-15 stsp goto done;
7704 2822a352 2019-10-15 stsp }
7705 2822a352 2019-10-15 stsp
7706 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7707 2822a352 2019-10-15 stsp if (err)
7708 2822a352 2019-10-15 stsp goto done;
7709 2822a352 2019-10-15 stsp
7710 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7711 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7712 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7713 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7714 2822a352 2019-10-15 stsp &ok_arg);
7715 2822a352 2019-10-15 stsp if (err)
7716 2822a352 2019-10-15 stsp goto done;
7717 2822a352 2019-10-15 stsp
7718 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7719 2822a352 2019-10-15 stsp if (err)
7720 2822a352 2019-10-15 stsp goto done;
7721 2822a352 2019-10-15 stsp
7722 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7723 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7724 2822a352 2019-10-15 stsp done:
7725 2822a352 2019-10-15 stsp if (err) {
7726 2822a352 2019-10-15 stsp if (*branch_ref) {
7727 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7728 2822a352 2019-10-15 stsp *branch_ref = NULL;
7729 2822a352 2019-10-15 stsp }
7730 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7731 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7732 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7733 2822a352 2019-10-15 stsp }
7734 2822a352 2019-10-15 stsp if (*fileindex) {
7735 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7736 2822a352 2019-10-15 stsp *fileindex = NULL;
7737 2822a352 2019-10-15 stsp }
7738 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7739 2822a352 2019-10-15 stsp }
7740 0cb83759 2019-08-03 stsp return err;
7741 0cb83759 2019-08-03 stsp }
7742 0cb83759 2019-08-03 stsp
7743 2822a352 2019-10-15 stsp const struct got_error *
7744 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7745 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7746 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7747 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7748 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7749 2822a352 2019-10-15 stsp {
7750 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7751 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7752 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7753 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7754 2822a352 2019-10-15 stsp
7755 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7756 2822a352 2019-10-15 stsp if (err)
7757 2822a352 2019-10-15 stsp goto done;
7758 2822a352 2019-10-15 stsp
7759 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7760 2822a352 2019-10-15 stsp if (err)
7761 2822a352 2019-10-15 stsp goto done;
7762 2822a352 2019-10-15 stsp
7763 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7764 a44927cc 2022-04-07 stsp if (err)
7765 a44927cc 2022-04-07 stsp goto done;
7766 a44927cc 2022-04-07 stsp
7767 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7768 2822a352 2019-10-15 stsp worktree->path_prefix);
7769 2822a352 2019-10-15 stsp if (err)
7770 2822a352 2019-10-15 stsp goto done;
7771 2822a352 2019-10-15 stsp
7772 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7773 2822a352 2019-10-15 stsp if (err)
7774 2822a352 2019-10-15 stsp goto done;
7775 2822a352 2019-10-15 stsp
7776 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7777 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7778 2822a352 2019-10-15 stsp if (err)
7779 2822a352 2019-10-15 stsp goto sync;
7780 2822a352 2019-10-15 stsp
7781 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7782 2822a352 2019-10-15 stsp if (err)
7783 2822a352 2019-10-15 stsp goto sync;
7784 2822a352 2019-10-15 stsp
7785 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7786 6e210706 2021-01-22 stsp if (err)
7787 6e210706 2021-01-22 stsp goto sync;
7788 6e210706 2021-01-22 stsp
7789 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7790 2822a352 2019-10-15 stsp sync:
7791 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7792 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7793 2822a352 2019-10-15 stsp err = sync_err;
7794 2822a352 2019-10-15 stsp
7795 2822a352 2019-10-15 stsp done:
7796 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7797 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7798 2822a352 2019-10-15 stsp err = unlockerr;
7799 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7800 2822a352 2019-10-15 stsp
7801 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7802 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7803 2822a352 2019-10-15 stsp err = unlockerr;
7804 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7805 2822a352 2019-10-15 stsp
7806 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7807 2822a352 2019-10-15 stsp free(fileindex_path);
7808 2822a352 2019-10-15 stsp free(tree_id);
7809 a44927cc 2022-04-07 stsp if (commit)
7810 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7811 2822a352 2019-10-15 stsp
7812 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7813 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7814 2822a352 2019-10-15 stsp err = unlockerr;
7815 2822a352 2019-10-15 stsp return err;
7816 2822a352 2019-10-15 stsp }
7817 2822a352 2019-10-15 stsp
7818 2822a352 2019-10-15 stsp const struct got_error *
7819 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7820 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7821 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7822 2822a352 2019-10-15 stsp {
7823 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7824 8b692cd0 2019-10-21 stsp
7825 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7826 8b692cd0 2019-10-21 stsp
7827 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7828 8b692cd0 2019-10-21 stsp
7829 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7830 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7831 8b692cd0 2019-10-21 stsp err = unlockerr;
7832 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7833 8b692cd0 2019-10-21 stsp
7834 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7835 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7836 8b692cd0 2019-10-21 stsp err = unlockerr;
7837 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7838 f259c4c1 2021-09-24 stsp
7839 f259c4c1 2021-09-24 stsp return err;
7840 f259c4c1 2021-09-24 stsp }
7841 f259c4c1 2021-09-24 stsp
7842 f259c4c1 2021-09-24 stsp const struct got_error *
7843 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7844 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7845 f259c4c1 2021-09-24 stsp {
7846 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7847 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7848 f259c4c1 2021-09-24 stsp
7849 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7850 f259c4c1 2021-09-24 stsp if (err)
7851 f259c4c1 2021-09-24 stsp goto done;
7852 8b692cd0 2019-10-21 stsp
7853 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7854 f259c4c1 2021-09-24 stsp
7855 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7856 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7857 f259c4c1 2021-09-24 stsp err = sync_err;
7858 f259c4c1 2021-09-24 stsp done:
7859 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7860 f259c4c1 2021-09-24 stsp free(fileindex_path);
7861 8b692cd0 2019-10-21 stsp return err;
7862 2822a352 2019-10-15 stsp }
7863 2822a352 2019-10-15 stsp
7864 f259c4c1 2021-09-24 stsp static const struct got_error *
7865 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7866 f259c4c1 2021-09-24 stsp {
7867 f259c4c1 2021-09-24 stsp const struct got_error *err;
7868 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7869 f259c4c1 2021-09-24 stsp
7870 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7871 f259c4c1 2021-09-24 stsp if (err)
7872 f259c4c1 2021-09-24 stsp goto done;
7873 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7874 f259c4c1 2021-09-24 stsp if (err)
7875 f259c4c1 2021-09-24 stsp goto done;
7876 f259c4c1 2021-09-24 stsp
7877 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7878 f259c4c1 2021-09-24 stsp if (err)
7879 f259c4c1 2021-09-24 stsp goto done;
7880 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7881 f259c4c1 2021-09-24 stsp if (err)
7882 f259c4c1 2021-09-24 stsp goto done;
7883 f259c4c1 2021-09-24 stsp
7884 f259c4c1 2021-09-24 stsp done:
7885 f259c4c1 2021-09-24 stsp free(branch_refname);
7886 f259c4c1 2021-09-24 stsp free(commit_refname);
7887 f259c4c1 2021-09-24 stsp return err;
7888 f259c4c1 2021-09-24 stsp }
7889 f259c4c1 2021-09-24 stsp
7890 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7891 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7892 f259c4c1 2021-09-24 stsp const char *branch_name;
7893 f259c4c1 2021-09-24 stsp };
7894 f259c4c1 2021-09-24 stsp
7895 f259c4c1 2021-09-24 stsp static const struct got_error *
7896 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7897 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7898 f259c4c1 2021-09-24 stsp {
7899 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7900 f259c4c1 2021-09-24 stsp
7901 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7902 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7903 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7904 f259c4c1 2021-09-24 stsp
7905 f259c4c1 2021-09-24 stsp return NULL;
7906 f259c4c1 2021-09-24 stsp }
7907 f259c4c1 2021-09-24 stsp
7908 f259c4c1 2021-09-24 stsp
7909 f259c4c1 2021-09-24 stsp const struct got_error *
7910 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7911 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7912 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7913 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7914 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7915 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7916 f259c4c1 2021-09-24 stsp {
7917 f259c4c1 2021-09-24 stsp const struct got_error *err;
7918 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7919 f259c4c1 2021-09-24 stsp
7920 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7921 f259c4c1 2021-09-24 stsp if (err)
7922 f259c4c1 2021-09-24 stsp goto done;
7923 f259c4c1 2021-09-24 stsp
7924 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7925 f259c4c1 2021-09-24 stsp worktree);
7926 f259c4c1 2021-09-24 stsp if (err)
7927 f259c4c1 2021-09-24 stsp goto done;
7928 f259c4c1 2021-09-24 stsp
7929 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7930 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7931 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7932 f259c4c1 2021-09-24 stsp done:
7933 f259c4c1 2021-09-24 stsp free(fileindex_path);
7934 f259c4c1 2021-09-24 stsp return err;
7935 f259c4c1 2021-09-24 stsp }
7936 f259c4c1 2021-09-24 stsp
7937 f259c4c1 2021-09-24 stsp const struct got_error *
7938 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7939 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7940 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7941 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7942 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
7943 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7944 0ff8d236 2021-09-28 stsp
7945 f259c4c1 2021-09-24 stsp {
7946 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7947 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7948 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7949 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7950 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7951 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7952 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7953 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7954 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7955 f259c4c1 2021-09-24 stsp
7956 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7957 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7958 f259c4c1 2021-09-24 stsp
7959 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7960 f259c4c1 2021-09-24 stsp
7961 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7962 f259c4c1 2021-09-24 stsp if (err)
7963 f259c4c1 2021-09-24 stsp goto done;
7964 f259c4c1 2021-09-24 stsp
7965 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7966 f259c4c1 2021-09-24 stsp if (err)
7967 f259c4c1 2021-09-24 stsp goto done;
7968 f259c4c1 2021-09-24 stsp
7969 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7970 f259c4c1 2021-09-24 stsp if (err)
7971 f259c4c1 2021-09-24 stsp goto done;
7972 f259c4c1 2021-09-24 stsp
7973 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7974 f259c4c1 2021-09-24 stsp &have_staged_files);
7975 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7976 f259c4c1 2021-09-24 stsp goto done;
7977 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7978 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7979 f259c4c1 2021-09-24 stsp goto done;
7980 f259c4c1 2021-09-24 stsp }
7981 f259c4c1 2021-09-24 stsp
7982 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7983 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7984 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7985 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7986 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7987 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7988 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
7989 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7990 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7991 f259c4c1 2021-09-24 stsp if (err)
7992 f259c4c1 2021-09-24 stsp goto done;
7993 f259c4c1 2021-09-24 stsp
7994 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7995 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7996 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7997 f259c4c1 2021-09-24 stsp goto done;
7998 f259c4c1 2021-09-24 stsp }
7999 f259c4c1 2021-09-24 stsp
8000 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8001 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8002 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
8003 f259c4c1 2021-09-24 stsp
8004 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
8005 f259c4c1 2021-09-24 stsp ct_path++;
8006 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
8007 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8008 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8009 f259c4c1 2021-09-24 stsp if (err)
8010 f259c4c1 2021-09-24 stsp goto done;
8011 f259c4c1 2021-09-24 stsp
8012 f259c4c1 2021-09-24 stsp }
8013 f259c4c1 2021-09-24 stsp
8014 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8015 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8016 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8017 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8018 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8019 f259c4c1 2021-09-24 stsp if (err)
8020 f259c4c1 2021-09-24 stsp goto done;
8021 f259c4c1 2021-09-24 stsp
8022 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8023 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8024 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8025 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8026 f259c4c1 2021-09-24 stsp err = sync_err;
8027 f259c4c1 2021-09-24 stsp done:
8028 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8029 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8030 d8bacb93 2023-01-10 mark
8031 f259c4c1 2021-09-24 stsp free_commitable(ct);
8032 f259c4c1 2021-09-24 stsp }
8033 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8034 f259c4c1 2021-09-24 stsp free(fileindex_path);
8035 f259c4c1 2021-09-24 stsp return err;
8036 f259c4c1 2021-09-24 stsp }
8037 f259c4c1 2021-09-24 stsp
8038 f259c4c1 2021-09-24 stsp const struct got_error *
8039 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8040 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8041 f259c4c1 2021-09-24 stsp {
8042 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8043 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8044 f259c4c1 2021-09-24 stsp
8045 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8046 f259c4c1 2021-09-24 stsp if (err)
8047 f259c4c1 2021-09-24 stsp goto done;
8048 f259c4c1 2021-09-24 stsp
8049 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8050 f259c4c1 2021-09-24 stsp if (err)
8051 f259c4c1 2021-09-24 stsp goto done;
8052 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8053 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8054 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8055 f259c4c1 2021-09-24 stsp err = sync_err;
8056 f259c4c1 2021-09-24 stsp done:
8057 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8058 f259c4c1 2021-09-24 stsp free(fileindex_path);
8059 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8060 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8061 f259c4c1 2021-09-24 stsp err = unlockerr;
8062 f259c4c1 2021-09-24 stsp return err;
8063 f259c4c1 2021-09-24 stsp }
8064 f259c4c1 2021-09-24 stsp
8065 f259c4c1 2021-09-24 stsp const struct got_error *
8066 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8067 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8068 f259c4c1 2021-09-24 stsp {
8069 f259c4c1 2021-09-24 stsp const struct got_error *err;
8070 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8071 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8072 f259c4c1 2021-09-24 stsp
8073 f259c4c1 2021-09-24 stsp *in_progress = 0;
8074 f259c4c1 2021-09-24 stsp
8075 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8076 f259c4c1 2021-09-24 stsp if (err)
8077 f259c4c1 2021-09-24 stsp return err;
8078 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8079 793fcac3 2021-09-24 stsp free(branch_refname);
8080 f259c4c1 2021-09-24 stsp if (err) {
8081 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8082 f259c4c1 2021-09-24 stsp return err;
8083 f259c4c1 2021-09-24 stsp } else
8084 f259c4c1 2021-09-24 stsp *in_progress = 1;
8085 f259c4c1 2021-09-24 stsp
8086 f259c4c1 2021-09-24 stsp return NULL;
8087 f259c4c1 2021-09-24 stsp }
8088 f259c4c1 2021-09-24 stsp
8089 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8090 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8091 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8092 f259c4c1 2021-09-24 stsp {
8093 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8094 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8095 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8096 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8097 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8098 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8099 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8100 f259c4c1 2021-09-24 stsp
8101 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8102 f259c4c1 2021-09-24 stsp
8103 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8104 f259c4c1 2021-09-24 stsp if (err)
8105 f259c4c1 2021-09-24 stsp return err;
8106 f259c4c1 2021-09-24 stsp
8107 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8108 f259c4c1 2021-09-24 stsp if (err)
8109 f259c4c1 2021-09-24 stsp goto done;
8110 f259c4c1 2021-09-24 stsp
8111 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8112 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8113 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8114 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8115 f259c4c1 2021-09-24 stsp &ok_arg);
8116 f259c4c1 2021-09-24 stsp if (err)
8117 f259c4c1 2021-09-24 stsp goto done;
8118 f259c4c1 2021-09-24 stsp
8119 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8120 f259c4c1 2021-09-24 stsp if (err)
8121 f259c4c1 2021-09-24 stsp return err;
8122 f259c4c1 2021-09-24 stsp
8123 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8124 f259c4c1 2021-09-24 stsp if (err)
8125 f259c4c1 2021-09-24 stsp return err;
8126 f259c4c1 2021-09-24 stsp
8127 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8128 f259c4c1 2021-09-24 stsp 0);
8129 f259c4c1 2021-09-24 stsp if (err)
8130 f259c4c1 2021-09-24 stsp goto done;
8131 f259c4c1 2021-09-24 stsp
8132 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8133 f259c4c1 2021-09-24 stsp if (err)
8134 f259c4c1 2021-09-24 stsp goto done;
8135 f259c4c1 2021-09-24 stsp
8136 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8137 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8138 f259c4c1 2021-09-24 stsp goto done;
8139 f259c4c1 2021-09-24 stsp }
8140 f259c4c1 2021-09-24 stsp
8141 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8142 f259c4c1 2021-09-24 stsp if (err)
8143 f259c4c1 2021-09-24 stsp goto done;
8144 f259c4c1 2021-09-24 stsp
8145 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8146 f259c4c1 2021-09-24 stsp if (err)
8147 f259c4c1 2021-09-24 stsp goto done;
8148 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8149 f259c4c1 2021-09-24 stsp if (err)
8150 f259c4c1 2021-09-24 stsp goto done;
8151 f259c4c1 2021-09-24 stsp
8152 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8153 f259c4c1 2021-09-24 stsp if (err)
8154 f259c4c1 2021-09-24 stsp goto done;
8155 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8156 f259c4c1 2021-09-24 stsp if (err)
8157 f259c4c1 2021-09-24 stsp goto done;
8158 f259c4c1 2021-09-24 stsp
8159 f259c4c1 2021-09-24 stsp done:
8160 f259c4c1 2021-09-24 stsp free(branch_refname);
8161 f259c4c1 2021-09-24 stsp free(commit_refname);
8162 f259c4c1 2021-09-24 stsp free(fileindex_path);
8163 f259c4c1 2021-09-24 stsp if (branch_ref)
8164 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8165 f259c4c1 2021-09-24 stsp if (commit_ref)
8166 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8167 f259c4c1 2021-09-24 stsp if (wt_branch)
8168 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8169 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8170 f259c4c1 2021-09-24 stsp if (err) {
8171 f259c4c1 2021-09-24 stsp if (*fileindex) {
8172 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8173 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8174 f259c4c1 2021-09-24 stsp }
8175 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8176 f259c4c1 2021-09-24 stsp }
8177 f259c4c1 2021-09-24 stsp return err;
8178 f259c4c1 2021-09-24 stsp }
8179 f259c4c1 2021-09-24 stsp
8180 f259c4c1 2021-09-24 stsp const struct got_error *
8181 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8182 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8183 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8184 f259c4c1 2021-09-24 stsp {
8185 f259c4c1 2021-09-24 stsp const struct got_error *err;
8186 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8187 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8188 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8189 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8190 f259c4c1 2021-09-24 stsp
8191 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8192 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8193 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8194 f259c4c1 2021-09-24 stsp
8195 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8196 f259c4c1 2021-09-24 stsp if (err)
8197 f259c4c1 2021-09-24 stsp return err;
8198 f259c4c1 2021-09-24 stsp
8199 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8200 f259c4c1 2021-09-24 stsp if (err)
8201 f259c4c1 2021-09-24 stsp goto done;
8202 f259c4c1 2021-09-24 stsp
8203 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8204 f259c4c1 2021-09-24 stsp &have_staged_files);
8205 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8206 f259c4c1 2021-09-24 stsp goto done;
8207 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8208 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8209 f259c4c1 2021-09-24 stsp goto done;
8210 f259c4c1 2021-09-24 stsp }
8211 f259c4c1 2021-09-24 stsp
8212 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8213 f259c4c1 2021-09-24 stsp if (err)
8214 f259c4c1 2021-09-24 stsp goto done;
8215 f259c4c1 2021-09-24 stsp
8216 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8217 f259c4c1 2021-09-24 stsp if (err)
8218 f259c4c1 2021-09-24 stsp goto done;
8219 f259c4c1 2021-09-24 stsp
8220 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8221 f259c4c1 2021-09-24 stsp if (err)
8222 f259c4c1 2021-09-24 stsp goto done;
8223 f259c4c1 2021-09-24 stsp
8224 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8225 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8226 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8227 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8228 f259c4c1 2021-09-24 stsp goto done;
8229 f259c4c1 2021-09-24 stsp }
8230 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8231 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8232 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8233 f259c4c1 2021-09-24 stsp goto done;
8234 f259c4c1 2021-09-24 stsp }
8235 f259c4c1 2021-09-24 stsp
8236 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8237 f259c4c1 2021-09-24 stsp if (err)
8238 f259c4c1 2021-09-24 stsp goto done;
8239 f259c4c1 2021-09-24 stsp
8240 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8241 f259c4c1 2021-09-24 stsp if (err)
8242 f259c4c1 2021-09-24 stsp goto done;
8243 f259c4c1 2021-09-24 stsp done:
8244 f259c4c1 2021-09-24 stsp free(commit_refname);
8245 f259c4c1 2021-09-24 stsp free(branch_refname);
8246 f259c4c1 2021-09-24 stsp free(fileindex_path);
8247 f259c4c1 2021-09-24 stsp if (commit_ref)
8248 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8249 f259c4c1 2021-09-24 stsp if (branch_ref)
8250 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8251 f259c4c1 2021-09-24 stsp if (err) {
8252 f259c4c1 2021-09-24 stsp if (*branch_name) {
8253 f259c4c1 2021-09-24 stsp free(*branch_name);
8254 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8255 f259c4c1 2021-09-24 stsp }
8256 f259c4c1 2021-09-24 stsp free(*branch_tip);
8257 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8258 f259c4c1 2021-09-24 stsp if (*fileindex) {
8259 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8260 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8261 f259c4c1 2021-09-24 stsp }
8262 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8263 f259c4c1 2021-09-24 stsp }
8264 f259c4c1 2021-09-24 stsp return err;
8265 f259c4c1 2021-09-24 stsp }
8266 f259c4c1 2021-09-24 stsp
8267 f259c4c1 2021-09-24 stsp const struct got_error *
8268 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8269 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8270 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8271 f259c4c1 2021-09-24 stsp {
8272 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8273 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8274 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8275 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8276 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8277 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8278 f259c4c1 2021-09-24 stsp
8279 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8280 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8281 a44927cc 2022-04-07 stsp if (err)
8282 a44927cc 2022-04-07 stsp goto done;
8283 a44927cc 2022-04-07 stsp
8284 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8285 a44927cc 2022-04-07 stsp worktree->path_prefix);
8286 f259c4c1 2021-09-24 stsp if (err)
8287 f259c4c1 2021-09-24 stsp goto done;
8288 f259c4c1 2021-09-24 stsp
8289 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8290 f259c4c1 2021-09-24 stsp if (err)
8291 f259c4c1 2021-09-24 stsp goto done;
8292 f259c4c1 2021-09-24 stsp
8293 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8294 f259c4c1 2021-09-24 stsp if (err)
8295 f259c4c1 2021-09-24 stsp goto done;
8296 f259c4c1 2021-09-24 stsp
8297 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8298 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8299 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8300 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8301 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8302 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8303 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8304 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8305 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8306 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8307 f259c4c1 2021-09-24 stsp if (err)
8308 f259c4c1 2021-09-24 stsp goto sync;
8309 f259c4c1 2021-09-24 stsp
8310 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8311 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8312 f259c4c1 2021-09-24 stsp sync:
8313 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8314 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8315 f259c4c1 2021-09-24 stsp err = sync_err;
8316 f259c4c1 2021-09-24 stsp done:
8317 f259c4c1 2021-09-24 stsp free(tree_id);
8318 f259c4c1 2021-09-24 stsp free(commit_id);
8319 a44927cc 2022-04-07 stsp if (commit)
8320 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8321 f259c4c1 2021-09-24 stsp if (fileindex)
8322 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8323 f259c4c1 2021-09-24 stsp free(fileindex_path);
8324 f259c4c1 2021-09-24 stsp
8325 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8326 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8327 f259c4c1 2021-09-24 stsp err = unlockerr;
8328 f259c4c1 2021-09-24 stsp return err;
8329 f259c4c1 2021-09-24 stsp }
8330 f259c4c1 2021-09-24 stsp
8331 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8332 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8333 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8334 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8335 2db2652d 2019-08-07 stsp struct got_repository *repo;
8336 2db2652d 2019-08-07 stsp int have_changes;
8337 2db2652d 2019-08-07 stsp };
8338 2db2652d 2019-08-07 stsp
8339 336075a4 2022-06-25 op static const struct got_error *
8340 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8341 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8342 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8343 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8344 735ef5ac 2019-08-03 stsp {
8345 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8346 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8347 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8348 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8349 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8350 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8351 8b13ce36 2019-08-08 stsp
8352 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8353 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8354 8b13ce36 2019-08-08 stsp return NULL;
8355 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8356 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8357 735ef5ac 2019-08-03 stsp
8358 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8359 735ef5ac 2019-08-03 stsp if (ie == NULL)
8360 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8361 735ef5ac 2019-08-03 stsp
8362 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8363 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8364 735ef5ac 2019-08-03 stsp relpath) == -1)
8365 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8366 735ef5ac 2019-08-03 stsp
8367 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8368 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8369 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8370 735ef5ac 2019-08-03 stsp }
8371 735ef5ac 2019-08-03 stsp
8372 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8373 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8374 3aa5969e 2019-08-06 stsp goto done;
8375 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8376 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8377 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8378 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8379 735ef5ac 2019-08-03 stsp goto done;
8380 3aa5969e 2019-08-06 stsp }
8381 735ef5ac 2019-08-03 stsp
8382 2db2652d 2019-08-07 stsp a->have_changes = 1;
8383 2db2652d 2019-08-07 stsp
8384 735ef5ac 2019-08-03 stsp p = in_repo_path;
8385 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8386 735ef5ac 2019-08-03 stsp p++;
8387 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8388 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8389 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8390 735ef5ac 2019-08-03 stsp done:
8391 735ef5ac 2019-08-03 stsp free(in_repo_path);
8392 dc424a06 2019-08-07 stsp return err;
8393 dc424a06 2019-08-07 stsp }
8394 dc424a06 2019-08-07 stsp
8395 2db2652d 2019-08-07 stsp struct stage_path_arg {
8396 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8397 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8398 2db2652d 2019-08-07 stsp struct got_repository *repo;
8399 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8400 2db2652d 2019-08-07 stsp void *status_arg;
8401 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8402 2db2652d 2019-08-07 stsp void *patch_arg;
8403 7b5dc508 2019-10-28 stsp int staged_something;
8404 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8405 2db2652d 2019-08-07 stsp };
8406 2db2652d 2019-08-07 stsp
8407 2db2652d 2019-08-07 stsp static const struct got_error *
8408 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8409 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8410 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8411 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8412 0cb83759 2019-08-03 stsp {
8413 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8414 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8415 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8416 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8417 0cb83759 2019-08-03 stsp uint32_t stage;
8418 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8419 0aeb8099 2020-07-23 stsp struct stat sb;
8420 8b13ce36 2019-08-08 stsp
8421 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8422 8b13ce36 2019-08-08 stsp return NULL;
8423 0cb83759 2019-08-03 stsp
8424 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8425 d3e7c587 2019-08-03 stsp if (ie == NULL)
8426 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8427 0cb83759 2019-08-03 stsp
8428 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8429 2db2652d 2019-08-07 stsp relpath)== -1)
8430 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8431 0cb83759 2019-08-03 stsp
8432 0cb83759 2019-08-03 stsp switch (status) {
8433 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8434 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8435 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8436 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8437 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8438 0aeb8099 2020-07-23 stsp break;
8439 0aeb8099 2020-07-23 stsp }
8440 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8441 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8442 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8443 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8444 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8445 dc424a06 2019-08-07 stsp if (err)
8446 dc424a06 2019-08-07 stsp break;
8447 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8448 dc424a06 2019-08-07 stsp break;
8449 dc424a06 2019-08-07 stsp } else {
8450 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8451 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8452 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8453 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8454 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8455 dc424a06 2019-08-07 stsp break;
8456 dc424a06 2019-08-07 stsp }
8457 dc424a06 2019-08-07 stsp }
8458 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8459 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8460 0cb83759 2019-08-03 stsp if (err)
8461 dc424a06 2019-08-07 stsp break;
8462 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8463 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8464 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8465 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8466 0cb83759 2019-08-03 stsp else
8467 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8468 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8469 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8470 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8471 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8472 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8473 35213c7c 2020-07-23 stsp ssize_t target_len;
8474 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8475 35213c7c 2020-07-23 stsp sizeof(target_path));
8476 35213c7c 2020-07-23 stsp if (target_len == -1) {
8477 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8478 35213c7c 2020-07-23 stsp ondisk_path);
8479 35213c7c 2020-07-23 stsp break;
8480 35213c7c 2020-07-23 stsp }
8481 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8482 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8483 35213c7c 2020-07-23 stsp a->worktree->root_path);
8484 35213c7c 2020-07-23 stsp if (err)
8485 35213c7c 2020-07-23 stsp break;
8486 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8487 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8488 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8489 35213c7c 2020-07-23 stsp break;
8490 35213c7c 2020-07-23 stsp }
8491 35213c7c 2020-07-23 stsp }
8492 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8493 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8494 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8495 35213c7c 2020-07-23 stsp else
8496 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8497 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8498 0aeb8099 2020-07-23 stsp } else {
8499 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8500 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8501 0aeb8099 2020-07-23 stsp }
8502 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8503 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8504 dc424a06 2019-08-07 stsp break;
8505 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8506 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8507 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8508 9fdde394 2022-06-04 op if (err)
8509 9fdde394 2022-06-04 op break;
8510 9fdde394 2022-06-04 op /*
8511 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8512 9fdde394 2022-06-04 op * implicitly unstage the file.
8513 9fdde394 2022-06-04 op */
8514 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8515 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8516 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8517 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8518 9fdde394 2022-06-04 op }
8519 0cb83759 2019-08-03 stsp break;
8520 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8521 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8522 d3e7c587 2019-08-03 stsp break;
8523 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8524 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8525 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8526 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8527 dc424a06 2019-08-07 stsp if (err)
8528 dc424a06 2019-08-07 stsp break;
8529 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8530 88f33a19 2019-08-08 stsp break;
8531 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8532 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8533 dc424a06 2019-08-07 stsp break;
8534 88f33a19 2019-08-08 stsp }
8535 dc424a06 2019-08-07 stsp }
8536 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8537 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8538 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8539 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8540 dc424a06 2019-08-07 stsp break;
8541 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8542 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8543 12463d8b 2019-12-13 stsp de_name);
8544 0cb83759 2019-08-03 stsp break;
8545 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8546 d3e7c587 2019-08-03 stsp break;
8547 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8548 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8549 ebf48fd5 2019-08-03 stsp break;
8550 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8551 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8552 2a06fe5f 2019-08-24 stsp break;
8553 0cb83759 2019-08-03 stsp default:
8554 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8555 537ac44b 2019-08-03 stsp break;
8556 0cb83759 2019-08-03 stsp }
8557 dc424a06 2019-08-07 stsp
8558 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8559 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8560 dc424a06 2019-08-07 stsp free(path_content);
8561 2db2652d 2019-08-07 stsp free(ondisk_path);
8562 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8563 0ebf8283 2019-07-24 stsp return err;
8564 0ebf8283 2019-07-24 stsp }
8565 0cb83759 2019-08-03 stsp
8566 0cb83759 2019-08-03 stsp const struct got_error *
8567 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8568 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8569 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8570 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8571 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8572 0cb83759 2019-08-03 stsp {
8573 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8574 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8575 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8576 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8577 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8578 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8579 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8580 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8581 0cb83759 2019-08-03 stsp
8582 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8583 0cb83759 2019-08-03 stsp if (err)
8584 0cb83759 2019-08-03 stsp return err;
8585 0cb83759 2019-08-03 stsp
8586 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8587 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8588 735ef5ac 2019-08-03 stsp if (err)
8589 735ef5ac 2019-08-03 stsp goto done;
8590 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8591 735ef5ac 2019-08-03 stsp if (err)
8592 735ef5ac 2019-08-03 stsp goto done;
8593 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8594 0cb83759 2019-08-03 stsp if (err)
8595 0cb83759 2019-08-03 stsp goto done;
8596 0cb83759 2019-08-03 stsp
8597 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8598 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8599 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8600 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8601 2db2652d 2019-08-07 stsp oka.repo = repo;
8602 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8603 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8604 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8605 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8606 735ef5ac 2019-08-03 stsp if (err)
8607 735ef5ac 2019-08-03 stsp goto done;
8608 735ef5ac 2019-08-03 stsp }
8609 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8610 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8611 2db2652d 2019-08-07 stsp goto done;
8612 2db2652d 2019-08-07 stsp }
8613 735ef5ac 2019-08-03 stsp
8614 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8615 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8616 2db2652d 2019-08-07 stsp spa.repo = repo;
8617 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8618 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8619 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8620 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8621 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8622 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8623 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8624 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8625 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8626 42005733 2019-08-03 stsp if (err)
8627 2db2652d 2019-08-07 stsp goto done;
8628 7b5dc508 2019-10-28 stsp }
8629 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8630 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8631 7b5dc508 2019-10-28 stsp goto done;
8632 0cb83759 2019-08-03 stsp }
8633 0cb83759 2019-08-03 stsp
8634 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8635 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8636 0cb83759 2019-08-03 stsp err = sync_err;
8637 0cb83759 2019-08-03 stsp done:
8638 735ef5ac 2019-08-03 stsp if (head_ref)
8639 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8640 735ef5ac 2019-08-03 stsp free(head_commit_id);
8641 0cb83759 2019-08-03 stsp free(fileindex_path);
8642 0cb83759 2019-08-03 stsp if (fileindex)
8643 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8644 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8645 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8646 0cb83759 2019-08-03 stsp err = unlockerr;
8647 0cb83759 2019-08-03 stsp return err;
8648 0cb83759 2019-08-03 stsp }
8649 ad493afc 2019-08-03 stsp
8650 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8651 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8652 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8653 ad493afc 2019-08-03 stsp struct got_repository *repo;
8654 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8655 ad493afc 2019-08-03 stsp void *progress_arg;
8656 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8657 2e1f37b0 2019-08-08 stsp void *patch_arg;
8658 ad493afc 2019-08-03 stsp };
8659 2e1f37b0 2019-08-08 stsp
8660 2e1f37b0 2019-08-08 stsp static const struct got_error *
8661 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8662 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8663 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8664 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8665 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8666 2e1f37b0 2019-08-08 stsp {
8667 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8668 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8669 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8670 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8671 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8672 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8673 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8674 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8675 2e1f37b0 2019-08-08 stsp
8676 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8677 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8678 2e1f37b0 2019-08-08 stsp
8679 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8680 2e1f37b0 2019-08-08 stsp if (err)
8681 2e1f37b0 2019-08-08 stsp return err;
8682 eb81bc23 2022-06-28 tracey
8683 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8684 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8685 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8686 eb81bc23 2022-06-28 tracey goto done;
8687 eb81bc23 2022-06-28 tracey }
8688 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8689 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8690 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8691 eb81bc23 2022-06-28 tracey goto done;
8692 eb81bc23 2022-06-28 tracey }
8693 eb81bc23 2022-06-28 tracey
8694 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8695 2e1f37b0 2019-08-08 stsp if (err)
8696 2e1f37b0 2019-08-08 stsp goto done;
8697 2e1f37b0 2019-08-08 stsp
8698 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8699 2e1f37b0 2019-08-08 stsp if (err)
8700 2e1f37b0 2019-08-08 stsp goto done;
8701 2e1f37b0 2019-08-08 stsp
8702 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8703 2e1f37b0 2019-08-08 stsp if (err)
8704 2e1f37b0 2019-08-08 stsp goto done;
8705 2e1f37b0 2019-08-08 stsp
8706 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8707 eb81bc23 2022-06-28 tracey fd2);
8708 2e1f37b0 2019-08-08 stsp if (err)
8709 2e1f37b0 2019-08-08 stsp goto done;
8710 2e1f37b0 2019-08-08 stsp
8711 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8712 2e1f37b0 2019-08-08 stsp if (err)
8713 2e1f37b0 2019-08-08 stsp goto done;
8714 ad493afc 2019-08-03 stsp
8715 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8716 2e1f37b0 2019-08-08 stsp if (err)
8717 2e1f37b0 2019-08-08 stsp goto done;
8718 2e1f37b0 2019-08-08 stsp
8719 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8720 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8721 2e1f37b0 2019-08-08 stsp if (err)
8722 2e1f37b0 2019-08-08 stsp goto done;
8723 2e1f37b0 2019-08-08 stsp
8724 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8725 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8726 2e1f37b0 2019-08-08 stsp if (err)
8727 2e1f37b0 2019-08-08 stsp goto done;
8728 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8729 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8730 2e1f37b0 2019-08-08 stsp if (err)
8731 2e1f37b0 2019-08-08 stsp goto done;
8732 2e1f37b0 2019-08-08 stsp
8733 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8734 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8735 2e1f37b0 2019-08-08 stsp goto done;
8736 2e1f37b0 2019-08-08 stsp }
8737 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8738 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8739 2e1f37b0 2019-08-08 stsp goto done;
8740 2e1f37b0 2019-08-08 stsp }
8741 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8742 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8743 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8744 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8745 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8746 fe621944 2020-11-10 stsp nchanges++;
8747 fe621944 2020-11-10 stsp }
8748 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8749 2e1f37b0 2019-08-08 stsp int choice;
8750 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8751 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8752 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8753 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8754 2e1f37b0 2019-08-08 stsp if (err)
8755 2e1f37b0 2019-08-08 stsp goto done;
8756 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8757 2e1f37b0 2019-08-08 stsp have_content = 1;
8758 19e4b907 2019-08-08 stsp else
8759 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8760 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8761 2e1f37b0 2019-08-08 stsp break;
8762 2e1f37b0 2019-08-08 stsp }
8763 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8764 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8765 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8766 2e1f37b0 2019-08-08 stsp done:
8767 2e1f37b0 2019-08-08 stsp free(label1);
8768 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8769 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8770 2e1f37b0 2019-08-08 stsp if (blob)
8771 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8772 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8773 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8774 2e1f37b0 2019-08-08 stsp if (staged_blob)
8775 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8776 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8777 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8778 fe621944 2020-11-10 stsp err = free_err;
8779 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8780 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8781 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8782 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8783 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8784 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8785 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8786 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8787 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8788 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8789 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8790 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8791 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8792 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8793 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8794 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8795 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8796 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8797 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8798 2e1f37b0 2019-08-08 stsp }
8799 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8800 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8801 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8802 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8803 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8804 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8805 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8806 2e1f37b0 2019-08-08 stsp }
8807 2e1f37b0 2019-08-08 stsp free(path1);
8808 2e1f37b0 2019-08-08 stsp free(path2);
8809 fda8017d 2020-07-23 stsp return err;
8810 fda8017d 2020-07-23 stsp }
8811 fda8017d 2020-07-23 stsp
8812 fda8017d 2020-07-23 stsp static const struct got_error *
8813 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8814 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8815 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8816 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8817 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8818 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8819 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8820 fda8017d 2020-07-23 stsp {
8821 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8822 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8823 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8824 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8825 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8826 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8827 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8828 fda8017d 2020-07-23 stsp struct stat sb;
8829 fda8017d 2020-07-23 stsp
8830 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8831 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8832 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8833 fda8017d 2020-07-23 stsp if (err)
8834 fda8017d 2020-07-23 stsp return err;
8835 fda8017d 2020-07-23 stsp
8836 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8837 fda8017d 2020-07-23 stsp return NULL;
8838 fda8017d 2020-07-23 stsp
8839 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8840 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8841 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8842 fda8017d 2020-07-23 stsp if (err)
8843 fda8017d 2020-07-23 stsp goto done;
8844 fda8017d 2020-07-23 stsp }
8845 fda8017d 2020-07-23 stsp
8846 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8847 fda8017d 2020-07-23 stsp if (f == NULL) {
8848 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8849 fda8017d 2020-07-23 stsp path_unstaged_content);
8850 fda8017d 2020-07-23 stsp goto done;
8851 fda8017d 2020-07-23 stsp }
8852 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8853 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8854 fda8017d 2020-07-23 stsp goto done;
8855 fda8017d 2020-07-23 stsp }
8856 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8857 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8858 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8859 fda8017d 2020-07-23 stsp size_t r;
8860 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8861 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8862 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8863 fda8017d 2020-07-23 stsp goto done;
8864 fda8017d 2020-07-23 stsp }
8865 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8866 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8867 fda8017d 2020-07-23 stsp goto done;
8868 fda8017d 2020-07-23 stsp }
8869 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8870 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8871 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8872 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8873 fda8017d 2020-07-23 stsp progress_arg);
8874 fda8017d 2020-07-23 stsp } else {
8875 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8876 67a66647 2021-05-31 stsp
8877 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8878 67a66647 2021-05-31 stsp if (err)
8879 67a66647 2021-05-31 stsp return err;
8880 67a66647 2021-05-31 stsp
8881 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8882 67a66647 2021-05-31 stsp parent) == -1) {
8883 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8884 67a66647 2021-05-31 stsp base_path = NULL;
8885 67a66647 2021-05-31 stsp goto done;
8886 67a66647 2021-05-31 stsp }
8887 67a66647 2021-05-31 stsp
8888 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8889 b90054ed 2022-11-01 stsp base_path, "");
8890 67a66647 2021-05-31 stsp if (err)
8891 67a66647 2021-05-31 stsp goto done;
8892 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8893 67a66647 2021-05-31 stsp blob_base);
8894 67a66647 2021-05-31 stsp if (err)
8895 67a66647 2021-05-31 stsp goto done;
8896 67a66647 2021-05-31 stsp
8897 5e91dae4 2022-08-30 stsp /*
8898 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8899 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8900 eec2f5a9 2021-06-03 stsp */
8901 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8902 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8903 eec2f5a9 2021-06-03 stsp ondisk_path);
8904 eec2f5a9 2021-06-03 stsp if (err)
8905 eec2f5a9 2021-06-03 stsp goto done;
8906 eec2f5a9 2021-06-03 stsp } else {
8907 eec2f5a9 2021-06-03 stsp int fd;
8908 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8909 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8910 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8911 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8912 eec2f5a9 2021-06-03 stsp goto done;
8913 eec2f5a9 2021-06-03 stsp }
8914 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8915 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8916 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8917 eec2f5a9 2021-06-03 stsp close(fd);
8918 eec2f5a9 2021-06-03 stsp goto done;
8919 eec2f5a9 2021-06-03 stsp }
8920 eec2f5a9 2021-06-03 stsp }
8921 eec2f5a9 2021-06-03 stsp
8922 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8923 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8924 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8925 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8926 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8927 fda8017d 2020-07-23 stsp }
8928 fda8017d 2020-07-23 stsp if (err)
8929 fda8017d 2020-07-23 stsp goto done;
8930 fda8017d 2020-07-23 stsp
8931 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8932 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8933 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8934 2ac8aa02 2020-11-09 stsp } else {
8935 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8936 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8937 2ac8aa02 2020-11-09 stsp }
8938 fda8017d 2020-07-23 stsp done:
8939 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8940 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8941 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8942 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8943 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8944 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8945 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8946 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8947 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8948 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8949 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8950 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8951 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8952 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8953 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8954 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8955 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8956 67a66647 2021-05-31 stsp free(blob_base_path);
8957 67a66647 2021-05-31 stsp free(parent);
8958 67a66647 2021-05-31 stsp free(base_path);
8959 2e1f37b0 2019-08-08 stsp return err;
8960 2e1f37b0 2019-08-08 stsp }
8961 2e1f37b0 2019-08-08 stsp
8962 ad493afc 2019-08-03 stsp static const struct got_error *
8963 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8964 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8965 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8966 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8967 ad493afc 2019-08-03 stsp {
8968 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8969 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8970 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8971 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8972 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8973 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8974 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8975 9bc94a15 2019-08-03 stsp struct stat sb;
8976 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8977 ad493afc 2019-08-03 stsp
8978 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8979 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8980 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8981 2e1f37b0 2019-08-08 stsp return NULL;
8982 2e1f37b0 2019-08-08 stsp
8983 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8984 ad493afc 2019-08-03 stsp if (ie == NULL)
8985 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8986 9bc94a15 2019-08-03 stsp
8987 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8988 9bc94a15 2019-08-03 stsp == -1)
8989 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8990 ad493afc 2019-08-03 stsp
8991 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8992 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8993 f69721c3 2019-10-21 stsp if (err)
8994 f69721c3 2019-10-21 stsp goto done;
8995 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8996 f69721c3 2019-10-21 stsp id_str) == -1) {
8997 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8998 eb81bc23 2022-06-28 tracey goto done;
8999 eb81bc23 2022-06-28 tracey }
9000 eb81bc23 2022-06-28 tracey
9001 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9002 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9003 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9004 eb81bc23 2022-06-28 tracey goto done;
9005 eb81bc23 2022-06-28 tracey }
9006 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9007 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9008 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9009 f69721c3 2019-10-21 stsp goto done;
9010 f69721c3 2019-10-21 stsp }
9011 f69721c3 2019-10-21 stsp
9012 ad493afc 2019-08-03 stsp switch (staged_status) {
9013 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9014 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9015 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9016 ad493afc 2019-08-03 stsp if (err)
9017 ad493afc 2019-08-03 stsp break;
9018 ad493afc 2019-08-03 stsp /* fall through */
9019 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9020 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9021 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9022 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9023 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9024 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9025 2e1f37b0 2019-08-08 stsp if (err)
9026 2e1f37b0 2019-08-08 stsp break;
9027 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9028 2e1f37b0 2019-08-08 stsp break;
9029 2e1f37b0 2019-08-08 stsp } else {
9030 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9031 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9032 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9033 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9034 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9035 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9036 2e1f37b0 2019-08-08 stsp }
9037 2e1f37b0 2019-08-08 stsp }
9038 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9039 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9040 ad493afc 2019-08-03 stsp if (err)
9041 ad493afc 2019-08-03 stsp break;
9042 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9043 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9044 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9045 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9046 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9047 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9048 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9049 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9050 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9051 ea7786be 2020-07-23 stsp break;
9052 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9053 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9054 36bf999c 2020-07-23 stsp char *staged_target;
9055 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9056 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9057 36bf999c 2020-07-23 stsp if (err)
9058 36bf999c 2020-07-23 stsp goto done;
9059 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9060 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9061 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9062 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9063 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9064 36bf999c 2020-07-23 stsp free(staged_target);
9065 dfe9fba0 2020-07-23 stsp } else {
9066 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9067 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9068 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9069 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9070 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9071 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9072 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9073 dfe9fba0 2020-07-23 stsp }
9074 ea7786be 2020-07-23 stsp break;
9075 ea7786be 2020-07-23 stsp default:
9076 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9077 ea7786be 2020-07-23 stsp break;
9078 ea7786be 2020-07-23 stsp }
9079 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9080 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9081 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9082 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9083 2ac8aa02 2020-11-09 stsp }
9084 ad493afc 2019-08-03 stsp break;
9085 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9086 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9087 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9088 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9089 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9090 2e1f37b0 2019-08-08 stsp if (err)
9091 2e1f37b0 2019-08-08 stsp break;
9092 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9093 2e1f37b0 2019-08-08 stsp break;
9094 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9095 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9096 2e1f37b0 2019-08-08 stsp break;
9097 2e1f37b0 2019-08-08 stsp }
9098 2e1f37b0 2019-08-08 stsp }
9099 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9100 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9101 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9102 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9103 9bc94a15 2019-08-03 stsp if (err)
9104 9bc94a15 2019-08-03 stsp break;
9105 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9106 ad493afc 2019-08-03 stsp break;
9107 ad493afc 2019-08-03 stsp }
9108 f69721c3 2019-10-21 stsp done:
9109 ad493afc 2019-08-03 stsp free(ondisk_path);
9110 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9111 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9112 ad493afc 2019-08-03 stsp if (blob_base)
9113 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9114 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9115 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9116 ad493afc 2019-08-03 stsp if (blob_staged)
9117 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9118 f69721c3 2019-10-21 stsp free(id_str);
9119 f69721c3 2019-10-21 stsp free(label_orig);
9120 ad493afc 2019-08-03 stsp return err;
9121 ad493afc 2019-08-03 stsp }
9122 ad493afc 2019-08-03 stsp
9123 ad493afc 2019-08-03 stsp const struct got_error *
9124 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9125 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9126 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9127 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9128 ad493afc 2019-08-03 stsp struct got_repository *repo)
9129 ad493afc 2019-08-03 stsp {
9130 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9131 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9132 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9133 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9134 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9135 ad493afc 2019-08-03 stsp
9136 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9137 ad493afc 2019-08-03 stsp if (err)
9138 ad493afc 2019-08-03 stsp return err;
9139 ad493afc 2019-08-03 stsp
9140 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9141 ad493afc 2019-08-03 stsp if (err)
9142 ad493afc 2019-08-03 stsp goto done;
9143 ad493afc 2019-08-03 stsp
9144 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9145 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9146 ad493afc 2019-08-03 stsp upa.repo = repo;
9147 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9148 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9149 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9150 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9151 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9152 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9153 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9154 ad493afc 2019-08-03 stsp if (err)
9155 ad493afc 2019-08-03 stsp goto done;
9156 ad493afc 2019-08-03 stsp }
9157 ad493afc 2019-08-03 stsp
9158 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9159 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9160 ad493afc 2019-08-03 stsp err = sync_err;
9161 ad493afc 2019-08-03 stsp done:
9162 ad493afc 2019-08-03 stsp free(fileindex_path);
9163 ad493afc 2019-08-03 stsp if (fileindex)
9164 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9165 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9166 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9167 ad493afc 2019-08-03 stsp err = unlockerr;
9168 ad493afc 2019-08-03 stsp return err;
9169 ad493afc 2019-08-03 stsp }
9170 b2118c49 2020-07-28 stsp
9171 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9172 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9173 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9174 b2118c49 2020-07-28 stsp void *info_arg;
9175 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9176 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9177 b2118c49 2020-07-28 stsp void *cancel_arg;
9178 b2118c49 2020-07-28 stsp };
9179 b2118c49 2020-07-28 stsp
9180 b2118c49 2020-07-28 stsp static const struct got_error *
9181 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9182 b2118c49 2020-07-28 stsp {
9183 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9184 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9185 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9186 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9187 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9188 b2118c49 2020-07-28 stsp int stage;
9189 b2118c49 2020-07-28 stsp
9190 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9191 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9192 b2118c49 2020-07-28 stsp
9193 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9194 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9195 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9196 b2118c49 2020-07-28 stsp break;
9197 b2118c49 2020-07-28 stsp }
9198 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9199 b2118c49 2020-07-28 stsp return NULL;
9200 b2118c49 2020-07-28 stsp
9201 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9202 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9203 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9204 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9205 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9206 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9207 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9208 b2118c49 2020-07-28 stsp }
9209 b2118c49 2020-07-28 stsp
9210 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9211 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9212 b2118c49 2020-07-28 stsp
9213 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9214 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9215 b2118c49 2020-07-28 stsp }
9216 b2118c49 2020-07-28 stsp
9217 b2118c49 2020-07-28 stsp const struct got_error *
9218 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9219 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9220 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9221 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9222 b2118c49 2020-07-28 stsp
9223 b2118c49 2020-07-28 stsp {
9224 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9225 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9226 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9227 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9228 b2118c49 2020-07-28 stsp
9229 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9230 b2118c49 2020-07-28 stsp if (err)
9231 b2118c49 2020-07-28 stsp return err;
9232 b2118c49 2020-07-28 stsp
9233 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9234 b2118c49 2020-07-28 stsp if (err)
9235 b2118c49 2020-07-28 stsp goto done;
9236 b2118c49 2020-07-28 stsp
9237 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9238 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9239 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9240 b2118c49 2020-07-28 stsp arg.paths = paths;
9241 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9242 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9243 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9244 b2118c49 2020-07-28 stsp &arg);
9245 b2118c49 2020-07-28 stsp done:
9246 b2118c49 2020-07-28 stsp free(fileindex_path);
9247 b2118c49 2020-07-28 stsp if (fileindex)
9248 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9249 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9250 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9251 b2118c49 2020-07-28 stsp err = unlockerr;
9252 b2118c49 2020-07-28 stsp return err;
9253 b2118c49 2020-07-28 stsp }
9254 78f5ac24 2022-03-19 op
9255 78f5ac24 2022-03-19 op static const struct got_error *
9256 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9257 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9258 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9259 78f5ac24 2022-03-19 op {
9260 78f5ac24 2022-03-19 op const struct got_error *err;
9261 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9262 78f5ac24 2022-03-19 op struct stat sb;
9263 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9264 78f5ac24 2022-03-19 op
9265 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9266 78f5ac24 2022-03-19 op if (err)
9267 78f5ac24 2022-03-19 op return err;
9268 78f5ac24 2022-03-19 op
9269 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9270 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9271 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9272 78f5ac24 2022-03-19 op
9273 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9274 78f5ac24 2022-03-19 op if (ie) {
9275 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9276 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9277 a05fb460 2022-04-23 op repo);
9278 78f5ac24 2022-03-19 op if (err)
9279 78f5ac24 2022-03-19 op goto done;
9280 78f5ac24 2022-03-19 op } else {
9281 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9282 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9283 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9284 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9285 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9286 78f5ac24 2022-03-19 op ondisk_path);
9287 78f5ac24 2022-03-19 op goto done;
9288 78f5ac24 2022-03-19 op }
9289 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9290 78f5ac24 2022-03-19 op }
9291 78f5ac24 2022-03-19 op }
9292 78f5ac24 2022-03-19 op
9293 78f5ac24 2022-03-19 op done:
9294 78f5ac24 2022-03-19 op free(ondisk_path);
9295 78f5ac24 2022-03-19 op return err;
9296 78f5ac24 2022-03-19 op }
9297 78f5ac24 2022-03-19 op
9298 78f5ac24 2022-03-19 op static const struct got_error *
9299 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9300 78f5ac24 2022-03-19 op unsigned char staged_status)
9301 78f5ac24 2022-03-19 op {
9302 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9303 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9304 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9305 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9306 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9307 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9308 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9309 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9310 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9311 78f5ac24 2022-03-19 op return NULL;
9312 78f5ac24 2022-03-19 op }
9313 78f5ac24 2022-03-19 op
9314 78f5ac24 2022-03-19 op static const struct got_error *
9315 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9316 78f5ac24 2022-03-19 op {
9317 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9318 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9319 78f5ac24 2022-03-19 op return NULL;
9320 78f5ac24 2022-03-19 op }
9321 78f5ac24 2022-03-19 op
9322 78f5ac24 2022-03-19 op static const struct got_error *
9323 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9324 78f5ac24 2022-03-19 op unsigned char staged_status)
9325 78f5ac24 2022-03-19 op {
9326 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9327 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9328 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9329 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9330 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9331 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9332 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9333 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9334 78f5ac24 2022-03-19 op return NULL;
9335 78f5ac24 2022-03-19 op }
9336 78f5ac24 2022-03-19 op
9337 78f5ac24 2022-03-19 op const struct got_error *
9338 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9339 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9340 78f5ac24 2022-03-19 op {
9341 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9342 78f5ac24 2022-03-19 op }
9343 78f5ac24 2022-03-19 op
9344 78f5ac24 2022-03-19 op const struct got_error *
9345 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9346 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9347 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9348 78f5ac24 2022-03-19 op {
9349 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9350 78f5ac24 2022-03-19 op int file_renamed = 0;
9351 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9352 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9353 78f5ac24 2022-03-19 op
9354 78f5ac24 2022-03-19 op *oldpath = NULL;
9355 78f5ac24 2022-03-19 op *newpath = NULL;
9356 78f5ac24 2022-03-19 op
9357 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9358 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9359 78f5ac24 2022-03-19 op if (err)
9360 78f5ac24 2022-03-19 op goto done;
9361 78f5ac24 2022-03-19 op
9362 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9363 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9364 78f5ac24 2022-03-19 op if (err)
9365 78f5ac24 2022-03-19 op goto done;
9366 78f5ac24 2022-03-19 op
9367 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9368 78f5ac24 2022-03-19 op file_renamed = 1;
9369 78f5ac24 2022-03-19 op
9370 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9371 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9372 78f5ac24 2022-03-19 op else if (file_renamed) {
9373 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9374 78f5ac24 2022-03-19 op if (err == NULL)
9375 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9376 78f5ac24 2022-03-19 op } else if (old == NULL)
9377 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9378 78f5ac24 2022-03-19 op else
9379 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9380 78f5ac24 2022-03-19 op
9381 78f5ac24 2022-03-19 op done:
9382 78f5ac24 2022-03-19 op if (err) {
9383 78f5ac24 2022-03-19 op free(*oldpath);
9384 78f5ac24 2022-03-19 op *oldpath = NULL;
9385 78f5ac24 2022-03-19 op free(*newpath);
9386 78f5ac24 2022-03-19 op *newpath = NULL;
9387 78f5ac24 2022-03-19 op }
9388 78f5ac24 2022-03-19 op return err;
9389 78f5ac24 2022-03-19 op }
9390 78f5ac24 2022-03-19 op
9391 78f5ac24 2022-03-19 op const struct got_error *
9392 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9393 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9394 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9395 78f5ac24 2022-03-19 op {
9396 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9397 f2dd7807 2022-05-17 op
9398 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9399 f2dd7807 2022-05-17 op saa.worktree = worktree;
9400 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9401 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9402 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9403 f2dd7807 2022-05-17 op saa.repo = repo;
9404 f2dd7807 2022-05-17 op
9405 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9406 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9407 78f5ac24 2022-03-19 op }
9408 f2dd7807 2022-05-17 op
9409 f2dd7807 2022-05-17 op const struct got_error *
9410 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9411 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9412 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9413 f2dd7807 2022-05-17 op {
9414 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9415 f2dd7807 2022-05-17 op
9416 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9417 f2dd7807 2022-05-17 op sda.worktree = worktree;
9418 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9419 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9420 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9421 f2dd7807 2022-05-17 op sda.repo = repo;
9422 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9423 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9424 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9425 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9426 f2dd7807 2022-05-17 op
9427 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9428 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9429 f2dd7807 2022-05-17 op }
9430 f2dd7807 2022-05-17 op
9431 f2dd7807 2022-05-17 op const struct got_error *
9432 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9433 4bcdc895 2022-05-17 op const char *fileindex_path)
9434 f2dd7807 2022-05-17 op {
9435 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9436 f2dd7807 2022-05-17 op
9437 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9438 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9439 f2dd7807 2022-05-17 op
9440 f2dd7807 2022-05-17 op return err;
9441 f2dd7807 2022-05-17 op }