Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 5822e79e 2023-02-23 op #include <sha2.h>
33 9d31a1d8 2018-03-11 stsp #include <zlib.h>
34 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
35 512f0d0e 2019-01-02 stsp #include <libgen.h>
36 ec22038e 2019-03-10 stsp #include <uuid.h>
37 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp
39 86c3caaf 2018-03-09 stsp #include "got_error.h"
40 86c3caaf 2018-03-09 stsp #include "got_repository.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 9d31a1d8 2018-03-11 stsp #include "got_object.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 234035bc 2019-06-01 stsp #include "got_diff.h"
48 86c3caaf 2018-03-09 stsp
49 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
50 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
54 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
55 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
56 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
57 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
58 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
59 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
60 9d31a1d8 2018-03-11 stsp
61 9d31a1d8 2018-03-11 stsp #ifndef MIN
62 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 9d31a1d8 2018-03-11 stsp #endif
64 f69721c3 2019-10-21 stsp
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
66 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 b2b3fce1 2022-10-29 op
68 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
69 86c3caaf 2018-03-09 stsp
70 99724ed4 2018-03-10 stsp static const struct got_error *
71 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
72 99724ed4 2018-03-10 stsp {
73 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
74 99724ed4 2018-03-10 stsp char *path;
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
78 99724ed4 2018-03-10 stsp
79 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
80 99724ed4 2018-03-10 stsp free(path);
81 507dc3bb 2018-12-29 stsp return err;
82 507dc3bb 2018-12-29 stsp }
83 507dc3bb 2018-12-29 stsp
84 507dc3bb 2018-12-29 stsp static const struct got_error *
85 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
86 507dc3bb 2018-12-29 stsp {
87 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
88 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
89 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
90 507dc3bb 2018-12-29 stsp char *path = NULL;
91 507dc3bb 2018-12-29 stsp
92 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
94 507dc3bb 2018-12-29 stsp path = NULL;
95 507dc3bb 2018-12-29 stsp goto done;
96 507dc3bb 2018-12-29 stsp }
97 507dc3bb 2018-12-29 stsp
98 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 507dc3bb 2018-12-29 stsp if (err)
100 507dc3bb 2018-12-29 stsp goto done;
101 507dc3bb 2018-12-29 stsp
102 507dc3bb 2018-12-29 stsp if (content) {
103 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
104 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
105 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
106 507dc3bb 2018-12-29 stsp goto done;
107 507dc3bb 2018-12-29 stsp }
108 507dc3bb 2018-12-29 stsp }
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
111 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
112 2a57020b 2019-02-20 stsp unlink(tmppath);
113 507dc3bb 2018-12-29 stsp goto done;
114 507dc3bb 2018-12-29 stsp }
115 507dc3bb 2018-12-29 stsp
116 507dc3bb 2018-12-29 stsp done:
117 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
118 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
119 230a42bd 2019-05-11 jcs free(tmppath);
120 09fe317a 2018-03-11 stsp return err;
121 09fe317a 2018-03-11 stsp }
122 09fe317a 2018-03-11 stsp
123 024e9686 2019-05-14 stsp static const struct got_error *
124 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
125 024e9686 2019-05-14 stsp {
126 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
127 024e9686 2019-05-14 stsp char *refstr = NULL;
128 024e9686 2019-05-14 stsp
129 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
130 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
131 024e9686 2019-05-14 stsp if (refstr == NULL)
132 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
133 024e9686 2019-05-14 stsp } else {
134 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
135 024e9686 2019-05-14 stsp if (refstr == NULL)
136 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
137 024e9686 2019-05-14 stsp }
138 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 024e9686 2019-05-14 stsp free(refstr);
140 024e9686 2019-05-14 stsp return err;
141 024e9686 2019-05-14 stsp }
142 024e9686 2019-05-14 stsp
143 86c3caaf 2018-03-09 stsp const struct got_error *
144 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
145 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
146 86c3caaf 2018-03-09 stsp {
147 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
148 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
149 ec22038e 2019-03-10 stsp uuid_t uuid;
150 ec22038e 2019-03-10 stsp uint32_t uuid_status;
151 65596e15 2018-12-24 stsp int obj_type;
152 7ac97322 2018-03-11 stsp char *path_got = NULL;
153 1451e70d 2018-03-10 stsp char *formatstr = NULL;
154 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
155 65596e15 2018-12-24 stsp char *basestr = NULL;
156 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
157 65596e15 2018-12-24 stsp
158 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
160 0c48fee2 2019-03-11 stsp goto done;
161 0c48fee2 2019-03-11 stsp }
162 0c48fee2 2019-03-11 stsp
163 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
164 65596e15 2018-12-24 stsp if (err)
165 65596e15 2018-12-24 stsp return err;
166 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
167 65596e15 2018-12-24 stsp if (err)
168 65596e15 2018-12-24 stsp return err;
169 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
171 86c3caaf 2018-03-09 stsp
172 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
173 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
174 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
175 0bb8a95e 2018-03-12 stsp }
176 577ec78f 2018-03-11 stsp
177 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
178 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
180 86c3caaf 2018-03-09 stsp goto done;
181 86c3caaf 2018-03-09 stsp }
182 86c3caaf 2018-03-09 stsp
183 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
184 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
186 86c3caaf 2018-03-09 stsp goto done;
187 86c3caaf 2018-03-09 stsp }
188 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
190 86c3caaf 2018-03-09 stsp goto done;
191 86c3caaf 2018-03-09 stsp }
192 86c3caaf 2018-03-09 stsp
193 056e7441 2018-03-11 stsp /* Create an empty lock file. */
194 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 056e7441 2018-03-11 stsp if (err)
196 056e7441 2018-03-11 stsp goto done;
197 056e7441 2018-03-11 stsp
198 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
199 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 99724ed4 2018-03-10 stsp if (err)
201 86c3caaf 2018-03-09 stsp goto done;
202 86c3caaf 2018-03-09 stsp
203 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
204 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
205 65596e15 2018-12-24 stsp if (err)
206 65596e15 2018-12-24 stsp goto done;
207 65596e15 2018-12-24 stsp
208 65596e15 2018-12-24 stsp /* Record our base commit. */
209 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
210 65596e15 2018-12-24 stsp if (err)
211 65596e15 2018-12-24 stsp goto done;
212 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 99724ed4 2018-03-10 stsp if (err)
214 86c3caaf 2018-03-09 stsp goto done;
215 86c3caaf 2018-03-09 stsp
216 1451e70d 2018-03-10 stsp /* Store path to repository. */
217 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
219 99724ed4 2018-03-10 stsp if (err)
220 86c3caaf 2018-03-09 stsp goto done;
221 86c3caaf 2018-03-09 stsp
222 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
223 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
225 ec22038e 2019-03-10 stsp if (err)
226 ec22038e 2019-03-10 stsp goto done;
227 ec22038e 2019-03-10 stsp
228 ec22038e 2019-03-10 stsp /* Generate UUID. */
229 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
230 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
231 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
232 ec22038e 2019-03-10 stsp goto done;
233 ec22038e 2019-03-10 stsp }
234 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
236 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
237 ec22038e 2019-03-10 stsp goto done;
238 ec22038e 2019-03-10 stsp }
239 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 577ec78f 2018-03-11 stsp if (err)
241 577ec78f 2018-03-11 stsp goto done;
242 577ec78f 2018-03-11 stsp
243 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
244 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 1451e70d 2018-03-10 stsp goto done;
247 1451e70d 2018-03-10 stsp }
248 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
249 99724ed4 2018-03-10 stsp if (err)
250 1451e70d 2018-03-10 stsp goto done;
251 1451e70d 2018-03-10 stsp
252 86c3caaf 2018-03-09 stsp done:
253 65596e15 2018-12-24 stsp free(commit_id);
254 7ac97322 2018-03-11 stsp free(path_got);
255 1451e70d 2018-03-10 stsp free(formatstr);
256 0bb8a95e 2018-03-12 stsp free(absprefix);
257 65596e15 2018-12-24 stsp free(basestr);
258 ec22038e 2019-03-10 stsp free(uuidstr);
259 86c3caaf 2018-03-09 stsp return err;
260 86c3caaf 2018-03-09 stsp }
261 86c3caaf 2018-03-09 stsp
262 247140b2 2019-02-05 stsp const struct got_error *
263 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 e5dc7198 2018-12-29 stsp const char *path_prefix)
265 e5dc7198 2018-12-29 stsp {
266 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
267 e5dc7198 2018-12-29 stsp
268 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
269 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
271 e5dc7198 2018-12-29 stsp }
272 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
273 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
274 e5dc7198 2018-12-29 stsp free(absprefix);
275 e5dc7198 2018-12-29 stsp return NULL;
276 86c3caaf 2018-03-09 stsp }
277 86c3caaf 2018-03-09 stsp
278 bc70eb79 2019-05-09 stsp const char *
279 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
280 86c3caaf 2018-03-09 stsp {
281 36a38700 2019-05-10 stsp return worktree->head_ref_name;
282 024e9686 2019-05-14 stsp }
283 024e9686 2019-05-14 stsp
284 024e9686 2019-05-14 stsp const struct got_error *
285 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
286 024e9686 2019-05-14 stsp struct got_reference *head_ref)
287 024e9686 2019-05-14 stsp {
288 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
289 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
290 024e9686 2019-05-14 stsp
291 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
293 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
294 024e9686 2019-05-14 stsp path_got = NULL;
295 024e9686 2019-05-14 stsp goto done;
296 024e9686 2019-05-14 stsp }
297 024e9686 2019-05-14 stsp
298 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
299 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
300 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
301 024e9686 2019-05-14 stsp goto done;
302 024e9686 2019-05-14 stsp }
303 024e9686 2019-05-14 stsp
304 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
305 024e9686 2019-05-14 stsp if (err)
306 024e9686 2019-05-14 stsp goto done;
307 024e9686 2019-05-14 stsp
308 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
309 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
310 024e9686 2019-05-14 stsp done:
311 024e9686 2019-05-14 stsp free(path_got);
312 024e9686 2019-05-14 stsp if (err)
313 024e9686 2019-05-14 stsp free(head_ref_name);
314 024e9686 2019-05-14 stsp return err;
315 507dc3bb 2018-12-29 stsp }
316 507dc3bb 2018-12-29 stsp
317 b72f483a 2019-02-05 stsp struct got_object_id *
318 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
319 507dc3bb 2018-12-29 stsp {
320 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 507dc3bb 2018-12-29 stsp const struct got_error *
324 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
326 507dc3bb 2018-12-29 stsp {
327 507dc3bb 2018-12-29 stsp const struct got_error *err;
328 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
329 507dc3bb 2018-12-29 stsp char *id_str = NULL;
330 507dc3bb 2018-12-29 stsp char *path_got = NULL;
331 507dc3bb 2018-12-29 stsp
332 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 507dc3bb 2018-12-29 stsp path_got = NULL;
336 507dc3bb 2018-12-29 stsp goto done;
337 507dc3bb 2018-12-29 stsp }
338 507dc3bb 2018-12-29 stsp
339 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
340 507dc3bb 2018-12-29 stsp if (err)
341 507dc3bb 2018-12-29 stsp return err;
342 507dc3bb 2018-12-29 stsp
343 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
345 507dc3bb 2018-12-29 stsp goto done;
346 507dc3bb 2018-12-29 stsp }
347 507dc3bb 2018-12-29 stsp
348 507dc3bb 2018-12-29 stsp /* Record our base commit. */
349 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
350 507dc3bb 2018-12-29 stsp if (err)
351 507dc3bb 2018-12-29 stsp goto done;
352 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 507dc3bb 2018-12-29 stsp if (err)
354 507dc3bb 2018-12-29 stsp goto done;
355 507dc3bb 2018-12-29 stsp
356 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
357 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
358 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
359 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
360 507dc3bb 2018-12-29 stsp goto done;
361 507dc3bb 2018-12-29 stsp }
362 507dc3bb 2018-12-29 stsp done:
363 507dc3bb 2018-12-29 stsp if (obj)
364 507dc3bb 2018-12-29 stsp got_object_close(obj);
365 507dc3bb 2018-12-29 stsp free(id_str);
366 507dc3bb 2018-12-29 stsp free(path_got);
367 507dc3bb 2018-12-29 stsp return err;
368 50b0790e 2020-09-11 stsp }
369 50b0790e 2020-09-11 stsp
370 50b0790e 2020-09-11 stsp const struct got_gotconfig *
371 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
372 50b0790e 2020-09-11 stsp {
373 50b0790e 2020-09-11 stsp return worktree->gotconfig;
374 507dc3bb 2018-12-29 stsp }
375 507dc3bb 2018-12-29 stsp
376 9d31a1d8 2018-03-11 stsp static const struct got_error *
377 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
378 86c3caaf 2018-03-09 stsp {
379 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
382 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
383 86c3caaf 2018-03-09 stsp return NULL;
384 21908da4 2019-01-13 stsp }
385 21908da4 2019-01-13 stsp
386 21908da4 2019-01-13 stsp static const struct got_error *
387 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
388 4a1ddfc2 2019-01-12 stsp {
389 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
390 4a1ddfc2 2019-01-12 stsp char *abspath;
391 4a1ddfc2 2019-01-12 stsp
392 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
394 4a1ddfc2 2019-01-12 stsp
395 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
396 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 ddcd8544 2019-03-11 stsp struct stat sb;
398 ddcd8544 2019-03-11 stsp err = NULL;
399 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
400 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
401 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
402 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
403 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
404 ddcd8544 2019-03-11 stsp }
405 ddcd8544 2019-03-11 stsp }
406 4a1ddfc2 2019-01-12 stsp free(abspath);
407 68c76935 2019-02-19 stsp return err;
408 68c76935 2019-02-19 stsp }
409 68c76935 2019-02-19 stsp
410 68c76935 2019-02-19 stsp static const struct got_error *
411 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
412 68c76935 2019-02-19 stsp {
413 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
414 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
415 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
416 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
417 68c76935 2019-02-19 stsp
418 68c76935 2019-02-19 stsp *same = 1;
419 68c76935 2019-02-19 stsp
420 230a42bd 2019-05-11 jcs for (;;) {
421 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
423 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
424 68c76935 2019-02-19 stsp break;
425 68c76935 2019-02-19 stsp }
426 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
429 68c76935 2019-02-19 stsp break;
430 68c76935 2019-02-19 stsp }
431 68c76935 2019-02-19 stsp if (flen1 == 0) {
432 68c76935 2019-02-19 stsp if (flen2 != 0)
433 68c76935 2019-02-19 stsp *same = 0;
434 68c76935 2019-02-19 stsp break;
435 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
436 68c76935 2019-02-19 stsp if (flen1 != 0)
437 68c76935 2019-02-19 stsp *same = 0;
438 68c76935 2019-02-19 stsp break;
439 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
440 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
441 68c76935 2019-02-19 stsp *same = 0;
442 68c76935 2019-02-19 stsp break;
443 68c76935 2019-02-19 stsp }
444 68c76935 2019-02-19 stsp } else {
445 68c76935 2019-02-19 stsp *same = 0;
446 68c76935 2019-02-19 stsp break;
447 68c76935 2019-02-19 stsp }
448 68c76935 2019-02-19 stsp }
449 68c76935 2019-02-19 stsp
450 68c76935 2019-02-19 stsp return err;
451 68c76935 2019-02-19 stsp }
452 68c76935 2019-02-19 stsp
453 68c76935 2019-02-19 stsp static const struct got_error *
454 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
455 68c76935 2019-02-19 stsp {
456 68c76935 2019-02-19 stsp struct stat sb;
457 68c76935 2019-02-19 stsp size_t size1, size2;
458 68c76935 2019-02-19 stsp
459 68c76935 2019-02-19 stsp *same = 1;
460 68c76935 2019-02-19 stsp
461 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
462 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
463 68c76935 2019-02-19 stsp size1 = sb.st_size;
464 68c76935 2019-02-19 stsp
465 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
466 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
467 68c76935 2019-02-19 stsp size2 = sb.st_size;
468 68c76935 2019-02-19 stsp
469 68c76935 2019-02-19 stsp if (size1 != size2) {
470 68c76935 2019-02-19 stsp *same = 0;
471 68c76935 2019-02-19 stsp return NULL;
472 68c76935 2019-02-19 stsp }
473 68c76935 2019-02-19 stsp
474 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
475 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
476 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
477 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
478 68c76935 2019-02-19 stsp
479 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
480 0e039681 2021-11-15 stsp }
481 0e039681 2021-11-15 stsp
482 0e039681 2021-11-15 stsp static const struct got_error *
483 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
484 0e039681 2021-11-15 stsp {
485 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
486 0e039681 2021-11-15 stsp size_t flen;
487 0e039681 2021-11-15 stsp ssize_t outlen;
488 0e039681 2021-11-15 stsp
489 0e039681 2021-11-15 stsp *outsize = 0;
490 0e039681 2021-11-15 stsp
491 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
492 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
493 0e039681 2021-11-15 stsp
494 0e039681 2021-11-15 stsp for (;;) {
495 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
496 0e039681 2021-11-15 stsp if (flen == 0) {
497 0e039681 2021-11-15 stsp if (ferror(f))
498 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
499 0e039681 2021-11-15 stsp if (feof(f))
500 0e039681 2021-11-15 stsp break;
501 0e039681 2021-11-15 stsp }
502 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
503 0e039681 2021-11-15 stsp if (outlen == -1)
504 0e039681 2021-11-15 stsp return got_error_from_errno("write");
505 0e039681 2021-11-15 stsp if (outlen != flen)
506 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
507 0e039681 2021-11-15 stsp *outsize += outlen;
508 0e039681 2021-11-15 stsp }
509 0e039681 2021-11-15 stsp
510 0e039681 2021-11-15 stsp return NULL;
511 0e039681 2021-11-15 stsp }
512 0e039681 2021-11-15 stsp
513 0e039681 2021-11-15 stsp static const struct got_error *
514 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
515 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 0e039681 2021-11-15 stsp const char *ondisk_path)
518 0e039681 2021-11-15 stsp {
519 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
520 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
521 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
525 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
526 0e039681 2021-11-15 stsp
527 0e039681 2021-11-15 stsp *overlapcnt = 0;
528 0e039681 2021-11-15 stsp
529 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 0e039681 2021-11-15 stsp if (err)
531 0e039681 2021-11-15 stsp return err;
532 0e039681 2021-11-15 stsp
533 0e039681 2021-11-15 stsp if (same_content)
534 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
535 0e039681 2021-11-15 stsp
536 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
537 0e039681 2021-11-15 stsp if (err)
538 0e039681 2021-11-15 stsp return err;
539 0e039681 2021-11-15 stsp changed_deriv = !same_content;
540 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
541 0e039681 2021-11-15 stsp if (err)
542 0e039681 2021-11-15 stsp return err;
543 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
544 0e039681 2021-11-15 stsp
545 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
546 0e039681 2021-11-15 stsp *overlapcnt = 1;
547 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
549 0e039681 2021-11-15 stsp goto done;
550 0e039681 2021-11-15 stsp }
551 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
553 0e039681 2021-11-15 stsp goto done;
554 0e039681 2021-11-15 stsp }
555 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
557 0e039681 2021-11-15 stsp goto done;
558 0e039681 2021-11-15 stsp }
559 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 b90054ed 2022-11-01 stsp base_path_orig, "");
561 0e039681 2021-11-15 stsp if (err)
562 0e039681 2021-11-15 stsp goto done;
563 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 b90054ed 2022-11-01 stsp base_path_deriv, "");
565 0e039681 2021-11-15 stsp if (err)
566 0e039681 2021-11-15 stsp goto done;
567 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 b90054ed 2022-11-01 stsp base_path_deriv2, "");
569 0e039681 2021-11-15 stsp if (err)
570 0e039681 2021-11-15 stsp goto done;
571 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
572 0e039681 2021-11-15 stsp if (err)
573 0e039681 2021-11-15 stsp goto done;
574 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
575 0e039681 2021-11-15 stsp if (err)
576 0e039681 2021-11-15 stsp goto done;
577 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 0e039681 2021-11-15 stsp if (err)
579 0e039681 2021-11-15 stsp goto done;
580 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
581 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
582 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
583 0e039681 2021-11-15 stsp goto done;
584 0e039681 2021-11-15 stsp }
585 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 0e039681 2021-11-15 stsp label_deriv ? " " : "",
588 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
589 0e039681 2021-11-15 stsp path_deriv) < 0) {
590 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
591 0e039681 2021-11-15 stsp goto done;
592 0e039681 2021-11-15 stsp }
593 0e039681 2021-11-15 stsp if (size_orig > 0) {
594 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
596 0e039681 2021-11-15 stsp label_orig ? " " : "",
597 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
598 0e039681 2021-11-15 stsp path_orig) < 0) {
599 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
600 0e039681 2021-11-15 stsp goto done;
601 0e039681 2021-11-15 stsp }
602 0e039681 2021-11-15 stsp }
603 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
605 0e039681 2021-11-15 stsp path_deriv2,
606 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
607 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
608 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
609 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
610 0e039681 2021-11-15 stsp goto done;
611 0e039681 2021-11-15 stsp }
612 0e039681 2021-11-15 stsp } else if (changed_deriv)
613 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 0e039681 2021-11-15 stsp else if (changed_deriv2)
615 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 0e039681 2021-11-15 stsp done:
617 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 0e039681 2021-11-15 stsp err == NULL)
619 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
620 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
622 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
624 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
626 0e039681 2021-11-15 stsp free(path_orig);
627 0e039681 2021-11-15 stsp free(path_deriv);
628 0e039681 2021-11-15 stsp free(path_deriv2);
629 0e039681 2021-11-15 stsp free(base_path_orig);
630 0e039681 2021-11-15 stsp free(base_path_deriv);
631 0e039681 2021-11-15 stsp free(base_path_deriv2);
632 0e039681 2021-11-15 stsp return err;
633 6353ad76 2019-02-08 stsp }
634 6353ad76 2019-02-08 stsp
635 6353ad76 2019-02-08 stsp /*
636 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
637 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
638 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
639 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
640 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
641 6353ad76 2019-02-08 stsp */
642 6353ad76 2019-02-08 stsp static const struct got_error *
643 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
646 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
649 6353ad76 2019-02-08 stsp {
650 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
651 6353ad76 2019-02-08 stsp int merged_fd = -1;
652 db590691 2021-06-02 stsp FILE *f_merged = NULL;
653 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
654 234035bc 2019-06-01 stsp int overlapcnt = 0;
655 3524bbf9 2020-10-20 stsp char *parent = NULL;
656 6353ad76 2019-02-08 stsp
657 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
658 234035bc 2019-06-01 stsp
659 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
660 3524bbf9 2020-10-20 stsp if (err)
661 3524bbf9 2020-10-20 stsp return err;
662 af54ae4a 2019-02-19 stsp
663 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
665 3524bbf9 2020-10-20 stsp goto done;
666 3524bbf9 2020-10-20 stsp }
667 af54ae4a 2019-02-19 stsp
668 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 6353ad76 2019-02-08 stsp if (err)
670 6353ad76 2019-02-08 stsp goto done;
671 3b9f0f87 2020-07-23 stsp
672 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 0e039681 2021-11-15 stsp if (err) {
675 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
676 0e039681 2021-11-15 stsp goto done;
677 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 0e039681 2021-11-15 stsp ondisk_path);
680 0e039681 2021-11-15 stsp if (err)
681 0e039681 2021-11-15 stsp goto done;
682 0e039681 2021-11-15 stsp }
683 6353ad76 2019-02-08 stsp
684 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
685 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 1ee397ad 2019-07-12 stsp if (err)
687 1ee397ad 2019-07-12 stsp goto done;
688 6353ad76 2019-02-08 stsp
689 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
691 816dc654 2019-02-16 stsp goto done;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp
694 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
695 db590691 2021-06-02 stsp if (f_merged == NULL) {
696 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
697 db590691 2021-06-02 stsp goto done;
698 db590691 2021-06-02 stsp }
699 db590691 2021-06-02 stsp merged_fd = -1;
700 db590691 2021-06-02 stsp
701 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
702 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
703 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
704 db590691 2021-06-02 stsp f_merged);
705 68c76935 2019-02-19 stsp if (err)
706 68c76935 2019-02-19 stsp goto done;
707 816dc654 2019-02-16 stsp }
708 6353ad76 2019-02-08 stsp
709 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
711 70a0c8ec 2019-02-20 stsp goto done;
712 70a0c8ec 2019-02-20 stsp }
713 70a0c8ec 2019-02-20 stsp
714 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
715 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
716 230a42bd 2019-05-11 jcs ondisk_path);
717 6353ad76 2019-02-08 stsp goto done;
718 6353ad76 2019-02-08 stsp }
719 6353ad76 2019-02-08 stsp done:
720 fdcb7daf 2019-12-15 stsp if (err) {
721 fdcb7daf 2019-12-15 stsp if (merged_path)
722 fdcb7daf 2019-12-15 stsp unlink(merged_path);
723 3b9f0f87 2020-07-23 stsp }
724 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
726 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
728 6353ad76 2019-02-08 stsp free(merged_path);
729 af54ae4a 2019-02-19 stsp free(base_path);
730 3524bbf9 2020-10-20 stsp free(parent);
731 af57b12a 2020-07-23 stsp return err;
732 af57b12a 2020-07-23 stsp }
733 af57b12a 2020-07-23 stsp
734 af57b12a 2020-07-23 stsp static const struct got_error *
735 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
736 af57b12a 2020-07-23 stsp size_t target_len)
737 af57b12a 2020-07-23 stsp {
738 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
739 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
740 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
741 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
742 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
743 af57b12a 2020-07-23 stsp ondisk_path);
744 af57b12a 2020-07-23 stsp return NULL;
745 af57b12a 2020-07-23 stsp }
746 af57b12a 2020-07-23 stsp
747 af57b12a 2020-07-23 stsp /*
748 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
750 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
751 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
752 993e2a1b 2020-07-23 stsp *
753 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
754 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
755 993e2a1b 2020-07-23 stsp *
756 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
757 993e2a1b 2020-07-23 stsp * has deleted this symlink.
758 11cc08c1 2020-07-23 stsp */
759 11cc08c1 2020-07-23 stsp static const struct got_error *
760 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
761 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
763 11cc08c1 2020-07-23 stsp {
764 11cc08c1 2020-07-23 stsp const struct got_error *err;
765 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 11cc08c1 2020-07-23 stsp FILE *f = NULL;
767 11cc08c1 2020-07-23 stsp
768 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
769 11cc08c1 2020-07-23 stsp if (err)
770 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
771 11cc08c1 2020-07-23 stsp
772 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
773 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
775 11cc08c1 2020-07-23 stsp goto done;
776 11cc08c1 2020-07-23 stsp }
777 11cc08c1 2020-07-23 stsp
778 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 11cc08c1 2020-07-23 stsp if (err)
780 3818e3c4 2020-11-01 naddy goto done;
781 3818e3c4 2020-11-01 naddy
782 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
784 11cc08c1 2020-07-23 stsp goto done;
785 3818e3c4 2020-11-01 naddy }
786 11cc08c1 2020-07-23 stsp
787 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
790 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
791 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
792 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
793 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
794 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
795 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
797 11cc08c1 2020-07-23 stsp goto done;
798 11cc08c1 2020-07-23 stsp }
799 11cc08c1 2020-07-23 stsp
800 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
801 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
802 11cc08c1 2020-07-23 stsp goto done;
803 11cc08c1 2020-07-23 stsp }
804 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
805 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
806 11cc08c1 2020-07-23 stsp goto done;
807 11cc08c1 2020-07-23 stsp }
808 11cc08c1 2020-07-23 stsp done:
809 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
810 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
811 11cc08c1 2020-07-23 stsp free(path);
812 11cc08c1 2020-07-23 stsp free(id_str);
813 11cc08c1 2020-07-23 stsp free(label_deriv);
814 11cc08c1 2020-07-23 stsp return err;
815 11cc08c1 2020-07-23 stsp }
816 d219f183 2020-07-23 stsp
817 d219f183 2020-07-23 stsp /* forward declaration */
818 d219f183 2020-07-23 stsp static const struct got_error *
819 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
821 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
822 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
823 11cc08c1 2020-07-23 stsp
824 11cc08c1 2020-07-23 stsp /*
825 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
826 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
827 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
828 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
829 af57b12a 2020-07-23 stsp */
830 af57b12a 2020-07-23 stsp static const struct got_error *
831 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
832 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
833 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
834 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
836 af57b12a 2020-07-23 stsp {
837 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
838 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
839 af57b12a 2020-07-23 stsp struct stat sb;
840 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
841 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
842 11cc08c1 2020-07-23 stsp int have_local_change = 0;
843 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
844 af57b12a 2020-07-23 stsp
845 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
846 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
847 af57b12a 2020-07-23 stsp
848 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
849 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
850 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
851 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
852 af57b12a 2020-07-23 stsp ondisk_path);
853 af57b12a 2020-07-23 stsp goto done;
854 af57b12a 2020-07-23 stsp }
855 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
856 af57b12a 2020-07-23 stsp
857 526a746f 2020-07-23 stsp if (blob_orig) {
858 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
859 526a746f 2020-07-23 stsp if (err)
860 526a746f 2020-07-23 stsp goto done;
861 526a746f 2020-07-23 stsp }
862 af57b12a 2020-07-23 stsp
863 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
864 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
865 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 11cc08c1 2020-07-23 stsp have_local_change = 1;
867 11cc08c1 2020-07-23 stsp
868 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
869 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
870 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
871 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
873 11cc08c1 2020-07-23 stsp
874 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
875 11cc08c1 2020-07-23 stsp if (ancestor_target) {
876 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
877 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 11cc08c1 2020-07-23 stsp path);
879 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
880 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
882 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 11cc08c1 2020-07-23 stsp path);
884 11cc08c1 2020-07-23 stsp } else {
885 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
886 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
887 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
888 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
889 11cc08c1 2020-07-23 stsp if (err)
890 11cc08c1 2020-07-23 stsp goto done;
891 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 11cc08c1 2020-07-23 stsp path);
893 11cc08c1 2020-07-23 stsp }
894 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
895 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
896 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
897 af57b12a 2020-07-23 stsp strlen(deriv_target));
898 af57b12a 2020-07-23 stsp if (err)
899 af57b12a 2020-07-23 stsp goto done;
900 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
902 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
903 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
905 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 ea7786be 2020-07-23 stsp path);
907 ea7786be 2020-07-23 stsp } else {
908 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
909 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
910 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
911 ea7786be 2020-07-23 stsp if (err)
912 ea7786be 2020-07-23 stsp goto done;
913 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 ea7786be 2020-07-23 stsp path);
915 ea7786be 2020-07-23 stsp }
916 6353ad76 2019-02-08 stsp }
917 11cc08c1 2020-07-23 stsp
918 af57b12a 2020-07-23 stsp done:
919 af57b12a 2020-07-23 stsp free(ancestor_target);
920 eec2f5a9 2021-06-03 stsp return err;
921 eec2f5a9 2021-06-03 stsp }
922 eec2f5a9 2021-06-03 stsp
923 eec2f5a9 2021-06-03 stsp static const struct got_error *
924 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
925 eec2f5a9 2021-06-03 stsp {
926 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
927 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
928 eec2f5a9 2021-06-03 stsp ssize_t target_len;
929 eec2f5a9 2021-06-03 stsp size_t n;
930 eec2f5a9 2021-06-03 stsp FILE *f;
931 eec2f5a9 2021-06-03 stsp
932 eec2f5a9 2021-06-03 stsp *outfile = NULL;
933 eec2f5a9 2021-06-03 stsp
934 eec2f5a9 2021-06-03 stsp f = got_opentemp();
935 eec2f5a9 2021-06-03 stsp if (f == NULL)
936 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
937 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
939 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
940 eec2f5a9 2021-06-03 stsp goto done;
941 eec2f5a9 2021-06-03 stsp }
942 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
943 eec2f5a9 2021-06-03 stsp if (n != target_len) {
944 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
945 eec2f5a9 2021-06-03 stsp goto done;
946 eec2f5a9 2021-06-03 stsp }
947 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
948 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
949 eec2f5a9 2021-06-03 stsp goto done;
950 eec2f5a9 2021-06-03 stsp }
951 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
952 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
953 eec2f5a9 2021-06-03 stsp goto done;
954 eec2f5a9 2021-06-03 stsp }
955 eec2f5a9 2021-06-03 stsp done:
956 eec2f5a9 2021-06-03 stsp if (err)
957 eec2f5a9 2021-06-03 stsp fclose(f);
958 eec2f5a9 2021-06-03 stsp else
959 eec2f5a9 2021-06-03 stsp *outfile = f;
960 14c901f1 2019-08-08 stsp return err;
961 14c901f1 2019-08-08 stsp }
962 14c901f1 2019-08-08 stsp
963 14c901f1 2019-08-08 stsp /*
964 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
966 14c901f1 2019-08-08 stsp * acts as the second derived version.
967 14c901f1 2019-08-08 stsp */
968 14c901f1 2019-08-08 stsp static const struct got_error *
969 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
971 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
972 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
973 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
975 14c901f1 2019-08-08 stsp {
976 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
977 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
979 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
981 14c901f1 2019-08-08 stsp
982 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
983 14c901f1 2019-08-08 stsp
984 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
985 ed6b5030 2020-10-20 stsp if (err)
986 ed6b5030 2020-10-20 stsp return err;
987 14c901f1 2019-08-08 stsp
988 67a66647 2021-05-31 stsp if (blob_orig) {
989 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 67a66647 2021-05-31 stsp parent) == -1) {
991 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
992 67a66647 2021-05-31 stsp base_path = NULL;
993 67a66647 2021-05-31 stsp goto done;
994 67a66647 2021-05-31 stsp }
995 67a66647 2021-05-31 stsp
996 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
997 b90054ed 2022-11-01 stsp base_path, "");
998 67a66647 2021-05-31 stsp if (err)
999 67a66647 2021-05-31 stsp goto done;
1000 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 67a66647 2021-05-31 stsp blob_orig);
1002 67a66647 2021-05-31 stsp if (err)
1003 67a66647 2021-05-31 stsp goto done;
1004 67a66647 2021-05-31 stsp free(base_path);
1005 db590691 2021-06-02 stsp } else {
1006 db590691 2021-06-02 stsp /*
1007 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1008 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1009 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1010 db590691 2021-06-02 stsp */
1011 db590691 2021-06-02 stsp f_orig = got_opentemp();
1012 db590691 2021-06-02 stsp if (f_orig == NULL) {
1013 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1014 db590691 2021-06-02 stsp goto done;
1015 db590691 2021-06-02 stsp }
1016 67a66647 2021-05-31 stsp }
1017 67a66647 2021-05-31 stsp
1018 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1020 14c901f1 2019-08-08 stsp base_path = NULL;
1021 14c901f1 2019-08-08 stsp goto done;
1022 14c901f1 2019-08-08 stsp }
1023 14c901f1 2019-08-08 stsp
1024 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 14c901f1 2019-08-08 stsp if (err)
1026 14c901f1 2019-08-08 stsp goto done;
1027 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 14c901f1 2019-08-08 stsp blob_deriv);
1029 14c901f1 2019-08-08 stsp if (err)
1030 14c901f1 2019-08-08 stsp goto done;
1031 14c901f1 2019-08-08 stsp
1032 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 14c901f1 2019-08-08 stsp if (err)
1034 14c901f1 2019-08-08 stsp goto done;
1035 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1036 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1038 14c901f1 2019-08-08 stsp goto done;
1039 eec2f5a9 2021-06-03 stsp }
1040 eec2f5a9 2021-06-03 stsp
1041 5e91dae4 2022-08-30 stsp /*
1042 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1043 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1044 eec2f5a9 2021-06-03 stsp */
1045 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1046 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 eec2f5a9 2021-06-03 stsp if (err)
1048 eec2f5a9 2021-06-03 stsp goto done;
1049 eec2f5a9 2021-06-03 stsp } else {
1050 eec2f5a9 2021-06-03 stsp int fd;
1051 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1053 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1054 eec2f5a9 2021-06-03 stsp goto done;
1055 eec2f5a9 2021-06-03 stsp }
1056 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1057 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1058 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1059 eec2f5a9 2021-06-03 stsp close(fd);
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp }
1062 14c901f1 2019-08-08 stsp }
1063 14c901f1 2019-08-08 stsp
1064 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 14c901f1 2019-08-08 stsp done:
1068 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1070 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1072 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1074 14c901f1 2019-08-08 stsp free(base_path);
1075 67a66647 2021-05-31 stsp if (blob_orig_path) {
1076 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1077 67a66647 2021-05-31 stsp free(blob_orig_path);
1078 67a66647 2021-05-31 stsp }
1079 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1080 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1081 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1082 14c901f1 2019-08-08 stsp }
1083 6353ad76 2019-02-08 stsp free(id_str);
1084 818c7501 2019-07-11 stsp free(label_deriv);
1085 ed6b5030 2020-10-20 stsp free(parent);
1086 4a1ddfc2 2019-01-12 stsp return err;
1087 4a1ddfc2 2019-01-12 stsp }
1088 4a1ddfc2 2019-01-12 stsp
1089 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1090 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1093 13d9040b 2019-03-27 stsp {
1094 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1095 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1096 13d9040b 2019-03-27 stsp
1097 65b05cec 2020-07-23 stsp *new_iep = NULL;
1098 65b05cec 2020-07-23 stsp
1099 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1100 054041d0 2020-06-24 stsp if (err)
1101 054041d0 2020-06-24 stsp return err;
1102 054041d0 2020-06-24 stsp
1103 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1105 054041d0 2020-06-24 stsp if (err)
1106 054041d0 2020-06-24 stsp goto done;
1107 054041d0 2020-06-24 stsp
1108 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1109 054041d0 2020-06-24 stsp done:
1110 054041d0 2020-06-24 stsp if (err)
1111 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1112 65b05cec 2020-07-23 stsp else
1113 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1114 13d9040b 2019-03-27 stsp return err;
1115 1ebedb77 2019-10-19 stsp }
1116 1ebedb77 2019-10-19 stsp
1117 1ebedb77 2019-10-19 stsp static mode_t
1118 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1119 1ebedb77 2019-10-19 stsp {
1120 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1121 1ebedb77 2019-10-19 stsp
1122 1ebedb77 2019-10-19 stsp if (executable) {
1123 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1124 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1125 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1126 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1127 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1128 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1129 1ebedb77 2019-10-19 stsp }
1130 1ebedb77 2019-10-19 stsp
1131 b2b3fce1 2022-10-29 op return st_mode;
1132 b2b3fce1 2022-10-29 op }
1133 b2b3fce1 2022-10-29 op
1134 b2b3fce1 2022-10-29 op static mode_t
1135 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1136 b2b3fce1 2022-10-29 op {
1137 b2b3fce1 2022-10-29 op mode_t um;
1138 b2b3fce1 2022-10-29 op
1139 b2b3fce1 2022-10-29 op um = umask(000);
1140 b2b3fce1 2022-10-29 op umask(um);
1141 b2b3fce1 2022-10-29 op return mode & ~um;
1142 13d9040b 2019-03-27 stsp }
1143 13d9040b 2019-03-27 stsp
1144 8ba819a3 2020-07-23 stsp /* forward declaration */
1145 13d9040b 2019-03-27 stsp static const struct got_error *
1146 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1148 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1149 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1150 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1151 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1152 8ba819a3 2020-07-23 stsp
1153 5a1fbc73 2020-07-23 stsp /*
1154 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1155 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1156 5a1fbc73 2020-07-23 stsp */
1157 8ba819a3 2020-07-23 stsp static const struct got_error *
1158 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1160 5a1fbc73 2020-07-23 stsp {
1161 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1162 5a1fbc73 2020-07-23 stsp ssize_t elen;
1163 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1164 5a1fbc73 2020-07-23 stsp int fd;
1165 5a1fbc73 2020-07-23 stsp
1166 c6e8a826 2021-04-05 stsp *did_something = 0;
1167 c6e8a826 2021-04-05 stsp
1168 5a1fbc73 2020-07-23 stsp /*
1169 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1170 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1171 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1172 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1173 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1174 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1175 5a1fbc73 2020-07-23 stsp */
1176 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1178 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1179 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1180 5a1fbc73 2020-07-23 stsp
1181 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1182 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 5a1fbc73 2020-07-23 stsp if (elen == -1)
1184 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1185 5a1fbc73 2020-07-23 stsp
1186 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1187 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1188 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1189 5a1fbc73 2020-07-23 stsp }
1190 5a1fbc73 2020-07-23 stsp
1191 c6e8a826 2021-04-05 stsp *did_something = 1;
1192 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1193 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1194 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1195 5a1fbc73 2020-07-23 stsp return err;
1196 3c1ec782 2020-07-23 stsp }
1197 3c1ec782 2020-07-23 stsp
1198 3c1ec782 2020-07-23 stsp static const struct got_error *
1199 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1201 3c1ec782 2020-07-23 stsp {
1202 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1203 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1204 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1205 3c1ec782 2020-07-23 stsp
1206 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1207 3c1ec782 2020-07-23 stsp
1208 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1209 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1210 3c1ec782 2020-07-23 stsp return NULL;
1211 3c1ec782 2020-07-23 stsp }
1212 3c1ec782 2020-07-23 stsp
1213 3c1ec782 2020-07-23 stsp /*
1214 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1215 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1216 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1217 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1218 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1219 3c1ec782 2020-07-23 stsp */
1220 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1221 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1222 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1223 ce031e9e 2020-10-20 stsp if (err)
1224 ce031e9e 2020-10-20 stsp return err;
1225 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 ce031e9e 2020-10-20 stsp free(parent);
1227 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1228 ce031e9e 2020-10-20 stsp }
1229 ce031e9e 2020-10-20 stsp free(parent);
1230 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1231 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 3c1ec782 2020-07-23 stsp free(abspath);
1233 3c1ec782 2020-07-23 stsp return err;
1234 3c1ec782 2020-07-23 stsp }
1235 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 3c1ec782 2020-07-23 stsp free(abspath);
1237 3c1ec782 2020-07-23 stsp if (err)
1238 3c1ec782 2020-07-23 stsp return err;
1239 3c1ec782 2020-07-23 stsp } else {
1240 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 3c1ec782 2020-07-23 stsp if (err)
1242 3c1ec782 2020-07-23 stsp return err;
1243 3c1ec782 2020-07-23 stsp }
1244 3c1ec782 2020-07-23 stsp
1245 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1246 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1248 3c1ec782 2020-07-23 stsp return NULL;
1249 3c1ec782 2020-07-23 stsp }
1250 3c1ec782 2020-07-23 stsp
1251 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1252 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1254 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1255 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1257 3c1ec782 2020-07-23 stsp
1258 3c1ec782 2020-07-23 stsp free(path_got);
1259 3c1ec782 2020-07-23 stsp return NULL;
1260 5a1fbc73 2020-07-23 stsp }
1261 5a1fbc73 2020-07-23 stsp
1262 5a1fbc73 2020-07-23 stsp static const struct got_error *
1263 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1266 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1267 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1268 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1269 9d31a1d8 2018-03-11 stsp {
1270 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1271 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1272 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1273 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1275 8ba819a3 2020-07-23 stsp
1276 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1277 2e1fa222 2020-07-23 stsp
1278 3c29341b 2022-07-21 florian /*
1279 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1280 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1281 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1282 8ba819a3 2020-07-23 stsp * in the blob object.
1283 8ba819a3 2020-07-23 stsp */
1284 8ba819a3 2020-07-23 stsp do {
1285 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1286 538b6881 2022-07-21 florian if (err)
1287 538b6881 2022-07-21 florian return err;
1288 538b6881 2022-07-21 florian
1289 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1290 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1291 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1292 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1293 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1294 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1296 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1297 3b9f0f87 2020-07-23 stsp progress_arg);
1298 8ba819a3 2020-07-23 stsp }
1299 8ba819a3 2020-07-23 stsp if (len > 0) {
1300 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1301 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1302 8ba819a3 2020-07-23 stsp len - hdrlen);
1303 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1304 8ba819a3 2020-07-23 stsp hdrlen = 0;
1305 8ba819a3 2020-07-23 stsp }
1306 8ba819a3 2020-07-23 stsp } while (len != 0);
1307 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1308 8ba819a3 2020-07-23 stsp
1309 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1311 3c1ec782 2020-07-23 stsp if (err)
1312 3c1ec782 2020-07-23 stsp return err;
1313 8ba819a3 2020-07-23 stsp
1314 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1315 906c123b 2020-07-23 stsp /* install as a regular file */
1316 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1317 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1318 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1320 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1321 3c29341b 2022-07-21 florian return err;
1322 906c123b 2020-07-23 stsp }
1323 906c123b 2020-07-23 stsp
1324 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1325 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1326 c6e8a826 2021-04-05 stsp int symlink_replaced;
1327 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1328 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1329 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1330 3c29341b 2022-07-21 florian return err;
1331 c90c8ce3 2020-07-23 stsp }
1332 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1333 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1334 5a1fbc73 2020-07-23 stsp if (err)
1335 3c29341b 2022-07-21 florian return err;
1336 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1337 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1338 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1339 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1340 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1341 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1342 c6e8a826 2021-04-05 stsp } else {
1343 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1344 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1345 c6e8a826 2021-04-05 stsp }
1346 f35fa46a 2020-07-23 stsp }
1347 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1348 f35fa46a 2020-07-23 stsp }
1349 f35fa46a 2020-07-23 stsp
1350 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1351 f4994adc 2020-10-20 stsp char *parent;
1352 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1353 f4994adc 2020-10-20 stsp if (err)
1354 3c29341b 2022-07-21 florian return err;
1355 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1356 f4994adc 2020-10-20 stsp free(parent);
1357 f35fa46a 2020-07-23 stsp if (err)
1358 3c29341b 2022-07-21 florian return err;
1359 f35fa46a 2020-07-23 stsp /*
1360 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1361 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1362 f35fa46a 2020-07-23 stsp */
1363 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1364 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1365 3c29341b 2022-07-21 florian return err;
1366 f35fa46a 2020-07-23 stsp }
1367 f35fa46a 2020-07-23 stsp }
1368 f35fa46a 2020-07-23 stsp
1369 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1370 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1371 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1372 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1373 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1374 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1375 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1377 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1378 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1379 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1380 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1381 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1382 8ba819a3 2020-07-23 stsp } else {
1383 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1384 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1385 8ba819a3 2020-07-23 stsp }
1386 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1387 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 8ba819a3 2020-07-23 stsp return err;
1390 8ba819a3 2020-07-23 stsp }
1391 8ba819a3 2020-07-23 stsp
1392 8ba819a3 2020-07-23 stsp static const struct got_error *
1393 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1395 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1396 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1397 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1398 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1399 8ba819a3 2020-07-23 stsp {
1400 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1401 507dc3bb 2018-12-29 stsp int fd = -1;
1402 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1403 507dc3bb 2018-12-29 stsp int update = 0;
1404 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1405 b2b3fce1 2022-10-29 op mode_t mode;
1406 8ba819a3 2020-07-23 stsp
1407 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1410 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1411 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1412 f5375317 2020-10-20 stsp char *parent;
1413 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1414 f5375317 2020-10-20 stsp if (err)
1415 f5375317 2020-10-20 stsp return err;
1416 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1417 f5375317 2020-10-20 stsp free(parent);
1418 21908da4 2019-01-13 stsp if (err)
1419 21908da4 2019-01-13 stsp return err;
1420 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1421 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 b2b3fce1 2022-10-29 op mode);
1423 21908da4 2019-01-13 stsp if (fd == -1)
1424 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1425 230a42bd 2019-05-11 jcs ondisk_path);
1426 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1427 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1428 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1429 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1430 3b9f0f87 2020-07-23 stsp goto done;
1431 3b9f0f87 2020-07-23 stsp }
1432 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1435 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1436 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1437 507dc3bb 2018-12-29 stsp goto done;
1438 d70b8e30 2018-12-27 stsp } else {
1439 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1440 b90054ed 2022-11-01 stsp ondisk_path, "");
1441 507dc3bb 2018-12-29 stsp if (err)
1442 507dc3bb 2018-12-29 stsp goto done;
1443 507dc3bb 2018-12-29 stsp update = 1;
1444 b2b3fce1 2022-10-29 op
1445 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1446 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1447 b2b3fce1 2022-10-29 op tmppath);
1448 b2b3fce1 2022-10-29 op goto done;
1449 b2b3fce1 2022-10-29 op }
1450 9d31a1d8 2018-03-11 stsp }
1451 507dc3bb 2018-12-29 stsp } else
1452 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1453 3818e3c4 2020-11-01 naddy }
1454 3818e3c4 2020-11-01 naddy
1455 bd6aa359 2020-07-23 stsp if (progress_cb) {
1456 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1457 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 bd6aa359 2020-07-23 stsp path);
1459 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1460 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 bd6aa359 2020-07-23 stsp path);
1462 bd6aa359 2020-07-23 stsp else
1463 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1464 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 bd6aa359 2020-07-23 stsp if (err)
1466 bd6aa359 2020-07-23 stsp goto done;
1467 bd6aa359 2020-07-23 stsp }
1468 d7b62c98 2018-12-27 stsp
1469 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1470 9d31a1d8 2018-03-11 stsp do {
1471 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1473 9d31a1d8 2018-03-11 stsp if (err)
1474 9d31a1d8 2018-03-11 stsp break;
1475 9d31a1d8 2018-03-11 stsp if (len > 0) {
1476 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1477 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1479 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1480 b87c6f83 2018-12-24 stsp goto done;
1481 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1482 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1483 b87c6f83 2018-12-24 stsp goto done;
1484 9d31a1d8 2018-03-11 stsp }
1485 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1486 9d31a1d8 2018-03-11 stsp }
1487 9d31a1d8 2018-03-11 stsp } while (len != 0);
1488 9d31a1d8 2018-03-11 stsp
1489 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1490 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1491 816dc654 2019-02-16 stsp goto done;
1492 816dc654 2019-02-16 stsp }
1493 9d31a1d8 2018-03-11 stsp
1494 507dc3bb 2018-12-29 stsp if (update) {
1495 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1497 f6d8c0ac 2020-11-09 stsp goto done;
1498 f6d8c0ac 2020-11-09 stsp }
1499 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1501 230a42bd 2019-05-11 jcs ondisk_path);
1502 68ed9ba5 2019-02-10 stsp goto done;
1503 68ed9ba5 2019-02-10 stsp }
1504 63df146d 2020-11-09 stsp free(tmppath);
1505 63df146d 2020-11-09 stsp tmppath = NULL;
1506 507dc3bb 2018-12-29 stsp }
1507 507dc3bb 2018-12-29 stsp
1508 9d31a1d8 2018-03-11 stsp done:
1509 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1510 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1511 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1513 507dc3bb 2018-12-29 stsp free(tmppath);
1514 6353ad76 2019-02-08 stsp return err;
1515 6353ad76 2019-02-08 stsp }
1516 6353ad76 2019-02-08 stsp
1517 12383673 2023-02-18 mark /*
1518 12383673 2023-02-18 mark * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1519 12383673 2023-02-18 mark * conflict marker is found in newly added lines only.
1520 12383673 2023-02-18 mark */
1521 6353ad76 2019-02-08 stsp static const struct got_error *
1522 12383673 2023-02-18 mark get_modified_file_content_status(unsigned char *status,
1523 12383673 2023-02-18 mark struct got_blob_object *blob, const char *path, struct stat *sb,
1524 12383673 2023-02-18 mark FILE *ondisk_file)
1525 7154f6ce 2019-03-27 stsp {
1526 d952957d 2023-02-19 mark const struct got_error *err, *free_err;
1527 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1528 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1529 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1530 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1531 7154f6ce 2019-03-27 stsp };
1532 d952957d 2023-02-19 mark FILE *f1 = NULL;
1533 d952957d 2023-02-19 mark struct got_diffreg_result *diffreg_result = NULL;
1534 d952957d 2023-02-19 mark struct diff_result *r;
1535 d952957d 2023-02-19 mark int nchunks_parsed, n, i = 0, ln = 0;
1536 9bdd68dd 2021-01-02 naddy char *line = NULL;
1537 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1538 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1539 7154f6ce 2019-03-27 stsp
1540 d952957d 2023-02-19 mark if (*status != GOT_STATUS_MODIFY)
1541 d952957d 2023-02-19 mark return NULL;
1542 12383673 2023-02-18 mark
1543 d952957d 2023-02-19 mark f1 = got_opentemp();
1544 d952957d 2023-02-19 mark if (f1 == NULL)
1545 d952957d 2023-02-19 mark return got_error_from_errno("got_opentemp");
1546 12383673 2023-02-18 mark
1547 d952957d 2023-02-19 mark if (blob) {
1548 d952957d 2023-02-19 mark got_object_blob_rewind(blob);
1549 d952957d 2023-02-19 mark err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1550 12383673 2023-02-18 mark if (err)
1551 12383673 2023-02-18 mark goto done;
1552 d952957d 2023-02-19 mark }
1553 12383673 2023-02-18 mark
1554 d952957d 2023-02-19 mark err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1555 d952957d 2023-02-19 mark 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 d952957d 2023-02-19 mark if (err)
1557 d952957d 2023-02-19 mark goto done;
1558 d952957d 2023-02-19 mark
1559 d952957d 2023-02-19 mark r = diffreg_result->result;
1560 d952957d 2023-02-19 mark
1561 d952957d 2023-02-19 mark for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1562 d952957d 2023-02-19 mark struct diff_chunk *c;
1563 d952957d 2023-02-19 mark struct diff_chunk_context cc = {};
1564 7783f73c 2023-02-20 mark off_t pos;
1565 d952957d 2023-02-19 mark
1566 d952957d 2023-02-19 mark /*
1567 d952957d 2023-02-19 mark * We can optimise a little by advancing straight
1568 d952957d 2023-02-19 mark * to the next chunk if this one has no added lines.
1569 d952957d 2023-02-19 mark */
1570 d952957d 2023-02-19 mark c = diff_chunk_get(r, n);
1571 d952957d 2023-02-19 mark
1572 45e6b2f4 2023-02-20 mark if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 d952957d 2023-02-19 mark nchunks_parsed = 1;
1574 7783f73c 2023-02-20 mark continue; /* removed or unchanged lines */
1575 7154f6ce 2019-03-27 stsp }
1576 7154f6ce 2019-03-27 stsp
1577 7783f73c 2023-02-20 mark pos = diff_chunk_get_right_start_pos(c);
1578 7783f73c 2023-02-20 mark if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1579 7783f73c 2023-02-20 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 7783f73c 2023-02-20 mark goto done;
1581 7154f6ce 2019-03-27 stsp }
1582 d952957d 2023-02-19 mark
1583 7783f73c 2023-02-20 mark diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1584 7783f73c 2023-02-20 mark ln = cc.right.start;
1585 7783f73c 2023-02-20 mark
1586 d952957d 2023-02-19 mark while (ln < cc.right.end) {
1587 d952957d 2023-02-19 mark linelen = getline(&line, &linesize, ondisk_file);
1588 d952957d 2023-02-19 mark if (linelen == -1) {
1589 d952957d 2023-02-19 mark if (feof(ondisk_file))
1590 d952957d 2023-02-19 mark break;
1591 d952957d 2023-02-19 mark err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 d952957d 2023-02-19 mark break;
1593 d952957d 2023-02-19 mark }
1594 d952957d 2023-02-19 mark
1595 d952957d 2023-02-19 mark if (line && strncmp(line, markers[i],
1596 d952957d 2023-02-19 mark strlen(markers[i])) == 0) {
1597 d952957d 2023-02-19 mark if (strcmp(markers[i],
1598 d952957d 2023-02-19 mark GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1599 d952957d 2023-02-19 mark *status = GOT_STATUS_CONFLICT;
1600 d952957d 2023-02-19 mark goto done;
1601 d952957d 2023-02-19 mark } else
1602 d952957d 2023-02-19 mark i++;
1603 d952957d 2023-02-19 mark }
1604 d952957d 2023-02-19 mark ++ln;
1605 d952957d 2023-02-19 mark }
1606 7154f6ce 2019-03-27 stsp }
1607 12383673 2023-02-18 mark
1608 12383673 2023-02-18 mark done:
1609 9bdd68dd 2021-01-02 naddy free(line);
1610 12383673 2023-02-18 mark if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1611 12383673 2023-02-18 mark err = got_error_from_errno("fclose");
1612 d952957d 2023-02-19 mark free_err = got_diffreg_result_free(diffreg_result);
1613 d952957d 2023-02-19 mark if (err == NULL)
1614 d952957d 2023-02-19 mark err = free_err;
1615 7154f6ce 2019-03-27 stsp
1616 7154f6ce 2019-03-27 stsp return err;
1617 e2b1e152 2019-07-13 stsp }
1618 e2b1e152 2019-07-13 stsp
1619 e2b1e152 2019-07-13 stsp static int
1620 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 1ebedb77 2019-10-19 stsp {
1622 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1623 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1624 1ebedb77 2019-10-19 stsp }
1625 1ebedb77 2019-10-19 stsp
1626 1ebedb77 2019-10-19 stsp static int
1627 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 e2b1e152 2019-07-13 stsp {
1629 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1630 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1631 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1632 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1633 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1634 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1635 c363b2c1 2019-08-03 stsp }
1636 c363b2c1 2019-08-03 stsp
1637 c363b2c1 2019-08-03 stsp static unsigned char
1638 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1639 c363b2c1 2019-08-03 stsp {
1640 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1641 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1642 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1643 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1644 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1645 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1646 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1647 c363b2c1 2019-08-03 stsp default:
1648 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1649 a919d5c4 2020-07-23 stsp }
1650 a919d5c4 2020-07-23 stsp }
1651 a919d5c4 2020-07-23 stsp
1652 a919d5c4 2020-07-23 stsp static const struct got_error *
1653 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1654 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1655 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1656 a919d5c4 2020-07-23 stsp {
1657 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1658 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1659 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1660 a919d5c4 2020-07-23 stsp ssize_t elen;
1661 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1662 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1663 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1666 a919d5c4 2020-07-23 stsp
1667 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1668 a919d5c4 2020-07-23 stsp do {
1669 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1670 a919d5c4 2020-07-23 stsp if (err)
1671 a919d5c4 2020-07-23 stsp return err;
1672 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1673 a919d5c4 2020-07-23 stsp /*
1674 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1675 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1676 a919d5c4 2020-07-23 stsp */
1677 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1678 a919d5c4 2020-07-23 stsp }
1679 a919d5c4 2020-07-23 stsp if (len > 0) {
1680 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1681 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1682 a919d5c4 2020-07-23 stsp len - hdrlen);
1683 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1684 a919d5c4 2020-07-23 stsp hdrlen = 0;
1685 a919d5c4 2020-07-23 stsp }
1686 a919d5c4 2020-07-23 stsp } while (len != 0);
1687 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1688 a919d5c4 2020-07-23 stsp
1689 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1690 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 a919d5c4 2020-07-23 stsp if (elen == -1)
1692 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1693 a919d5c4 2020-07-23 stsp } else {
1694 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1695 a919d5c4 2020-07-23 stsp if (elen == -1)
1696 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1697 c363b2c1 2019-08-03 stsp }
1698 a919d5c4 2020-07-23 stsp
1699 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1700 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1701 a919d5c4 2020-07-23 stsp
1702 a919d5c4 2020-07-23 stsp return NULL;
1703 7154f6ce 2019-03-27 stsp }
1704 7154f6ce 2019-03-27 stsp
1705 7154f6ce 2019-03-27 stsp static const struct got_error *
1706 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1707 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1708 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1709 6353ad76 2019-02-08 stsp {
1710 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1711 6353ad76 2019-02-08 stsp struct got_object_id id;
1712 6353ad76 2019-02-08 stsp size_t hdrlen;
1713 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1714 6353ad76 2019-02-08 stsp FILE *f = NULL;
1715 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1716 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1717 6353ad76 2019-02-08 stsp size_t flen, blen;
1718 2c4740ad 2023-02-12 mark unsigned char staged_status;
1719 6353ad76 2019-02-08 stsp
1720 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
1721 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1722 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1723 6353ad76 2019-02-08 stsp
1724 7f91a133 2019-12-13 stsp /*
1725 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1726 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1727 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1728 7f91a133 2019-12-13 stsp */
1729 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1730 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1731 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1732 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1733 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1734 882ef1b9 2019-12-13 stsp else
1735 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1736 882ef1b9 2019-12-13 stsp goto done;
1737 882ef1b9 2019-12-13 stsp }
1738 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1739 3d35a492 2019-12-13 stsp goto done;
1740 3d35a492 2019-12-13 stsp }
1741 7f91a133 2019-12-13 stsp } else {
1742 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1744 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1745 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1746 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1747 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1748 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1749 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1750 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1751 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1752 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1753 3d35a492 2019-12-13 stsp else
1754 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1755 3d35a492 2019-12-13 stsp goto done;
1756 3d35a492 2019-12-13 stsp }
1757 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1758 1338848f 2019-12-13 stsp goto done;
1759 a378724f 2019-02-10 stsp }
1760 a378724f 2019-02-10 stsp }
1761 6353ad76 2019-02-08 stsp
1762 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1763 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1764 1338848f 2019-12-13 stsp goto done;
1765 3f148bc6 2019-07-27 stsp }
1766 efdd40df 2019-07-27 stsp
1767 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1768 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1769 1338848f 2019-12-13 stsp goto done;
1770 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1771 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1772 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1773 1338848f 2019-12-13 stsp goto done;
1774 d00136be 2019-03-26 stsp }
1775 b8f41171 2019-02-10 stsp
1776 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1777 f179e66d 2020-07-23 stsp goto done;
1778 f179e66d 2020-07-23 stsp
1779 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1780 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1781 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1782 1338848f 2019-12-13 stsp goto done;
1783 f179e66d 2020-07-23 stsp }
1784 6353ad76 2019-02-08 stsp
1785 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1786 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1787 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 c363b2c1 2019-08-03 stsp else
1789 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
1790 c363b2c1 2019-08-03 stsp
1791 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1792 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1793 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1794 eb81bc23 2022-06-28 tracey goto done;
1795 eb81bc23 2022-06-28 tracey }
1796 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 6353ad76 2019-02-08 stsp if (err)
1798 1338848f 2019-12-13 stsp goto done;
1799 6353ad76 2019-02-08 stsp
1800 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1801 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1802 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1803 a919d5c4 2020-07-23 stsp goto done;
1804 a919d5c4 2020-07-23 stsp }
1805 a919d5c4 2020-07-23 stsp
1806 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1807 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 ab0d4361 2019-12-13 stsp if (fd == -1) {
1809 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1810 ab0d4361 2019-12-13 stsp goto done;
1811 ab0d4361 2019-12-13 stsp }
1812 3d35a492 2019-12-13 stsp }
1813 3d35a492 2019-12-13 stsp
1814 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1815 6353ad76 2019-02-08 stsp if (f == NULL) {
1816 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1817 6353ad76 2019-02-08 stsp goto done;
1818 6353ad76 2019-02-08 stsp }
1819 1338848f 2019-12-13 stsp fd = -1;
1820 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1821 656b1f76 2019-05-11 jcs for (;;) {
1822 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1824 6353ad76 2019-02-08 stsp if (err)
1825 7154f6ce 2019-03-27 stsp goto done;
1826 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1827 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1829 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1830 7154f6ce 2019-03-27 stsp goto done;
1831 80c5c120 2019-02-19 stsp }
1832 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1833 6353ad76 2019-02-08 stsp if (flen != 0)
1834 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1835 6353ad76 2019-02-08 stsp break;
1836 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1837 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1838 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1839 6353ad76 2019-02-08 stsp break;
1840 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1841 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1842 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1844 6353ad76 2019-02-08 stsp break;
1845 6353ad76 2019-02-08 stsp }
1846 6353ad76 2019-02-08 stsp } else {
1847 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1848 6353ad76 2019-02-08 stsp break;
1849 6353ad76 2019-02-08 stsp }
1850 6353ad76 2019-02-08 stsp hdrlen = 0;
1851 6353ad76 2019-02-08 stsp }
1852 7154f6ce 2019-03-27 stsp
1853 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1854 7154f6ce 2019-03-27 stsp rewind(f);
1855 12383673 2023-02-18 mark err = get_modified_file_content_status(status, blob, ie->path,
1856 12383673 2023-02-18 mark sb, f);
1857 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1858 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1859 6353ad76 2019-02-08 stsp done:
1860 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1861 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1862 6353ad76 2019-02-08 stsp if (blob)
1863 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1864 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1865 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1866 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1867 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1868 9d31a1d8 2018-03-11 stsp return err;
1869 e2b1e152 2019-07-13 stsp }
1870 e2b1e152 2019-07-13 stsp
1871 e2b1e152 2019-07-13 stsp /*
1872 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1873 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1874 e2b1e152 2019-07-13 stsp */
1875 e2b1e152 2019-07-13 stsp static const struct got_error *
1876 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1877 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1878 e2b1e152 2019-07-13 stsp {
1879 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1880 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1881 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1882 e2b1e152 2019-07-13 stsp
1883 e2b1e152 2019-07-13 stsp return NULL;
1884 9d31a1d8 2018-03-11 stsp }
1885 9d31a1d8 2018-03-11 stsp
1886 9d31a1d8 2018-03-11 stsp static const struct got_error *
1887 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1888 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1889 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1890 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1891 0584f854 2019-04-06 stsp void *progress_arg)
1892 9d31a1d8 2018-03-11 stsp {
1893 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1894 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1895 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1896 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1897 b8f41171 2019-02-10 stsp struct stat sb;
1898 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1899 9d31a1d8 2018-03-11 stsp
1900 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1901 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1902 6353ad76 2019-02-08 stsp
1903 abb4604f 2019-07-27 stsp if (ie) {
1904 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1905 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1906 a76c42e6 2019-08-03 stsp goto done;
1907 a76c42e6 2019-08-03 stsp }
1908 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 7f91a133 2019-12-13 stsp repo);
1910 abb4604f 2019-07-27 stsp if (err)
1911 abb4604f 2019-07-27 stsp goto done;
1912 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1913 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1914 c90c8ce3 2020-07-23 stsp } else {
1915 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1916 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1917 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1918 f6764181 2021-09-24 stsp ondisk_path);
1919 f6764181 2021-09-24 stsp goto done;
1920 f6764181 2021-09-24 stsp }
1921 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1922 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1923 f6764181 2021-09-24 stsp } else {
1924 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1925 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1926 f6764181 2021-09-24 stsp else
1927 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1928 f6764181 2021-09-24 stsp }
1929 c90c8ce3 2020-07-23 stsp }
1930 6353ad76 2019-02-08 stsp
1931 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1932 2c41dce7 2021-06-27 stsp if (ie)
1933 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1934 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1935 b8f41171 2019-02-10 stsp goto done;
1936 b8f41171 2019-02-10 stsp }
1937 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1938 a769b60b 2021-06-27 stsp if (ie)
1939 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1940 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1941 5036ab18 2020-04-18 stsp path);
1942 5036ab18 2020-04-18 stsp goto done;
1943 5036ab18 2020-04-18 stsp }
1944 b8f41171 2019-02-10 stsp
1945 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1946 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1947 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1948 c6e8a826 2021-04-05 stsp /*
1949 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1950 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1951 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1952 c6e8a826 2021-04-05 stsp * updating contents of this file.
1953 c6e8a826 2021-04-05 stsp */
1954 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1955 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1956 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1957 c6e8a826 2021-04-05 stsp /* Same commit. */
1958 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1959 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1960 e2b1e152 2019-07-13 stsp if (err)
1961 e2b1e152 2019-07-13 stsp goto done;
1962 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1963 b8f41171 2019-02-10 stsp path);
1964 6353ad76 2019-02-08 stsp goto done;
1965 a378724f 2019-02-10 stsp }
1966 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1967 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1968 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1969 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1970 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1971 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1972 0f58026f 2021-04-05 stsp if (err)
1973 0f58026f 2021-04-05 stsp goto done;
1974 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1975 0f58026f 2021-04-05 stsp path);
1976 b8f41171 2019-02-10 stsp goto done;
1977 e2b1e152 2019-07-13 stsp }
1978 9d31a1d8 2018-03-11 stsp }
1979 9d31a1d8 2018-03-11 stsp
1980 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1981 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1982 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1983 eb81bc23 2022-06-28 tracey goto done;
1984 eb81bc23 2022-06-28 tracey }
1985 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1986 8da9e5f4 2019-01-12 stsp if (err)
1987 6353ad76 2019-02-08 stsp goto done;
1988 512f0d0e 2019-01-02 stsp
1989 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1990 234035bc 2019-06-01 stsp int update_timestamps;
1991 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1992 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1993 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1994 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1995 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1996 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1997 eb81bc23 2022-06-28 tracey goto done;
1998 eb81bc23 2022-06-28 tracey }
1999 234035bc 2019-06-01 stsp struct got_object_id id2;
2000 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id2, ie);
2001 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2002 eb81bc23 2022-06-28 tracey fd2);
2003 234035bc 2019-06-01 stsp if (err)
2004 f69721c3 2019-10-21 stsp goto done;
2005 f69721c3 2019-10-21 stsp }
2006 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2007 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2008 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2009 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2010 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2011 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2012 f69721c3 2019-10-21 stsp goto done;
2013 f69721c3 2019-10-21 stsp }
2014 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2015 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2016 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2017 234035bc 2019-06-01 stsp goto done;
2018 f69721c3 2019-10-21 stsp }
2019 234035bc 2019-06-01 stsp }
2020 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2021 36bf999c 2020-07-23 stsp char *link_target;
2022 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2023 36bf999c 2020-07-23 stsp if (err)
2024 36bf999c 2020-07-23 stsp goto done;
2025 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2026 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2027 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2028 36bf999c 2020-07-23 stsp free(link_target);
2029 993e2a1b 2020-07-23 stsp } else {
2030 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2031 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2032 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2033 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2034 993e2a1b 2020-07-23 stsp }
2035 f69721c3 2019-10-21 stsp free(label_orig);
2036 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2037 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2038 eb81bc23 2022-06-28 tracey goto done;
2039 eb81bc23 2022-06-28 tracey }
2040 234035bc 2019-06-01 stsp if (blob2)
2041 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2042 909d120e 2019-09-22 stsp if (err)
2043 909d120e 2019-09-22 stsp goto done;
2044 234035bc 2019-06-01 stsp /*
2045 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2046 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2047 234035bc 2019-06-01 stsp * unmodified files again.
2048 234035bc 2019-06-01 stsp */
2049 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2050 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2051 234035bc 2019-06-01 stsp update_timestamps);
2052 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2053 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2054 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2055 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2056 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2057 1ee397ad 2019-07-12 stsp if (err)
2058 1ee397ad 2019-07-12 stsp goto done;
2059 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2060 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2061 13d9040b 2019-03-27 stsp if (err)
2062 13d9040b 2019-03-27 stsp goto done;
2063 13d9040b 2019-03-27 stsp } else {
2064 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2065 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2066 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2067 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2068 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2069 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2070 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2071 2e1fa222 2020-07-23 stsp } else {
2072 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2073 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2074 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2075 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2076 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2077 2e1fa222 2020-07-23 stsp }
2078 13d9040b 2019-03-27 stsp if (err)
2079 13d9040b 2019-03-27 stsp goto done;
2080 65b05cec 2020-07-23 stsp
2081 054041d0 2020-06-24 stsp if (ie) {
2082 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2083 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2084 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2085 054041d0 2020-06-24 stsp } else {
2086 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2087 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2088 054041d0 2020-06-24 stsp &blob->id);
2089 054041d0 2020-06-24 stsp }
2090 13d9040b 2019-03-27 stsp if (err)
2091 13d9040b 2019-03-27 stsp goto done;
2092 65b05cec 2020-07-23 stsp
2093 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2094 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2095 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2096 2e1fa222 2020-07-23 stsp }
2097 13d9040b 2019-03-27 stsp }
2098 eb81bc23 2022-06-28 tracey
2099 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2100 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2101 eb81bc23 2022-06-28 tracey goto done;
2102 eb81bc23 2022-06-28 tracey }
2103 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2104 6353ad76 2019-02-08 stsp done:
2105 6353ad76 2019-02-08 stsp free(ondisk_path);
2106 8da9e5f4 2019-01-12 stsp return err;
2107 90285c3b 2019-01-08 stsp }
2108 90285c3b 2019-01-08 stsp
2109 90285c3b 2019-01-08 stsp static const struct got_error *
2110 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2111 90285c3b 2019-01-08 stsp {
2112 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2113 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2114 90285c3b 2019-01-08 stsp
2115 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2116 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2117 90285c3b 2019-01-08 stsp
2118 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2119 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2120 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2121 c97665ae 2019-01-13 stsp } else {
2122 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2123 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2124 1c4cdd89 2021-06-20 stsp if (err)
2125 1c4cdd89 2021-06-20 stsp goto done;
2126 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2127 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2128 fddefe3b 2020-10-20 stsp free(ondisk_path);
2129 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2130 1c4cdd89 2021-06-20 stsp parent = NULL;
2131 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2132 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2133 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2134 fddefe3b 2020-10-20 stsp ondisk_path);
2135 90285c3b 2019-01-08 stsp break;
2136 90285c3b 2019-01-08 stsp }
2137 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2138 1c4cdd89 2021-06-20 stsp if (err)
2139 1c4cdd89 2021-06-20 stsp break;
2140 1c4cdd89 2021-06-20 stsp }
2141 90285c3b 2019-01-08 stsp }
2142 1c4cdd89 2021-06-20 stsp done:
2143 90285c3b 2019-01-08 stsp free(ondisk_path);
2144 1c4cdd89 2021-06-20 stsp free(parent);
2145 90285c3b 2019-01-08 stsp return err;
2146 512f0d0e 2019-01-02 stsp }
2147 512f0d0e 2019-01-02 stsp
2148 708d8e67 2019-03-27 stsp static const struct got_error *
2149 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2150 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2151 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2152 708d8e67 2019-03-27 stsp {
2153 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2154 708d8e67 2019-03-27 stsp unsigned char status;
2155 708d8e67 2019-03-27 stsp struct stat sb;
2156 708d8e67 2019-03-27 stsp char *ondisk_path;
2157 708d8e67 2019-03-27 stsp
2158 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2159 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2160 a76c42e6 2019-08-03 stsp
2161 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2162 708d8e67 2019-03-27 stsp == -1)
2163 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2164 708d8e67 2019-03-27 stsp
2165 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2166 708d8e67 2019-03-27 stsp if (err)
2167 5a58a424 2020-06-23 stsp goto done;
2168 708d8e67 2019-03-27 stsp
2169 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2170 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2171 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2172 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2173 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2174 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2175 993e2a1b 2020-07-23 stsp goto done;
2176 993e2a1b 2020-07-23 stsp }
2177 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2178 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2179 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2180 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2181 993e2a1b 2020-07-23 stsp if (err)
2182 993e2a1b 2020-07-23 stsp goto done;
2183 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2184 993e2a1b 2020-07-23 stsp ie->path);
2185 993e2a1b 2020-07-23 stsp goto done;
2186 993e2a1b 2020-07-23 stsp }
2187 993e2a1b 2020-07-23 stsp
2188 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2189 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2190 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2191 1ee397ad 2019-07-12 stsp if (err)
2192 5a58a424 2020-06-23 stsp goto done;
2193 fc6346c4 2019-03-27 stsp /*
2194 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2195 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2196 fc6346c4 2019-03-27 stsp */
2197 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2198 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2199 fc6346c4 2019-03-27 stsp } else {
2200 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2201 1ee397ad 2019-07-12 stsp if (err)
2202 5a58a424 2020-06-23 stsp goto done;
2203 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2204 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2205 fc6346c4 2019-03-27 stsp if (err)
2206 5a58a424 2020-06-23 stsp goto done;
2207 fc6346c4 2019-03-27 stsp }
2208 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2209 708d8e67 2019-03-27 stsp }
2210 5a58a424 2020-06-23 stsp done:
2211 5a58a424 2020-06-23 stsp free(ondisk_path);
2212 fc6346c4 2019-03-27 stsp return err;
2213 708d8e67 2019-03-27 stsp }
2214 708d8e67 2019-03-27 stsp
2215 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2216 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2217 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2218 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2219 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2220 8da9e5f4 2019-01-12 stsp void *progress_arg;
2221 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2222 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2223 8da9e5f4 2019-01-12 stsp };
2224 8da9e5f4 2019-01-12 stsp
2225 512f0d0e 2019-01-02 stsp static const struct got_error *
2226 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2227 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2228 512f0d0e 2019-01-02 stsp {
2229 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2230 0584f854 2019-04-06 stsp
2231 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2232 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2233 512f0d0e 2019-01-02 stsp
2234 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2235 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 8da9e5f4 2019-01-12 stsp }
2237 512f0d0e 2019-01-02 stsp
2238 8da9e5f4 2019-01-12 stsp static const struct got_error *
2239 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2240 8da9e5f4 2019-01-12 stsp {
2241 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2242 7a9df742 2019-01-08 stsp
2243 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2245 0584f854 2019-04-06 stsp
2246 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2247 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2248 9d31a1d8 2018-03-11 stsp }
2249 9d31a1d8 2018-03-11 stsp
2250 9d31a1d8 2018-03-11 stsp static const struct got_error *
2251 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2252 9d31a1d8 2018-03-11 stsp {
2253 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2254 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2255 8da9e5f4 2019-01-12 stsp char *path;
2256 9d31a1d8 2018-03-11 stsp
2257 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2258 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2259 0584f854 2019-04-06 stsp
2260 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2261 63c5ca5d 2019-08-24 stsp return NULL;
2262 63c5ca5d 2019-08-24 stsp
2263 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2264 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2265 8da9e5f4 2019-01-12 stsp == -1)
2266 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2267 9d31a1d8 2018-03-11 stsp
2268 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2269 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2270 8da9e5f4 2019-01-12 stsp else
2271 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2272 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2273 9d31a1d8 2018-03-11 stsp
2274 8da9e5f4 2019-01-12 stsp free(path);
2275 0cd1c46a 2019-03-11 stsp return err;
2276 0cd1c46a 2019-03-11 stsp }
2277 0cd1c46a 2019-03-11 stsp
2278 b2118c49 2020-07-28 stsp const struct got_error *
2279 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2280 b2118c49 2020-07-28 stsp {
2281 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2282 b2118c49 2020-07-28 stsp
2283 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2284 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2285 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2286 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2287 b2118c49 2020-07-28 stsp }
2288 b2118c49 2020-07-28 stsp
2289 b2118c49 2020-07-28 stsp return NULL;
2290 b2118c49 2020-07-28 stsp }
2291 b2118c49 2020-07-28 stsp
2292 818c7501 2019-07-11 stsp static const struct got_error *
2293 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2294 0cd1c46a 2019-03-11 stsp {
2295 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2296 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2297 0cd1c46a 2019-03-11 stsp
2298 517bab73 2019-03-11 stsp *refname = NULL;
2299 517bab73 2019-03-11 stsp
2300 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2301 b2118c49 2020-07-28 stsp if (err)
2302 b2118c49 2020-07-28 stsp return err;
2303 0cd1c46a 2019-03-11 stsp
2304 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2306 0647c563 2019-03-11 stsp *refname = NULL;
2307 0cd1c46a 2019-03-11 stsp }
2308 517bab73 2019-03-11 stsp free(uuidstr);
2309 517bab73 2019-03-11 stsp return err;
2310 517bab73 2019-03-11 stsp }
2311 517bab73 2019-03-11 stsp
2312 818c7501 2019-07-11 stsp const struct got_error *
2313 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2314 9587e6cc 2023-01-28 mark const char *prefix)
2315 9587e6cc 2023-01-28 mark {
2316 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2317 9587e6cc 2023-01-28 mark }
2318 9587e6cc 2023-01-28 mark
2319 9587e6cc 2023-01-28 mark const struct got_error *
2320 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2321 818c7501 2019-07-11 stsp {
2322 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2323 818c7501 2019-07-11 stsp }
2324 818c7501 2019-07-11 stsp
2325 818c7501 2019-07-11 stsp static const struct got_error *
2326 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2327 818c7501 2019-07-11 stsp {
2328 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2329 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2330 818c7501 2019-07-11 stsp }
2331 818c7501 2019-07-11 stsp
2332 818c7501 2019-07-11 stsp static const struct got_error *
2333 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2334 818c7501 2019-07-11 stsp {
2335 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2336 818c7501 2019-07-11 stsp }
2337 818c7501 2019-07-11 stsp
2338 818c7501 2019-07-11 stsp static const struct got_error *
2339 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2340 818c7501 2019-07-11 stsp {
2341 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2342 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2343 818c7501 2019-07-11 stsp }
2344 818c7501 2019-07-11 stsp
2345 818c7501 2019-07-11 stsp static const struct got_error *
2346 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2347 818c7501 2019-07-11 stsp {
2348 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2349 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2350 0ebf8283 2019-07-24 stsp }
2351 0ebf8283 2019-07-24 stsp
2352 0ebf8283 2019-07-24 stsp static const struct got_error *
2353 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2354 0ebf8283 2019-07-24 stsp {
2355 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2356 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2357 0ebf8283 2019-07-24 stsp }
2358 0ebf8283 2019-07-24 stsp
2359 0ebf8283 2019-07-24 stsp static const struct got_error *
2360 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2361 0ebf8283 2019-07-24 stsp {
2362 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2363 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2364 0ebf8283 2019-07-24 stsp }
2365 0ebf8283 2019-07-24 stsp
2366 0ebf8283 2019-07-24 stsp static const struct got_error *
2367 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2368 0ebf8283 2019-07-24 stsp {
2369 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2370 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2371 818c7501 2019-07-11 stsp }
2372 818c7501 2019-07-11 stsp
2373 0ebf8283 2019-07-24 stsp static const struct got_error *
2374 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2375 0ebf8283 2019-07-24 stsp {
2376 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2377 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2378 0ebf8283 2019-07-24 stsp }
2379 818c7501 2019-07-11 stsp
2380 0ebf8283 2019-07-24 stsp const struct got_error *
2381 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2382 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2383 0ebf8283 2019-07-24 stsp {
2384 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2385 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2386 0ebf8283 2019-07-24 stsp *path = NULL;
2387 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2388 0ebf8283 2019-07-24 stsp }
2389 0ebf8283 2019-07-24 stsp return NULL;
2390 f259c4c1 2021-09-24 stsp }
2391 f259c4c1 2021-09-24 stsp
2392 f259c4c1 2021-09-24 stsp static const struct got_error *
2393 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2394 f259c4c1 2021-09-24 stsp {
2395 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2396 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2397 0ebf8283 2019-07-24 stsp }
2398 0ebf8283 2019-07-24 stsp
2399 f259c4c1 2021-09-24 stsp static const struct got_error *
2400 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2401 f259c4c1 2021-09-24 stsp {
2402 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2403 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2404 f259c4c1 2021-09-24 stsp }
2405 f259c4c1 2021-09-24 stsp
2406 517bab73 2019-03-11 stsp /*
2407 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2408 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2409 517bab73 2019-03-11 stsp */
2410 517bab73 2019-03-11 stsp static const struct got_error *
2411 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2412 517bab73 2019-03-11 stsp {
2413 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2414 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2415 517bab73 2019-03-11 stsp char *refname;
2416 517bab73 2019-03-11 stsp
2417 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2418 517bab73 2019-03-11 stsp if (err)
2419 517bab73 2019-03-11 stsp return err;
2420 0cd1c46a 2019-03-11 stsp
2421 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2422 0cd1c46a 2019-03-11 stsp if (err)
2423 0cd1c46a 2019-03-11 stsp goto done;
2424 0cd1c46a 2019-03-11 stsp
2425 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2426 0cd1c46a 2019-03-11 stsp done:
2427 0cd1c46a 2019-03-11 stsp free(refname);
2428 0cd1c46a 2019-03-11 stsp if (ref)
2429 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2430 3e3a69f1 2019-07-25 stsp return err;
2431 3e3a69f1 2019-07-25 stsp }
2432 3e3a69f1 2019-07-25 stsp
2433 3e3a69f1 2019-07-25 stsp static const struct got_error *
2434 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2435 3e3a69f1 2019-07-25 stsp {
2436 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2437 3e3a69f1 2019-07-25 stsp
2438 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2439 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2440 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2441 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2442 3e3a69f1 2019-07-25 stsp }
2443 9d31a1d8 2018-03-11 stsp return err;
2444 9d31a1d8 2018-03-11 stsp }
2445 9d31a1d8 2018-03-11 stsp
2446 3e3a69f1 2019-07-25 stsp
2447 ebf99748 2019-05-09 stsp static const struct got_error *
2448 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2449 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2450 ebf99748 2019-05-09 stsp {
2451 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2452 ebf99748 2019-05-09 stsp FILE *index = NULL;
2453 0cd1c46a 2019-03-11 stsp
2454 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2455 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2456 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2457 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2458 ebf99748 2019-05-09 stsp
2459 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2460 3e3a69f1 2019-07-25 stsp if (err)
2461 ebf99748 2019-05-09 stsp goto done;
2462 ebf99748 2019-05-09 stsp
2463 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2464 ebf99748 2019-05-09 stsp if (index == NULL) {
2465 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2466 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2467 ebf99748 2019-05-09 stsp } else {
2468 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2469 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2470 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2471 ebf99748 2019-05-09 stsp }
2472 ebf99748 2019-05-09 stsp done:
2473 ebf99748 2019-05-09 stsp if (err) {
2474 ebf99748 2019-05-09 stsp free(*fileindex_path);
2475 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2476 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2477 ebf99748 2019-05-09 stsp *fileindex = NULL;
2478 ebf99748 2019-05-09 stsp }
2479 ebf99748 2019-05-09 stsp return err;
2480 ebf99748 2019-05-09 stsp }
2481 c932eeeb 2019-05-22 stsp
2482 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2483 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2484 c932eeeb 2019-05-22 stsp const char *path;
2485 c932eeeb 2019-05-22 stsp size_t path_len;
2486 c932eeeb 2019-05-22 stsp const char *entry_name;
2487 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2488 a484d721 2019-06-10 stsp void *progress_arg;
2489 c932eeeb 2019-05-22 stsp };
2490 ebf99748 2019-05-09 stsp
2491 c932eeeb 2019-05-22 stsp static const struct got_error *
2492 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2493 c932eeeb 2019-05-22 stsp {
2494 1ee397ad 2019-07-12 stsp const struct got_error *err;
2495 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2496 c932eeeb 2019-05-22 stsp
2497 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2498 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2499 c932eeeb 2019-05-22 stsp return NULL;
2500 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2501 102ff934 2019-06-11 stsp return NULL;
2502 102ff934 2019-06-11 stsp
2503 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2504 a769b60b 2021-06-27 stsp return NULL;
2505 a769b60b 2021-06-27 stsp
2506 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2507 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2508 c932eeeb 2019-05-22 stsp return NULL;
2509 c932eeeb 2019-05-22 stsp
2510 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2511 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2512 edd02c5e 2019-07-11 stsp ie->path);
2513 1ee397ad 2019-07-12 stsp if (err)
2514 1ee397ad 2019-07-12 stsp return err;
2515 1ee397ad 2019-07-12 stsp }
2516 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2517 c932eeeb 2019-05-22 stsp return NULL;
2518 a615e0e7 2020-12-16 stsp }
2519 a615e0e7 2020-12-16 stsp
2520 b0a0c9ed 2023-02-06 op /* Bump base commit ID of all files within an updated part of the work tree. */
2521 a615e0e7 2020-12-16 stsp static const struct got_error *
2522 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2523 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2524 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2525 a615e0e7 2020-12-16 stsp {
2526 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2527 a615e0e7 2020-12-16 stsp
2528 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2529 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2530 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2531 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2532 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2533 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2534 a615e0e7 2020-12-16 stsp
2535 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2536 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2537 9c6338c4 2019-06-02 stsp }
2538 9c6338c4 2019-06-02 stsp
2539 9c6338c4 2019-06-02 stsp static const struct got_error *
2540 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2541 9c6338c4 2019-06-02 stsp {
2542 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2543 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2544 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2545 867630bb 2020-01-17 stsp struct timespec timeout;
2546 9c6338c4 2019-06-02 stsp
2547 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2548 b90054ed 2022-11-01 stsp fileindex_path, "");
2549 9c6338c4 2019-06-02 stsp if (err)
2550 9c6338c4 2019-06-02 stsp goto done;
2551 9c6338c4 2019-06-02 stsp
2552 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2553 9c6338c4 2019-06-02 stsp if (err)
2554 9c6338c4 2019-06-02 stsp goto done;
2555 9c6338c4 2019-06-02 stsp
2556 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2557 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2558 9c6338c4 2019-06-02 stsp fileindex_path);
2559 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2560 9c6338c4 2019-06-02 stsp }
2561 867630bb 2020-01-17 stsp
2562 867630bb 2020-01-17 stsp /*
2563 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2564 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2565 867630bb 2020-01-17 stsp * was recorded in the file index.
2566 867630bb 2020-01-17 stsp */
2567 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2568 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2569 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2570 9c6338c4 2019-06-02 stsp done:
2571 9c6338c4 2019-06-02 stsp if (new_index)
2572 9c6338c4 2019-06-02 stsp fclose(new_index);
2573 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2574 9c6338c4 2019-06-02 stsp return err;
2575 c932eeeb 2019-05-22 stsp }
2576 6ced4176 2019-07-12 stsp
2577 6ced4176 2019-07-12 stsp static const struct got_error *
2578 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2579 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2580 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2581 a44927cc 2022-04-07 stsp struct got_repository *repo)
2582 6ced4176 2019-07-12 stsp {
2583 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2584 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2585 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2586 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2587 6ced4176 2019-07-12 stsp
2588 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2589 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2590 6ced4176 2019-07-12 stsp *tree_id = NULL;
2591 6ced4176 2019-07-12 stsp
2592 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2593 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2594 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2595 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2596 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2597 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2598 6ced4176 2019-07-12 stsp goto done;
2599 6ced4176 2019-07-12 stsp }
2600 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2601 a44927cc 2022-04-07 stsp worktree->path_prefix);
2602 6ced4176 2019-07-12 stsp if (err)
2603 6ced4176 2019-07-12 stsp goto done;
2604 6ced4176 2019-07-12 stsp return NULL;
2605 6ced4176 2019-07-12 stsp }
2606 6ced4176 2019-07-12 stsp
2607 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2608 6ced4176 2019-07-12 stsp
2609 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2610 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2611 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2612 6ced4176 2019-07-12 stsp goto done;
2613 6ced4176 2019-07-12 stsp }
2614 6ced4176 2019-07-12 stsp
2615 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2616 6ced4176 2019-07-12 stsp if (err)
2617 6ced4176 2019-07-12 stsp goto done;
2618 6ced4176 2019-07-12 stsp
2619 6ced4176 2019-07-12 stsp free(in_repo_path);
2620 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2621 6ced4176 2019-07-12 stsp
2622 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2623 6ced4176 2019-07-12 stsp if (err)
2624 6ced4176 2019-07-12 stsp goto done;
2625 c932eeeb 2019-05-22 stsp
2626 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2627 6ced4176 2019-07-12 stsp /* Check out a single file. */
2628 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2629 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2630 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2631 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2632 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2633 6ced4176 2019-07-12 stsp goto done;
2634 6ced4176 2019-07-12 stsp }
2635 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2636 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2637 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2638 6ced4176 2019-07-12 stsp goto done;
2639 6ced4176 2019-07-12 stsp }
2640 6ced4176 2019-07-12 stsp } else {
2641 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2642 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2643 6ced4176 2019-07-12 stsp if (err)
2644 6ced4176 2019-07-12 stsp return err;
2645 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2646 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2647 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2648 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2649 6ced4176 2019-07-12 stsp goto done;
2650 6ced4176 2019-07-12 stsp }
2651 6ced4176 2019-07-12 stsp }
2652 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2653 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2654 6ced4176 2019-07-12 stsp } else {
2655 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2656 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2657 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2658 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2659 6ced4176 2019-07-12 stsp goto done;
2660 6ced4176 2019-07-12 stsp }
2661 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2662 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2663 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2664 6ced4176 2019-07-12 stsp goto done;
2665 6ced4176 2019-07-12 stsp }
2666 6ced4176 2019-07-12 stsp }
2667 6ced4176 2019-07-12 stsp done:
2668 6ced4176 2019-07-12 stsp free(id);
2669 6ced4176 2019-07-12 stsp free(in_repo_path);
2670 6ced4176 2019-07-12 stsp if (err) {
2671 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2672 6ced4176 2019-07-12 stsp free(*tree_relpath);
2673 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2674 6ced4176 2019-07-12 stsp free(*tree_id);
2675 6ced4176 2019-07-12 stsp *tree_id = NULL;
2676 6ced4176 2019-07-12 stsp }
2677 07ed472d 2019-07-12 stsp return err;
2678 07ed472d 2019-07-12 stsp }
2679 07ed472d 2019-07-12 stsp
2680 07ed472d 2019-07-12 stsp static const struct got_error *
2681 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2682 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2683 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2684 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2685 07ed472d 2019-07-12 stsp {
2686 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2687 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2688 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2689 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2690 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2691 07ed472d 2019-07-12 stsp
2692 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2693 7f47418f 2019-12-20 stsp if (err) {
2694 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2695 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2696 7f47418f 2019-12-20 stsp goto done;
2697 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2698 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2699 7f47418f 2019-12-20 stsp if (err)
2700 7f47418f 2019-12-20 stsp return err;
2701 7f47418f 2019-12-20 stsp }
2702 07ed472d 2019-07-12 stsp
2703 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2704 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2705 07ed472d 2019-07-12 stsp if (err)
2706 07ed472d 2019-07-12 stsp goto done;
2707 07ed472d 2019-07-12 stsp
2708 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2709 07ed472d 2019-07-12 stsp if (err)
2710 07ed472d 2019-07-12 stsp goto done;
2711 07ed472d 2019-07-12 stsp
2712 07ed472d 2019-07-12 stsp if (entry_name &&
2713 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2714 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2715 07ed472d 2019-07-12 stsp goto done;
2716 07ed472d 2019-07-12 stsp }
2717 07ed472d 2019-07-12 stsp
2718 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2719 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2720 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2721 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2722 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2723 07ed472d 2019-07-12 stsp arg.repo = repo;
2724 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2725 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2726 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2727 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2728 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2729 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2730 07ed472d 2019-07-12 stsp done:
2731 07ed472d 2019-07-12 stsp if (tree)
2732 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2733 07ed472d 2019-07-12 stsp if (commit)
2734 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2735 6ced4176 2019-07-12 stsp return err;
2736 6ced4176 2019-07-12 stsp }
2737 6ced4176 2019-07-12 stsp
2738 9d31a1d8 2018-03-11 stsp const struct got_error *
2739 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2740 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2741 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2742 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2743 9d31a1d8 2018-03-11 stsp {
2744 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2745 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2746 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2747 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2748 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2749 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2750 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2751 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2752 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2753 f2ea84fa 2019-07-27 stsp int entry_type;
2754 f2ea84fa 2019-07-27 stsp char *relpath;
2755 f2ea84fa 2019-07-27 stsp char *entry_name;
2756 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2757 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2758 9d31a1d8 2018-03-11 stsp
2759 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2760 f2ea84fa 2019-07-27 stsp
2761 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2762 9d31a1d8 2018-03-11 stsp if (err)
2763 9d31a1d8 2018-03-11 stsp return err;
2764 a44927cc 2022-04-07 stsp
2765 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2766 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2767 a44927cc 2022-04-07 stsp if (err)
2768 a44927cc 2022-04-07 stsp goto done;
2769 93a30277 2018-12-24 stsp
2770 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2771 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2772 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2773 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2774 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2775 f2ea84fa 2019-07-27 stsp goto done;
2776 f2ea84fa 2019-07-27 stsp }
2777 6ced4176 2019-07-12 stsp
2778 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2780 a44927cc 2022-04-07 stsp worktree, repo);
2781 f2ea84fa 2019-07-27 stsp if (err) {
2782 f2ea84fa 2019-07-27 stsp free(tpd);
2783 6ced4176 2019-07-12 stsp goto done;
2784 6ced4176 2019-07-12 stsp }
2785 f2ea84fa 2019-07-27 stsp
2786 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2787 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2788 f2ea84fa 2019-07-27 stsp if (err) {
2789 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2790 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2791 f2ea84fa 2019-07-27 stsp free(tpd);
2792 f2ea84fa 2019-07-27 stsp goto done;
2793 f2ea84fa 2019-07-27 stsp }
2794 f2ea84fa 2019-07-27 stsp } else
2795 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2796 f2ea84fa 2019-07-27 stsp
2797 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2798 6ced4176 2019-07-12 stsp }
2799 6ced4176 2019-07-12 stsp
2800 51514078 2018-12-25 stsp /*
2801 51514078 2018-12-25 stsp * Read the file index.
2802 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2803 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2804 51514078 2018-12-25 stsp */
2805 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2806 8da9e5f4 2019-01-12 stsp if (err)
2807 c4cdcb68 2019-04-03 stsp goto done;
2808 c4cdcb68 2019-04-03 stsp
2809 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2810 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2811 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2812 f2ea84fa 2019-07-27 stsp
2813 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2814 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2815 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2816 f2ea84fa 2019-07-27 stsp if (err)
2817 f2ea84fa 2019-07-27 stsp break;
2818 f2ea84fa 2019-07-27 stsp
2819 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2820 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2821 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2822 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2823 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2824 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2825 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2826 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2827 f2ea84fa 2019-07-27 stsp if (err)
2828 f2ea84fa 2019-07-27 stsp break;
2829 f2ea84fa 2019-07-27 stsp
2830 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2831 711d9cd9 2019-07-12 stsp }
2832 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2833 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2834 af12c6b9 2019-06-04 stsp err = sync_err;
2835 9d31a1d8 2018-03-11 stsp done:
2836 9c6338c4 2019-06-02 stsp free(fileindex_path);
2837 edfa7d7f 2018-09-11 stsp if (tree)
2838 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2839 9d31a1d8 2018-03-11 stsp if (commit)
2840 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2841 5ade8233 2019-07-12 stsp if (fileindex)
2842 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2843 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2844 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2845 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2846 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2847 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2848 f2ea84fa 2019-07-27 stsp free(tpd);
2849 f2ea84fa 2019-07-27 stsp }
2850 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2851 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2852 9d31a1d8 2018-03-11 stsp err = unlockerr;
2853 f8d1f275 2019-02-04 stsp return err;
2854 f8d1f275 2019-02-04 stsp }
2855 f8d1f275 2019-02-04 stsp
2856 9a298e5c 2023-03-10 stsp static const struct got_error *
2857 9a298e5c 2023-03-10 stsp add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2858 9a298e5c 2023-03-10 stsp struct got_fileindex_entry *ie, const char *ondisk_path,
2859 9a298e5c 2023-03-10 stsp const char *path2, struct got_blob_object *blob2, mode_t mode2,
2860 9a298e5c 2023-03-10 stsp int restoring_missing_file, int reverting_versioned_file,
2861 9a298e5c 2023-03-10 stsp int path_is_unversioned, int allow_bad_symlinks,
2862 9a298e5c 2023-03-10 stsp struct got_repository *repo,
2863 9a298e5c 2023-03-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2864 9a298e5c 2023-03-10 stsp {
2865 9a298e5c 2023-03-10 stsp const struct got_error *err = NULL;
2866 9a298e5c 2023-03-10 stsp int is_bad_symlink = 0;
2867 9a298e5c 2023-03-10 stsp
2868 9a298e5c 2023-03-10 stsp if (S_ISLNK(mode2)) {
2869 9a298e5c 2023-03-10 stsp err = install_symlink(&is_bad_symlink,
2870 9a298e5c 2023-03-10 stsp worktree, ondisk_path, path2, blob2,
2871 9a298e5c 2023-03-10 stsp restoring_missing_file,
2872 9a298e5c 2023-03-10 stsp reverting_versioned_file,
2873 9a298e5c 2023-03-10 stsp path_is_unversioned, allow_bad_symlinks,
2874 9a298e5c 2023-03-10 stsp repo, progress_cb, progress_arg);
2875 9a298e5c 2023-03-10 stsp } else {
2876 9a298e5c 2023-03-10 stsp err = install_blob(worktree, ondisk_path, path2,
2877 9a298e5c 2023-03-10 stsp mode2, GOT_DEFAULT_FILE_MODE, blob2,
2878 9a298e5c 2023-03-10 stsp restoring_missing_file, reverting_versioned_file, 0,
2879 9a298e5c 2023-03-10 stsp path_is_unversioned, repo, progress_cb, progress_arg);
2880 9a298e5c 2023-03-10 stsp }
2881 9a298e5c 2023-03-10 stsp if (err)
2882 9a298e5c 2023-03-10 stsp return err;
2883 9a298e5c 2023-03-10 stsp if (ie == NULL) {
2884 9a298e5c 2023-03-10 stsp /* Adding an unversioned file. */
2885 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_alloc(&ie, path2);
2886 9a298e5c 2023-03-10 stsp if (err)
2887 9a298e5c 2023-03-10 stsp return err;
2888 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2889 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, NULL, NULL, 1);
2890 9a298e5c 2023-03-10 stsp if (err) {
2891 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2892 9a298e5c 2023-03-10 stsp return err;
2893 9a298e5c 2023-03-10 stsp }
2894 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_add(fileindex, ie);
2895 9a298e5c 2023-03-10 stsp if (err) {
2896 9a298e5c 2023-03-10 stsp got_fileindex_entry_free(ie);
2897 9a298e5c 2023-03-10 stsp return err;
2898 9a298e5c 2023-03-10 stsp }
2899 9a298e5c 2023-03-10 stsp } else {
2900 9a298e5c 2023-03-10 stsp /* Re-adding a locally deleted file. */
2901 9a298e5c 2023-03-10 stsp err = got_fileindex_entry_update(ie,
2902 9a298e5c 2023-03-10 stsp worktree->root_fd, path2, ie->blob_sha1,
2903 9a298e5c 2023-03-10 stsp worktree->base_commit_id->sha1, 0);
2904 9a298e5c 2023-03-10 stsp if (err)
2905 9a298e5c 2023-03-10 stsp return err;
2906 9a298e5c 2023-03-10 stsp }
2907 9a298e5c 2023-03-10 stsp
2908 9a298e5c 2023-03-10 stsp if (is_bad_symlink) {
2909 9a298e5c 2023-03-10 stsp got_fileindex_entry_filetype_set(ie,
2910 9a298e5c 2023-03-10 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2911 9a298e5c 2023-03-10 stsp }
2912 9a298e5c 2023-03-10 stsp
2913 9a298e5c 2023-03-10 stsp return NULL;
2914 9a298e5c 2023-03-10 stsp }
2915 9a298e5c 2023-03-10 stsp
2916 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2917 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2918 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2919 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2920 234035bc 2019-06-01 stsp void *progress_arg;
2921 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2922 234035bc 2019-06-01 stsp void *cancel_arg;
2923 f69721c3 2019-10-21 stsp const char *label_orig;
2924 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2925 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2926 234035bc 2019-06-01 stsp };
2927 234035bc 2019-06-01 stsp
2928 234035bc 2019-06-01 stsp static const struct got_error *
2929 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2930 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2931 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2932 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2933 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2934 234035bc 2019-06-01 stsp {
2935 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2936 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2937 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2938 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2939 234035bc 2019-06-01 stsp struct stat sb;
2940 234035bc 2019-06-01 stsp unsigned char status;
2941 234035bc 2019-06-01 stsp int local_changes_subsumed;
2942 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2943 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2944 234035bc 2019-06-01 stsp
2945 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2946 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2947 d6c87207 2019-08-02 stsp strlen(path2));
2948 1ee397ad 2019-07-12 stsp if (ie == NULL)
2949 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2950 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2951 234035bc 2019-06-01 stsp
2952 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2953 234035bc 2019-06-01 stsp path2) == -1)
2954 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2955 234035bc 2019-06-01 stsp
2956 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2957 7f91a133 2019-12-13 stsp repo);
2958 234035bc 2019-06-01 stsp if (err)
2959 234035bc 2019-06-01 stsp goto done;
2960 234035bc 2019-06-01 stsp
2961 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2962 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2963 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2964 234035bc 2019-06-01 stsp goto done;
2965 234035bc 2019-06-01 stsp }
2966 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2967 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2968 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2969 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2970 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2971 234035bc 2019-06-01 stsp goto done;
2972 234035bc 2019-06-01 stsp }
2973 234035bc 2019-06-01 stsp
2974 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2975 36bf999c 2020-07-23 stsp char *link_target2;
2976 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2977 36bf999c 2020-07-23 stsp if (err)
2978 36bf999c 2020-07-23 stsp goto done;
2979 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2980 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2981 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2982 36bf999c 2020-07-23 stsp free(link_target2);
2983 af57b12a 2020-07-23 stsp } else {
2984 1af628f4 2021-06-03 stsp int fd;
2985 1af628f4 2021-06-03 stsp
2986 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2987 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2988 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2989 1af628f4 2021-06-03 stsp goto done;
2990 1af628f4 2021-06-03 stsp }
2991 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2992 1af628f4 2021-06-03 stsp f_orig, blob1);
2993 1af628f4 2021-06-03 stsp if (err)
2994 1af628f4 2021-06-03 stsp goto done;
2995 1af628f4 2021-06-03 stsp
2996 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2997 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2998 1af628f4 2021-06-03 stsp goto done;
2999 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3000 54d5be07 2021-06-03 stsp f_deriv2, blob2);
3001 1af628f4 2021-06-03 stsp if (err)
3002 1af628f4 2021-06-03 stsp goto done;
3003 1af628f4 2021-06-03 stsp
3004 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
3005 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3006 1af628f4 2021-06-03 stsp if (fd == -1) {
3007 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
3008 1af628f4 2021-06-03 stsp ondisk_path);
3009 1af628f4 2021-06-03 stsp goto done;
3010 1af628f4 2021-06-03 stsp }
3011 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
3012 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
3013 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
3014 1af628f4 2021-06-03 stsp ondisk_path);
3015 1af628f4 2021-06-03 stsp close(fd);
3016 1af628f4 2021-06-03 stsp goto done;
3017 1af628f4 2021-06-03 stsp }
3018 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
3019 1af628f4 2021-06-03 stsp if (err)
3020 1af628f4 2021-06-03 stsp goto done;
3021 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
3022 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3023 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
3024 1af628f4 2021-06-03 stsp goto done;
3025 1af628f4 2021-06-03 stsp }
3026 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
3027 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3028 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
3029 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
3030 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
3031 af57b12a 2020-07-23 stsp }
3032 234035bc 2019-06-01 stsp } else if (blob1) {
3033 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
3034 d6c87207 2019-08-02 stsp strlen(path1));
3035 1ee397ad 2019-07-12 stsp if (ie == NULL)
3036 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
3037 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
3038 2b92fad7 2019-06-02 stsp
3039 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3040 2b92fad7 2019-06-02 stsp path1) == -1)
3041 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
3042 2b92fad7 2019-06-02 stsp
3043 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3044 7f91a133 2019-12-13 stsp repo);
3045 2b92fad7 2019-06-02 stsp if (err)
3046 2b92fad7 2019-06-02 stsp goto done;
3047 2b92fad7 2019-06-02 stsp
3048 2b92fad7 2019-06-02 stsp switch (status) {
3049 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
3050 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3051 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3052 1ee397ad 2019-07-12 stsp if (err)
3053 1ee397ad 2019-07-12 stsp goto done;
3054 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
3055 2b92fad7 2019-06-02 stsp if (err)
3056 2b92fad7 2019-06-02 stsp goto done;
3057 2b92fad7 2019-06-02 stsp if (ie)
3058 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3059 2b92fad7 2019-06-02 stsp break;
3060 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
3061 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
3062 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3063 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3064 1ee397ad 2019-07-12 stsp if (err)
3065 1ee397ad 2019-07-12 stsp goto done;
3066 2b92fad7 2019-06-02 stsp if (ie)
3067 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3068 2b92fad7 2019-06-02 stsp break;
3069 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3070 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3071 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3072 72840534 2022-01-19 stsp off_t blob1_size;
3073 0a22ca1a 2020-09-23 stsp /*
3074 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3075 0a22ca1a 2020-09-23 stsp * exists in the repository.
3076 0a22ca1a 2020-09-23 stsp */
3077 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
3078 72840534 2022-01-19 stsp &blob1_size, path1);
3079 0a22ca1a 2020-09-23 stsp if (err)
3080 0a22ca1a 2020-09-23 stsp goto done;
3081 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3082 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3083 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3084 0a22ca1a 2020-09-23 stsp if (err)
3085 0a22ca1a 2020-09-23 stsp goto done;
3086 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3087 0a22ca1a 2020-09-23 stsp path1);
3088 0a22ca1a 2020-09-23 stsp if (err)
3089 0a22ca1a 2020-09-23 stsp goto done;
3090 0a22ca1a 2020-09-23 stsp if (ie)
3091 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3092 0a22ca1a 2020-09-23 stsp ie);
3093 0a22ca1a 2020-09-23 stsp } else {
3094 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3095 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3096 0a22ca1a 2020-09-23 stsp }
3097 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3098 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3099 0a22ca1a 2020-09-23 stsp free(id);
3100 0a22ca1a 2020-09-23 stsp if (err)
3101 0a22ca1a 2020-09-23 stsp goto done;
3102 0a22ca1a 2020-09-23 stsp break;
3103 0a22ca1a 2020-09-23 stsp }
3104 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3105 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3106 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3107 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3108 1ee397ad 2019-07-12 stsp if (err)
3109 1ee397ad 2019-07-12 stsp goto done;
3110 2b92fad7 2019-06-02 stsp break;
3111 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3112 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3113 1ee397ad 2019-07-12 stsp if (err)
3114 1ee397ad 2019-07-12 stsp goto done;
3115 2b92fad7 2019-06-02 stsp break;
3116 2b92fad7 2019-06-02 stsp default:
3117 2b92fad7 2019-06-02 stsp break;
3118 2b92fad7 2019-06-02 stsp }
3119 234035bc 2019-06-01 stsp } else if (blob2) {
3120 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3121 234035bc 2019-06-01 stsp path2) == -1)
3122 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3123 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3124 d6c87207 2019-08-02 stsp strlen(path2));
3125 234035bc 2019-06-01 stsp if (ie) {
3126 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3127 7f91a133 2019-12-13 stsp -1, NULL, repo);
3128 234035bc 2019-06-01 stsp if (err)
3129 234035bc 2019-06-01 stsp goto done;
3130 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3131 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3132 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3133 9a298e5c 2023-03-10 stsp status != GOT_STATUS_ADD &&
3134 9a298e5c 2023-03-10 stsp status != GOT_STATUS_DELETE) {
3135 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3136 1ee397ad 2019-07-12 stsp status, path2);
3137 234035bc 2019-06-01 stsp goto done;
3138 234035bc 2019-06-01 stsp }
3139 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3140 36bf999c 2020-07-23 stsp char *link_target2;
3141 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3142 36bf999c 2020-07-23 stsp blob2);
3143 36bf999c 2020-07-23 stsp if (err)
3144 36bf999c 2020-07-23 stsp goto done;
3145 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3146 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3147 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3148 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3149 36bf999c 2020-07-23 stsp free(link_target2);
3150 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3151 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3152 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3153 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3154 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3155 526a746f 2020-07-23 stsp a->progress_arg);
3156 9a298e5c 2023-03-10 stsp } else if (status != GOT_STATUS_DELETE) {
3157 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3158 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3159 526a746f 2020-07-23 stsp }
3160 dfe9fba0 2020-07-23 stsp if (err)
3161 dfe9fba0 2020-07-23 stsp goto done;
3162 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3163 9a298e5c 2023-03-10 stsp /* Re-add file with content from new blob. */
3164 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, ie,
3165 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3166 9a298e5c 2023-03-10 stsp 0, 0, 0, a->allow_bad_symlinks,
3167 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3168 234035bc 2019-06-01 stsp if (err)
3169 234035bc 2019-06-01 stsp goto done;
3170 234035bc 2019-06-01 stsp }
3171 234035bc 2019-06-01 stsp } else {
3172 9a298e5c 2023-03-10 stsp err = add_file(a->worktree, a->fileindex, NULL,
3173 9a298e5c 2023-03-10 stsp ondisk_path, path2, blob2, mode2,
3174 9a298e5c 2023-03-10 stsp 0, 0, 1, a->allow_bad_symlinks,
3175 9a298e5c 2023-03-10 stsp repo, a->progress_cb, a->progress_arg);
3176 234035bc 2019-06-01 stsp if (err)
3177 2b92fad7 2019-06-02 stsp goto done;
3178 234035bc 2019-06-01 stsp }
3179 234035bc 2019-06-01 stsp }
3180 234035bc 2019-06-01 stsp done:
3181 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3182 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3183 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3184 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3185 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3186 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3187 1af628f4 2021-06-03 stsp free(id_str);
3188 54d5be07 2021-06-03 stsp free(label_deriv2);
3189 234035bc 2019-06-01 stsp free(ondisk_path);
3190 234035bc 2019-06-01 stsp return err;
3191 234035bc 2019-06-01 stsp }
3192 234035bc 2019-06-01 stsp
3193 69de9dd4 2021-09-03 stsp static const struct got_error *
3194 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3195 69de9dd4 2021-09-03 stsp {
3196 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3197 69de9dd4 2021-09-03 stsp
3198 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3199 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3200 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3201 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3202 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3203 69de9dd4 2021-09-03 stsp
3204 69de9dd4 2021-09-03 stsp return NULL;
3205 69de9dd4 2021-09-03 stsp }
3206 69de9dd4 2021-09-03 stsp
3207 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3208 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3209 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3210 234035bc 2019-06-01 stsp struct got_repository *repo;
3211 234035bc 2019-06-01 stsp };
3212 234035bc 2019-06-01 stsp
3213 234035bc 2019-06-01 stsp static const struct got_error *
3214 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3215 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3216 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3217 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3218 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3219 234035bc 2019-06-01 stsp {
3220 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3221 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3222 234035bc 2019-06-01 stsp unsigned char status;
3223 234035bc 2019-06-01 stsp struct stat sb;
3224 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3225 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3226 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3227 234035bc 2019-06-01 stsp char *ondisk_path;
3228 234035bc 2019-06-01 stsp
3229 69de9dd4 2021-09-03 stsp if (id == NULL)
3230 69de9dd4 2021-09-03 stsp return NULL;
3231 234035bc 2019-06-01 stsp
3232 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3233 69de9dd4 2021-09-03 stsp if (ie == NULL)
3234 69de9dd4 2021-09-03 stsp return NULL;
3235 69de9dd4 2021-09-03 stsp
3236 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3237 234035bc 2019-06-01 stsp == -1)
3238 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3239 234035bc 2019-06-01 stsp
3240 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3241 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3242 5546d466 2021-09-02 stsp free(ondisk_path);
3243 234035bc 2019-06-01 stsp if (err)
3244 234035bc 2019-06-01 stsp return err;
3245 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3246 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3247 234035bc 2019-06-01 stsp
3248 234035bc 2019-06-01 stsp return NULL;
3249 234035bc 2019-06-01 stsp }
3250 234035bc 2019-06-01 stsp
3251 818c7501 2019-07-11 stsp static const struct got_error *
3252 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3253 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3254 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3255 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3256 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3257 234035bc 2019-06-01 stsp {
3258 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3259 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3260 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3261 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3262 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3263 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3264 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3265 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3266 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3267 234035bc 2019-06-01 stsp
3268 03415a1a 2019-06-02 stsp if (commit_id1) {
3269 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3270 a44927cc 2022-04-07 stsp if (err)
3271 a44927cc 2022-04-07 stsp goto done;
3272 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3273 03415a1a 2019-06-02 stsp worktree->path_prefix);
3274 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3275 03415a1a 2019-06-02 stsp goto done;
3276 69d57f3d 2020-07-31 stsp }
3277 69d57f3d 2020-07-31 stsp if (tree_id1) {
3278 69d57f3d 2020-07-31 stsp char *id_str;
3279 03415a1a 2019-06-02 stsp
3280 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3281 03415a1a 2019-06-02 stsp if (err)
3282 03415a1a 2019-06-02 stsp goto done;
3283 f69721c3 2019-10-21 stsp
3284 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3285 f69721c3 2019-10-21 stsp if (err)
3286 f69721c3 2019-10-21 stsp goto done;
3287 f69721c3 2019-10-21 stsp
3288 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3289 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3290 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3291 f69721c3 2019-10-21 stsp free(id_str);
3292 f69721c3 2019-10-21 stsp goto done;
3293 f69721c3 2019-10-21 stsp }
3294 f69721c3 2019-10-21 stsp free(id_str);
3295 b72706c3 2022-06-01 stsp
3296 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3297 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3298 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3299 b72706c3 2022-06-01 stsp goto done;
3300 b72706c3 2022-06-01 stsp }
3301 03415a1a 2019-06-02 stsp }
3302 234035bc 2019-06-01 stsp
3303 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3304 a44927cc 2022-04-07 stsp if (err)
3305 a44927cc 2022-04-07 stsp goto done;
3306 a44927cc 2022-04-07 stsp
3307 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3308 234035bc 2019-06-01 stsp worktree->path_prefix);
3309 234035bc 2019-06-01 stsp if (err)
3310 234035bc 2019-06-01 stsp goto done;
3311 234035bc 2019-06-01 stsp
3312 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3313 234035bc 2019-06-01 stsp if (err)
3314 234035bc 2019-06-01 stsp goto done;
3315 234035bc 2019-06-01 stsp
3316 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3317 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3318 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3319 f9d37699 2022-06-28 stsp goto done;
3320 f9d37699 2022-06-28 stsp }
3321 f9d37699 2022-06-28 stsp
3322 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3323 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3324 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3325 b72706c3 2022-06-01 stsp goto done;
3326 b72706c3 2022-06-01 stsp }
3327 b72706c3 2022-06-01 stsp
3328 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3329 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3330 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3331 f9d37699 2022-06-28 stsp goto done;
3332 f9d37699 2022-06-28 stsp }
3333 f9d37699 2022-06-28 stsp
3334 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3335 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3336 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3337 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3338 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3339 69de9dd4 2021-09-03 stsp if (err)
3340 69de9dd4 2021-09-03 stsp goto done;
3341 69de9dd4 2021-09-03 stsp
3342 234035bc 2019-06-01 stsp arg.worktree = worktree;
3343 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3344 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3345 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3346 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3347 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3348 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3349 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3350 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3351 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3352 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3353 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3354 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3355 af12c6b9 2019-06-04 stsp err = sync_err;
3356 234035bc 2019-06-01 stsp done:
3357 a44927cc 2022-04-07 stsp if (commit1)
3358 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3359 a44927cc 2022-04-07 stsp if (commit2)
3360 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3361 234035bc 2019-06-01 stsp if (tree1)
3362 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3363 234035bc 2019-06-01 stsp if (tree2)
3364 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3365 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3366 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3367 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3368 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3369 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3370 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3371 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3372 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3373 f69721c3 2019-10-21 stsp free(label_orig);
3374 818c7501 2019-07-11 stsp return err;
3375 818c7501 2019-07-11 stsp }
3376 234035bc 2019-06-01 stsp
3377 818c7501 2019-07-11 stsp const struct got_error *
3378 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3379 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3380 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3381 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3382 818c7501 2019-07-11 stsp {
3383 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3384 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3385 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3386 818c7501 2019-07-11 stsp
3387 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3388 818c7501 2019-07-11 stsp if (err)
3389 818c7501 2019-07-11 stsp return err;
3390 818c7501 2019-07-11 stsp
3391 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3392 818c7501 2019-07-11 stsp if (err)
3393 818c7501 2019-07-11 stsp goto done;
3394 818c7501 2019-07-11 stsp
3395 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3396 69de9dd4 2021-09-03 stsp worktree);
3397 818c7501 2019-07-11 stsp if (err)
3398 818c7501 2019-07-11 stsp goto done;
3399 818c7501 2019-07-11 stsp
3400 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3401 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3402 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3403 818c7501 2019-07-11 stsp done:
3404 818c7501 2019-07-11 stsp if (fileindex)
3405 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3406 818c7501 2019-07-11 stsp free(fileindex_path);
3407 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3408 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3409 234035bc 2019-06-01 stsp err = unlockerr;
3410 234035bc 2019-06-01 stsp return err;
3411 234035bc 2019-06-01 stsp }
3412 234035bc 2019-06-01 stsp
3413 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3414 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3415 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3416 927df6b7 2019-02-10 stsp const char *status_path;
3417 927df6b7 2019-02-10 stsp size_t status_path_len;
3418 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3419 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3420 f8d1f275 2019-02-04 stsp void *status_arg;
3421 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3422 f8d1f275 2019-02-04 stsp void *cancel_arg;
3423 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3424 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3425 f2a9dc41 2019-12-13 tracey int report_unchanged;
3426 3143d852 2020-06-25 stsp int no_ignores;
3427 f8d1f275 2019-02-04 stsp };
3428 88d0e355 2019-08-03 stsp
3429 f8d1f275 2019-02-04 stsp static const struct got_error *
3430 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3431 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3432 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3433 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3434 927df6b7 2019-02-10 stsp {
3435 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3436 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3437 2c4740ad 2023-02-12 mark unsigned char staged_status;
3438 927df6b7 2019-02-10 stsp struct stat sb;
3439 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3440 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3441 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3442 927df6b7 2019-02-10 stsp
3443 2c4740ad 2023-02-12 mark staged_status = get_staged_status(ie);
3444 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3445 98eaaa12 2019-08-03 stsp if (err)
3446 98eaaa12 2019-08-03 stsp return err;
3447 98eaaa12 2019-08-03 stsp
3448 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3449 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3450 98eaaa12 2019-08-03 stsp return NULL;
3451 98eaaa12 2019-08-03 stsp
3452 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
3453 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3454 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
3455 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3456 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3457 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3458 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3459 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
3460 98eaaa12 2019-08-03 stsp }
3461 98eaaa12 2019-08-03 stsp
3462 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3463 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3464 927df6b7 2019-02-10 stsp }
3465 927df6b7 2019-02-10 stsp
3466 927df6b7 2019-02-10 stsp static const struct got_error *
3467 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3468 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3469 f8d1f275 2019-02-04 stsp {
3470 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3471 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3472 f8d1f275 2019-02-04 stsp char *abspath;
3473 f8d1f275 2019-02-04 stsp
3474 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3475 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3476 0584f854 2019-04-06 stsp
3477 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3478 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3479 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3480 927df6b7 2019-02-10 stsp return NULL;
3481 927df6b7 2019-02-10 stsp
3482 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3483 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3484 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3485 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3486 f8d1f275 2019-02-04 stsp } else {
3487 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3488 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3489 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3490 f8d1f275 2019-02-04 stsp }
3491 f8d1f275 2019-02-04 stsp
3492 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3493 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3494 f8d1f275 2019-02-04 stsp free(abspath);
3495 9d31a1d8 2018-03-11 stsp return err;
3496 9d31a1d8 2018-03-11 stsp }
3497 f8d1f275 2019-02-04 stsp
3498 f8d1f275 2019-02-04 stsp static const struct got_error *
3499 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3500 f8d1f275 2019-02-04 stsp {
3501 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3502 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3503 2ec1f75b 2019-03-26 stsp unsigned char status;
3504 927df6b7 2019-02-10 stsp
3505 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3507 0584f854 2019-04-06 stsp
3508 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3509 927df6b7 2019-02-10 stsp return NULL;
3510 927df6b7 2019-02-10 stsp
3511 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&blob_id, ie);
3512 b4b2adf5 2023-02-09 op got_fileindex_entry_get_commit_id(&commit_id, ie);
3513 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3514 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3515 2ec1f75b 2019-03-26 stsp else
3516 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3517 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3518 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3519 6841da00 2019-08-08 stsp }
3520 6841da00 2019-08-08 stsp
3521 336075a4 2022-06-25 op static void
3522 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3523 6841da00 2019-08-08 stsp {
3524 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3525 6841da00 2019-08-08 stsp
3526 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3527 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3528 d8bacb93 2023-01-10 mark
3529 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3530 6841da00 2019-08-08 stsp }
3531 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3532 6841da00 2019-08-08 stsp }
3533 6841da00 2019-08-08 stsp
3534 6841da00 2019-08-08 stsp static const struct got_error *
3535 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3536 6841da00 2019-08-08 stsp {
3537 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3538 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3539 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3540 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3541 6841da00 2019-08-08 stsp size_t linesize = 0;
3542 6841da00 2019-08-08 stsp ssize_t linelen;
3543 6841da00 2019-08-08 stsp
3544 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3545 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3546 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3547 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3548 6841da00 2019-08-08 stsp
3549 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3550 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3551 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3552 bd8de430 2019-10-04 stsp
3553 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3554 bd8de430 2019-10-04 stsp if (line[0] == '#')
3555 bd8de430 2019-10-04 stsp continue;
3556 bd8de430 2019-10-04 stsp
3557 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3558 bd8de430 2019-10-04 stsp if (line[0] == '!')
3559 bd8de430 2019-10-04 stsp continue;
3560 bd8de430 2019-10-04 stsp
3561 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3562 6841da00 2019-08-08 stsp line) == -1) {
3563 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3564 6841da00 2019-08-08 stsp goto done;
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3567 6841da00 2019-08-08 stsp if (err)
3568 6841da00 2019-08-08 stsp goto done;
3569 6841da00 2019-08-08 stsp }
3570 6841da00 2019-08-08 stsp if (ferror(f)) {
3571 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3572 6841da00 2019-08-08 stsp goto done;
3573 6841da00 2019-08-08 stsp }
3574 6841da00 2019-08-08 stsp
3575 6841da00 2019-08-08 stsp dirpath = strdup(path);
3576 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3577 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3578 6841da00 2019-08-08 stsp goto done;
3579 6841da00 2019-08-08 stsp }
3580 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3581 6841da00 2019-08-08 stsp done:
3582 6841da00 2019-08-08 stsp free(line);
3583 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3584 6841da00 2019-08-08 stsp free(dirpath);
3585 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3586 6841da00 2019-08-08 stsp }
3587 6841da00 2019-08-08 stsp return err;
3588 249b637c 2023-02-20 stsp }
3589 249b637c 2023-02-20 stsp
3590 249b637c 2023-02-20 stsp static int
3591 249b637c 2023-02-20 stsp match_path(const char *pattern, size_t pattern_len, const char *path,
3592 249b637c 2023-02-20 stsp int flags)
3593 249b637c 2023-02-20 stsp {
3594 249b637c 2023-02-20 stsp char buf[PATH_MAX];
3595 249b637c 2023-02-20 stsp
3596 249b637c 2023-02-20 stsp /*
3597 249b637c 2023-02-20 stsp * Trailing slashes signify directories.
3598 249b637c 2023-02-20 stsp * Append a * to make such patterns conform to fnmatch rules.
3599 249b637c 2023-02-20 stsp */
3600 249b637c 2023-02-20 stsp if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3601 249b637c 2023-02-20 stsp if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3602 249b637c 2023-02-20 stsp return FNM_NOMATCH; /* XXX */
3603 249b637c 2023-02-20 stsp
3604 249b637c 2023-02-20 stsp return fnmatch(buf, path, flags);
3605 249b637c 2023-02-20 stsp }
3606 249b637c 2023-02-20 stsp
3607 249b637c 2023-02-20 stsp return fnmatch(pattern, path, flags);
3608 6841da00 2019-08-08 stsp }
3609 6841da00 2019-08-08 stsp
3610 336075a4 2022-06-25 op static int
3611 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3612 6841da00 2019-08-08 stsp {
3613 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3614 bd8de430 2019-10-04 stsp
3615 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3616 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3617 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3618 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3619 bd8de430 2019-10-04 stsp
3620 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3621 249b637c 2023-02-20 stsp const char *p;
3622 6841da00 2019-08-08 stsp
3623 249b637c 2023-02-20 stsp if (pi->path_len < 3 ||
3624 249b637c 2023-02-20 stsp strncmp(pi->path, "**/", 3) != 0)
3625 bd8de430 2019-10-04 stsp continue;
3626 bd8de430 2019-10-04 stsp p = path;
3627 bd8de430 2019-10-04 stsp while (*p) {
3628 249b637c 2023-02-20 stsp if (match_path(pi->path + 3,
3629 249b637c 2023-02-20 stsp pi->path_len - 3, p,
3630 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3631 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3632 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3633 bd8de430 2019-10-04 stsp p++;
3634 bd8de430 2019-10-04 stsp while (*p == '/')
3635 bd8de430 2019-10-04 stsp p++;
3636 bd8de430 2019-10-04 stsp continue;
3637 bd8de430 2019-10-04 stsp }
3638 bd8de430 2019-10-04 stsp return 1;
3639 bd8de430 2019-10-04 stsp }
3640 bd8de430 2019-10-04 stsp }
3641 bd8de430 2019-10-04 stsp }
3642 bd8de430 2019-10-04 stsp
3643 6841da00 2019-08-08 stsp /*
3644 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3645 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3646 6841da00 2019-08-08 stsp * ignores backwards.
3647 6841da00 2019-08-08 stsp */
3648 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3649 6841da00 2019-08-08 stsp while (pe) {
3650 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3651 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3652 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3653 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3654 249b637c 2023-02-20 stsp int flags = FNM_LEADING_DIR;
3655 249b637c 2023-02-20 stsp if (strstr(pi->path, "/**/") == NULL)
3656 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3657 249b637c 2023-02-20 stsp if (match_path(pi->path, pi->path_len,
3658 249b637c 2023-02-20 stsp path, flags))
3659 6841da00 2019-08-08 stsp continue;
3660 6841da00 2019-08-08 stsp return 1;
3661 6841da00 2019-08-08 stsp }
3662 6841da00 2019-08-08 stsp }
3663 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3664 6841da00 2019-08-08 stsp }
3665 6841da00 2019-08-08 stsp
3666 6841da00 2019-08-08 stsp return 0;
3667 6841da00 2019-08-08 stsp }
3668 6841da00 2019-08-08 stsp
3669 6841da00 2019-08-08 stsp static const struct got_error *
3670 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3671 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3672 6841da00 2019-08-08 stsp {
3673 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3674 6841da00 2019-08-08 stsp char *ignorespath;
3675 886cec17 2019-12-15 stsp int fd = -1;
3676 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3677 6841da00 2019-08-08 stsp
3678 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3679 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3680 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3681 6841da00 2019-08-08 stsp
3682 886cec17 2019-12-15 stsp if (dirfd != -1) {
3683 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3684 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3685 886cec17 2019-12-15 stsp if (fd == -1) {
3686 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3687 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3688 886cec17 2019-12-15 stsp ignorespath);
3689 886cec17 2019-12-15 stsp } else {
3690 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3691 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3692 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3693 886cec17 2019-12-15 stsp ignorespath);
3694 886cec17 2019-12-15 stsp else {
3695 886cec17 2019-12-15 stsp fd = -1;
3696 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3697 886cec17 2019-12-15 stsp }
3698 886cec17 2019-12-15 stsp }
3699 886cec17 2019-12-15 stsp } else {
3700 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3701 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3702 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3703 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3704 886cec17 2019-12-15 stsp ignorespath);
3705 886cec17 2019-12-15 stsp } else
3706 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3707 886cec17 2019-12-15 stsp }
3708 6841da00 2019-08-08 stsp
3709 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3710 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3711 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3712 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3713 6841da00 2019-08-08 stsp free(ignorespath);
3714 6841da00 2019-08-08 stsp return err;
3715 f8d1f275 2019-02-04 stsp }
3716 f8d1f275 2019-02-04 stsp
3717 f8d1f275 2019-02-04 stsp static const struct got_error *
3718 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3719 62da3196 2021-10-01 stsp int dirfd)
3720 f8d1f275 2019-02-04 stsp {
3721 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3722 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3723 f8d1f275 2019-02-04 stsp char *path = NULL;
3724 62da3196 2021-10-01 stsp
3725 62da3196 2021-10-01 stsp if (ignore != NULL)
3726 62da3196 2021-10-01 stsp *ignore = 0;
3727 f8d1f275 2019-02-04 stsp
3728 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3729 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3730 0584f854 2019-04-06 stsp
3731 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3732 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3733 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3734 f8d1f275 2019-02-04 stsp } else {
3735 f8d1f275 2019-02-04 stsp path = de->d_name;
3736 f8d1f275 2019-02-04 stsp }
3737 f8d1f275 2019-02-04 stsp
3738 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3739 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3740 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3741 62da3196 2021-10-01 stsp *ignore = 1;
3742 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3743 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3744 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3745 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3746 f8d1f275 2019-02-04 stsp if (parent_path[0])
3747 f8d1f275 2019-02-04 stsp free(path);
3748 b72f483a 2019-02-05 stsp return err;
3749 f8d1f275 2019-02-04 stsp }
3750 f8d1f275 2019-02-04 stsp
3751 347d1d3e 2019-07-12 stsp static const struct got_error *
3752 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3753 3143d852 2020-06-25 stsp {
3754 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3755 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3756 3143d852 2020-06-25 stsp
3757 3143d852 2020-06-25 stsp if (a->no_ignores)
3758 3143d852 2020-06-25 stsp return NULL;
3759 3143d852 2020-06-25 stsp
3760 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3761 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3762 3143d852 2020-06-25 stsp if (err)
3763 3143d852 2020-06-25 stsp return err;
3764 3143d852 2020-06-25 stsp
3765 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3766 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3767 3143d852 2020-06-25 stsp
3768 3143d852 2020-06-25 stsp return err;
3769 3143d852 2020-06-25 stsp }
3770 3143d852 2020-06-25 stsp
3771 3143d852 2020-06-25 stsp static const struct got_error *
3772 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3773 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3774 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3775 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3776 abb4604f 2019-07-27 stsp {
3777 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3778 abb4604f 2019-07-27 stsp struct stat sb;
3779 ff56836b 2021-07-08 stsp
3780 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3781 abb4604f 2019-07-27 stsp if (ie)
3782 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3783 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3784 abb4604f 2019-07-27 stsp
3785 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3786 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3787 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3788 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3789 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3790 abb4604f 2019-07-27 stsp }
3791 abb4604f 2019-07-27 stsp
3792 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3793 916237f3 2022-02-11 stsp return NULL;
3794 916237f3 2022-02-11 stsp
3795 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3796 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3797 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3798 abb4604f 2019-07-27 stsp
3799 abb4604f 2019-07-27 stsp return NULL;
3800 3143d852 2020-06-25 stsp }
3801 3143d852 2020-06-25 stsp
3802 3143d852 2020-06-25 stsp static const struct got_error *
3803 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3804 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3805 3143d852 2020-06-25 stsp {
3806 3143d852 2020-06-25 stsp const struct got_error *err;
3807 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3808 3143d852 2020-06-25 stsp
3809 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3810 3143d852 2020-06-25 stsp ".cvsignore");
3811 3143d852 2020-06-25 stsp if (err)
3812 3143d852 2020-06-25 stsp return err;
3813 3143d852 2020-06-25 stsp
3814 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3815 3143d852 2020-06-25 stsp ".gitignore");
3816 3143d852 2020-06-25 stsp if (err)
3817 3143d852 2020-06-25 stsp return err;
3818 3143d852 2020-06-25 stsp
3819 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3820 3143d852 2020-06-25 stsp if (err) {
3821 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3822 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3823 3143d852 2020-06-25 stsp return err;
3824 3143d852 2020-06-25 stsp }
3825 3143d852 2020-06-25 stsp for (;;) {
3826 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3827 3143d852 2020-06-25 stsp ".cvsignore");
3828 3143d852 2020-06-25 stsp if (err)
3829 3143d852 2020-06-25 stsp break;
3830 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3831 3143d852 2020-06-25 stsp ".gitignore");
3832 3143d852 2020-06-25 stsp if (err)
3833 3143d852 2020-06-25 stsp break;
3834 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3835 3143d852 2020-06-25 stsp if (err) {
3836 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3837 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3838 3143d852 2020-06-25 stsp break;
3839 3143d852 2020-06-25 stsp }
3840 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3841 ff56836b 2021-07-08 stsp break;
3842 b737c85a 2020-06-26 stsp free(parent_path);
3843 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3844 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3845 3143d852 2020-06-25 stsp }
3846 3143d852 2020-06-25 stsp
3847 b737c85a 2020-06-26 stsp free(parent_path);
3848 b737c85a 2020-06-26 stsp free(next_parent_path);
3849 3143d852 2020-06-25 stsp return err;
3850 abb4604f 2019-07-27 stsp }
3851 abb4604f 2019-07-27 stsp
3852 abb4604f 2019-07-27 stsp static const struct got_error *
3853 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3854 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3855 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3856 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3857 f2a9dc41 2019-12-13 tracey int report_unchanged)
3858 f8d1f275 2019-02-04 stsp {
3859 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3860 6fc93f37 2019-12-13 stsp int fd = -1;
3861 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3862 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3863 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3864 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3865 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3866 3143d852 2020-06-25 stsp
3867 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3868 f8d1f275 2019-02-04 stsp
3869 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3870 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3871 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3872 8dc303cc 2019-07-27 stsp
3873 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3874 a84c0d30 2022-03-12 stsp if (ie) {
3875 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3876 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3877 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3878 a84c0d30 2022-03-12 stsp goto done;
3879 a84c0d30 2022-03-12 stsp }
3880 a84c0d30 2022-03-12 stsp
3881 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3882 6fc93f37 2019-12-13 stsp if (fd == -1) {
3883 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3884 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3885 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3886 ff56836b 2021-07-08 stsp else {
3887 ff56836b 2021-07-08 stsp if (!no_ignores) {
3888 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3889 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3890 ff56836b 2021-07-08 stsp if (err)
3891 ff56836b 2021-07-08 stsp goto done;
3892 ff56836b 2021-07-08 stsp }
3893 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3894 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3895 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3896 ff56836b 2021-07-08 stsp }
3897 abb4604f 2019-07-27 stsp } else {
3898 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3899 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3900 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3901 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3902 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3903 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3904 abb4604f 2019-07-27 stsp arg.status_path = path;
3905 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3906 abb4604f 2019-07-27 stsp arg.repo = repo;
3907 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3908 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3909 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3910 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3911 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3912 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3913 022fae89 2019-12-06 tracey if (!no_ignores) {
3914 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3915 3143d852 2020-06-25 stsp worktree->root_path, path);
3916 3143d852 2020-06-25 stsp if (err)
3917 3143d852 2020-06-25 stsp goto done;
3918 022fae89 2019-12-06 tracey }
3919 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3920 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3921 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3922 927df6b7 2019-02-10 stsp }
3923 3143d852 2020-06-25 stsp done:
3924 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3925 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3926 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3927 927df6b7 2019-02-10 stsp free(ondisk_path);
3928 347d1d3e 2019-07-12 stsp return err;
3929 347d1d3e 2019-07-12 stsp }
3930 347d1d3e 2019-07-12 stsp
3931 347d1d3e 2019-07-12 stsp const struct got_error *
3932 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3933 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3934 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3935 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3936 347d1d3e 2019-07-12 stsp {
3937 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3938 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3939 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3940 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3941 347d1d3e 2019-07-12 stsp
3942 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3943 347d1d3e 2019-07-12 stsp if (err)
3944 347d1d3e 2019-07-12 stsp return err;
3945 347d1d3e 2019-07-12 stsp
3946 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3947 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3948 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3949 f6343036 2021-06-22 stsp no_ignores, 0);
3950 72ea6654 2019-07-27 stsp if (err)
3951 72ea6654 2019-07-27 stsp break;
3952 72ea6654 2019-07-27 stsp }
3953 f8d1f275 2019-02-04 stsp free(fileindex_path);
3954 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3955 f8d1f275 2019-02-04 stsp return err;
3956 f8d1f275 2019-02-04 stsp }
3957 6c7ab921 2019-03-18 stsp
3958 6c7ab921 2019-03-18 stsp const struct got_error *
3959 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3960 6c7ab921 2019-03-18 stsp const char *arg)
3961 6c7ab921 2019-03-18 stsp {
3962 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3963 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3964 6c7ab921 2019-03-18 stsp size_t len;
3965 00bb5ea0 2020-07-23 stsp struct stat sb;
3966 01740607 2020-11-04 stsp char *abspath = NULL;
3967 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3968 6c7ab921 2019-03-18 stsp
3969 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3970 6c7ab921 2019-03-18 stsp
3971 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3972 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3973 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3974 00bb5ea0 2020-07-23 stsp
3975 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3976 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3977 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3978 00bb5ea0 2020-07-23 stsp goto done;
3979 00bb5ea0 2020-07-23 stsp }
3980 727173c3 2020-11-06 stsp sb.st_mode = 0;
3981 00bb5ea0 2020-07-23 stsp }
3982 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3983 00bb5ea0 2020-07-23 stsp /*
3984 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3985 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3986 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3987 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3988 00bb5ea0 2020-07-23 stsp */
3989 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3990 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3991 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3992 00bb5ea0 2020-07-23 stsp goto done;
3993 00bb5ea0 2020-07-23 stsp }
3994 00bb5ea0 2020-07-23 stsp
3995 00bb5ea0 2020-07-23 stsp }
3996 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3997 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3998 00bb5ea0 2020-07-23 stsp if (err)
3999 d0710d08 2019-07-22 stsp goto done;
4000 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
4001 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4002 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
4003 00bb5ea0 2020-07-23 stsp goto done;
4004 00bb5ea0 2020-07-23 stsp }
4005 00bb5ea0 2020-07-23 stsp } else {
4006 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
4007 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
4008 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
4009 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
4010 00bb5ea0 2020-07-23 stsp goto done;
4011 00bb5ea0 2020-07-23 stsp }
4012 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4013 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
4014 01740607 2020-11-04 stsp goto done;
4015 01740607 2020-11-04 stsp }
4016 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
4017 01740607 2020-11-04 stsp sizeof(canonpath));
4018 01740607 2020-11-04 stsp if (err)
4019 00bb5ea0 2020-07-23 stsp goto done;
4020 01740607 2020-11-04 stsp resolved = strdup(canonpath);
4021 01740607 2020-11-04 stsp if (resolved == NULL) {
4022 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
4023 01740607 2020-11-04 stsp goto done;
4024 00bb5ea0 2020-07-23 stsp }
4025 d0710d08 2019-07-22 stsp }
4026 d0710d08 2019-07-22 stsp }
4027 6c7ab921 2019-03-18 stsp
4028 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
4029 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
4030 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4031 6c7ab921 2019-03-18 stsp goto done;
4032 6c7ab921 2019-03-18 stsp }
4033 6c7ab921 2019-03-18 stsp
4034 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4035 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
4036 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
4037 f6d88e1a 2019-05-29 stsp if (err)
4038 f6d88e1a 2019-05-29 stsp goto done;
4039 f6d88e1a 2019-05-29 stsp } else {
4040 f6d88e1a 2019-05-29 stsp path = strdup("");
4041 f6d88e1a 2019-05-29 stsp if (path == NULL) {
4042 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
4043 f6d88e1a 2019-05-29 stsp goto done;
4044 f6d88e1a 2019-05-29 stsp }
4045 6c7ab921 2019-03-18 stsp }
4046 6c7ab921 2019-03-18 stsp
4047 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
4048 6c7ab921 2019-03-18 stsp len = strlen(path);
4049 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
4050 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
4051 6c7ab921 2019-03-18 stsp len--;
4052 6c7ab921 2019-03-18 stsp }
4053 6c7ab921 2019-03-18 stsp done:
4054 01740607 2020-11-04 stsp free(abspath);
4055 6c7ab921 2019-03-18 stsp free(resolved);
4056 d0710d08 2019-07-22 stsp free(cwd);
4057 6c7ab921 2019-03-18 stsp if (err == NULL)
4058 6c7ab921 2019-03-18 stsp *wt_path = path;
4059 6c7ab921 2019-03-18 stsp else
4060 6c7ab921 2019-03-18 stsp free(path);
4061 d00136be 2019-03-26 stsp return err;
4062 d00136be 2019-03-26 stsp }
4063 d00136be 2019-03-26 stsp
4064 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
4065 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
4066 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
4067 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
4068 4e68cba3 2019-11-23 stsp void *progress_arg;
4069 4e68cba3 2019-11-23 stsp struct got_repository *repo;
4070 4e68cba3 2019-11-23 stsp };
4071 4e68cba3 2019-11-23 stsp
4072 4e68cba3 2019-11-23 stsp static const struct got_error *
4073 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4074 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
4075 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4076 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4077 07f5b47a 2019-06-02 stsp {
4078 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
4079 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
4080 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
4081 6d022e97 2019-08-04 stsp struct stat sb;
4082 dbb83fbd 2019-12-12 stsp char *ondisk_path;
4083 07f5b47a 2019-06-02 stsp
4084 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4085 dbb83fbd 2019-12-12 stsp relpath) == -1)
4086 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
4087 dbb83fbd 2019-12-12 stsp
4088 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 6d022e97 2019-08-04 stsp if (ie) {
4090 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4091 12463d8b 2019-12-13 stsp de_name, a->repo);
4092 6d022e97 2019-08-04 stsp if (err)
4093 dbb83fbd 2019-12-12 stsp goto done;
4094 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
4095 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
4096 dbb83fbd 2019-12-12 stsp goto done;
4097 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4098 dbb83fbd 2019-12-12 stsp if (err)
4099 dbb83fbd 2019-12-12 stsp goto done;
4100 6d022e97 2019-08-04 stsp }
4101 07f5b47a 2019-06-02 stsp
4102 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
4103 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
4104 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
4105 9b4603c0 2022-01-31 stsp else
4106 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4107 dbb83fbd 2019-12-12 stsp goto done;
4108 4e68cba3 2019-11-23 stsp }
4109 07f5b47a 2019-06-02 stsp
4110 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4111 4e68cba3 2019-11-23 stsp if (err)
4112 4e68cba3 2019-11-23 stsp goto done;
4113 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4114 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4115 3969253a 2020-03-07 stsp if (err) {
4116 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4117 3969253a 2020-03-07 stsp goto done;
4118 3969253a 2020-03-07 stsp }
4119 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4120 07f5b47a 2019-06-02 stsp if (err) {
4121 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4122 4e68cba3 2019-11-23 stsp goto done;
4123 07f5b47a 2019-06-02 stsp }
4124 4e68cba3 2019-11-23 stsp done:
4125 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4126 dbb83fbd 2019-12-12 stsp if (err)
4127 dbb83fbd 2019-12-12 stsp return err;
4128 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4129 dbb83fbd 2019-12-12 stsp return NULL;
4130 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4131 07f5b47a 2019-06-02 stsp }
4132 07f5b47a 2019-06-02 stsp
4133 d00136be 2019-03-26 stsp const struct got_error *
4134 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4135 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4136 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4137 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4138 d00136be 2019-03-26 stsp {
4139 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4140 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4141 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4142 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4143 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4144 d00136be 2019-03-26 stsp
4145 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4146 d00136be 2019-03-26 stsp if (err)
4147 d00136be 2019-03-26 stsp return err;
4148 d00136be 2019-03-26 stsp
4149 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4150 d00136be 2019-03-26 stsp if (err)
4151 d00136be 2019-03-26 stsp goto done;
4152 d00136be 2019-03-26 stsp
4153 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4154 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4155 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4156 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4157 4e68cba3 2019-11-23 stsp saa.repo = repo;
4158 4e68cba3 2019-11-23 stsp
4159 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4160 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4161 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4162 1dd54920 2019-05-11 stsp if (err)
4163 af12c6b9 2019-06-04 stsp break;
4164 1dd54920 2019-05-11 stsp }
4165 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4166 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4167 af12c6b9 2019-06-04 stsp err = sync_err;
4168 d00136be 2019-03-26 stsp done:
4169 fb399478 2019-07-12 stsp free(fileindex_path);
4170 d00136be 2019-03-26 stsp if (fileindex)
4171 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4172 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4173 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4174 d00136be 2019-03-26 stsp err = unlockerr;
4175 6c7ab921 2019-03-18 stsp return err;
4176 6c7ab921 2019-03-18 stsp }
4177 17ed4618 2019-06-02 stsp
4178 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4179 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4180 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4181 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4182 f2a9dc41 2019-12-13 tracey void *progress_arg;
4183 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4184 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4185 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4186 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4187 766841c2 2020-08-13 stsp const char *status_codes;
4188 f2a9dc41 2019-12-13 tracey };
4189 f2a9dc41 2019-12-13 tracey
4190 f2a9dc41 2019-12-13 tracey static const struct got_error *
4191 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4192 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4193 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4194 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4195 17ed4618 2019-06-02 stsp {
4196 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4197 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4198 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4199 17ed4618 2019-06-02 stsp struct stat sb;
4200 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4201 4e12cd97 2022-01-25 stsp
4202 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4203 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4204 4e12cd97 2022-01-25 stsp return NULL;
4205 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4206 4e12cd97 2022-01-25 stsp }
4207 17ed4618 2019-06-02 stsp
4208 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4209 17ed4618 2019-06-02 stsp if (ie == NULL)
4210 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4211 2ec1f75b 2019-03-26 stsp
4212 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4213 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4214 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4215 9acbc4fa 2019-08-03 stsp return NULL;
4216 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4217 9acbc4fa 2019-08-03 stsp }
4218 9acbc4fa 2019-08-03 stsp
4219 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4220 f2a9dc41 2019-12-13 tracey relpath) == -1)
4221 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4222 f2a9dc41 2019-12-13 tracey
4223 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4224 12463d8b 2019-12-13 stsp a->repo);
4225 17ed4618 2019-06-02 stsp if (err)
4226 f2a9dc41 2019-12-13 tracey goto done;
4227 17ed4618 2019-06-02 stsp
4228 766841c2 2020-08-13 stsp if (a->status_codes) {
4229 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4230 766841c2 2020-08-13 stsp int i;
4231 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4232 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4233 766841c2 2020-08-13 stsp break;
4234 766841c2 2020-08-13 stsp }
4235 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4236 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4237 766841c2 2020-08-13 stsp free(ondisk_path);
4238 766841c2 2020-08-13 stsp return NULL;
4239 766841c2 2020-08-13 stsp }
4240 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4241 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4242 766841c2 2020-08-13 stsp static char msg[64];
4243 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4244 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4245 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4246 766841c2 2020-08-13 stsp goto done;
4247 766841c2 2020-08-13 stsp }
4248 766841c2 2020-08-13 stsp }
4249 766841c2 2020-08-13 stsp
4250 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4251 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4252 f2a9dc41 2019-12-13 tracey goto done;
4253 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4254 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4255 f2a9dc41 2019-12-13 tracey goto done;
4256 f2a9dc41 2019-12-13 tracey }
4257 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4258 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4259 4e12cd97 2022-01-25 stsp goto done;
4260 4e12cd97 2022-01-25 stsp }
4261 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4262 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4263 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4264 f2a9dc41 2019-12-13 tracey goto done;
4265 f2a9dc41 2019-12-13 tracey }
4266 17ed4618 2019-06-02 stsp }
4267 17ed4618 2019-06-02 stsp
4268 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4269 20a2ad1c 2020-10-20 stsp size_t root_len;
4270 20a2ad1c 2020-10-20 stsp
4271 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4272 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4273 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4274 12463d8b 2019-12-13 stsp ondisk_path);
4275 12463d8b 2019-12-13 stsp goto done;
4276 12463d8b 2019-12-13 stsp }
4277 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4278 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4279 12463d8b 2019-12-13 stsp goto done;
4280 15341bfd 2020-03-05 tracey }
4281 15341bfd 2020-03-05 tracey
4282 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4283 20a2ad1c 2020-10-20 stsp do {
4284 20a2ad1c 2020-10-20 stsp char *parent;
4285 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4286 20a2ad1c 2020-10-20 stsp if (err)
4287 2513f20a 2020-10-20 stsp goto done;
4288 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4289 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4290 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4291 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4292 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4293 20a2ad1c 2020-10-20 stsp ondisk_path);
4294 15341bfd 2020-03-05 tracey break;
4295 15341bfd 2020-03-05 tracey }
4296 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4297 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4298 f2a9dc41 2019-12-13 tracey }
4299 17ed4618 2019-06-02 stsp
4300 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4301 f2a9dc41 2019-12-13 tracey done:
4302 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4303 f2a9dc41 2019-12-13 tracey if (err)
4304 f2a9dc41 2019-12-13 tracey return err;
4305 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4306 f2a9dc41 2019-12-13 tracey return NULL;
4307 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4308 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4309 17ed4618 2019-06-02 stsp }
4310 17ed4618 2019-06-02 stsp
4311 2ec1f75b 2019-03-26 stsp const struct got_error *
4312 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4313 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4314 766841c2 2020-08-13 stsp const char *status_codes,
4315 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4316 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4317 2ec1f75b 2019-03-26 stsp {
4318 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4319 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4320 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4321 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4322 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4323 2ec1f75b 2019-03-26 stsp
4324 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4325 2ec1f75b 2019-03-26 stsp if (err)
4326 2ec1f75b 2019-03-26 stsp return err;
4327 2ec1f75b 2019-03-26 stsp
4328 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4329 2ec1f75b 2019-03-26 stsp if (err)
4330 2ec1f75b 2019-03-26 stsp goto done;
4331 f2a9dc41 2019-12-13 tracey
4332 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4333 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4334 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4335 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4336 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4337 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4338 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4339 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4340 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4341 2ec1f75b 2019-03-26 stsp
4342 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4343 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4344 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4345 17ed4618 2019-06-02 stsp if (err)
4346 af12c6b9 2019-06-04 stsp break;
4347 2ec1f75b 2019-03-26 stsp }
4348 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4349 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4350 af12c6b9 2019-06-04 stsp err = sync_err;
4351 2ec1f75b 2019-03-26 stsp done:
4352 fb399478 2019-07-12 stsp free(fileindex_path);
4353 2ec1f75b 2019-03-26 stsp if (fileindex)
4354 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4355 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4356 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4357 2ec1f75b 2019-03-26 stsp err = unlockerr;
4358 33aa809d 2019-08-08 stsp return err;
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_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4363 33aa809d 2019-08-08 stsp {
4364 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4365 33aa809d 2019-08-08 stsp char *line = NULL;
4366 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4367 33aa809d 2019-08-08 stsp ssize_t linelen;
4368 33aa809d 2019-08-08 stsp
4369 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4370 33aa809d 2019-08-08 stsp if (linelen == -1) {
4371 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4372 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4373 33aa809d 2019-08-08 stsp goto done;
4374 33aa809d 2019-08-08 stsp }
4375 33aa809d 2019-08-08 stsp return NULL;
4376 33aa809d 2019-08-08 stsp }
4377 33aa809d 2019-08-08 stsp if (outfile) {
4378 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4379 33aa809d 2019-08-08 stsp if (n != linelen) {
4380 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4381 33aa809d 2019-08-08 stsp goto done;
4382 33aa809d 2019-08-08 stsp }
4383 33aa809d 2019-08-08 stsp }
4384 33aa809d 2019-08-08 stsp if (rejectfile) {
4385 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4386 33aa809d 2019-08-08 stsp if (n != linelen)
4387 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4388 33aa809d 2019-08-08 stsp }
4389 33aa809d 2019-08-08 stsp done:
4390 33aa809d 2019-08-08 stsp free(line);
4391 2ec1f75b 2019-03-26 stsp return err;
4392 2ec1f75b 2019-03-26 stsp }
4393 1f1abb7e 2019-08-08 stsp
4394 33aa809d 2019-08-08 stsp static const struct got_error *
4395 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4396 33aa809d 2019-08-08 stsp {
4397 33aa809d 2019-08-08 stsp char *line = NULL;
4398 33aa809d 2019-08-08 stsp size_t linesize = 0;
4399 33aa809d 2019-08-08 stsp ssize_t linelen;
4400 33aa809d 2019-08-08 stsp
4401 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4402 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4403 500467ff 2019-09-25 hiltjo if (ferror(f))
4404 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4405 500467ff 2019-09-25 hiltjo return NULL;
4406 500467ff 2019-09-25 hiltjo }
4407 33aa809d 2019-08-08 stsp free(line);
4408 33aa809d 2019-08-08 stsp return NULL;
4409 33aa809d 2019-08-08 stsp }
4410 33aa809d 2019-08-08 stsp
4411 33aa809d 2019-08-08 stsp static const struct got_error *
4412 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4413 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4414 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4415 abc59930 2021-09-05 naddy {
4416 33aa809d 2019-08-08 stsp const struct got_error *err;
4417 33aa809d 2019-08-08 stsp
4418 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4419 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4420 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4421 33aa809d 2019-08-08 stsp if (err)
4422 33aa809d 2019-08-08 stsp return err;
4423 33aa809d 2019-08-08 stsp (*line_cur1)++;
4424 33aa809d 2019-08-08 stsp }
4425 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4426 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4427 33aa809d 2019-08-08 stsp if (rejectfile)
4428 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4429 33aa809d 2019-08-08 stsp else
4430 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4431 33aa809d 2019-08-08 stsp if (err)
4432 33aa809d 2019-08-08 stsp return err;
4433 33aa809d 2019-08-08 stsp (*line_cur2)++;
4434 33aa809d 2019-08-08 stsp }
4435 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4436 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4437 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4438 33aa809d 2019-08-08 stsp if (err)
4439 33aa809d 2019-08-08 stsp return err;
4440 33aa809d 2019-08-08 stsp (*line_cur2)++;
4441 33aa809d 2019-08-08 stsp }
4442 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4443 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4444 33aa809d 2019-08-08 stsp if (rejectfile)
4445 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4446 33aa809d 2019-08-08 stsp else
4447 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4448 33aa809d 2019-08-08 stsp if (err)
4449 33aa809d 2019-08-08 stsp return err;
4450 33aa809d 2019-08-08 stsp (*line_cur1)++;
4451 33aa809d 2019-08-08 stsp }
4452 f1e81a05 2019-08-10 stsp
4453 f1e81a05 2019-08-10 stsp return NULL;
4454 f1e81a05 2019-08-10 stsp }
4455 f1e81a05 2019-08-10 stsp
4456 f1e81a05 2019-08-10 stsp static const struct got_error *
4457 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4458 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4459 f1e81a05 2019-08-10 stsp {
4460 f1e81a05 2019-08-10 stsp const struct got_error *err;
4461 f1e81a05 2019-08-10 stsp
4462 f1e81a05 2019-08-10 stsp if (outfile) {
4463 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4464 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4465 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4466 f1e81a05 2019-08-10 stsp if (err)
4467 f1e81a05 2019-08-10 stsp return err;
4468 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4469 f1e81a05 2019-08-10 stsp }
4470 f1e81a05 2019-08-10 stsp }
4471 f1e81a05 2019-08-10 stsp if (rejectfile) {
4472 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4473 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4474 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4475 f1e81a05 2019-08-10 stsp if (err)
4476 f1e81a05 2019-08-10 stsp return err;
4477 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4478 f1e81a05 2019-08-10 stsp }
4479 33aa809d 2019-08-08 stsp }
4480 33aa809d 2019-08-08 stsp
4481 33aa809d 2019-08-08 stsp return NULL;
4482 33aa809d 2019-08-08 stsp }
4483 33aa809d 2019-08-08 stsp
4484 33aa809d 2019-08-08 stsp static const struct got_error *
4485 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4486 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4487 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4488 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4489 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4490 33aa809d 2019-08-08 stsp {
4491 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4492 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4493 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4494 33aa809d 2019-08-08 stsp FILE *hunkfile;
4495 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4496 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4497 fe621944 2020-11-10 stsp int rc;
4498 33aa809d 2019-08-08 stsp
4499 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4500 33aa809d 2019-08-08 stsp
4501 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4502 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4503 fe621944 2020-11-10 stsp start_old = cc.left.start;
4504 fe621944 2020-11-10 stsp end_old = cc.left.end;
4505 fe621944 2020-11-10 stsp start_new = cc.right.start;
4506 fe621944 2020-11-10 stsp end_new = cc.right.end;
4507 33aa809d 2019-08-08 stsp
4508 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4509 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4510 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4511 33aa809d 2019-08-08 stsp
4512 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4513 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4514 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4515 33aa809d 2019-08-08 stsp
4516 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4517 fe621944 2020-11-10 stsp if (diff_state == NULL)
4518 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4519 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4520 fe621944 2020-11-10 stsp
4521 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4522 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4523 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4524 33aa809d 2019-08-08 stsp goto done;
4525 33aa809d 2019-08-08 stsp }
4526 fe621944 2020-11-10 stsp
4527 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4528 fe621944 2020-11-10 stsp diff_result, &cc);
4529 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4530 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4531 33aa809d 2019-08-08 stsp goto done;
4532 33aa809d 2019-08-08 stsp }
4533 fe621944 2020-11-10 stsp
4534 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4535 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4536 33aa809d 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp }
4538 33aa809d 2019-08-08 stsp
4539 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4540 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4541 33aa809d 2019-08-08 stsp if (err)
4542 33aa809d 2019-08-08 stsp goto done;
4543 33aa809d 2019-08-08 stsp
4544 33aa809d 2019-08-08 stsp switch (*choice) {
4545 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4546 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4547 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4548 33aa809d 2019-08-08 stsp break;
4549 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4550 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4551 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4552 33aa809d 2019-08-08 stsp break;
4553 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4554 33aa809d 2019-08-08 stsp break;
4555 33aa809d 2019-08-08 stsp default:
4556 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4557 33aa809d 2019-08-08 stsp break;
4558 33aa809d 2019-08-08 stsp }
4559 33aa809d 2019-08-08 stsp done:
4560 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4561 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4562 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4563 33aa809d 2019-08-08 stsp return err;
4564 33aa809d 2019-08-08 stsp }
4565 33aa809d 2019-08-08 stsp
4566 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4567 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4568 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4569 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4570 1f1abb7e 2019-08-08 stsp void *progress_arg;
4571 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4572 33aa809d 2019-08-08 stsp void *patch_arg;
4573 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4574 f259c4c1 2021-09-24 stsp int unlink_added_files;
4575 1f1abb7e 2019-08-08 stsp };
4576 a129376b 2019-03-28 stsp
4577 e20a8b6f 2019-06-04 stsp static const struct got_error *
4578 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4579 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4580 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4581 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4582 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4583 33aa809d 2019-08-08 stsp {
4584 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4585 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4586 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4587 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4588 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4589 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4590 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4591 fe621944 2020-11-10 stsp struct stat sb2;
4592 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4593 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4594 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4595 33aa809d 2019-08-08 stsp
4596 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4597 33aa809d 2019-08-08 stsp
4598 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4599 33aa809d 2019-08-08 stsp if (err)
4600 33aa809d 2019-08-08 stsp return err;
4601 33aa809d 2019-08-08 stsp
4602 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4603 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4604 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4605 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4606 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4607 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4608 fa3cef63 2020-07-23 stsp goto done;
4609 fa3cef63 2020-07-23 stsp }
4610 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4611 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4612 c0df5966 2021-12-31 stsp if (link_len == -1) {
4613 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4614 c0df5966 2021-12-31 stsp path2);
4615 c0df5966 2021-12-31 stsp }
4616 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4617 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4618 12463d8b 2019-12-13 stsp }
4619 12463d8b 2019-12-13 stsp } else {
4620 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4621 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4622 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4623 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4624 fa3cef63 2020-07-23 stsp goto done;
4625 fa3cef63 2020-07-23 stsp }
4626 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4627 fa3cef63 2020-07-23 stsp sizeof(link_target));
4628 fa3cef63 2020-07-23 stsp if (link_len == -1)
4629 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4630 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4631 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4632 12463d8b 2019-12-13 stsp }
4633 1ebedb77 2019-10-19 stsp }
4634 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4635 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4636 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4637 fa3cef63 2020-07-23 stsp goto done;
4638 fa3cef63 2020-07-23 stsp }
4639 1ebedb77 2019-10-19 stsp
4640 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4641 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4642 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4643 fa3cef63 2020-07-23 stsp goto done;
4644 fa3cef63 2020-07-23 stsp }
4645 fa3cef63 2020-07-23 stsp fd2 = -1;
4646 fa3cef63 2020-07-23 stsp } else {
4647 fa3cef63 2020-07-23 stsp size_t n;
4648 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4649 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4650 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4651 fa3cef63 2020-07-23 stsp goto done;
4652 fa3cef63 2020-07-23 stsp }
4653 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4654 fa3cef63 2020-07-23 stsp if (n != link_len) {
4655 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4656 fa3cef63 2020-07-23 stsp goto done;
4657 fa3cef63 2020-07-23 stsp }
4658 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4659 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4660 fa3cef63 2020-07-23 stsp goto done;
4661 fa3cef63 2020-07-23 stsp }
4662 fa3cef63 2020-07-23 stsp rewind(f2);
4663 33aa809d 2019-08-08 stsp }
4664 33aa809d 2019-08-08 stsp
4665 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4666 eb81bc23 2022-06-28 tracey if (fd == -1) {
4667 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4668 eb81bc23 2022-06-28 tracey goto done;
4669 eb81bc23 2022-06-28 tracey }
4670 eb81bc23 2022-06-28 tracey
4671 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4672 33aa809d 2019-08-08 stsp if (err)
4673 33aa809d 2019-08-08 stsp goto done;
4674 33aa809d 2019-08-08 stsp
4675 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4676 33aa809d 2019-08-08 stsp if (err)
4677 33aa809d 2019-08-08 stsp goto done;
4678 33aa809d 2019-08-08 stsp
4679 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4680 33aa809d 2019-08-08 stsp if (err)
4681 33aa809d 2019-08-08 stsp goto done;
4682 33aa809d 2019-08-08 stsp
4683 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4684 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4685 33aa809d 2019-08-08 stsp if (err)
4686 33aa809d 2019-08-08 stsp goto done;
4687 33aa809d 2019-08-08 stsp
4688 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4689 b90054ed 2022-11-01 stsp "");
4690 33aa809d 2019-08-08 stsp if (err)
4691 33aa809d 2019-08-08 stsp goto done;
4692 33aa809d 2019-08-08 stsp
4693 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4694 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4695 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4696 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4697 fe621944 2020-11-10 stsp
4698 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4699 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4700 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4701 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4702 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4703 fe621944 2020-11-10 stsp nchanges++;
4704 fe621944 2020-11-10 stsp }
4705 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4706 33aa809d 2019-08-08 stsp int choice;
4707 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4708 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4709 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4710 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4711 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4712 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4713 33aa809d 2019-08-08 stsp if (err)
4714 33aa809d 2019-08-08 stsp goto done;
4715 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4716 33aa809d 2019-08-08 stsp have_content = 1;
4717 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4718 33aa809d 2019-08-08 stsp break;
4719 33aa809d 2019-08-08 stsp }
4720 1ebedb77 2019-10-19 stsp if (have_content) {
4721 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4722 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4723 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4724 1ebedb77 2019-10-19 stsp if (err)
4725 1ebedb77 2019-10-19 stsp goto done;
4726 1ebedb77 2019-10-19 stsp
4727 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4728 b2b3fce1 2022-10-29 op mode_t mode;
4729 b2b3fce1 2022-10-29 op
4730 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4731 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4732 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4733 fa3cef63 2020-07-23 stsp goto done;
4734 fa3cef63 2020-07-23 stsp }
4735 1ebedb77 2019-10-19 stsp }
4736 1ebedb77 2019-10-19 stsp }
4737 33aa809d 2019-08-08 stsp done:
4738 33aa809d 2019-08-08 stsp free(id_str);
4739 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4740 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4741 33aa809d 2019-08-08 stsp if (blob)
4742 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4743 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4744 fe621944 2020-11-10 stsp if (err == NULL)
4745 fe621944 2020-11-10 stsp err = free_err;
4746 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4747 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4748 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4749 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4750 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4751 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4752 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4753 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4754 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4755 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4756 33aa809d 2019-08-08 stsp if (err || !have_content) {
4757 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4758 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4759 33aa809d 2019-08-08 stsp free(*path_outfile);
4760 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4761 33aa809d 2019-08-08 stsp }
4762 33aa809d 2019-08-08 stsp free(path1);
4763 33aa809d 2019-08-08 stsp return err;
4764 33aa809d 2019-08-08 stsp }
4765 33aa809d 2019-08-08 stsp
4766 33aa809d 2019-08-08 stsp static const struct got_error *
4767 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4768 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4769 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4770 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4771 a129376b 2019-03-28 stsp {
4772 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4773 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4774 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4775 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4776 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4777 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4778 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4779 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4780 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4781 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4782 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4783 eb81bc23 2022-06-28 tracey int fd = -1;
4784 a129376b 2019-03-28 stsp
4785 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4786 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4787 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4788 d3bcc3d1 2019-08-08 stsp return NULL;
4789 3d69ad8d 2019-08-17 semarie
4790 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4791 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4792 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4793 d3bcc3d1 2019-08-08 stsp
4794 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4795 65084dad 2019-08-08 stsp if (ie == NULL)
4796 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4797 a129376b 2019-03-28 stsp
4798 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4799 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4800 a129376b 2019-03-28 stsp if (err) {
4801 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4802 a129376b 2019-03-28 stsp goto done;
4803 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4804 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4805 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4806 e20a8b6f 2019-06-04 stsp goto done;
4807 e20a8b6f 2019-06-04 stsp }
4808 a129376b 2019-03-28 stsp }
4809 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4810 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4811 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4812 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4813 a129376b 2019-03-28 stsp goto done;
4814 a129376b 2019-03-28 stsp }
4815 a129376b 2019-03-28 stsp } else {
4816 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4817 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4818 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4819 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4820 a129376b 2019-03-28 stsp goto done;
4821 a129376b 2019-03-28 stsp }
4822 a129376b 2019-03-28 stsp } else {
4823 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4824 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4825 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4826 a129376b 2019-03-28 stsp goto done;
4827 a129376b 2019-03-28 stsp }
4828 a129376b 2019-03-28 stsp }
4829 a129376b 2019-03-28 stsp }
4830 a129376b 2019-03-28 stsp
4831 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4832 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4833 a44927cc 2022-04-07 stsp if (err)
4834 a44927cc 2022-04-07 stsp goto done;
4835 a44927cc 2022-04-07 stsp
4836 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4837 a9fa2909 2019-07-27 stsp if (err) {
4838 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4839 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4840 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4841 a9fa2909 2019-07-27 stsp goto done;
4842 a9fa2909 2019-07-27 stsp } else {
4843 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4844 a9fa2909 2019-07-27 stsp if (err)
4845 a9fa2909 2019-07-27 stsp goto done;
4846 a9fa2909 2019-07-27 stsp
4847 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4848 1233e6b6 2020-10-19 stsp if (err)
4849 a9fa2909 2019-07-27 stsp goto done;
4850 a9fa2909 2019-07-27 stsp
4851 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4852 1233e6b6 2020-10-19 stsp free(te_name);
4853 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4854 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4855 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4856 a9fa2909 2019-07-27 stsp goto done;
4857 a9fa2909 2019-07-27 stsp }
4858 a129376b 2019-03-28 stsp }
4859 a129376b 2019-03-28 stsp
4860 a129376b 2019-03-28 stsp switch (status) {
4861 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4862 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4863 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4864 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4865 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4866 33aa809d 2019-08-08 stsp if (err)
4867 33aa809d 2019-08-08 stsp goto done;
4868 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4869 33aa809d 2019-08-08 stsp break;
4870 33aa809d 2019-08-08 stsp }
4871 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4872 1f1abb7e 2019-08-08 stsp ie->path);
4873 1ee397ad 2019-07-12 stsp if (err)
4874 1ee397ad 2019-07-12 stsp goto done;
4875 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4876 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4877 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4878 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4879 f259c4c1 2021-09-24 stsp relpath) == -1) {
4880 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4881 f259c4c1 2021-09-24 stsp goto done;
4882 f259c4c1 2021-09-24 stsp }
4883 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4884 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4885 f259c4c1 2021-09-24 stsp ondisk_path);
4886 f259c4c1 2021-09-24 stsp break;
4887 f259c4c1 2021-09-24 stsp }
4888 f259c4c1 2021-09-24 stsp }
4889 a129376b 2019-03-28 stsp break;
4890 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4891 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4892 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4893 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4894 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4895 33aa809d 2019-08-08 stsp if (err)
4896 33aa809d 2019-08-08 stsp goto done;
4897 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4898 33aa809d 2019-08-08 stsp break;
4899 33aa809d 2019-08-08 stsp }
4900 33aa809d 2019-08-08 stsp /* fall through */
4901 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4902 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4903 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4904 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4905 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4906 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4907 b4b2adf5 2023-02-09 op staged_status == GOT_STATUS_MODIFY)
4908 b4b2adf5 2023-02-09 op got_fileindex_entry_get_staged_blob_id(&id, ie);
4909 b4b2adf5 2023-02-09 op else
4910 b4b2adf5 2023-02-09 op got_fileindex_entry_get_blob_id(&id, ie);
4911 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4912 eb81bc23 2022-06-28 tracey if (fd == -1) {
4913 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4914 eb81bc23 2022-06-28 tracey goto done;
4915 eb81bc23 2022-06-28 tracey }
4916 eb81bc23 2022-06-28 tracey
4917 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4918 a129376b 2019-03-28 stsp if (err)
4919 65084dad 2019-08-08 stsp goto done;
4920 65084dad 2019-08-08 stsp
4921 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4922 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4923 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4924 a129376b 2019-03-28 stsp goto done;
4925 65084dad 2019-08-08 stsp }
4926 65084dad 2019-08-08 stsp
4927 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4928 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4929 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4930 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4931 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4932 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4933 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4934 33aa809d 2019-08-08 stsp break;
4935 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4936 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4937 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4938 369fd7e5 2020-07-23 stsp path_content);
4939 369fd7e5 2020-07-23 stsp break;
4940 369fd7e5 2020-07-23 stsp }
4941 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4942 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4943 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4944 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4945 369fd7e5 2020-07-23 stsp } else {
4946 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4947 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4948 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4949 369fd7e5 2020-07-23 stsp goto done;
4950 369fd7e5 2020-07-23 stsp }
4951 33aa809d 2019-08-08 stsp }
4952 33aa809d 2019-08-08 stsp } else {
4953 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4954 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4955 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4956 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4957 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4958 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4959 2e1fa222 2020-07-23 stsp } else {
4960 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4961 2e1fa222 2020-07-23 stsp ie->path,
4962 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4963 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4964 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4965 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4966 2e1fa222 2020-07-23 stsp }
4967 a129376b 2019-03-28 stsp if (err)
4968 a129376b 2019-03-28 stsp goto done;
4969 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4970 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4971 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4972 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4973 437adc9d 2020-12-10 yzhong blob->id.sha1,
4974 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4975 2e1fa222 2020-07-23 stsp if (err)
4976 2e1fa222 2020-07-23 stsp goto done;
4977 2e1fa222 2020-07-23 stsp }
4978 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4979 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4980 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4981 33aa809d 2019-08-08 stsp }
4982 a129376b 2019-03-28 stsp }
4983 a129376b 2019-03-28 stsp break;
4984 e20a8b6f 2019-06-04 stsp }
4985 a129376b 2019-03-28 stsp default:
4986 1f1abb7e 2019-08-08 stsp break;
4987 a129376b 2019-03-28 stsp }
4988 a129376b 2019-03-28 stsp done:
4989 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4990 33aa809d 2019-08-08 stsp free(path_content);
4991 e20a8b6f 2019-06-04 stsp free(parent_path);
4992 a129376b 2019-03-28 stsp free(tree_path);
4993 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4994 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4995 a129376b 2019-03-28 stsp if (blob)
4996 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4997 a129376b 2019-03-28 stsp if (tree)
4998 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4999 a129376b 2019-03-28 stsp free(tree_id);
5000 a44927cc 2022-04-07 stsp if (base_commit)
5001 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
5002 e20a8b6f 2019-06-04 stsp return err;
5003 e20a8b6f 2019-06-04 stsp }
5004 e20a8b6f 2019-06-04 stsp
5005 e20a8b6f 2019-06-04 stsp const struct got_error *
5006 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
5007 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
5008 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5009 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
5010 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
5011 e20a8b6f 2019-06-04 stsp {
5012 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
5013 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
5014 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5015 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
5016 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
5017 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5018 e20a8b6f 2019-06-04 stsp
5019 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
5020 e20a8b6f 2019-06-04 stsp if (err)
5021 e20a8b6f 2019-06-04 stsp return err;
5022 e20a8b6f 2019-06-04 stsp
5023 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5024 e20a8b6f 2019-06-04 stsp if (err)
5025 e20a8b6f 2019-06-04 stsp goto done;
5026 e20a8b6f 2019-06-04 stsp
5027 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5028 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5029 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5030 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5031 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
5032 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
5033 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5034 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
5035 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
5036 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5037 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
5038 e20a8b6f 2019-06-04 stsp if (err)
5039 af12c6b9 2019-06-04 stsp break;
5040 e20a8b6f 2019-06-04 stsp }
5041 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5042 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
5043 e20a8b6f 2019-06-04 stsp err = sync_err;
5044 af12c6b9 2019-06-04 stsp done:
5045 fb399478 2019-07-12 stsp free(fileindex_path);
5046 a129376b 2019-03-28 stsp if (fileindex)
5047 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
5048 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5049 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
5050 a129376b 2019-03-28 stsp err = unlockerr;
5051 c4296144 2019-05-09 stsp return err;
5052 c4296144 2019-05-09 stsp }
5053 c4296144 2019-05-09 stsp
5054 cf066bf8 2019-05-09 stsp static void
5055 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
5056 cf066bf8 2019-05-09 stsp {
5057 24519714 2019-05-09 stsp free(ct->path);
5058 44d03001 2019-05-09 stsp free(ct->in_repo_path);
5059 768aea60 2019-05-09 stsp free(ct->ondisk_path);
5060 e75eb4da 2019-05-10 stsp free(ct->blob_id);
5061 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
5062 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
5063 b416585c 2019-05-13 stsp free(ct->base_commit_id);
5064 cf066bf8 2019-05-09 stsp free(ct);
5065 cf066bf8 2019-05-09 stsp }
5066 24519714 2019-05-09 stsp
5067 ed175427 2019-05-09 stsp struct collect_commitables_arg {
5068 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
5069 24519714 2019-05-09 stsp struct got_repository *repo;
5070 24519714 2019-05-09 stsp struct got_worktree *worktree;
5071 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
5072 5f8a88c6 2019-08-03 stsp int have_staged_files;
5073 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
5074 2a47b1e5 2022-11-01 stsp int diff_header_shown;
5075 12383673 2023-02-18 mark int commit_conflicts;
5076 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
5077 2a47b1e5 2022-11-01 stsp FILE *f1;
5078 2a47b1e5 2022-11-01 stsp FILE *f2;
5079 24519714 2019-05-09 stsp };
5080 2a47b1e5 2022-11-01 stsp
5081 2a47b1e5 2022-11-01 stsp /*
5082 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
5083 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
5084 2a47b1e5 2022-11-01 stsp */
5085 2a47b1e5 2022-11-01 stsp static const struct got_error *
5086 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5087 2a47b1e5 2022-11-01 stsp const char *abspath)
5088 2a47b1e5 2022-11-01 stsp {
5089 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5090 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
5091 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
5092 2a47b1e5 2022-11-01 stsp
5093 2a47b1e5 2022-11-01 stsp *fd = -1;
5094 2a47b1e5 2022-11-01 stsp
5095 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5096 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5097 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5098 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
5099 2a47b1e5 2022-11-01 stsp } else {
5100 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
5101 2a47b1e5 2022-11-01 stsp if (target_len == -1)
5102 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
5103 2a47b1e5 2022-11-01 stsp }
5104 2a47b1e5 2022-11-01 stsp
5105 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
5106 2a47b1e5 2022-11-01 stsp if (*fd == -1)
5107 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
5108 2a47b1e5 2022-11-01 stsp
5109 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5110 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5111 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5112 2a47b1e5 2022-11-01 stsp goto done;
5113 2a47b1e5 2022-11-01 stsp }
5114 2a47b1e5 2022-11-01 stsp
5115 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5116 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5117 2a47b1e5 2022-11-01 stsp goto done;
5118 2a47b1e5 2022-11-01 stsp }
5119 2a47b1e5 2022-11-01 stsp done:
5120 2a47b1e5 2022-11-01 stsp if (err) {
5121 2a47b1e5 2022-11-01 stsp close(*fd);
5122 2a47b1e5 2022-11-01 stsp *fd = -1;
5123 2a47b1e5 2022-11-01 stsp }
5124 2a47b1e5 2022-11-01 stsp return err;
5125 2a47b1e5 2022-11-01 stsp }
5126 2a47b1e5 2022-11-01 stsp
5127 2a47b1e5 2022-11-01 stsp static const struct got_error *
5128 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5129 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5130 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5131 2a47b1e5 2022-11-01 stsp {
5132 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5133 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5134 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5135 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5136 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5137 2a47b1e5 2022-11-01 stsp struct stat sb;
5138 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5139 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5140 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5141 2a47b1e5 2022-11-01 stsp
5142 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5143 4ba5cca9 2022-11-01 stsp
5144 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5145 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5146 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5147 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5148 4ba5cca9 2022-11-01 stsp return NULL;
5149 4ba5cca9 2022-11-01 stsp } else {
5150 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5151 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5152 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5153 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5154 4ba5cca9 2022-11-01 stsp return NULL;
5155 4ba5cca9 2022-11-01 stsp }
5156 2a47b1e5 2022-11-01 stsp
5157 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5158 2a47b1e5 2022-11-01 stsp if (err)
5159 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5160 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5161 2a47b1e5 2022-11-01 stsp if (err)
5162 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5163 2a47b1e5 2022-11-01 stsp
5164 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5165 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5166 2a47b1e5 2022-11-01 stsp if (err)
5167 2a47b1e5 2022-11-01 stsp return err;
5168 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5169 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5170 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5171 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5172 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5173 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5174 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5175 2a47b1e5 2022-11-01 stsp }
5176 2a47b1e5 2022-11-01 stsp
5177 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5178 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5179 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5180 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5181 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5182 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5183 2a47b1e5 2022-11-01 stsp break;
5184 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5185 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5186 2a47b1e5 2022-11-01 stsp break;
5187 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5188 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5189 2a47b1e5 2022-11-01 stsp break;
5190 2a47b1e5 2022-11-01 stsp default:
5191 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5192 2a47b1e5 2022-11-01 stsp }
5193 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5194 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5195 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5196 2a47b1e5 2022-11-01 stsp goto done;
5197 2a47b1e5 2022-11-01 stsp }
5198 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5199 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5200 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5201 2a47b1e5 2022-11-01 stsp goto done;
5202 2a47b1e5 2022-11-01 stsp }
5203 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5204 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5205 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5206 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5207 2a47b1e5 2022-11-01 stsp goto done;
5208 2a47b1e5 2022-11-01 stsp }
5209 2a47b1e5 2022-11-01 stsp
5210 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5211 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5212 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5213 2a47b1e5 2022-11-01 stsp goto done;
5214 2a47b1e5 2022-11-01 stsp }
5215 2a47b1e5 2022-11-01 stsp
5216 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5217 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5218 2a47b1e5 2022-11-01 stsp 8192, fd1);
5219 2a47b1e5 2022-11-01 stsp if (err)
5220 2a47b1e5 2022-11-01 stsp goto done;
5221 2a47b1e5 2022-11-01 stsp }
5222 2a47b1e5 2022-11-01 stsp
5223 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5224 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5225 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5226 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5227 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5228 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5229 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5230 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5231 2a47b1e5 2022-11-01 stsp goto done;
5232 2a47b1e5 2022-11-01 stsp }
5233 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5234 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5235 2a47b1e5 2022-11-01 stsp if (err)
5236 2a47b1e5 2022-11-01 stsp goto done;
5237 2a47b1e5 2022-11-01 stsp }
5238 2a47b1e5 2022-11-01 stsp } else {
5239 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5240 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5241 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5242 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5243 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5244 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5245 2a47b1e5 2022-11-01 stsp goto done;
5246 2a47b1e5 2022-11-01 stsp }
5247 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5248 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5249 2a47b1e5 2022-11-01 stsp if (err)
5250 2a47b1e5 2022-11-01 stsp goto done;
5251 2a47b1e5 2022-11-01 stsp }
5252 2a47b1e5 2022-11-01 stsp }
5253 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5254 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5255 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5256 2a47b1e5 2022-11-01 stsp goto done;
5257 2a47b1e5 2022-11-01 stsp }
5258 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5259 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5260 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5261 2a47b1e5 2022-11-01 stsp goto done;
5262 2a47b1e5 2022-11-01 stsp }
5263 2a47b1e5 2022-11-01 stsp fd = -1;
5264 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5265 2a47b1e5 2022-11-01 stsp }
5266 2a47b1e5 2022-11-01 stsp
5267 2a47b1e5 2022-11-01 stsp if (blob1) {
5268 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5269 2a47b1e5 2022-11-01 stsp f1, blob1);
5270 2a47b1e5 2022-11-01 stsp if (err)
5271 2a47b1e5 2022-11-01 stsp goto done;
5272 2a47b1e5 2022-11-01 stsp }
5273 2a47b1e5 2022-11-01 stsp
5274 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5275 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5276 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5277 2a47b1e5 2022-11-01 stsp done:
5278 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5279 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5280 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5281 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5282 2a47b1e5 2022-11-01 stsp if (blob1)
5283 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5284 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5285 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5286 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5287 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5288 2a47b1e5 2022-11-01 stsp return err;
5289 2a47b1e5 2022-11-01 stsp }
5290 cf066bf8 2019-05-09 stsp
5291 c4296144 2019-05-09 stsp static const struct got_error *
5292 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5293 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5294 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5295 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5296 c4296144 2019-05-09 stsp {
5297 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5298 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5299 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5300 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5301 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5302 768aea60 2019-05-09 stsp struct stat sb;
5303 c4296144 2019-05-09 stsp
5304 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5305 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5306 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5307 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5308 5f8a88c6 2019-08-03 stsp return NULL;
5309 5f8a88c6 2019-08-03 stsp } else {
5310 12383673 2023-02-18 mark if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5311 12383673 2023-02-18 mark printf("C %s\n", relpath);
5312 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5313 12383673 2023-02-18 mark }
5314 c4296144 2019-05-09 stsp
5315 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5316 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5317 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5318 12383673 2023-02-18 mark status != GOT_STATUS_DELETE &&
5319 12383673 2023-02-18 mark status != GOT_STATUS_CONFLICT)
5320 5f8a88c6 2019-08-03 stsp return NULL;
5321 5f8a88c6 2019-08-03 stsp }
5322 0b5cc0d6 2019-05-09 stsp
5323 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5324 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5325 036813ee 2019-05-09 stsp goto done;
5326 036813ee 2019-05-09 stsp }
5327 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5328 036813ee 2019-05-09 stsp parent_path = strdup("");
5329 69960a46 2019-05-09 stsp if (parent_path == NULL)
5330 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5331 69960a46 2019-05-09 stsp } else {
5332 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5333 69960a46 2019-05-09 stsp if (err)
5334 69960a46 2019-05-09 stsp return err;
5335 69960a46 2019-05-09 stsp }
5336 c4296144 2019-05-09 stsp
5337 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5338 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5339 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5340 c4296144 2019-05-09 stsp goto done;
5341 768aea60 2019-05-09 stsp }
5342 768aea60 2019-05-09 stsp
5343 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5344 768aea60 2019-05-09 stsp relpath) == -1) {
5345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5346 768aea60 2019-05-09 stsp goto done;
5347 768aea60 2019-05-09 stsp }
5348 0aeb8099 2020-07-23 stsp
5349 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5350 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5351 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5352 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5353 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5354 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5355 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5356 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5357 0aeb8099 2020-07-23 stsp break;
5358 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5359 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5360 0aeb8099 2020-07-23 stsp break;
5361 0aeb8099 2020-07-23 stsp default:
5362 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5363 0aeb8099 2020-07-23 stsp goto done;
5364 0aeb8099 2020-07-23 stsp }
5365 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5366 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5367 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5368 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5369 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5370 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5371 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5372 12463d8b 2019-12-13 stsp ct->ondisk_path);
5373 12463d8b 2019-12-13 stsp goto done;
5374 12463d8b 2019-12-13 stsp }
5375 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5376 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5377 768aea60 2019-05-09 stsp goto done;
5378 768aea60 2019-05-09 stsp }
5379 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5380 c4296144 2019-05-09 stsp }
5381 c4296144 2019-05-09 stsp
5382 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5383 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5384 44d03001 2019-05-09 stsp relpath) == -1) {
5385 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5386 44d03001 2019-05-09 stsp goto done;
5387 44d03001 2019-05-09 stsp }
5388 44d03001 2019-05-09 stsp
5389 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5390 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5391 35213c7c 2020-07-23 stsp int is_bad_symlink;
5392 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5393 35213c7c 2020-07-23 stsp ssize_t target_len;
5394 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5395 35213c7c 2020-07-23 stsp sizeof(target_path));
5396 35213c7c 2020-07-23 stsp if (target_len == -1) {
5397 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5398 35213c7c 2020-07-23 stsp ct->ondisk_path);
5399 35213c7c 2020-07-23 stsp goto done;
5400 35213c7c 2020-07-23 stsp }
5401 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5402 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5403 35213c7c 2020-07-23 stsp if (err)
5404 35213c7c 2020-07-23 stsp goto done;
5405 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5406 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5407 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5408 35213c7c 2020-07-23 stsp goto done;
5409 35213c7c 2020-07-23 stsp }
5410 35213c7c 2020-07-23 stsp }
5411 35213c7c 2020-07-23 stsp
5412 35213c7c 2020-07-23 stsp
5413 cf066bf8 2019-05-09 stsp ct->status = status;
5414 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5415 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5416 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5417 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5418 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5419 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5420 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5421 b416585c 2019-05-13 stsp goto done;
5422 b416585c 2019-05-13 stsp }
5423 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5424 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5425 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5426 f0b75401 2019-08-03 stsp goto done;
5427 f0b75401 2019-08-03 stsp }
5428 f0b75401 2019-08-03 stsp }
5429 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5430 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5431 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5432 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5433 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5434 036813ee 2019-05-09 stsp goto done;
5435 036813ee 2019-05-09 stsp }
5436 ca2503ea 2019-05-09 stsp }
5437 24519714 2019-05-09 stsp ct->path = strdup(path);
5438 24519714 2019-05-09 stsp if (ct->path == NULL) {
5439 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5440 24519714 2019-05-09 stsp goto done;
5441 24519714 2019-05-09 stsp }
5442 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5443 2a47b1e5 2022-11-01 stsp if (err)
5444 2a47b1e5 2022-11-01 stsp goto done;
5445 2a47b1e5 2022-11-01 stsp
5446 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5447 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5448 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5449 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5450 2a47b1e5 2022-11-01 stsp if (err)
5451 2a47b1e5 2022-11-01 stsp goto done;
5452 2a47b1e5 2022-11-01 stsp }
5453 c4296144 2019-05-09 stsp done:
5454 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5455 ed175427 2019-05-09 stsp free_commitable(ct);
5456 24519714 2019-05-09 stsp free(parent_path);
5457 036813ee 2019-05-09 stsp free(path);
5458 a129376b 2019-03-28 stsp return err;
5459 a129376b 2019-03-28 stsp }
5460 c4296144 2019-05-09 stsp
5461 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5462 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5463 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5464 036813ee 2019-05-09 stsp struct got_repository *);
5465 ed175427 2019-05-09 stsp
5466 ed175427 2019-05-09 stsp static const struct got_error *
5467 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5468 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5469 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5470 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5471 afa376bf 2019-05-09 stsp struct got_repository *repo)
5472 ed175427 2019-05-09 stsp {
5473 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5474 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5475 036813ee 2019-05-09 stsp char *subpath;
5476 ed175427 2019-05-09 stsp
5477 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5478 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5479 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5480 ed175427 2019-05-09 stsp
5481 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5482 ed175427 2019-05-09 stsp if (err)
5483 ed175427 2019-05-09 stsp return err;
5484 ed175427 2019-05-09 stsp
5485 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5486 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5487 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5488 036813ee 2019-05-09 stsp free(subpath);
5489 036813ee 2019-05-09 stsp return err;
5490 036813ee 2019-05-09 stsp }
5491 ed175427 2019-05-09 stsp
5492 036813ee 2019-05-09 stsp static const struct got_error *
5493 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5494 036813ee 2019-05-09 stsp {
5495 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5496 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5497 ed175427 2019-05-09 stsp
5498 036813ee 2019-05-09 stsp *match = 0;
5499 ed175427 2019-05-09 stsp
5500 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5501 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5502 0f63689d 2019-05-10 stsp return NULL;
5503 036813ee 2019-05-09 stsp }
5504 0b5cc0d6 2019-05-09 stsp
5505 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5506 0f63689d 2019-05-10 stsp if (err)
5507 0f63689d 2019-05-10 stsp return err;
5508 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5509 036813ee 2019-05-09 stsp free(ct_parent_path);
5510 036813ee 2019-05-09 stsp return err;
5511 036813ee 2019-05-09 stsp }
5512 0b5cc0d6 2019-05-09 stsp
5513 768aea60 2019-05-09 stsp static mode_t
5514 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5515 768aea60 2019-05-09 stsp {
5516 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5517 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5518 3d9a4ec4 2020-07-23 stsp
5519 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5520 768aea60 2019-05-09 stsp }
5521 768aea60 2019-05-09 stsp
5522 036813ee 2019-05-09 stsp static const struct got_error *
5523 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5524 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5525 036813ee 2019-05-09 stsp {
5526 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5527 ca2503ea 2019-05-09 stsp
5528 036813ee 2019-05-09 stsp *new_te = NULL;
5529 0b5cc0d6 2019-05-09 stsp
5530 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5531 036813ee 2019-05-09 stsp if (err)
5532 036813ee 2019-05-09 stsp goto done;
5533 0b5cc0d6 2019-05-09 stsp
5534 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5535 036813ee 2019-05-09 stsp
5536 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5537 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5538 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5539 5f8a88c6 2019-08-03 stsp else
5540 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5541 036813ee 2019-05-09 stsp done:
5542 036813ee 2019-05-09 stsp if (err && *new_te) {
5543 56e0773d 2019-11-28 stsp free(*new_te);
5544 036813ee 2019-05-09 stsp *new_te = NULL;
5545 036813ee 2019-05-09 stsp }
5546 036813ee 2019-05-09 stsp return err;
5547 036813ee 2019-05-09 stsp }
5548 036813ee 2019-05-09 stsp
5549 036813ee 2019-05-09 stsp static const struct got_error *
5550 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5551 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5552 036813ee 2019-05-09 stsp {
5553 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5554 102b254e 2020-10-19 stsp char *ct_name = NULL;
5555 036813ee 2019-05-09 stsp
5556 036813ee 2019-05-09 stsp *new_te = NULL;
5557 036813ee 2019-05-09 stsp
5558 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5559 036813ee 2019-05-09 stsp if (*new_te == NULL)
5560 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5561 036813ee 2019-05-09 stsp
5562 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5563 102b254e 2020-10-19 stsp if (err)
5564 036813ee 2019-05-09 stsp goto done;
5565 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5566 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5567 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5568 036813ee 2019-05-09 stsp goto done;
5569 036813ee 2019-05-09 stsp }
5570 036813ee 2019-05-09 stsp
5571 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5572 036813ee 2019-05-09 stsp
5573 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5574 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5575 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5576 5f8a88c6 2019-08-03 stsp else
5577 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5578 036813ee 2019-05-09 stsp done:
5579 102b254e 2020-10-19 stsp free(ct_name);
5580 036813ee 2019-05-09 stsp if (err && *new_te) {
5581 56e0773d 2019-11-28 stsp free(*new_te);
5582 036813ee 2019-05-09 stsp *new_te = NULL;
5583 036813ee 2019-05-09 stsp }
5584 036813ee 2019-05-09 stsp return err;
5585 036813ee 2019-05-09 stsp }
5586 036813ee 2019-05-09 stsp
5587 036813ee 2019-05-09 stsp static const struct got_error *
5588 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5589 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5590 036813ee 2019-05-09 stsp {
5591 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5592 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5593 036813ee 2019-05-09 stsp
5594 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5595 036813ee 2019-05-09 stsp if (err)
5596 036813ee 2019-05-09 stsp return err;
5597 036813ee 2019-05-09 stsp if (new_pe == NULL)
5598 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5599 036813ee 2019-05-09 stsp return NULL;
5600 afa376bf 2019-05-09 stsp }
5601 afa376bf 2019-05-09 stsp
5602 afa376bf 2019-05-09 stsp static const struct got_error *
5603 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5604 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5605 afa376bf 2019-05-09 stsp {
5606 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5607 5f8a88c6 2019-08-03 stsp unsigned char status;
5608 0ff8d236 2021-09-28 stsp
5609 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5610 0ff8d236 2021-09-28 stsp return NULL;
5611 5f8a88c6 2019-08-03 stsp
5612 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5613 afa376bf 2019-05-09 stsp ct_path++;
5614 5f8a88c6 2019-08-03 stsp
5615 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5616 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5617 5f8a88c6 2019-08-03 stsp else
5618 5f8a88c6 2019-08-03 stsp status = ct->status;
5619 5f8a88c6 2019-08-03 stsp
5620 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5621 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5622 036813ee 2019-05-09 stsp }
5623 036813ee 2019-05-09 stsp
5624 036813ee 2019-05-09 stsp static const struct got_error *
5625 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5626 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5627 44d03001 2019-05-09 stsp {
5628 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5629 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5630 44d03001 2019-05-09 stsp char *te_path;
5631 44d03001 2019-05-09 stsp
5632 44d03001 2019-05-09 stsp *modified = 0;
5633 44d03001 2019-05-09 stsp
5634 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5635 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5636 44d03001 2019-05-09 stsp te->name) == -1)
5637 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5638 44d03001 2019-05-09 stsp
5639 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5640 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5641 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5642 44d03001 2019-05-09 stsp strlen(te_path));
5643 62d463ca 2020-10-20 naddy if (*modified)
5644 44d03001 2019-05-09 stsp break;
5645 44d03001 2019-05-09 stsp }
5646 44d03001 2019-05-09 stsp
5647 44d03001 2019-05-09 stsp free(te_path);
5648 44d03001 2019-05-09 stsp return err;
5649 44d03001 2019-05-09 stsp }
5650 44d03001 2019-05-09 stsp
5651 44d03001 2019-05-09 stsp static const struct got_error *
5652 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5653 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5654 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5655 036813ee 2019-05-09 stsp {
5656 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5657 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5658 036813ee 2019-05-09 stsp
5659 036813ee 2019-05-09 stsp *ctp = NULL;
5660 036813ee 2019-05-09 stsp
5661 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5662 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5663 036813ee 2019-05-09 stsp char *ct_name = NULL;
5664 036813ee 2019-05-09 stsp int path_matches;
5665 036813ee 2019-05-09 stsp
5666 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5667 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5668 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5669 12383673 2023-02-18 mark ct->status != GOT_STATUS_DELETE &&
5670 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
5671 5f8a88c6 2019-08-03 stsp continue;
5672 5f8a88c6 2019-08-03 stsp } else {
5673 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5674 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5675 5f8a88c6 2019-08-03 stsp continue;
5676 5f8a88c6 2019-08-03 stsp }
5677 036813ee 2019-05-09 stsp
5678 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5679 036813ee 2019-05-09 stsp continue;
5680 036813ee 2019-05-09 stsp
5681 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5682 62d463ca 2020-10-20 naddy if (err)
5683 036813ee 2019-05-09 stsp return err;
5684 036813ee 2019-05-09 stsp if (!path_matches)
5685 036813ee 2019-05-09 stsp continue;
5686 036813ee 2019-05-09 stsp
5687 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5688 d34b633e 2020-10-19 stsp if (err)
5689 d34b633e 2020-10-19 stsp return err;
5690 d34b633e 2020-10-19 stsp
5691 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5692 d34b633e 2020-10-19 stsp free(ct_name);
5693 036813ee 2019-05-09 stsp continue;
5694 d34b633e 2020-10-19 stsp }
5695 d34b633e 2020-10-19 stsp free(ct_name);
5696 036813ee 2019-05-09 stsp
5697 036813ee 2019-05-09 stsp *ctp = ct;
5698 036813ee 2019-05-09 stsp break;
5699 036813ee 2019-05-09 stsp }
5700 2b496619 2019-07-10 stsp
5701 2b496619 2019-07-10 stsp return err;
5702 2b496619 2019-07-10 stsp }
5703 2b496619 2019-07-10 stsp
5704 2b496619 2019-07-10 stsp static const struct got_error *
5705 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5706 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5707 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5708 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5709 2b496619 2019-07-10 stsp struct got_repository *repo)
5710 2b496619 2019-07-10 stsp {
5711 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5712 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5713 2b496619 2019-07-10 stsp char *subtree_path;
5714 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5715 ba580f68 2020-03-22 stsp int nentries;
5716 2b496619 2019-07-10 stsp
5717 2b496619 2019-07-10 stsp *new_tep = NULL;
5718 2b496619 2019-07-10 stsp
5719 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5720 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5721 2b496619 2019-07-10 stsp child_path) == -1)
5722 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5723 036813ee 2019-05-09 stsp
5724 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5725 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5726 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5727 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5728 56e0773d 2019-11-28 stsp
5729 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5730 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5731 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5732 2b496619 2019-07-10 stsp goto done;
5733 2b496619 2019-07-10 stsp }
5734 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5735 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5736 2b496619 2019-07-10 stsp if (err) {
5737 56e0773d 2019-11-28 stsp free(new_te);
5738 2b496619 2019-07-10 stsp goto done;
5739 2b496619 2019-07-10 stsp }
5740 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5741 2b496619 2019-07-10 stsp done:
5742 56e0773d 2019-11-28 stsp free(id);
5743 2b496619 2019-07-10 stsp free(subtree_path);
5744 2b496619 2019-07-10 stsp if (err == NULL)
5745 2b496619 2019-07-10 stsp *new_tep = new_te;
5746 036813ee 2019-05-09 stsp return err;
5747 036813ee 2019-05-09 stsp }
5748 036813ee 2019-05-09 stsp
5749 036813ee 2019-05-09 stsp static const struct got_error *
5750 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5751 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5752 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5753 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5754 036813ee 2019-05-09 stsp struct got_repository *repo)
5755 036813ee 2019-05-09 stsp {
5756 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5757 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5758 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5759 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5760 036813ee 2019-05-09 stsp
5761 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5762 ba580f68 2020-03-22 stsp *nentries = 0;
5763 036813ee 2019-05-09 stsp
5764 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5765 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5766 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5767 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5768 036813ee 2019-05-09 stsp
5769 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5770 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5771 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5772 036813ee 2019-05-09 stsp continue;
5773 036813ee 2019-05-09 stsp
5774 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5775 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5776 036813ee 2019-05-09 stsp continue;
5777 036813ee 2019-05-09 stsp
5778 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5779 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5780 036813ee 2019-05-09 stsp if (err)
5781 036813ee 2019-05-09 stsp goto done;
5782 036813ee 2019-05-09 stsp
5783 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5784 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5785 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5786 036813ee 2019-05-09 stsp if (err)
5787 036813ee 2019-05-09 stsp goto done;
5788 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5789 afa376bf 2019-05-09 stsp if (err)
5790 afa376bf 2019-05-09 stsp goto done;
5791 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5792 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5793 2b496619 2019-07-10 stsp if (err)
5794 2f51b5b3 2019-05-09 stsp goto done;
5795 ba580f68 2020-03-22 stsp (*nentries)++;
5796 2b496619 2019-07-10 stsp } else {
5797 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5798 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5799 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5800 2b496619 2019-07-10 stsp == NULL) {
5801 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5802 2b496619 2019-07-10 stsp child_path, path_base_tree,
5803 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5804 2b496619 2019-07-10 stsp repo);
5805 2b496619 2019-07-10 stsp if (err)
5806 2b496619 2019-07-10 stsp goto done;
5807 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5808 2b496619 2019-07-10 stsp if (err)
5809 2b496619 2019-07-10 stsp goto done;
5810 ba580f68 2020-03-22 stsp (*nentries)++;
5811 9ba0479c 2019-05-10 stsp }
5812 ed175427 2019-05-09 stsp }
5813 2f51b5b3 2019-05-09 stsp }
5814 2f51b5b3 2019-05-09 stsp
5815 2f51b5b3 2019-05-09 stsp if (base_tree) {
5816 56e0773d 2019-11-28 stsp int i, nbase_entries;
5817 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5818 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5819 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5820 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5821 63c5ca5d 2019-08-24 stsp
5822 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5823 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5824 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5825 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5826 63c5ca5d 2019-08-24 stsp if (err)
5827 63c5ca5d 2019-08-24 stsp goto done;
5828 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5829 63c5ca5d 2019-08-24 stsp if (err)
5830 63c5ca5d 2019-08-24 stsp goto done;
5831 ba580f68 2020-03-22 stsp (*nentries)++;
5832 63c5ca5d 2019-08-24 stsp continue;
5833 63c5ca5d 2019-08-24 stsp }
5834 2f51b5b3 2019-05-09 stsp
5835 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5836 44d03001 2019-05-09 stsp int modified;
5837 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5838 036813ee 2019-05-09 stsp if (err)
5839 036813ee 2019-05-09 stsp goto done;
5840 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5841 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5842 2f51b5b3 2019-05-09 stsp if (err)
5843 2f51b5b3 2019-05-09 stsp goto done;
5844 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5845 44d03001 2019-05-09 stsp if (modified) {
5846 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5847 ba580f68 2020-03-22 stsp int nsubentries;
5848 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5849 ba580f68 2020-03-22 stsp &nsubentries, te,
5850 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5851 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5852 44d03001 2019-05-09 stsp if (err)
5853 44d03001 2019-05-09 stsp goto done;
5854 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5855 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5856 ba580f68 2020-03-22 stsp free(new_id);
5857 ba580f68 2020-03-22 stsp continue;
5858 ba580f68 2020-03-22 stsp }
5859 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5860 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5861 56e0773d 2019-11-28 stsp free(new_id);
5862 44d03001 2019-05-09 stsp }
5863 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5864 036813ee 2019-05-09 stsp if (err)
5865 036813ee 2019-05-09 stsp goto done;
5866 ba580f68 2020-03-22 stsp (*nentries)++;
5867 2f51b5b3 2019-05-09 stsp continue;
5868 036813ee 2019-05-09 stsp }
5869 2f51b5b3 2019-05-09 stsp
5870 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5871 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5872 f66c734c 2019-09-22 stsp if (err)
5873 f66c734c 2019-09-22 stsp goto done;
5874 2f51b5b3 2019-05-09 stsp if (ct) {
5875 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5876 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5877 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5878 12383673 2023-02-18 mark ct->status == GOT_STATUS_CONFLICT ||
5879 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5880 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5881 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5882 2f51b5b3 2019-05-09 stsp if (err)
5883 2f51b5b3 2019-05-09 stsp goto done;
5884 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5885 2f51b5b3 2019-05-09 stsp if (err)
5886 2f51b5b3 2019-05-09 stsp goto done;
5887 ba580f68 2020-03-22 stsp (*nentries)++;
5888 2f51b5b3 2019-05-09 stsp }
5889 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5890 b416585c 2019-05-13 stsp status_arg);
5891 afa376bf 2019-05-09 stsp if (err)
5892 afa376bf 2019-05-09 stsp goto done;
5893 2f51b5b3 2019-05-09 stsp } else {
5894 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5895 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5896 2f51b5b3 2019-05-09 stsp if (err)
5897 2f51b5b3 2019-05-09 stsp goto done;
5898 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5899 2f51b5b3 2019-05-09 stsp if (err)
5900 2f51b5b3 2019-05-09 stsp goto done;
5901 ba580f68 2020-03-22 stsp (*nentries)++;
5902 2f51b5b3 2019-05-09 stsp }
5903 ca2503ea 2019-05-09 stsp }
5904 ed175427 2019-05-09 stsp }
5905 0b5cc0d6 2019-05-09 stsp
5906 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5907 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5908 ed175427 2019-05-09 stsp done:
5909 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5910 2e1fa222 2020-07-23 stsp return err;
5911 2e1fa222 2020-07-23 stsp }
5912 2e1fa222 2020-07-23 stsp
5913 2e1fa222 2020-07-23 stsp static const struct got_error *
5914 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5915 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5916 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5917 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5918 ebf99748 2019-05-09 stsp {
5919 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5920 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5921 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5922 ebf99748 2019-05-09 stsp
5923 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5924 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5925 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5926 ebf99748 2019-05-09 stsp
5927 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5928 437adc9d 2020-12-10 yzhong
5929 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5930 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5931 437adc9d 2020-12-10 yzhong if (err)
5932 437adc9d 2020-12-10 yzhong goto done;
5933 437adc9d 2020-12-10 yzhong
5934 ebf99748 2019-05-09 stsp if (ie) {
5935 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5936 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5937 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5938 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5939 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5940 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5941 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5942 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5943 437adc9d 2020-12-10 yzhong
5944 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5945 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5946 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5947 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5948 72fd46fa 2019-09-06 stsp !have_staged_files);
5949 ebf99748 2019-05-09 stsp } else
5950 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5951 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5952 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5953 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5954 72fd46fa 2019-09-06 stsp !have_staged_files);
5955 ebf99748 2019-05-09 stsp } else {
5956 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5957 ebf99748 2019-05-09 stsp if (err)
5958 437adc9d 2020-12-10 yzhong goto done;
5959 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5960 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5961 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5962 3969253a 2020-03-07 stsp if (err) {
5963 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5964 437adc9d 2020-12-10 yzhong goto done;
5965 3969253a 2020-03-07 stsp }
5966 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5967 3969253a 2020-03-07 stsp if (err) {
5968 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5969 437adc9d 2020-12-10 yzhong goto done;
5970 3969253a 2020-03-07 stsp }
5971 ebf99748 2019-05-09 stsp }
5972 437adc9d 2020-12-10 yzhong free(relpath);
5973 437adc9d 2020-12-10 yzhong relpath = NULL;
5974 ebf99748 2019-05-09 stsp }
5975 437adc9d 2020-12-10 yzhong done:
5976 437adc9d 2020-12-10 yzhong free(relpath);
5977 d56d26ce 2019-05-10 stsp return err;
5978 d56d26ce 2019-05-10 stsp }
5979 735ef5ac 2019-08-03 stsp
5980 d56d26ce 2019-05-10 stsp
5981 d56d26ce 2019-05-10 stsp static const struct got_error *
5982 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5983 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5984 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5985 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5986 735ef5ac 2019-08-03 stsp int ood_errcode)
5987 d56d26ce 2019-05-10 stsp {
5988 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5989 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5990 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5991 1a36436d 2019-06-10 stsp
5992 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5993 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5994 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5995 1a36436d 2019-06-10 stsp return NULL;
5996 9bead371 2019-07-28 stsp /*
5997 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5998 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5999 9bead371 2019-07-28 stsp */
6000 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6001 a44927cc 2022-04-07 stsp if (err)
6002 a44927cc 2022-04-07 stsp goto done;
6003 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6004 9bead371 2019-07-28 stsp if (err) {
6005 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
6006 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
6007 1a36436d 2019-06-10 stsp goto done;
6008 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
6009 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
6010 1a36436d 2019-06-10 stsp } else {
6011 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
6012 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
6013 a44927cc 2022-04-07 stsp if (err)
6014 a44927cc 2022-04-07 stsp goto done;
6015 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6016 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6017 1a36436d 2019-06-10 stsp goto done;
6018 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
6019 1a36436d 2019-06-10 stsp }
6020 1a36436d 2019-06-10 stsp done:
6021 1a36436d 2019-06-10 stsp free(id);
6022 a44927cc 2022-04-07 stsp if (commit)
6023 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6024 1a36436d 2019-06-10 stsp return err;
6025 ebf99748 2019-05-09 stsp }
6026 ebf99748 2019-05-09 stsp
6027 336075a4 2022-06-25 op static const struct got_error *
6028 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
6029 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
6030 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
6031 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
6032 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
6033 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
6034 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6035 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
6036 afa376bf 2019-05-09 stsp struct got_repository *repo)
6037 c4296144 2019-05-09 stsp {
6038 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6039 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
6040 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
6041 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
6042 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
6043 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
6044 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
6045 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
6046 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
6047 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
6048 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
6049 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
6050 f8399b8f 2022-07-22 stsp time_t timestamp;
6051 c4296144 2019-05-09 stsp
6052 c4296144 2019-05-09 stsp *new_commit_id = NULL;
6053 c4296144 2019-05-09 stsp
6054 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
6055 675c7539 2019-05-09 stsp
6056 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6057 588edf97 2019-05-10 stsp if (err)
6058 588edf97 2019-05-10 stsp goto done;
6059 de18fc63 2019-05-09 stsp
6060 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6061 588edf97 2019-05-10 stsp if (err)
6062 588edf97 2019-05-10 stsp goto done;
6063 588edf97 2019-05-10 stsp
6064 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
6065 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
6066 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
6067 33ad4cbe 2019-05-12 jcs if (err)
6068 33ad4cbe 2019-05-12 jcs goto done;
6069 33ad4cbe 2019-05-12 jcs }
6070 c4296144 2019-05-09 stsp
6071 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
6072 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6073 33ad4cbe 2019-05-12 jcs goto done;
6074 33ad4cbe 2019-05-12 jcs }
6075 33ad4cbe 2019-05-12 jcs
6076 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
6077 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6078 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6079 cf066bf8 2019-05-09 stsp char *ondisk_path;
6080 cf066bf8 2019-05-09 stsp
6081 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
6082 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
6083 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
6084 5f8a88c6 2019-08-03 stsp continue;
6085 5f8a88c6 2019-08-03 stsp
6086 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
6087 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
6088 12383673 2023-02-18 mark ct->status != GOT_STATUS_MODE_CHANGE &&
6089 12383673 2023-02-18 mark ct->status != GOT_STATUS_CONFLICT)
6090 cf066bf8 2019-05-09 stsp continue;
6091 cf066bf8 2019-05-09 stsp
6092 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
6093 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
6094 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6095 cf066bf8 2019-05-09 stsp goto done;
6096 cf066bf8 2019-05-09 stsp }
6097 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6098 2e1fa222 2020-07-23 stsp free(ondisk_path);
6099 2e1fa222 2020-07-23 stsp if (err)
6100 cf066bf8 2019-05-09 stsp goto done;
6101 cf066bf8 2019-05-09 stsp }
6102 036813ee 2019-05-09 stsp
6103 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
6104 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6105 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
6106 036813ee 2019-05-09 stsp if (err)
6107 036813ee 2019-05-09 stsp goto done;
6108 036813ee 2019-05-09 stsp
6109 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6110 de18fc63 2019-05-09 stsp if (err)
6111 de18fc63 2019-05-09 stsp goto done;
6112 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6113 f259c4c1 2021-09-24 stsp nparents++;
6114 f259c4c1 2021-09-24 stsp if (parent_id2) {
6115 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6116 f259c4c1 2021-09-24 stsp if (err)
6117 f259c4c1 2021-09-24 stsp goto done;
6118 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6119 f259c4c1 2021-09-24 stsp nparents++;
6120 f259c4c1 2021-09-24 stsp }
6121 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6122 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6123 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6124 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6125 33ad4cbe 2019-05-12 jcs free(logmsg);
6126 9d40349a 2019-05-09 stsp if (err)
6127 9d40349a 2019-05-09 stsp goto done;
6128 9d40349a 2019-05-09 stsp
6129 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6130 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6131 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6132 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6133 09f5bd90 2019-05-09 stsp goto done;
6134 09f5bd90 2019-05-09 stsp }
6135 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6136 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6137 09f5bd90 2019-05-09 stsp if (err)
6138 09f5bd90 2019-05-09 stsp goto done;
6139 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6140 ebf99748 2019-05-09 stsp if (err)
6141 ebf99748 2019-05-09 stsp goto done;
6142 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6143 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6144 09f5bd90 2019-05-09 stsp goto done;
6145 09f5bd90 2019-05-09 stsp }
6146 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6147 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6148 f2c16586 2019-05-09 stsp if (err)
6149 f2c16586 2019-05-09 stsp goto done;
6150 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6151 f2c16586 2019-05-09 stsp if (err)
6152 f2c16586 2019-05-09 stsp goto done;
6153 f2c16586 2019-05-09 stsp
6154 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6155 09f5bd90 2019-05-09 stsp if (err)
6156 09f5bd90 2019-05-09 stsp goto done;
6157 09f5bd90 2019-05-09 stsp
6158 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6159 ebf99748 2019-05-09 stsp if (err)
6160 ebf99748 2019-05-09 stsp goto done;
6161 c4296144 2019-05-09 stsp done:
6162 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6163 588edf97 2019-05-10 stsp if (head_tree)
6164 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6165 588edf97 2019-05-10 stsp if (head_commit)
6166 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6167 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6168 2f17228e 2019-05-12 stsp if (head_ref2) {
6169 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6170 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6171 2f17228e 2019-05-12 stsp err = unlockerr;
6172 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6173 39cd0ff6 2019-07-12 stsp }
6174 39cd0ff6 2019-07-12 stsp return err;
6175 39cd0ff6 2019-07-12 stsp }
6176 5c1e53bc 2019-07-28 stsp
6177 5c1e53bc 2019-07-28 stsp static const struct got_error *
6178 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6179 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6180 5c1e53bc 2019-07-28 stsp {
6181 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6182 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6183 39cd0ff6 2019-07-12 stsp
6184 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6185 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6186 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6187 5c1e53bc 2019-07-28 stsp
6188 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6189 5c1e53bc 2019-07-28 stsp ct_path++;
6190 5c1e53bc 2019-07-28 stsp
6191 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6192 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6193 5c1e53bc 2019-07-28 stsp break;
6194 5c1e53bc 2019-07-28 stsp }
6195 5c1e53bc 2019-07-28 stsp
6196 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6197 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6198 5c1e53bc 2019-07-28 stsp
6199 5c1e53bc 2019-07-28 stsp return NULL;
6200 5c1e53bc 2019-07-28 stsp }
6201 5c1e53bc 2019-07-28 stsp
6202 f0b75401 2019-08-03 stsp static const struct got_error *
6203 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6204 f0b75401 2019-08-03 stsp {
6205 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6206 f0b75401 2019-08-03 stsp
6207 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6208 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6209 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6210 f0b75401 2019-08-03 stsp }
6211 f0b75401 2019-08-03 stsp
6212 f0b75401 2019-08-03 stsp return NULL;
6213 f0b75401 2019-08-03 stsp }
6214 f0b75401 2019-08-03 stsp
6215 5f8a88c6 2019-08-03 stsp static const struct got_error *
6216 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6217 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6218 5f8a88c6 2019-08-03 stsp {
6219 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6220 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6221 5f8a88c6 2019-08-03 stsp
6222 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6223 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6224 5f8a88c6 2019-08-03 stsp continue;
6225 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6226 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6227 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6228 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6229 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6230 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6231 5f8a88c6 2019-08-03 stsp }
6232 5f8a88c6 2019-08-03 stsp
6233 5f8a88c6 2019-08-03 stsp return NULL;
6234 5f8a88c6 2019-08-03 stsp }
6235 5f8a88c6 2019-08-03 stsp
6236 39cd0ff6 2019-07-12 stsp const struct got_error *
6237 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6238 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6239 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6240 12383673 2023-02-18 mark int show_diff, int commit_conflicts,
6241 12383673 2023-02-18 mark got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6242 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6243 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6244 39cd0ff6 2019-07-12 stsp {
6245 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6246 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6247 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6248 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6249 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6250 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6251 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6252 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6253 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6254 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6255 39cd0ff6 2019-07-12 stsp
6256 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6257 39cd0ff6 2019-07-12 stsp
6258 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6259 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6260 39cd0ff6 2019-07-12 stsp
6261 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6262 39cd0ff6 2019-07-12 stsp if (err)
6263 39cd0ff6 2019-07-12 stsp goto done;
6264 39cd0ff6 2019-07-12 stsp
6265 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6266 39cd0ff6 2019-07-12 stsp if (err)
6267 39cd0ff6 2019-07-12 stsp goto done;
6268 39cd0ff6 2019-07-12 stsp
6269 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6270 39cd0ff6 2019-07-12 stsp if (err)
6271 39cd0ff6 2019-07-12 stsp goto done;
6272 39cd0ff6 2019-07-12 stsp
6273 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6274 39cd0ff6 2019-07-12 stsp if (err)
6275 39cd0ff6 2019-07-12 stsp goto done;
6276 39cd0ff6 2019-07-12 stsp
6277 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6278 5f8a88c6 2019-08-03 stsp &have_staged_files);
6279 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6280 5f8a88c6 2019-08-03 stsp goto done;
6281 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6282 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6283 5f8a88c6 2019-08-03 stsp if (err)
6284 5f8a88c6 2019-08-03 stsp goto done;
6285 5f8a88c6 2019-08-03 stsp }
6286 5f8a88c6 2019-08-03 stsp
6287 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6288 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6289 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6290 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6291 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6292 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6293 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6294 12383673 2023-02-18 mark cc_arg.commit_conflicts = commit_conflicts;
6295 2a47b1e5 2022-11-01 stsp if (show_diff) {
6296 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6297 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6298 2a47b1e5 2022-11-01 stsp if (err)
6299 2a47b1e5 2022-11-01 stsp goto done;
6300 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6301 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6302 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6303 2a47b1e5 2022-11-01 stsp goto done;
6304 2a47b1e5 2022-11-01 stsp }
6305 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6306 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6307 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6308 2a47b1e5 2022-11-01 stsp goto done;
6309 2a47b1e5 2022-11-01 stsp }
6310 2a47b1e5 2022-11-01 stsp }
6311 2a47b1e5 2022-11-01 stsp
6312 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6313 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6314 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6315 5c1e53bc 2019-07-28 stsp if (err)
6316 5c1e53bc 2019-07-28 stsp goto done;
6317 5c1e53bc 2019-07-28 stsp }
6318 39cd0ff6 2019-07-12 stsp
6319 2a47b1e5 2022-11-01 stsp if (show_diff) {
6320 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6321 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6322 2a47b1e5 2022-11-01 stsp goto done;
6323 2a47b1e5 2022-11-01 stsp }
6324 2a47b1e5 2022-11-01 stsp }
6325 2a47b1e5 2022-11-01 stsp
6326 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6327 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6328 39cd0ff6 2019-07-12 stsp goto done;
6329 5c1e53bc 2019-07-28 stsp }
6330 5c1e53bc 2019-07-28 stsp
6331 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6332 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6333 5c1e53bc 2019-07-28 stsp if (err)
6334 5c1e53bc 2019-07-28 stsp goto done;
6335 39cd0ff6 2019-07-12 stsp }
6336 f0b75401 2019-08-03 stsp
6337 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6338 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6339 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6340 f0b75401 2019-08-03 stsp
6341 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6342 f0b75401 2019-08-03 stsp ct_path++;
6343 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6344 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6345 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6346 b50cabdf 2019-07-12 stsp if (err)
6347 b50cabdf 2019-07-12 stsp goto done;
6348 f0b75401 2019-08-03 stsp
6349 b50cabdf 2019-07-12 stsp }
6350 b50cabdf 2019-07-12 stsp
6351 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6352 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6353 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6354 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6355 39cd0ff6 2019-07-12 stsp if (err)
6356 39cd0ff6 2019-07-12 stsp goto done;
6357 39cd0ff6 2019-07-12 stsp
6358 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6359 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6360 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6361 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6362 39cd0ff6 2019-07-12 stsp err = sync_err;
6363 39cd0ff6 2019-07-12 stsp done:
6364 39cd0ff6 2019-07-12 stsp if (fileindex)
6365 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6366 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6367 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6368 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6369 39cd0ff6 2019-07-12 stsp err = unlockerr;
6370 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6371 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6372 d8bacb93 2023-01-10 mark
6373 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6374 39cd0ff6 2019-07-12 stsp }
6375 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6376 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6377 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6378 2a47b1e5 2022-11-01 stsp free(diff_path);
6379 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6380 2a47b1e5 2022-11-01 stsp err == NULL)
6381 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6382 c4296144 2019-05-09 stsp return err;
6383 8656d6c4 2019-05-20 stsp }
6384 8656d6c4 2019-05-20 stsp
6385 8656d6c4 2019-05-20 stsp const char *
6386 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6387 8656d6c4 2019-05-20 stsp {
6388 8656d6c4 2019-05-20 stsp return ct->path;
6389 8656d6c4 2019-05-20 stsp }
6390 8656d6c4 2019-05-20 stsp
6391 8656d6c4 2019-05-20 stsp unsigned int
6392 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6393 8656d6c4 2019-05-20 stsp {
6394 8656d6c4 2019-05-20 stsp return ct->status;
6395 818c7501 2019-07-11 stsp }
6396 818c7501 2019-07-11 stsp
6397 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6398 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6399 818c7501 2019-07-11 stsp struct got_repository *repo;
6400 818c7501 2019-07-11 stsp };
6401 818c7501 2019-07-11 stsp
6402 818c7501 2019-07-11 stsp static const struct got_error *
6403 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6404 818c7501 2019-07-11 stsp {
6405 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6406 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6407 818c7501 2019-07-11 stsp unsigned char status;
6408 818c7501 2019-07-11 stsp struct stat sb;
6409 818c7501 2019-07-11 stsp char *ondisk_path;
6410 818c7501 2019-07-11 stsp
6411 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6412 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6413 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6414 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6415 818c7501 2019-07-11 stsp
6416 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6417 818c7501 2019-07-11 stsp == -1)
6418 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6419 818c7501 2019-07-11 stsp
6420 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6421 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6422 818c7501 2019-07-11 stsp free(ondisk_path);
6423 818c7501 2019-07-11 stsp if (err)
6424 818c7501 2019-07-11 stsp return err;
6425 818c7501 2019-07-11 stsp
6426 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6427 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6428 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6429 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6430 818c7501 2019-07-11 stsp
6431 818c7501 2019-07-11 stsp return NULL;
6432 818c7501 2019-07-11 stsp }
6433 818c7501 2019-07-11 stsp
6434 818c7501 2019-07-11 stsp const struct got_error *
6435 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6436 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6437 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6438 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6439 818c7501 2019-07-11 stsp {
6440 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6441 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6442 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6443 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6444 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6445 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6446 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6447 818c7501 2019-07-11 stsp
6448 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6449 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6450 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6451 818c7501 2019-07-11 stsp
6452 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6453 818c7501 2019-07-11 stsp if (err)
6454 818c7501 2019-07-11 stsp return err;
6455 818c7501 2019-07-11 stsp
6456 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
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 ok_arg.worktree = worktree;
6461 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6462 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6463 818c7501 2019-07-11 stsp &ok_arg);
6464 818c7501 2019-07-11 stsp if (err)
6465 818c7501 2019-07-11 stsp goto done;
6466 818c7501 2019-07-11 stsp
6467 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6468 818c7501 2019-07-11 stsp if (err)
6469 818c7501 2019-07-11 stsp goto done;
6470 818c7501 2019-07-11 stsp
6471 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6472 818c7501 2019-07-11 stsp if (err)
6473 818c7501 2019-07-11 stsp goto done;
6474 818c7501 2019-07-11 stsp
6475 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6476 818c7501 2019-07-11 stsp if (err)
6477 818c7501 2019-07-11 stsp goto done;
6478 818c7501 2019-07-11 stsp
6479 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6480 818c7501 2019-07-11 stsp 0);
6481 e51d7b55 2020-01-04 stsp if (err)
6482 e51d7b55 2020-01-04 stsp goto done;
6483 e51d7b55 2020-01-04 stsp
6484 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6485 818c7501 2019-07-11 stsp if (err)
6486 818c7501 2019-07-11 stsp goto done;
6487 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6488 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6489 e51d7b55 2020-01-04 stsp goto done;
6490 e51d7b55 2020-01-04 stsp }
6491 818c7501 2019-07-11 stsp
6492 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6493 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6494 818c7501 2019-07-11 stsp if (err)
6495 818c7501 2019-07-11 stsp goto done;
6496 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6497 818c7501 2019-07-11 stsp if (err)
6498 818c7501 2019-07-11 stsp goto done;
6499 818c7501 2019-07-11 stsp
6500 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6501 818c7501 2019-07-11 stsp
6502 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6503 818c7501 2019-07-11 stsp if (err)
6504 818c7501 2019-07-11 stsp goto done;
6505 818c7501 2019-07-11 stsp
6506 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6507 818c7501 2019-07-11 stsp if (err)
6508 818c7501 2019-07-11 stsp goto done;
6509 818c7501 2019-07-11 stsp
6510 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6511 818c7501 2019-07-11 stsp worktree->base_commit_id);
6512 818c7501 2019-07-11 stsp if (err)
6513 818c7501 2019-07-11 stsp goto done;
6514 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6515 818c7501 2019-07-11 stsp if (err)
6516 818c7501 2019-07-11 stsp goto done;
6517 818c7501 2019-07-11 stsp
6518 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6519 818c7501 2019-07-11 stsp if (err)
6520 818c7501 2019-07-11 stsp goto done;
6521 818c7501 2019-07-11 stsp done:
6522 818c7501 2019-07-11 stsp free(fileindex_path);
6523 818c7501 2019-07-11 stsp free(tmp_branch_name);
6524 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6525 818c7501 2019-07-11 stsp free(branch_ref_name);
6526 818c7501 2019-07-11 stsp if (branch_ref)
6527 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6528 818c7501 2019-07-11 stsp if (wt_branch)
6529 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6530 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6531 818c7501 2019-07-11 stsp if (err) {
6532 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6533 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6534 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6535 818c7501 2019-07-11 stsp }
6536 818c7501 2019-07-11 stsp if (*tmp_branch) {
6537 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6538 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6539 818c7501 2019-07-11 stsp }
6540 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6541 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6542 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6543 3e3a69f1 2019-07-25 stsp }
6544 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6545 818c7501 2019-07-11 stsp }
6546 818c7501 2019-07-11 stsp return err;
6547 818c7501 2019-07-11 stsp }
6548 818c7501 2019-07-11 stsp
6549 818c7501 2019-07-11 stsp const struct got_error *
6550 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6551 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6552 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6553 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6554 818c7501 2019-07-11 stsp {
6555 818c7501 2019-07-11 stsp const struct got_error *err;
6556 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6557 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6558 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6559 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6560 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6561 818c7501 2019-07-11 stsp
6562 818c7501 2019-07-11 stsp *commit_id = NULL;
6563 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6564 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6565 3e3a69f1 2019-07-25 stsp *branch = NULL;
6566 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6567 3e3a69f1 2019-07-25 stsp
6568 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6569 3e3a69f1 2019-07-25 stsp if (err)
6570 3e3a69f1 2019-07-25 stsp return err;
6571 818c7501 2019-07-11 stsp
6572 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6573 3e3a69f1 2019-07-25 stsp if (err)
6574 f032f1f7 2019-08-04 stsp goto done;
6575 f032f1f7 2019-08-04 stsp
6576 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6577 f032f1f7 2019-08-04 stsp &have_staged_files);
6578 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6579 f032f1f7 2019-08-04 stsp goto done;
6580 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6581 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6582 3e3a69f1 2019-07-25 stsp goto done;
6583 f032f1f7 2019-08-04 stsp }
6584 3e3a69f1 2019-07-25 stsp
6585 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6586 818c7501 2019-07-11 stsp if (err)
6587 f032f1f7 2019-08-04 stsp goto done;
6588 818c7501 2019-07-11 stsp
6589 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6590 818c7501 2019-07-11 stsp if (err)
6591 818c7501 2019-07-11 stsp goto done;
6592 818c7501 2019-07-11 stsp
6593 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6594 818c7501 2019-07-11 stsp if (err)
6595 818c7501 2019-07-11 stsp goto done;
6596 818c7501 2019-07-11 stsp
6597 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6598 818c7501 2019-07-11 stsp if (err)
6599 818c7501 2019-07-11 stsp goto done;
6600 818c7501 2019-07-11 stsp
6601 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6602 818c7501 2019-07-11 stsp if (err)
6603 818c7501 2019-07-11 stsp goto done;
6604 818c7501 2019-07-11 stsp
6605 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6606 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6607 818c7501 2019-07-11 stsp if (err)
6608 818c7501 2019-07-11 stsp goto done;
6609 818c7501 2019-07-11 stsp
6610 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6611 818c7501 2019-07-11 stsp if (err)
6612 818c7501 2019-07-11 stsp goto done;
6613 818c7501 2019-07-11 stsp
6614 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6615 818c7501 2019-07-11 stsp if (err)
6616 818c7501 2019-07-11 stsp goto done;
6617 818c7501 2019-07-11 stsp
6618 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6619 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6620 818c7501 2019-07-11 stsp if (err)
6621 818c7501 2019-07-11 stsp goto done;
6622 818c7501 2019-07-11 stsp
6623 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6624 818c7501 2019-07-11 stsp if (err)
6625 818c7501 2019-07-11 stsp goto done;
6626 818c7501 2019-07-11 stsp done:
6627 818c7501 2019-07-11 stsp free(commit_ref_name);
6628 818c7501 2019-07-11 stsp free(branch_ref_name);
6629 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6630 818c7501 2019-07-11 stsp if (commit_ref)
6631 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6632 818c7501 2019-07-11 stsp if (branch_ref)
6633 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6634 818c7501 2019-07-11 stsp if (err) {
6635 818c7501 2019-07-11 stsp free(*commit_id);
6636 818c7501 2019-07-11 stsp *commit_id = NULL;
6637 818c7501 2019-07-11 stsp if (*tmp_branch) {
6638 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6639 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6640 818c7501 2019-07-11 stsp }
6641 818c7501 2019-07-11 stsp if (*new_base_branch) {
6642 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6643 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6644 818c7501 2019-07-11 stsp }
6645 818c7501 2019-07-11 stsp if (*branch) {
6646 818c7501 2019-07-11 stsp got_ref_close(*branch);
6647 818c7501 2019-07-11 stsp *branch = NULL;
6648 818c7501 2019-07-11 stsp }
6649 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6650 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6651 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6652 3e3a69f1 2019-07-25 stsp }
6653 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6654 818c7501 2019-07-11 stsp }
6655 818c7501 2019-07-11 stsp return err;
6656 c4296144 2019-05-09 stsp }
6657 818c7501 2019-07-11 stsp
6658 818c7501 2019-07-11 stsp const struct got_error *
6659 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6660 818c7501 2019-07-11 stsp {
6661 818c7501 2019-07-11 stsp const struct got_error *err;
6662 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6663 818c7501 2019-07-11 stsp
6664 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6665 818c7501 2019-07-11 stsp if (err)
6666 818c7501 2019-07-11 stsp return err;
6667 818c7501 2019-07-11 stsp
6668 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6669 818c7501 2019-07-11 stsp free(tmp_branch_name);
6670 818c7501 2019-07-11 stsp return NULL;
6671 818c7501 2019-07-11 stsp }
6672 818c7501 2019-07-11 stsp
6673 818c7501 2019-07-11 stsp static const struct got_error *
6674 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6675 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6676 818c7501 2019-07-11 stsp {
6677 0ebf8283 2019-07-24 stsp *logmsg = arg;
6678 818c7501 2019-07-11 stsp return NULL;
6679 818c7501 2019-07-11 stsp }
6680 818c7501 2019-07-11 stsp
6681 818c7501 2019-07-11 stsp static const struct got_error *
6682 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6683 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6684 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6685 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6686 818c7501 2019-07-11 stsp {
6687 818c7501 2019-07-11 stsp return NULL;
6688 01757395 2019-07-12 stsp }
6689 01757395 2019-07-12 stsp
6690 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6691 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6692 01757395 2019-07-12 stsp void *progress_arg;
6693 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6694 01757395 2019-07-12 stsp };
6695 01757395 2019-07-12 stsp
6696 01757395 2019-07-12 stsp static const struct got_error *
6697 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6698 01757395 2019-07-12 stsp {
6699 01757395 2019-07-12 stsp const struct got_error *err;
6700 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6701 01757395 2019-07-12 stsp char *p;
6702 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6703 01757395 2019-07-12 stsp
6704 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6705 01757395 2019-07-12 stsp if (err)
6706 01757395 2019-07-12 stsp return err;
6707 01757395 2019-07-12 stsp
6708 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6709 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6710 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6711 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6712 01757395 2019-07-12 stsp return NULL;
6713 01757395 2019-07-12 stsp
6714 01757395 2019-07-12 stsp p = strdup(path);
6715 01757395 2019-07-12 stsp if (p == NULL)
6716 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6717 01757395 2019-07-12 stsp
6718 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6719 01757395 2019-07-12 stsp if (err || new == NULL)
6720 01757395 2019-07-12 stsp free(p);
6721 01757395 2019-07-12 stsp return err;
6722 818c7501 2019-07-11 stsp }
6723 818c7501 2019-07-11 stsp
6724 0ebf8283 2019-07-24 stsp static const struct got_error *
6725 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6726 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6727 818c7501 2019-07-11 stsp {
6728 818c7501 2019-07-11 stsp const struct got_error *err;
6729 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6730 818c7501 2019-07-11 stsp
6731 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6732 818c7501 2019-07-11 stsp if (err) {
6733 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6734 818c7501 2019-07-11 stsp goto done;
6735 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6736 818c7501 2019-07-11 stsp if (err)
6737 818c7501 2019-07-11 stsp goto done;
6738 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6739 818c7501 2019-07-11 stsp if (err)
6740 818c7501 2019-07-11 stsp goto done;
6741 de05890f 2020-03-05 stsp } else if (is_rebase) {
6742 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6743 818c7501 2019-07-11 stsp int cmp;
6744 818c7501 2019-07-11 stsp
6745 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6746 818c7501 2019-07-11 stsp if (err)
6747 818c7501 2019-07-11 stsp goto done;
6748 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6749 818c7501 2019-07-11 stsp free(stored_id);
6750 818c7501 2019-07-11 stsp if (cmp != 0) {
6751 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6752 818c7501 2019-07-11 stsp goto done;
6753 818c7501 2019-07-11 stsp }
6754 818c7501 2019-07-11 stsp }
6755 0ebf8283 2019-07-24 stsp done:
6756 0ebf8283 2019-07-24 stsp if (commit_ref)
6757 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6758 0ebf8283 2019-07-24 stsp return err;
6759 0ebf8283 2019-07-24 stsp }
6760 0ebf8283 2019-07-24 stsp
6761 0ebf8283 2019-07-24 stsp static const struct got_error *
6762 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6763 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6764 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6765 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6766 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6767 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6768 0ebf8283 2019-07-24 stsp {
6769 0ebf8283 2019-07-24 stsp const struct got_error *err;
6770 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6771 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6772 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6773 818c7501 2019-07-11 stsp
6774 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6775 0ebf8283 2019-07-24 stsp
6776 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6777 0ebf8283 2019-07-24 stsp if (err)
6778 0ebf8283 2019-07-24 stsp return err;
6779 0ebf8283 2019-07-24 stsp
6780 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6781 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6782 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6783 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6784 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6785 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6786 818c7501 2019-07-11 stsp if (commit_ref)
6787 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6788 818c7501 2019-07-11 stsp return err;
6789 818c7501 2019-07-11 stsp }
6790 818c7501 2019-07-11 stsp
6791 818c7501 2019-07-11 stsp const struct got_error *
6792 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6793 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6794 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6795 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6796 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6797 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6798 818c7501 2019-07-11 stsp {
6799 0ebf8283 2019-07-24 stsp const struct got_error *err;
6800 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6801 0ebf8283 2019-07-24 stsp
6802 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6803 0ebf8283 2019-07-24 stsp if (err)
6804 0ebf8283 2019-07-24 stsp return err;
6805 0ebf8283 2019-07-24 stsp
6806 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6807 0ebf8283 2019-07-24 stsp if (err)
6808 0ebf8283 2019-07-24 stsp goto done;
6809 0ebf8283 2019-07-24 stsp
6810 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6811 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6812 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6813 0ebf8283 2019-07-24 stsp done:
6814 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6815 0ebf8283 2019-07-24 stsp return err;
6816 0ebf8283 2019-07-24 stsp }
6817 0ebf8283 2019-07-24 stsp
6818 0ebf8283 2019-07-24 stsp const struct got_error *
6819 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6820 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6821 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6822 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6823 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6824 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6825 0ebf8283 2019-07-24 stsp {
6826 0ebf8283 2019-07-24 stsp const struct got_error *err;
6827 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6830 0ebf8283 2019-07-24 stsp if (err)
6831 0ebf8283 2019-07-24 stsp return err;
6832 0ebf8283 2019-07-24 stsp
6833 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6834 0ebf8283 2019-07-24 stsp if (err)
6835 0ebf8283 2019-07-24 stsp goto done;
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6838 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6839 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6840 0ebf8283 2019-07-24 stsp done:
6841 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6842 0ebf8283 2019-07-24 stsp return err;
6843 0ebf8283 2019-07-24 stsp }
6844 0ebf8283 2019-07-24 stsp
6845 0ebf8283 2019-07-24 stsp static const struct got_error *
6846 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6847 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6848 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6849 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6850 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6851 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
6852 0ebf8283 2019-07-24 stsp {
6853 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6854 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6855 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6856 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6857 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6858 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6859 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6860 818c7501 2019-07-11 stsp
6861 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6862 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6863 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6864 a0e95631 2019-07-12 stsp
6865 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6866 818c7501 2019-07-11 stsp
6867 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6868 a0e95631 2019-07-12 stsp if (err)
6869 3e3a69f1 2019-07-25 stsp return err;
6870 a0e95631 2019-07-12 stsp
6871 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6872 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6873 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6874 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6875 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
6876 01757395 2019-07-12 stsp /*
6877 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6878 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6879 6c13b005 2021-09-02 stsp *
6880 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6881 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6882 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6883 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6884 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6885 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6886 01757395 2019-07-12 stsp */
6887 01757395 2019-07-12 stsp if (merged_paths) {
6888 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6889 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6890 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6891 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6892 f2a9dc41 2019-12-13 tracey 0);
6893 01757395 2019-07-12 stsp if (err)
6894 01757395 2019-07-12 stsp goto done;
6895 01757395 2019-07-12 stsp }
6896 01757395 2019-07-12 stsp } else {
6897 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6898 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6899 01757395 2019-07-12 stsp if (err)
6900 01757395 2019-07-12 stsp goto done;
6901 01757395 2019-07-12 stsp }
6902 a0e95631 2019-07-12 stsp
6903 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6904 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6905 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6906 a0e95631 2019-07-12 stsp if (err)
6907 a0e95631 2019-07-12 stsp goto done;
6908 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6909 a0e95631 2019-07-12 stsp goto done;
6910 ff0d2220 2019-07-11 stsp }
6911 818c7501 2019-07-11 stsp
6912 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6913 edd02c5e 2019-07-11 stsp if (err)
6914 edd02c5e 2019-07-11 stsp goto done;
6915 edd02c5e 2019-07-11 stsp
6916 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6917 818c7501 2019-07-11 stsp if (err)
6918 818c7501 2019-07-11 stsp goto done;
6919 818c7501 2019-07-11 stsp
6920 5943eee2 2019-08-13 stsp if (new_logmsg) {
6921 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6922 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6923 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6924 5943eee2 2019-08-13 stsp goto done;
6925 5943eee2 2019-08-13 stsp }
6926 5943eee2 2019-08-13 stsp } else {
6927 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6928 5943eee2 2019-08-13 stsp if (err)
6929 5943eee2 2019-08-13 stsp goto done;
6930 5943eee2 2019-08-13 stsp }
6931 0ebf8283 2019-07-24 stsp
6932 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6933 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6934 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6935 50e7a649 2022-07-22 stsp committer ? committer :
6936 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6937 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6938 a0e95631 2019-07-12 stsp if (err)
6939 a0e95631 2019-07-12 stsp goto done;
6940 a0e95631 2019-07-12 stsp
6941 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6942 a0e95631 2019-07-12 stsp if (err)
6943 a0e95631 2019-07-12 stsp goto done;
6944 a0e95631 2019-07-12 stsp
6945 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6946 a0e95631 2019-07-12 stsp if (err)
6947 a0e95631 2019-07-12 stsp goto done;
6948 a0e95631 2019-07-12 stsp
6949 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6950 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6951 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6952 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6953 a0e95631 2019-07-12 stsp err = sync_err;
6954 818c7501 2019-07-11 stsp done:
6955 a0e95631 2019-07-12 stsp free(fileindex_path);
6956 a0e95631 2019-07-12 stsp free(head_commit_id);
6957 a0e95631 2019-07-12 stsp if (head_ref)
6958 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6959 818c7501 2019-07-11 stsp if (err) {
6960 818c7501 2019-07-11 stsp free(*new_commit_id);
6961 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6962 818c7501 2019-07-11 stsp }
6963 818c7501 2019-07-11 stsp return err;
6964 818c7501 2019-07-11 stsp }
6965 818c7501 2019-07-11 stsp
6966 818c7501 2019-07-11 stsp const struct got_error *
6967 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6968 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6969 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6970 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6971 12383673 2023-02-18 mark struct got_object_id *orig_commit_id, int allow_conflict,
6972 12383673 2023-02-18 mark struct got_repository *repo)
6973 0ebf8283 2019-07-24 stsp {
6974 0ebf8283 2019-07-24 stsp const struct got_error *err;
6975 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6976 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6977 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6978 0ebf8283 2019-07-24 stsp
6979 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp return err;
6982 0ebf8283 2019-07-24 stsp
6983 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6984 0ebf8283 2019-07-24 stsp if (err)
6985 0ebf8283 2019-07-24 stsp goto done;
6986 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6987 0ebf8283 2019-07-24 stsp if (err)
6988 0ebf8283 2019-07-24 stsp goto done;
6989 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6990 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6991 0ebf8283 2019-07-24 stsp goto done;
6992 0ebf8283 2019-07-24 stsp }
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6995 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6996 12383673 2023-02-18 mark NULL, allow_conflict, repo);
6997 0ebf8283 2019-07-24 stsp done:
6998 0ebf8283 2019-07-24 stsp if (commit_ref)
6999 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7000 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7001 0ebf8283 2019-07-24 stsp free(commit_id);
7002 0ebf8283 2019-07-24 stsp return err;
7003 0ebf8283 2019-07-24 stsp }
7004 0ebf8283 2019-07-24 stsp
7005 0ebf8283 2019-07-24 stsp const struct got_error *
7006 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7007 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7008 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7009 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
7010 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
7011 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo)
7012 0ebf8283 2019-07-24 stsp {
7013 0ebf8283 2019-07-24 stsp const struct got_error *err;
7014 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7015 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7016 0ebf8283 2019-07-24 stsp
7017 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7018 0ebf8283 2019-07-24 stsp if (err)
7019 0ebf8283 2019-07-24 stsp return err;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7022 0ebf8283 2019-07-24 stsp if (err)
7023 0ebf8283 2019-07-24 stsp goto done;
7024 0ebf8283 2019-07-24 stsp
7025 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7026 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
7027 12383673 2023-02-18 mark new_logmsg, allow_conflict, repo);
7028 0ebf8283 2019-07-24 stsp done:
7029 0ebf8283 2019-07-24 stsp if (commit_ref)
7030 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7031 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7032 0ebf8283 2019-07-24 stsp return err;
7033 0ebf8283 2019-07-24 stsp }
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp const struct got_error *
7036 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
7037 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7038 818c7501 2019-07-11 stsp {
7039 3e3a69f1 2019-07-25 stsp if (fileindex)
7040 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7041 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
7042 69844fba 2019-07-11 stsp }
7043 69844fba 2019-07-11 stsp
7044 69844fba 2019-07-11 stsp static const struct got_error *
7045 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
7046 69844fba 2019-07-11 stsp {
7047 69844fba 2019-07-11 stsp const struct got_error *err;
7048 69844fba 2019-07-11 stsp struct got_reference *ref;
7049 69844fba 2019-07-11 stsp
7050 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
7051 69844fba 2019-07-11 stsp if (err) {
7052 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
7053 69844fba 2019-07-11 stsp return NULL;
7054 69844fba 2019-07-11 stsp return err;
7055 69844fba 2019-07-11 stsp }
7056 69844fba 2019-07-11 stsp
7057 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
7058 69844fba 2019-07-11 stsp got_ref_close(ref);
7059 69844fba 2019-07-11 stsp return err;
7060 818c7501 2019-07-11 stsp }
7061 818c7501 2019-07-11 stsp
7062 69844fba 2019-07-11 stsp static const struct got_error *
7063 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7064 69844fba 2019-07-11 stsp {
7065 69844fba 2019-07-11 stsp const struct got_error *err;
7066 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7067 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7068 69844fba 2019-07-11 stsp
7069 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7070 69844fba 2019-07-11 stsp if (err)
7071 69844fba 2019-07-11 stsp goto done;
7072 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
7073 69844fba 2019-07-11 stsp if (err)
7074 69844fba 2019-07-11 stsp goto done;
7075 69844fba 2019-07-11 stsp
7076 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7077 69844fba 2019-07-11 stsp if (err)
7078 69844fba 2019-07-11 stsp goto done;
7079 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
7080 69844fba 2019-07-11 stsp if (err)
7081 69844fba 2019-07-11 stsp goto done;
7082 69844fba 2019-07-11 stsp
7083 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7084 69844fba 2019-07-11 stsp if (err)
7085 69844fba 2019-07-11 stsp goto done;
7086 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
7087 69844fba 2019-07-11 stsp if (err)
7088 69844fba 2019-07-11 stsp goto done;
7089 69844fba 2019-07-11 stsp
7090 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7091 69844fba 2019-07-11 stsp if (err)
7092 69844fba 2019-07-11 stsp goto done;
7093 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
7094 69844fba 2019-07-11 stsp if (err)
7095 69844fba 2019-07-11 stsp goto done;
7096 69844fba 2019-07-11 stsp
7097 69844fba 2019-07-11 stsp done:
7098 69844fba 2019-07-11 stsp free(tmp_branch_name);
7099 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
7100 69844fba 2019-07-11 stsp free(branch_ref_name);
7101 69844fba 2019-07-11 stsp free(commit_ref_name);
7102 e600f124 2021-03-21 stsp return err;
7103 e600f124 2021-03-21 stsp }
7104 e600f124 2021-03-21 stsp
7105 336075a4 2022-06-25 op static const struct got_error *
7106 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7107 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
7108 e600f124 2021-03-21 stsp {
7109 e600f124 2021-03-21 stsp const struct got_error *err;
7110 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
7111 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
7112 e600f124 2021-03-21 stsp const char *branch_name = NULL;
7113 e600f124 2021-03-21 stsp char *new_id_str = NULL;
7114 e600f124 2021-03-21 stsp char *refname = NULL;
7115 e600f124 2021-03-21 stsp
7116 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
7117 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
7118 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7119 e600f124 2021-03-21 stsp branch_name += 11;
7120 e600f124 2021-03-21 stsp
7121 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7122 e600f124 2021-03-21 stsp if (err)
7123 e600f124 2021-03-21 stsp return err;
7124 e600f124 2021-03-21 stsp
7125 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7126 e600f124 2021-03-21 stsp new_id_str) == -1) {
7127 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7128 e600f124 2021-03-21 stsp goto done;
7129 e600f124 2021-03-21 stsp }
7130 e600f124 2021-03-21 stsp
7131 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7132 e600f124 2021-03-21 stsp if (err)
7133 e600f124 2021-03-21 stsp goto done;
7134 e600f124 2021-03-21 stsp
7135 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7136 e600f124 2021-03-21 stsp if (err)
7137 e600f124 2021-03-21 stsp goto done;
7138 e600f124 2021-03-21 stsp
7139 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7140 e600f124 2021-03-21 stsp done:
7141 e600f124 2021-03-21 stsp free(new_id_str);
7142 e600f124 2021-03-21 stsp free(refname);
7143 e600f124 2021-03-21 stsp free(old_commit_id);
7144 e600f124 2021-03-21 stsp if (ref)
7145 e600f124 2021-03-21 stsp got_ref_close(ref);
7146 69844fba 2019-07-11 stsp return err;
7147 69844fba 2019-07-11 stsp }
7148 69844fba 2019-07-11 stsp
7149 818c7501 2019-07-11 stsp const struct got_error *
7150 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7151 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7152 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7153 ef85a376 2023-02-01 mark int create_backup)
7154 818c7501 2019-07-11 stsp {
7155 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7156 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7157 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7158 818c7501 2019-07-11 stsp
7159 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7160 818c7501 2019-07-11 stsp if (err)
7161 818c7501 2019-07-11 stsp return err;
7162 e600f124 2021-03-21 stsp
7163 e600f124 2021-03-21 stsp if (create_backup) {
7164 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7165 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7166 e600f124 2021-03-21 stsp if (err)
7167 e600f124 2021-03-21 stsp goto done;
7168 e600f124 2021-03-21 stsp }
7169 818c7501 2019-07-11 stsp
7170 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7171 818c7501 2019-07-11 stsp if (err)
7172 818c7501 2019-07-11 stsp goto done;
7173 818c7501 2019-07-11 stsp
7174 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7175 818c7501 2019-07-11 stsp if (err)
7176 818c7501 2019-07-11 stsp goto done;
7177 818c7501 2019-07-11 stsp
7178 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7179 818c7501 2019-07-11 stsp if (err)
7180 818c7501 2019-07-11 stsp goto done;
7181 818c7501 2019-07-11 stsp
7182 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7183 a615e0e7 2020-12-16 stsp if (err)
7184 a615e0e7 2020-12-16 stsp goto done;
7185 a615e0e7 2020-12-16 stsp
7186 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7187 a615e0e7 2020-12-16 stsp if (err)
7188 a615e0e7 2020-12-16 stsp goto done;
7189 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7190 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7191 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7192 a615e0e7 2020-12-16 stsp err = sync_err;
7193 818c7501 2019-07-11 stsp done:
7194 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7195 a615e0e7 2020-12-16 stsp free(fileindex_path);
7196 818c7501 2019-07-11 stsp free(new_head_commit_id);
7197 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7198 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7199 818c7501 2019-07-11 stsp err = unlockerr;
7200 818c7501 2019-07-11 stsp return err;
7201 818c7501 2019-07-11 stsp }
7202 818c7501 2019-07-11 stsp
7203 818c7501 2019-07-11 stsp const struct got_error *
7204 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7205 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7206 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7207 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7208 818c7501 2019-07-11 stsp {
7209 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7210 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7211 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7212 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7213 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7214 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7215 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7216 818c7501 2019-07-11 stsp
7217 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7218 818c7501 2019-07-11 stsp if (err)
7219 818c7501 2019-07-11 stsp return err;
7220 818c7501 2019-07-11 stsp
7221 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7222 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7223 a44927cc 2022-04-07 stsp if (err)
7224 a44927cc 2022-04-07 stsp goto done;
7225 a44927cc 2022-04-07 stsp
7226 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7227 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7228 818c7501 2019-07-11 stsp if (err)
7229 818c7501 2019-07-11 stsp goto done;
7230 818c7501 2019-07-11 stsp
7231 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7232 818c7501 2019-07-11 stsp if (err)
7233 818c7501 2019-07-11 stsp goto done;
7234 818c7501 2019-07-11 stsp
7235 818c7501 2019-07-11 stsp /*
7236 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7237 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7238 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7239 818c7501 2019-07-11 stsp */
7240 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7241 818c7501 2019-07-11 stsp if (err)
7242 818c7501 2019-07-11 stsp goto done;
7243 818c7501 2019-07-11 stsp
7244 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7245 818c7501 2019-07-11 stsp if (err)
7246 818c7501 2019-07-11 stsp goto done;
7247 818c7501 2019-07-11 stsp
7248 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7249 a44927cc 2022-04-07 stsp worktree->path_prefix);
7250 ca355955 2019-07-12 stsp if (err)
7251 ca355955 2019-07-12 stsp goto done;
7252 ca355955 2019-07-12 stsp
7253 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7254 ca355955 2019-07-12 stsp if (err)
7255 ca355955 2019-07-12 stsp goto done;
7256 ca355955 2019-07-12 stsp
7257 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7258 818c7501 2019-07-11 stsp if (err)
7259 818c7501 2019-07-11 stsp goto done;
7260 818c7501 2019-07-11 stsp
7261 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7262 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7263 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7264 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7265 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7266 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7267 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7268 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7269 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7270 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7271 55bd499d 2019-07-12 stsp if (err)
7272 1f1abb7e 2019-08-08 stsp goto sync;
7273 55bd499d 2019-07-12 stsp
7274 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7275 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7276 ca355955 2019-07-12 stsp sync:
7277 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7278 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7279 ca355955 2019-07-12 stsp err = sync_err;
7280 818c7501 2019-07-11 stsp done:
7281 818c7501 2019-07-11 stsp got_ref_close(resolved);
7282 a3a2faf2 2019-07-12 stsp free(tree_id);
7283 818c7501 2019-07-11 stsp free(commit_id);
7284 a44927cc 2022-04-07 stsp if (commit)
7285 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7286 0ebf8283 2019-07-24 stsp if (fileindex)
7287 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7288 0ebf8283 2019-07-24 stsp free(fileindex_path);
7289 0ebf8283 2019-07-24 stsp
7290 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7291 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7292 0ebf8283 2019-07-24 stsp err = unlockerr;
7293 0ebf8283 2019-07-24 stsp return err;
7294 0ebf8283 2019-07-24 stsp }
7295 0ebf8283 2019-07-24 stsp
7296 0ebf8283 2019-07-24 stsp const struct got_error *
7297 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7298 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7299 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7300 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7301 0ebf8283 2019-07-24 stsp {
7302 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7303 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7304 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7305 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7306 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7307 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7308 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7309 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7310 0ebf8283 2019-07-24 stsp
7311 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7312 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7313 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7314 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7315 0ebf8283 2019-07-24 stsp
7316 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7317 0ebf8283 2019-07-24 stsp if (err)
7318 0ebf8283 2019-07-24 stsp return err;
7319 0ebf8283 2019-07-24 stsp
7320 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7321 0ebf8283 2019-07-24 stsp if (err)
7322 0ebf8283 2019-07-24 stsp goto done;
7323 0ebf8283 2019-07-24 stsp
7324 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7325 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7326 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7327 0ebf8283 2019-07-24 stsp &ok_arg);
7328 0ebf8283 2019-07-24 stsp if (err)
7329 0ebf8283 2019-07-24 stsp goto done;
7330 0ebf8283 2019-07-24 stsp
7331 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7332 0ebf8283 2019-07-24 stsp if (err)
7333 0ebf8283 2019-07-24 stsp goto done;
7334 0ebf8283 2019-07-24 stsp
7335 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7336 0ebf8283 2019-07-24 stsp if (err)
7337 0ebf8283 2019-07-24 stsp goto done;
7338 0ebf8283 2019-07-24 stsp
7339 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7340 0ebf8283 2019-07-24 stsp worktree);
7341 0ebf8283 2019-07-24 stsp if (err)
7342 0ebf8283 2019-07-24 stsp goto done;
7343 0ebf8283 2019-07-24 stsp
7344 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7345 0ebf8283 2019-07-24 stsp 0);
7346 0ebf8283 2019-07-24 stsp if (err)
7347 0ebf8283 2019-07-24 stsp goto done;
7348 0ebf8283 2019-07-24 stsp
7349 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7350 0ebf8283 2019-07-24 stsp if (err)
7351 0ebf8283 2019-07-24 stsp goto done;
7352 0ebf8283 2019-07-24 stsp
7353 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7354 0ebf8283 2019-07-24 stsp if (err)
7355 0ebf8283 2019-07-24 stsp goto done;
7356 0ebf8283 2019-07-24 stsp
7357 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7358 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7359 0ebf8283 2019-07-24 stsp if (err)
7360 0ebf8283 2019-07-24 stsp goto done;
7361 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7362 0ebf8283 2019-07-24 stsp if (err)
7363 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7365 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7366 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7367 0ebf8283 2019-07-24 stsp goto done;
7368 0ebf8283 2019-07-24 stsp }
7369 0ebf8283 2019-07-24 stsp
7370 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7371 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7372 0ebf8283 2019-07-24 stsp if (err)
7373 0ebf8283 2019-07-24 stsp goto done;
7374 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7375 0ebf8283 2019-07-24 stsp if (err)
7376 0ebf8283 2019-07-24 stsp goto done;
7377 0ebf8283 2019-07-24 stsp
7378 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7379 0ebf8283 2019-07-24 stsp if (err)
7380 0ebf8283 2019-07-24 stsp goto done;
7381 0ebf8283 2019-07-24 stsp done:
7382 0ebf8283 2019-07-24 stsp free(fileindex_path);
7383 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7384 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7385 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7386 0ebf8283 2019-07-24 stsp if (wt_branch)
7387 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7388 0ebf8283 2019-07-24 stsp if (err) {
7389 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7390 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7391 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7392 0ebf8283 2019-07-24 stsp }
7393 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7394 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7395 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7396 0ebf8283 2019-07-24 stsp }
7397 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7398 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7399 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7400 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7401 3e3a69f1 2019-07-25 stsp }
7402 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7403 0ebf8283 2019-07-24 stsp }
7404 0ebf8283 2019-07-24 stsp return err;
7405 0ebf8283 2019-07-24 stsp }
7406 0ebf8283 2019-07-24 stsp
7407 0ebf8283 2019-07-24 stsp const struct got_error *
7408 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7409 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7410 0ebf8283 2019-07-24 stsp {
7411 3e3a69f1 2019-07-25 stsp if (fileindex)
7412 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7413 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7414 0ebf8283 2019-07-24 stsp }
7415 0ebf8283 2019-07-24 stsp
7416 0ebf8283 2019-07-24 stsp const struct got_error *
7417 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7418 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7419 0ebf8283 2019-07-24 stsp {
7420 0ebf8283 2019-07-24 stsp const struct got_error *err;
7421 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7422 0ebf8283 2019-07-24 stsp
7423 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7424 0ebf8283 2019-07-24 stsp if (err)
7425 0ebf8283 2019-07-24 stsp return err;
7426 0ebf8283 2019-07-24 stsp
7427 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7428 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7429 0ebf8283 2019-07-24 stsp return NULL;
7430 0ebf8283 2019-07-24 stsp }
7431 0ebf8283 2019-07-24 stsp
7432 0ebf8283 2019-07-24 stsp const struct got_error *
7433 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7434 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7435 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7436 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7437 0ebf8283 2019-07-24 stsp {
7438 0ebf8283 2019-07-24 stsp const struct got_error *err;
7439 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7440 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7441 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7442 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7443 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7444 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7445 0ebf8283 2019-07-24 stsp
7446 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7447 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7448 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7449 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7450 0ebf8283 2019-07-24 stsp
7451 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7452 3e3a69f1 2019-07-25 stsp if (err)
7453 3e3a69f1 2019-07-25 stsp return err;
7454 3e3a69f1 2019-07-25 stsp
7455 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7456 3e3a69f1 2019-07-25 stsp if (err)
7457 f032f1f7 2019-08-04 stsp goto done;
7458 f032f1f7 2019-08-04 stsp
7459 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7460 f032f1f7 2019-08-04 stsp &have_staged_files);
7461 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7462 f032f1f7 2019-08-04 stsp goto done;
7463 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7464 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7465 3e3a69f1 2019-07-25 stsp goto done;
7466 f032f1f7 2019-08-04 stsp }
7467 3e3a69f1 2019-07-25 stsp
7468 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7469 0ebf8283 2019-07-24 stsp if (err)
7470 f032f1f7 2019-08-04 stsp goto done;
7471 0ebf8283 2019-07-24 stsp
7472 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7473 0ebf8283 2019-07-24 stsp if (err)
7474 0ebf8283 2019-07-24 stsp goto done;
7475 0ebf8283 2019-07-24 stsp
7476 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7477 0ebf8283 2019-07-24 stsp if (err)
7478 0ebf8283 2019-07-24 stsp goto done;
7479 0ebf8283 2019-07-24 stsp
7480 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7481 0ebf8283 2019-07-24 stsp worktree);
7482 0ebf8283 2019-07-24 stsp if (err)
7483 0ebf8283 2019-07-24 stsp goto done;
7484 0ebf8283 2019-07-24 stsp
7485 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7486 0ebf8283 2019-07-24 stsp if (err)
7487 0ebf8283 2019-07-24 stsp goto done;
7488 0ebf8283 2019-07-24 stsp
7489 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7490 0ebf8283 2019-07-24 stsp if (err)
7491 0ebf8283 2019-07-24 stsp goto done;
7492 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
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 = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7497 0ebf8283 2019-07-24 stsp if (err)
7498 0ebf8283 2019-07-24 stsp goto done;
7499 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7500 0ebf8283 2019-07-24 stsp if (err)
7501 0ebf8283 2019-07-24 stsp goto done;
7502 0ebf8283 2019-07-24 stsp
7503 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7504 0ebf8283 2019-07-24 stsp if (err)
7505 0ebf8283 2019-07-24 stsp goto done;
7506 0ebf8283 2019-07-24 stsp done:
7507 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7508 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7509 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7510 0ebf8283 2019-07-24 stsp if (commit_ref)
7511 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7512 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7513 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7514 0ebf8283 2019-07-24 stsp if (err) {
7515 0ebf8283 2019-07-24 stsp free(*commit_id);
7516 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7517 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7518 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7519 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7520 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7521 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7522 0ebf8283 2019-07-24 stsp }
7523 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7524 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7525 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7526 3e3a69f1 2019-07-25 stsp }
7527 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7528 0ebf8283 2019-07-24 stsp }
7529 0ebf8283 2019-07-24 stsp return err;
7530 0ebf8283 2019-07-24 stsp }
7531 0ebf8283 2019-07-24 stsp
7532 0ebf8283 2019-07-24 stsp static const struct got_error *
7533 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7534 0ebf8283 2019-07-24 stsp {
7535 0ebf8283 2019-07-24 stsp const struct got_error *err;
7536 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7537 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7538 0ebf8283 2019-07-24 stsp
7539 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7540 0ebf8283 2019-07-24 stsp if (err)
7541 0ebf8283 2019-07-24 stsp goto done;
7542 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7543 0ebf8283 2019-07-24 stsp if (err)
7544 0ebf8283 2019-07-24 stsp goto done;
7545 0ebf8283 2019-07-24 stsp
7546 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7547 0ebf8283 2019-07-24 stsp worktree);
7548 0ebf8283 2019-07-24 stsp if (err)
7549 0ebf8283 2019-07-24 stsp goto done;
7550 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7551 0ebf8283 2019-07-24 stsp if (err)
7552 0ebf8283 2019-07-24 stsp goto done;
7553 0ebf8283 2019-07-24 stsp
7554 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7555 0ebf8283 2019-07-24 stsp if (err)
7556 0ebf8283 2019-07-24 stsp goto done;
7557 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7558 0ebf8283 2019-07-24 stsp if (err)
7559 0ebf8283 2019-07-24 stsp goto done;
7560 0ebf8283 2019-07-24 stsp
7561 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7562 0ebf8283 2019-07-24 stsp if (err)
7563 0ebf8283 2019-07-24 stsp goto done;
7564 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7565 0ebf8283 2019-07-24 stsp if (err)
7566 0ebf8283 2019-07-24 stsp goto done;
7567 0ebf8283 2019-07-24 stsp done:
7568 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7569 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7570 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7571 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7572 0ebf8283 2019-07-24 stsp return err;
7573 0ebf8283 2019-07-24 stsp }
7574 0ebf8283 2019-07-24 stsp
7575 0ebf8283 2019-07-24 stsp const struct got_error *
7576 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7577 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7578 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7579 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7580 0ebf8283 2019-07-24 stsp {
7581 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7582 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7583 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7584 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7585 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7586 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7587 0ebf8283 2019-07-24 stsp
7588 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7589 0ebf8283 2019-07-24 stsp if (err)
7590 0ebf8283 2019-07-24 stsp return err;
7591 0ebf8283 2019-07-24 stsp
7592 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7593 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7594 a44927cc 2022-04-07 stsp if (err)
7595 a44927cc 2022-04-07 stsp goto done;
7596 a44927cc 2022-04-07 stsp
7597 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7598 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7599 0ebf8283 2019-07-24 stsp if (err)
7600 0ebf8283 2019-07-24 stsp goto done;
7601 0ebf8283 2019-07-24 stsp
7602 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7603 0ebf8283 2019-07-24 stsp if (err)
7604 0ebf8283 2019-07-24 stsp goto done;
7605 0ebf8283 2019-07-24 stsp
7606 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7607 0ebf8283 2019-07-24 stsp if (err)
7608 0ebf8283 2019-07-24 stsp goto done;
7609 0ebf8283 2019-07-24 stsp
7610 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7611 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7612 0ebf8283 2019-07-24 stsp if (err)
7613 0ebf8283 2019-07-24 stsp goto done;
7614 0ebf8283 2019-07-24 stsp
7615 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7616 0ebf8283 2019-07-24 stsp if (err)
7617 0ebf8283 2019-07-24 stsp goto done;
7618 0ebf8283 2019-07-24 stsp
7619 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7620 0ebf8283 2019-07-24 stsp if (err)
7621 0ebf8283 2019-07-24 stsp goto done;
7622 0ebf8283 2019-07-24 stsp
7623 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7624 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7625 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7626 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7627 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7628 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7629 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7630 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7631 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7632 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7633 0ebf8283 2019-07-24 stsp if (err)
7634 1f1abb7e 2019-08-08 stsp goto sync;
7635 0ebf8283 2019-07-24 stsp
7636 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7637 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7638 0ebf8283 2019-07-24 stsp sync:
7639 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7640 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7641 0ebf8283 2019-07-24 stsp err = sync_err;
7642 0ebf8283 2019-07-24 stsp done:
7643 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7644 0ebf8283 2019-07-24 stsp free(tree_id);
7645 818c7501 2019-07-11 stsp free(fileindex_path);
7646 818c7501 2019-07-11 stsp
7647 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7648 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7649 818c7501 2019-07-11 stsp err = unlockerr;
7650 818c7501 2019-07-11 stsp return err;
7651 818c7501 2019-07-11 stsp }
7652 0ebf8283 2019-07-24 stsp
7653 0ebf8283 2019-07-24 stsp const struct got_error *
7654 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7655 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7656 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7657 0ebf8283 2019-07-24 stsp {
7658 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7659 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7660 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7661 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7662 0ebf8283 2019-07-24 stsp
7663 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7664 0ebf8283 2019-07-24 stsp if (err)
7665 0ebf8283 2019-07-24 stsp return err;
7666 0ebf8283 2019-07-24 stsp
7667 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7668 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7669 e600f124 2021-03-21 stsp if (err)
7670 e600f124 2021-03-21 stsp goto done;
7671 e600f124 2021-03-21 stsp
7672 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7673 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7674 0ebf8283 2019-07-24 stsp if (err)
7675 0ebf8283 2019-07-24 stsp goto done;
7676 0ebf8283 2019-07-24 stsp
7677 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7678 0ebf8283 2019-07-24 stsp if (err)
7679 0ebf8283 2019-07-24 stsp goto done;
7680 0ebf8283 2019-07-24 stsp
7681 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7682 0ebf8283 2019-07-24 stsp if (err)
7683 0ebf8283 2019-07-24 stsp goto done;
7684 0ebf8283 2019-07-24 stsp
7685 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7686 0ebf8283 2019-07-24 stsp if (err)
7687 0ebf8283 2019-07-24 stsp goto done;
7688 0ebf8283 2019-07-24 stsp
7689 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7690 a615e0e7 2020-12-16 stsp if (err)
7691 a615e0e7 2020-12-16 stsp goto done;
7692 a615e0e7 2020-12-16 stsp
7693 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7694 a615e0e7 2020-12-16 stsp if (err)
7695 a615e0e7 2020-12-16 stsp goto done;
7696 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7697 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7698 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7699 a615e0e7 2020-12-16 stsp err = sync_err;
7700 0ebf8283 2019-07-24 stsp done:
7701 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7702 a615e0e7 2020-12-16 stsp free(fileindex_path);
7703 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7704 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7705 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7706 0ebf8283 2019-07-24 stsp err = unlockerr;
7707 0ebf8283 2019-07-24 stsp return err;
7708 0ebf8283 2019-07-24 stsp }
7709 0ebf8283 2019-07-24 stsp
7710 0ebf8283 2019-07-24 stsp const struct got_error *
7711 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7712 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7713 0ebf8283 2019-07-24 stsp {
7714 0ebf8283 2019-07-24 stsp const struct got_error *err;
7715 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7716 0ebf8283 2019-07-24 stsp
7717 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7718 0ebf8283 2019-07-24 stsp if (err)
7719 0ebf8283 2019-07-24 stsp return err;
7720 0ebf8283 2019-07-24 stsp
7721 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7722 0ebf8283 2019-07-24 stsp if (err)
7723 0ebf8283 2019-07-24 stsp goto done;
7724 0ebf8283 2019-07-24 stsp
7725 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7726 0ebf8283 2019-07-24 stsp done:
7727 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7728 2822a352 2019-10-15 stsp return err;
7729 2822a352 2019-10-15 stsp }
7730 2822a352 2019-10-15 stsp
7731 2822a352 2019-10-15 stsp const struct got_error *
7732 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7733 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7734 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7735 2822a352 2019-10-15 stsp struct got_repository *repo)
7736 2822a352 2019-10-15 stsp {
7737 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7738 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7739 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7740 2822a352 2019-10-15 stsp
7741 2822a352 2019-10-15 stsp *fileindex = NULL;
7742 2822a352 2019-10-15 stsp *branch_ref = NULL;
7743 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7744 2822a352 2019-10-15 stsp
7745 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7746 2822a352 2019-10-15 stsp if (err)
7747 2822a352 2019-10-15 stsp return err;
7748 2822a352 2019-10-15 stsp
7749 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7750 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7751 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7752 2822a352 2019-10-15 stsp "update -b or different branch name required");
7753 2822a352 2019-10-15 stsp goto done;
7754 2822a352 2019-10-15 stsp }
7755 2822a352 2019-10-15 stsp
7756 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7757 2822a352 2019-10-15 stsp if (err)
7758 2822a352 2019-10-15 stsp goto done;
7759 2822a352 2019-10-15 stsp
7760 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7761 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7762 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7763 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7764 2822a352 2019-10-15 stsp &ok_arg);
7765 2822a352 2019-10-15 stsp if (err)
7766 2822a352 2019-10-15 stsp goto done;
7767 2822a352 2019-10-15 stsp
7768 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
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_ref_open(base_branch_ref, repo,
7773 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7774 2822a352 2019-10-15 stsp done:
7775 2822a352 2019-10-15 stsp if (err) {
7776 2822a352 2019-10-15 stsp if (*branch_ref) {
7777 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7778 2822a352 2019-10-15 stsp *branch_ref = NULL;
7779 2822a352 2019-10-15 stsp }
7780 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7781 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7782 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7783 2822a352 2019-10-15 stsp }
7784 2822a352 2019-10-15 stsp if (*fileindex) {
7785 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7786 2822a352 2019-10-15 stsp *fileindex = NULL;
7787 2822a352 2019-10-15 stsp }
7788 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7789 2822a352 2019-10-15 stsp }
7790 0cb83759 2019-08-03 stsp return err;
7791 0cb83759 2019-08-03 stsp }
7792 0cb83759 2019-08-03 stsp
7793 2822a352 2019-10-15 stsp const struct got_error *
7794 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7795 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7796 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7797 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7798 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7799 2822a352 2019-10-15 stsp {
7800 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7801 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7802 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7803 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7804 2822a352 2019-10-15 stsp
7805 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7806 2822a352 2019-10-15 stsp if (err)
7807 2822a352 2019-10-15 stsp goto done;
7808 2822a352 2019-10-15 stsp
7809 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7810 2822a352 2019-10-15 stsp if (err)
7811 2822a352 2019-10-15 stsp goto done;
7812 2822a352 2019-10-15 stsp
7813 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7814 a44927cc 2022-04-07 stsp if (err)
7815 a44927cc 2022-04-07 stsp goto done;
7816 a44927cc 2022-04-07 stsp
7817 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7818 2822a352 2019-10-15 stsp worktree->path_prefix);
7819 2822a352 2019-10-15 stsp if (err)
7820 2822a352 2019-10-15 stsp goto done;
7821 2822a352 2019-10-15 stsp
7822 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7823 2822a352 2019-10-15 stsp if (err)
7824 2822a352 2019-10-15 stsp goto done;
7825 2822a352 2019-10-15 stsp
7826 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7827 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7828 2822a352 2019-10-15 stsp if (err)
7829 2822a352 2019-10-15 stsp goto sync;
7830 2822a352 2019-10-15 stsp
7831 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7832 2822a352 2019-10-15 stsp if (err)
7833 2822a352 2019-10-15 stsp goto sync;
7834 2822a352 2019-10-15 stsp
7835 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7836 6e210706 2021-01-22 stsp if (err)
7837 6e210706 2021-01-22 stsp goto sync;
7838 6e210706 2021-01-22 stsp
7839 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7840 2822a352 2019-10-15 stsp sync:
7841 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7842 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7843 2822a352 2019-10-15 stsp err = sync_err;
7844 2822a352 2019-10-15 stsp
7845 2822a352 2019-10-15 stsp done:
7846 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7847 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7848 2822a352 2019-10-15 stsp err = unlockerr;
7849 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7850 2822a352 2019-10-15 stsp
7851 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7852 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7853 2822a352 2019-10-15 stsp err = unlockerr;
7854 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7855 2822a352 2019-10-15 stsp
7856 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7857 2822a352 2019-10-15 stsp free(fileindex_path);
7858 2822a352 2019-10-15 stsp free(tree_id);
7859 a44927cc 2022-04-07 stsp if (commit)
7860 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7861 2822a352 2019-10-15 stsp
7862 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7863 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7864 2822a352 2019-10-15 stsp err = unlockerr;
7865 2822a352 2019-10-15 stsp return err;
7866 2822a352 2019-10-15 stsp }
7867 2822a352 2019-10-15 stsp
7868 2822a352 2019-10-15 stsp const struct got_error *
7869 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7870 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7871 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7872 2822a352 2019-10-15 stsp {
7873 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7874 8b692cd0 2019-10-21 stsp
7875 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7876 8b692cd0 2019-10-21 stsp
7877 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7878 8b692cd0 2019-10-21 stsp
7879 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7880 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7881 8b692cd0 2019-10-21 stsp err = unlockerr;
7882 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7883 8b692cd0 2019-10-21 stsp
7884 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7885 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7886 8b692cd0 2019-10-21 stsp err = unlockerr;
7887 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7888 f259c4c1 2021-09-24 stsp
7889 f259c4c1 2021-09-24 stsp return err;
7890 f259c4c1 2021-09-24 stsp }
7891 f259c4c1 2021-09-24 stsp
7892 f259c4c1 2021-09-24 stsp const struct got_error *
7893 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7894 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7895 f259c4c1 2021-09-24 stsp {
7896 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7897 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7898 f259c4c1 2021-09-24 stsp
7899 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7900 f259c4c1 2021-09-24 stsp if (err)
7901 f259c4c1 2021-09-24 stsp goto done;
7902 8b692cd0 2019-10-21 stsp
7903 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7904 f259c4c1 2021-09-24 stsp
7905 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7906 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7907 f259c4c1 2021-09-24 stsp err = sync_err;
7908 f259c4c1 2021-09-24 stsp done:
7909 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7910 f259c4c1 2021-09-24 stsp free(fileindex_path);
7911 8b692cd0 2019-10-21 stsp return err;
7912 2822a352 2019-10-15 stsp }
7913 2822a352 2019-10-15 stsp
7914 f259c4c1 2021-09-24 stsp static const struct got_error *
7915 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7916 f259c4c1 2021-09-24 stsp {
7917 f259c4c1 2021-09-24 stsp const struct got_error *err;
7918 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7919 f259c4c1 2021-09-24 stsp
7920 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7921 f259c4c1 2021-09-24 stsp if (err)
7922 f259c4c1 2021-09-24 stsp goto done;
7923 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7924 f259c4c1 2021-09-24 stsp if (err)
7925 f259c4c1 2021-09-24 stsp goto done;
7926 f259c4c1 2021-09-24 stsp
7927 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7928 f259c4c1 2021-09-24 stsp if (err)
7929 f259c4c1 2021-09-24 stsp goto done;
7930 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7931 f259c4c1 2021-09-24 stsp if (err)
7932 f259c4c1 2021-09-24 stsp goto done;
7933 f259c4c1 2021-09-24 stsp
7934 f259c4c1 2021-09-24 stsp done:
7935 f259c4c1 2021-09-24 stsp free(branch_refname);
7936 f259c4c1 2021-09-24 stsp free(commit_refname);
7937 f259c4c1 2021-09-24 stsp return err;
7938 f259c4c1 2021-09-24 stsp }
7939 f259c4c1 2021-09-24 stsp
7940 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7941 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7942 f259c4c1 2021-09-24 stsp const char *branch_name;
7943 f259c4c1 2021-09-24 stsp };
7944 f259c4c1 2021-09-24 stsp
7945 f259c4c1 2021-09-24 stsp static const struct got_error *
7946 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7947 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7948 f259c4c1 2021-09-24 stsp {
7949 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7950 f259c4c1 2021-09-24 stsp
7951 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7952 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7953 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7954 f259c4c1 2021-09-24 stsp
7955 f259c4c1 2021-09-24 stsp return NULL;
7956 f259c4c1 2021-09-24 stsp }
7957 f259c4c1 2021-09-24 stsp
7958 f259c4c1 2021-09-24 stsp
7959 f259c4c1 2021-09-24 stsp const struct got_error *
7960 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7961 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7962 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7963 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7964 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7965 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7966 f259c4c1 2021-09-24 stsp {
7967 f259c4c1 2021-09-24 stsp const struct got_error *err;
7968 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7969 f259c4c1 2021-09-24 stsp
7970 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7971 f259c4c1 2021-09-24 stsp if (err)
7972 f259c4c1 2021-09-24 stsp goto done;
7973 f259c4c1 2021-09-24 stsp
7974 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7975 f259c4c1 2021-09-24 stsp worktree);
7976 f259c4c1 2021-09-24 stsp if (err)
7977 f259c4c1 2021-09-24 stsp goto done;
7978 f259c4c1 2021-09-24 stsp
7979 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7980 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7981 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7982 f259c4c1 2021-09-24 stsp done:
7983 f259c4c1 2021-09-24 stsp free(fileindex_path);
7984 f259c4c1 2021-09-24 stsp return err;
7985 f259c4c1 2021-09-24 stsp }
7986 f259c4c1 2021-09-24 stsp
7987 f259c4c1 2021-09-24 stsp const struct got_error *
7988 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7989 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7990 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7991 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7992 12383673 2023-02-18 mark int allow_conflict, struct got_repository *repo,
7993 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7994 0ff8d236 2021-09-28 stsp
7995 f259c4c1 2021-09-24 stsp {
7996 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7997 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7998 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7999 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
8000 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
8001 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
8002 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8003 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
8004 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8005 f259c4c1 2021-09-24 stsp
8006 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
8007 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
8008 f259c4c1 2021-09-24 stsp
8009 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
8010 f259c4c1 2021-09-24 stsp
8011 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8012 f259c4c1 2021-09-24 stsp if (err)
8013 f259c4c1 2021-09-24 stsp goto done;
8014 f259c4c1 2021-09-24 stsp
8015 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8016 f259c4c1 2021-09-24 stsp if (err)
8017 f259c4c1 2021-09-24 stsp goto done;
8018 f259c4c1 2021-09-24 stsp
8019 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8020 f259c4c1 2021-09-24 stsp if (err)
8021 f259c4c1 2021-09-24 stsp goto done;
8022 f259c4c1 2021-09-24 stsp
8023 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8024 f259c4c1 2021-09-24 stsp &have_staged_files);
8025 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8026 f259c4c1 2021-09-24 stsp goto done;
8027 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8028 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8029 f259c4c1 2021-09-24 stsp goto done;
8030 f259c4c1 2021-09-24 stsp }
8031 f259c4c1 2021-09-24 stsp
8032 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
8033 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
8034 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
8035 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
8036 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
8037 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8038 12383673 2023-02-18 mark cc_arg.commit_conflicts = allow_conflict;
8039 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8040 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8041 f259c4c1 2021-09-24 stsp if (err)
8042 f259c4c1 2021-09-24 stsp goto done;
8043 f259c4c1 2021-09-24 stsp
8044 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
8045 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8046 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
8047 f259c4c1 2021-09-24 stsp goto done;
8048 f259c4c1 2021-09-24 stsp }
8049 f259c4c1 2021-09-24 stsp
8050 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8051 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8052 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
8053 f259c4c1 2021-09-24 stsp
8054 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
8055 f259c4c1 2021-09-24 stsp ct_path++;
8056 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
8057 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8058 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8059 f259c4c1 2021-09-24 stsp if (err)
8060 f259c4c1 2021-09-24 stsp goto done;
8061 f259c4c1 2021-09-24 stsp
8062 f259c4c1 2021-09-24 stsp }
8063 f259c4c1 2021-09-24 stsp
8064 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
8065 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
8066 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
8067 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
8068 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8069 f259c4c1 2021-09-24 stsp if (err)
8070 f259c4c1 2021-09-24 stsp goto done;
8071 f259c4c1 2021-09-24 stsp
8072 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
8073 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
8074 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8075 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8076 f259c4c1 2021-09-24 stsp err = sync_err;
8077 f259c4c1 2021-09-24 stsp done:
8078 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
8079 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
8080 d8bacb93 2023-01-10 mark
8081 f259c4c1 2021-09-24 stsp free_commitable(ct);
8082 f259c4c1 2021-09-24 stsp }
8083 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8084 f259c4c1 2021-09-24 stsp free(fileindex_path);
8085 f259c4c1 2021-09-24 stsp return err;
8086 f259c4c1 2021-09-24 stsp }
8087 f259c4c1 2021-09-24 stsp
8088 f259c4c1 2021-09-24 stsp const struct got_error *
8089 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
8090 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
8091 f259c4c1 2021-09-24 stsp {
8092 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8093 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8094 f259c4c1 2021-09-24 stsp
8095 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8096 f259c4c1 2021-09-24 stsp if (err)
8097 f259c4c1 2021-09-24 stsp goto done;
8098 f259c4c1 2021-09-24 stsp
8099 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8100 f259c4c1 2021-09-24 stsp if (err)
8101 f259c4c1 2021-09-24 stsp goto done;
8102 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8103 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8104 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8105 f259c4c1 2021-09-24 stsp err = sync_err;
8106 f259c4c1 2021-09-24 stsp done:
8107 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8108 f259c4c1 2021-09-24 stsp free(fileindex_path);
8109 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8110 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8111 f259c4c1 2021-09-24 stsp err = unlockerr;
8112 f259c4c1 2021-09-24 stsp return err;
8113 f259c4c1 2021-09-24 stsp }
8114 f259c4c1 2021-09-24 stsp
8115 f259c4c1 2021-09-24 stsp const struct got_error *
8116 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8117 f259c4c1 2021-09-24 stsp struct got_repository *repo)
8118 f259c4c1 2021-09-24 stsp {
8119 f259c4c1 2021-09-24 stsp const struct got_error *err;
8120 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8121 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8122 f259c4c1 2021-09-24 stsp
8123 f259c4c1 2021-09-24 stsp *in_progress = 0;
8124 f259c4c1 2021-09-24 stsp
8125 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8126 f259c4c1 2021-09-24 stsp if (err)
8127 f259c4c1 2021-09-24 stsp return err;
8128 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8129 793fcac3 2021-09-24 stsp free(branch_refname);
8130 f259c4c1 2021-09-24 stsp if (err) {
8131 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8132 f259c4c1 2021-09-24 stsp return err;
8133 f259c4c1 2021-09-24 stsp } else
8134 f259c4c1 2021-09-24 stsp *in_progress = 1;
8135 f259c4c1 2021-09-24 stsp
8136 f259c4c1 2021-09-24 stsp return NULL;
8137 f259c4c1 2021-09-24 stsp }
8138 f259c4c1 2021-09-24 stsp
8139 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8140 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8141 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8142 f259c4c1 2021-09-24 stsp {
8143 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8144 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8145 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8146 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8147 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8148 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8149 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8150 f259c4c1 2021-09-24 stsp
8151 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8152 f259c4c1 2021-09-24 stsp
8153 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8154 f259c4c1 2021-09-24 stsp if (err)
8155 f259c4c1 2021-09-24 stsp return err;
8156 f259c4c1 2021-09-24 stsp
8157 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8158 f259c4c1 2021-09-24 stsp if (err)
8159 f259c4c1 2021-09-24 stsp goto done;
8160 f259c4c1 2021-09-24 stsp
8161 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8162 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8163 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8164 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8165 f259c4c1 2021-09-24 stsp &ok_arg);
8166 f259c4c1 2021-09-24 stsp if (err)
8167 f259c4c1 2021-09-24 stsp goto done;
8168 f259c4c1 2021-09-24 stsp
8169 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8170 f259c4c1 2021-09-24 stsp if (err)
8171 f259c4c1 2021-09-24 stsp return err;
8172 f259c4c1 2021-09-24 stsp
8173 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8174 f259c4c1 2021-09-24 stsp if (err)
8175 f259c4c1 2021-09-24 stsp return err;
8176 f259c4c1 2021-09-24 stsp
8177 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8178 f259c4c1 2021-09-24 stsp 0);
8179 f259c4c1 2021-09-24 stsp if (err)
8180 f259c4c1 2021-09-24 stsp goto done;
8181 f259c4c1 2021-09-24 stsp
8182 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8183 f259c4c1 2021-09-24 stsp if (err)
8184 f259c4c1 2021-09-24 stsp goto done;
8185 f259c4c1 2021-09-24 stsp
8186 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8187 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8188 f259c4c1 2021-09-24 stsp goto done;
8189 f259c4c1 2021-09-24 stsp }
8190 f259c4c1 2021-09-24 stsp
8191 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8192 f259c4c1 2021-09-24 stsp if (err)
8193 f259c4c1 2021-09-24 stsp goto done;
8194 f259c4c1 2021-09-24 stsp
8195 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8196 f259c4c1 2021-09-24 stsp if (err)
8197 f259c4c1 2021-09-24 stsp goto done;
8198 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8199 f259c4c1 2021-09-24 stsp if (err)
8200 f259c4c1 2021-09-24 stsp goto done;
8201 f259c4c1 2021-09-24 stsp
8202 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8203 f259c4c1 2021-09-24 stsp if (err)
8204 f259c4c1 2021-09-24 stsp goto done;
8205 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8206 f259c4c1 2021-09-24 stsp if (err)
8207 f259c4c1 2021-09-24 stsp goto done;
8208 f259c4c1 2021-09-24 stsp
8209 f259c4c1 2021-09-24 stsp done:
8210 f259c4c1 2021-09-24 stsp free(branch_refname);
8211 f259c4c1 2021-09-24 stsp free(commit_refname);
8212 f259c4c1 2021-09-24 stsp free(fileindex_path);
8213 f259c4c1 2021-09-24 stsp if (branch_ref)
8214 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8215 f259c4c1 2021-09-24 stsp if (commit_ref)
8216 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8217 f259c4c1 2021-09-24 stsp if (wt_branch)
8218 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8219 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8220 f259c4c1 2021-09-24 stsp if (err) {
8221 f259c4c1 2021-09-24 stsp if (*fileindex) {
8222 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8223 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8224 f259c4c1 2021-09-24 stsp }
8225 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8226 f259c4c1 2021-09-24 stsp }
8227 f259c4c1 2021-09-24 stsp return err;
8228 f259c4c1 2021-09-24 stsp }
8229 f259c4c1 2021-09-24 stsp
8230 f259c4c1 2021-09-24 stsp const struct got_error *
8231 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8232 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8233 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8234 f259c4c1 2021-09-24 stsp {
8235 f259c4c1 2021-09-24 stsp const struct got_error *err;
8236 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8237 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8238 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8239 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8240 f259c4c1 2021-09-24 stsp
8241 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8242 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8243 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8244 f259c4c1 2021-09-24 stsp
8245 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8246 f259c4c1 2021-09-24 stsp if (err)
8247 f259c4c1 2021-09-24 stsp return err;
8248 f259c4c1 2021-09-24 stsp
8249 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8250 f259c4c1 2021-09-24 stsp if (err)
8251 f259c4c1 2021-09-24 stsp goto done;
8252 f259c4c1 2021-09-24 stsp
8253 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8254 f259c4c1 2021-09-24 stsp &have_staged_files);
8255 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8256 f259c4c1 2021-09-24 stsp goto done;
8257 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8258 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8259 f259c4c1 2021-09-24 stsp goto done;
8260 f259c4c1 2021-09-24 stsp }
8261 f259c4c1 2021-09-24 stsp
8262 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8263 f259c4c1 2021-09-24 stsp if (err)
8264 f259c4c1 2021-09-24 stsp goto done;
8265 f259c4c1 2021-09-24 stsp
8266 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8267 f259c4c1 2021-09-24 stsp if (err)
8268 f259c4c1 2021-09-24 stsp goto done;
8269 f259c4c1 2021-09-24 stsp
8270 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8271 f259c4c1 2021-09-24 stsp if (err)
8272 f259c4c1 2021-09-24 stsp goto done;
8273 f259c4c1 2021-09-24 stsp
8274 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8275 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8276 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8277 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8278 f259c4c1 2021-09-24 stsp goto done;
8279 f259c4c1 2021-09-24 stsp }
8280 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8281 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8282 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8283 f259c4c1 2021-09-24 stsp goto done;
8284 f259c4c1 2021-09-24 stsp }
8285 f259c4c1 2021-09-24 stsp
8286 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8287 f259c4c1 2021-09-24 stsp if (err)
8288 f259c4c1 2021-09-24 stsp goto done;
8289 f259c4c1 2021-09-24 stsp
8290 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8291 f259c4c1 2021-09-24 stsp if (err)
8292 f259c4c1 2021-09-24 stsp goto done;
8293 f259c4c1 2021-09-24 stsp done:
8294 f259c4c1 2021-09-24 stsp free(commit_refname);
8295 f259c4c1 2021-09-24 stsp free(branch_refname);
8296 f259c4c1 2021-09-24 stsp free(fileindex_path);
8297 f259c4c1 2021-09-24 stsp if (commit_ref)
8298 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8299 f259c4c1 2021-09-24 stsp if (branch_ref)
8300 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8301 f259c4c1 2021-09-24 stsp if (err) {
8302 f259c4c1 2021-09-24 stsp if (*branch_name) {
8303 f259c4c1 2021-09-24 stsp free(*branch_name);
8304 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8305 f259c4c1 2021-09-24 stsp }
8306 f259c4c1 2021-09-24 stsp free(*branch_tip);
8307 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8308 f259c4c1 2021-09-24 stsp if (*fileindex) {
8309 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8310 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8311 f259c4c1 2021-09-24 stsp }
8312 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8313 f259c4c1 2021-09-24 stsp }
8314 f259c4c1 2021-09-24 stsp return err;
8315 f259c4c1 2021-09-24 stsp }
8316 f259c4c1 2021-09-24 stsp
8317 f259c4c1 2021-09-24 stsp const struct got_error *
8318 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8319 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8320 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8321 f259c4c1 2021-09-24 stsp {
8322 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8323 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8324 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8325 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8326 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8327 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8328 f259c4c1 2021-09-24 stsp
8329 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8330 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8331 a44927cc 2022-04-07 stsp if (err)
8332 a44927cc 2022-04-07 stsp goto done;
8333 a44927cc 2022-04-07 stsp
8334 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8335 a44927cc 2022-04-07 stsp worktree->path_prefix);
8336 f259c4c1 2021-09-24 stsp if (err)
8337 f259c4c1 2021-09-24 stsp goto done;
8338 f259c4c1 2021-09-24 stsp
8339 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8340 f259c4c1 2021-09-24 stsp if (err)
8341 f259c4c1 2021-09-24 stsp goto done;
8342 f259c4c1 2021-09-24 stsp
8343 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8344 f259c4c1 2021-09-24 stsp if (err)
8345 f259c4c1 2021-09-24 stsp goto done;
8346 f259c4c1 2021-09-24 stsp
8347 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8348 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8349 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8350 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8351 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8352 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8353 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8354 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8355 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8356 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8357 f259c4c1 2021-09-24 stsp if (err)
8358 f259c4c1 2021-09-24 stsp goto sync;
8359 f259c4c1 2021-09-24 stsp
8360 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8361 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8362 f259c4c1 2021-09-24 stsp sync:
8363 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8364 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8365 f259c4c1 2021-09-24 stsp err = sync_err;
8366 f259c4c1 2021-09-24 stsp done:
8367 f259c4c1 2021-09-24 stsp free(tree_id);
8368 f259c4c1 2021-09-24 stsp free(commit_id);
8369 a44927cc 2022-04-07 stsp if (commit)
8370 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8371 f259c4c1 2021-09-24 stsp if (fileindex)
8372 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8373 f259c4c1 2021-09-24 stsp free(fileindex_path);
8374 f259c4c1 2021-09-24 stsp
8375 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8376 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8377 f259c4c1 2021-09-24 stsp err = unlockerr;
8378 f259c4c1 2021-09-24 stsp return err;
8379 f259c4c1 2021-09-24 stsp }
8380 f259c4c1 2021-09-24 stsp
8381 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8382 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8383 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8384 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8385 2db2652d 2019-08-07 stsp struct got_repository *repo;
8386 2db2652d 2019-08-07 stsp int have_changes;
8387 2db2652d 2019-08-07 stsp };
8388 2db2652d 2019-08-07 stsp
8389 336075a4 2022-06-25 op static const struct got_error *
8390 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8391 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8392 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8393 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8394 735ef5ac 2019-08-03 stsp {
8395 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8396 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8397 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8398 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8399 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8400 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8401 8b13ce36 2019-08-08 stsp
8402 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8403 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8404 8b13ce36 2019-08-08 stsp return NULL;
8405 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8406 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8407 735ef5ac 2019-08-03 stsp
8408 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8409 735ef5ac 2019-08-03 stsp if (ie == NULL)
8410 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8411 735ef5ac 2019-08-03 stsp
8412 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8413 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8414 735ef5ac 2019-08-03 stsp relpath) == -1)
8415 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8416 735ef5ac 2019-08-03 stsp
8417 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8418 b4b2adf5 2023-02-09 op base_commit_idp = got_fileindex_entry_get_commit_id(
8419 b4b2adf5 2023-02-09 op &base_commit_id, ie);
8420 735ef5ac 2019-08-03 stsp }
8421 735ef5ac 2019-08-03 stsp
8422 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8423 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8424 3aa5969e 2019-08-06 stsp goto done;
8425 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8426 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8427 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8428 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8429 735ef5ac 2019-08-03 stsp goto done;
8430 3aa5969e 2019-08-06 stsp }
8431 735ef5ac 2019-08-03 stsp
8432 2db2652d 2019-08-07 stsp a->have_changes = 1;
8433 2db2652d 2019-08-07 stsp
8434 735ef5ac 2019-08-03 stsp p = in_repo_path;
8435 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8436 735ef5ac 2019-08-03 stsp p++;
8437 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8438 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8439 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8440 735ef5ac 2019-08-03 stsp done:
8441 735ef5ac 2019-08-03 stsp free(in_repo_path);
8442 dc424a06 2019-08-07 stsp return err;
8443 dc424a06 2019-08-07 stsp }
8444 dc424a06 2019-08-07 stsp
8445 2db2652d 2019-08-07 stsp struct stage_path_arg {
8446 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8447 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8448 2db2652d 2019-08-07 stsp struct got_repository *repo;
8449 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8450 2db2652d 2019-08-07 stsp void *status_arg;
8451 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8452 2db2652d 2019-08-07 stsp void *patch_arg;
8453 7b5dc508 2019-10-28 stsp int staged_something;
8454 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8455 2db2652d 2019-08-07 stsp };
8456 2db2652d 2019-08-07 stsp
8457 2db2652d 2019-08-07 stsp static const struct got_error *
8458 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8459 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8460 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8461 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8462 0cb83759 2019-08-03 stsp {
8463 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8464 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8465 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8466 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8467 0cb83759 2019-08-03 stsp uint32_t stage;
8468 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8469 0aeb8099 2020-07-23 stsp struct stat sb;
8470 8b13ce36 2019-08-08 stsp
8471 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8472 8b13ce36 2019-08-08 stsp return NULL;
8473 0cb83759 2019-08-03 stsp
8474 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8475 d3e7c587 2019-08-03 stsp if (ie == NULL)
8476 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8477 0cb83759 2019-08-03 stsp
8478 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8479 2db2652d 2019-08-07 stsp relpath)== -1)
8480 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8481 0cb83759 2019-08-03 stsp
8482 0cb83759 2019-08-03 stsp switch (status) {
8483 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8484 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8485 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8486 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8487 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8488 0aeb8099 2020-07-23 stsp break;
8489 0aeb8099 2020-07-23 stsp }
8490 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8491 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8492 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8493 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8494 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8495 dc424a06 2019-08-07 stsp if (err)
8496 dc424a06 2019-08-07 stsp break;
8497 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8498 dc424a06 2019-08-07 stsp break;
8499 dc424a06 2019-08-07 stsp } else {
8500 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8501 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8502 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8503 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8504 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8505 dc424a06 2019-08-07 stsp break;
8506 dc424a06 2019-08-07 stsp }
8507 dc424a06 2019-08-07 stsp }
8508 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8509 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8510 0cb83759 2019-08-03 stsp if (err)
8511 dc424a06 2019-08-07 stsp break;
8512 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8513 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8514 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8515 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8516 0cb83759 2019-08-03 stsp else
8517 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8518 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8519 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8520 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8521 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8522 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8523 35213c7c 2020-07-23 stsp ssize_t target_len;
8524 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8525 35213c7c 2020-07-23 stsp sizeof(target_path));
8526 35213c7c 2020-07-23 stsp if (target_len == -1) {
8527 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8528 35213c7c 2020-07-23 stsp ondisk_path);
8529 35213c7c 2020-07-23 stsp break;
8530 35213c7c 2020-07-23 stsp }
8531 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8532 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8533 35213c7c 2020-07-23 stsp a->worktree->root_path);
8534 35213c7c 2020-07-23 stsp if (err)
8535 35213c7c 2020-07-23 stsp break;
8536 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8537 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8538 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8539 35213c7c 2020-07-23 stsp break;
8540 35213c7c 2020-07-23 stsp }
8541 35213c7c 2020-07-23 stsp }
8542 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8543 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8544 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8545 35213c7c 2020-07-23 stsp else
8546 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8547 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8548 0aeb8099 2020-07-23 stsp } else {
8549 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8550 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8551 0aeb8099 2020-07-23 stsp }
8552 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8553 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8554 dc424a06 2019-08-07 stsp break;
8555 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8556 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8557 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8558 9fdde394 2022-06-04 op if (err)
8559 9fdde394 2022-06-04 op break;
8560 9fdde394 2022-06-04 op /*
8561 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8562 9fdde394 2022-06-04 op * implicitly unstage the file.
8563 9fdde394 2022-06-04 op */
8564 9fdde394 2022-06-04 op if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8565 9fdde394 2022-06-04 op sizeof(ie->blob_sha1)) == 0) {
8566 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8567 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8568 9fdde394 2022-06-04 op }
8569 0cb83759 2019-08-03 stsp break;
8570 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8571 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8572 d3e7c587 2019-08-03 stsp break;
8573 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8574 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8575 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8576 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8577 dc424a06 2019-08-07 stsp if (err)
8578 dc424a06 2019-08-07 stsp break;
8579 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8580 88f33a19 2019-08-08 stsp break;
8581 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8582 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8583 dc424a06 2019-08-07 stsp break;
8584 88f33a19 2019-08-08 stsp }
8585 dc424a06 2019-08-07 stsp }
8586 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8587 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8588 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8589 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8590 dc424a06 2019-08-07 stsp break;
8591 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8592 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8593 12463d8b 2019-12-13 stsp de_name);
8594 0cb83759 2019-08-03 stsp break;
8595 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8596 d3e7c587 2019-08-03 stsp break;
8597 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8598 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8599 ebf48fd5 2019-08-03 stsp break;
8600 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8601 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8602 2a06fe5f 2019-08-24 stsp break;
8603 0cb83759 2019-08-03 stsp default:
8604 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8605 537ac44b 2019-08-03 stsp break;
8606 0cb83759 2019-08-03 stsp }
8607 dc424a06 2019-08-07 stsp
8608 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8609 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8610 dc424a06 2019-08-07 stsp free(path_content);
8611 2db2652d 2019-08-07 stsp free(ondisk_path);
8612 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8613 0ebf8283 2019-07-24 stsp return err;
8614 0ebf8283 2019-07-24 stsp }
8615 0cb83759 2019-08-03 stsp
8616 0cb83759 2019-08-03 stsp const struct got_error *
8617 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8618 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8619 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8620 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8621 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8622 0cb83759 2019-08-03 stsp {
8623 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8624 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8625 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8626 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8627 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8628 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8629 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8630 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8631 0cb83759 2019-08-03 stsp
8632 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8633 0cb83759 2019-08-03 stsp if (err)
8634 0cb83759 2019-08-03 stsp return err;
8635 0cb83759 2019-08-03 stsp
8636 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8637 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8638 735ef5ac 2019-08-03 stsp if (err)
8639 735ef5ac 2019-08-03 stsp goto done;
8640 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8641 735ef5ac 2019-08-03 stsp if (err)
8642 735ef5ac 2019-08-03 stsp goto done;
8643 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8644 0cb83759 2019-08-03 stsp if (err)
8645 0cb83759 2019-08-03 stsp goto done;
8646 0cb83759 2019-08-03 stsp
8647 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8648 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8649 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8650 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8651 2db2652d 2019-08-07 stsp oka.repo = repo;
8652 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8653 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8654 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8655 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8656 735ef5ac 2019-08-03 stsp if (err)
8657 735ef5ac 2019-08-03 stsp goto done;
8658 735ef5ac 2019-08-03 stsp }
8659 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8660 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8661 2db2652d 2019-08-07 stsp goto done;
8662 2db2652d 2019-08-07 stsp }
8663 735ef5ac 2019-08-03 stsp
8664 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8665 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8666 2db2652d 2019-08-07 stsp spa.repo = repo;
8667 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8668 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8669 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8670 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8671 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8672 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8673 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8674 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8675 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8676 42005733 2019-08-03 stsp if (err)
8677 2db2652d 2019-08-07 stsp goto done;
8678 7b5dc508 2019-10-28 stsp }
8679 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8680 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8681 7b5dc508 2019-10-28 stsp goto done;
8682 0cb83759 2019-08-03 stsp }
8683 0cb83759 2019-08-03 stsp
8684 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8685 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8686 0cb83759 2019-08-03 stsp err = sync_err;
8687 0cb83759 2019-08-03 stsp done:
8688 735ef5ac 2019-08-03 stsp if (head_ref)
8689 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8690 735ef5ac 2019-08-03 stsp free(head_commit_id);
8691 0cb83759 2019-08-03 stsp free(fileindex_path);
8692 0cb83759 2019-08-03 stsp if (fileindex)
8693 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8694 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8695 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8696 0cb83759 2019-08-03 stsp err = unlockerr;
8697 0cb83759 2019-08-03 stsp return err;
8698 0cb83759 2019-08-03 stsp }
8699 ad493afc 2019-08-03 stsp
8700 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8701 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8702 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8703 ad493afc 2019-08-03 stsp struct got_repository *repo;
8704 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8705 ad493afc 2019-08-03 stsp void *progress_arg;
8706 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8707 2e1f37b0 2019-08-08 stsp void *patch_arg;
8708 ad493afc 2019-08-03 stsp };
8709 2e1f37b0 2019-08-08 stsp
8710 2e1f37b0 2019-08-08 stsp static const struct got_error *
8711 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8712 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8713 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8714 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8715 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8716 2e1f37b0 2019-08-08 stsp {
8717 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8718 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8719 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8720 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8721 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8722 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8723 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8724 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8725 2e1f37b0 2019-08-08 stsp
8726 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8727 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8728 2e1f37b0 2019-08-08 stsp
8729 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8730 2e1f37b0 2019-08-08 stsp if (err)
8731 2e1f37b0 2019-08-08 stsp return err;
8732 eb81bc23 2022-06-28 tracey
8733 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8734 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8735 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8736 eb81bc23 2022-06-28 tracey goto done;
8737 eb81bc23 2022-06-28 tracey }
8738 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8739 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8740 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8741 eb81bc23 2022-06-28 tracey goto done;
8742 eb81bc23 2022-06-28 tracey }
8743 eb81bc23 2022-06-28 tracey
8744 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8745 2e1f37b0 2019-08-08 stsp if (err)
8746 2e1f37b0 2019-08-08 stsp goto done;
8747 2e1f37b0 2019-08-08 stsp
8748 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8749 2e1f37b0 2019-08-08 stsp if (err)
8750 2e1f37b0 2019-08-08 stsp goto done;
8751 2e1f37b0 2019-08-08 stsp
8752 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8753 2e1f37b0 2019-08-08 stsp if (err)
8754 2e1f37b0 2019-08-08 stsp goto done;
8755 2e1f37b0 2019-08-08 stsp
8756 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8757 eb81bc23 2022-06-28 tracey fd2);
8758 2e1f37b0 2019-08-08 stsp if (err)
8759 2e1f37b0 2019-08-08 stsp goto done;
8760 2e1f37b0 2019-08-08 stsp
8761 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8762 2e1f37b0 2019-08-08 stsp if (err)
8763 2e1f37b0 2019-08-08 stsp goto done;
8764 ad493afc 2019-08-03 stsp
8765 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8766 2e1f37b0 2019-08-08 stsp if (err)
8767 2e1f37b0 2019-08-08 stsp goto done;
8768 2e1f37b0 2019-08-08 stsp
8769 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8770 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8771 2e1f37b0 2019-08-08 stsp if (err)
8772 2e1f37b0 2019-08-08 stsp goto done;
8773 2e1f37b0 2019-08-08 stsp
8774 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8775 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8776 2e1f37b0 2019-08-08 stsp if (err)
8777 2e1f37b0 2019-08-08 stsp goto done;
8778 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8779 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8780 2e1f37b0 2019-08-08 stsp if (err)
8781 2e1f37b0 2019-08-08 stsp goto done;
8782 2e1f37b0 2019-08-08 stsp
8783 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8784 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8785 2e1f37b0 2019-08-08 stsp goto done;
8786 2e1f37b0 2019-08-08 stsp }
8787 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8788 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8789 2e1f37b0 2019-08-08 stsp goto done;
8790 2e1f37b0 2019-08-08 stsp }
8791 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8792 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8793 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8794 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8795 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8796 fe621944 2020-11-10 stsp nchanges++;
8797 fe621944 2020-11-10 stsp }
8798 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8799 2e1f37b0 2019-08-08 stsp int choice;
8800 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8801 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8802 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8803 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8804 2e1f37b0 2019-08-08 stsp if (err)
8805 2e1f37b0 2019-08-08 stsp goto done;
8806 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8807 2e1f37b0 2019-08-08 stsp have_content = 1;
8808 19e4b907 2019-08-08 stsp else
8809 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8810 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8811 2e1f37b0 2019-08-08 stsp break;
8812 2e1f37b0 2019-08-08 stsp }
8813 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8814 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8815 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8816 2e1f37b0 2019-08-08 stsp done:
8817 2e1f37b0 2019-08-08 stsp free(label1);
8818 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8819 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8820 2e1f37b0 2019-08-08 stsp if (blob)
8821 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8822 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8823 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8824 2e1f37b0 2019-08-08 stsp if (staged_blob)
8825 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8826 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8827 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8828 fe621944 2020-11-10 stsp err = free_err;
8829 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8830 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8831 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8832 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8833 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8834 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8835 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8836 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8837 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8838 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8839 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8840 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8841 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8842 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8843 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8844 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8845 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8846 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8847 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8848 2e1f37b0 2019-08-08 stsp }
8849 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8850 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8851 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8852 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8853 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8854 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8855 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8856 2e1f37b0 2019-08-08 stsp }
8857 2e1f37b0 2019-08-08 stsp free(path1);
8858 2e1f37b0 2019-08-08 stsp free(path2);
8859 fda8017d 2020-07-23 stsp return err;
8860 fda8017d 2020-07-23 stsp }
8861 fda8017d 2020-07-23 stsp
8862 fda8017d 2020-07-23 stsp static const struct got_error *
8863 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8864 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8865 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8866 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8867 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8868 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8869 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8870 fda8017d 2020-07-23 stsp {
8871 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8872 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8873 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8874 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8875 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8876 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8877 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8878 fda8017d 2020-07-23 stsp struct stat sb;
8879 fda8017d 2020-07-23 stsp
8880 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8881 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8882 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8883 fda8017d 2020-07-23 stsp if (err)
8884 fda8017d 2020-07-23 stsp return err;
8885 fda8017d 2020-07-23 stsp
8886 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8887 fda8017d 2020-07-23 stsp return NULL;
8888 fda8017d 2020-07-23 stsp
8889 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8890 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8891 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8892 fda8017d 2020-07-23 stsp if (err)
8893 fda8017d 2020-07-23 stsp goto done;
8894 fda8017d 2020-07-23 stsp }
8895 fda8017d 2020-07-23 stsp
8896 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8897 fda8017d 2020-07-23 stsp if (f == NULL) {
8898 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8899 fda8017d 2020-07-23 stsp path_unstaged_content);
8900 fda8017d 2020-07-23 stsp goto done;
8901 fda8017d 2020-07-23 stsp }
8902 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8903 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8904 fda8017d 2020-07-23 stsp goto done;
8905 fda8017d 2020-07-23 stsp }
8906 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8907 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8908 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8909 fda8017d 2020-07-23 stsp size_t r;
8910 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8911 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8912 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8913 fda8017d 2020-07-23 stsp goto done;
8914 fda8017d 2020-07-23 stsp }
8915 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8916 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8917 fda8017d 2020-07-23 stsp goto done;
8918 fda8017d 2020-07-23 stsp }
8919 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8920 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8921 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8922 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8923 fda8017d 2020-07-23 stsp progress_arg);
8924 fda8017d 2020-07-23 stsp } else {
8925 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8926 67a66647 2021-05-31 stsp
8927 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8928 67a66647 2021-05-31 stsp if (err)
8929 67a66647 2021-05-31 stsp return err;
8930 67a66647 2021-05-31 stsp
8931 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8932 67a66647 2021-05-31 stsp parent) == -1) {
8933 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8934 67a66647 2021-05-31 stsp base_path = NULL;
8935 67a66647 2021-05-31 stsp goto done;
8936 67a66647 2021-05-31 stsp }
8937 67a66647 2021-05-31 stsp
8938 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8939 b90054ed 2022-11-01 stsp base_path, "");
8940 67a66647 2021-05-31 stsp if (err)
8941 67a66647 2021-05-31 stsp goto done;
8942 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8943 67a66647 2021-05-31 stsp blob_base);
8944 67a66647 2021-05-31 stsp if (err)
8945 67a66647 2021-05-31 stsp goto done;
8946 67a66647 2021-05-31 stsp
8947 5e91dae4 2022-08-30 stsp /*
8948 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8949 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8950 eec2f5a9 2021-06-03 stsp */
8951 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8952 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8953 eec2f5a9 2021-06-03 stsp ondisk_path);
8954 eec2f5a9 2021-06-03 stsp if (err)
8955 eec2f5a9 2021-06-03 stsp goto done;
8956 eec2f5a9 2021-06-03 stsp } else {
8957 eec2f5a9 2021-06-03 stsp int fd;
8958 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8959 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8960 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8961 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8962 eec2f5a9 2021-06-03 stsp goto done;
8963 eec2f5a9 2021-06-03 stsp }
8964 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8965 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8966 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8967 eec2f5a9 2021-06-03 stsp close(fd);
8968 eec2f5a9 2021-06-03 stsp goto done;
8969 eec2f5a9 2021-06-03 stsp }
8970 eec2f5a9 2021-06-03 stsp }
8971 eec2f5a9 2021-06-03 stsp
8972 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8973 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8974 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8975 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8976 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8977 fda8017d 2020-07-23 stsp }
8978 fda8017d 2020-07-23 stsp if (err)
8979 fda8017d 2020-07-23 stsp goto done;
8980 fda8017d 2020-07-23 stsp
8981 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8982 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8983 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8984 2ac8aa02 2020-11-09 stsp } else {
8985 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8986 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8987 2ac8aa02 2020-11-09 stsp }
8988 fda8017d 2020-07-23 stsp done:
8989 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8990 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8991 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8992 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8993 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8994 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8995 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8996 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8997 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8998 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8999 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9000 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9001 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
9002 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9003 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
9004 fda8017d 2020-07-23 stsp free(path_unstaged_content);
9005 fda8017d 2020-07-23 stsp free(path_new_staged_content);
9006 67a66647 2021-05-31 stsp free(blob_base_path);
9007 67a66647 2021-05-31 stsp free(parent);
9008 67a66647 2021-05-31 stsp free(base_path);
9009 2e1f37b0 2019-08-08 stsp return err;
9010 2e1f37b0 2019-08-08 stsp }
9011 2e1f37b0 2019-08-08 stsp
9012 ad493afc 2019-08-03 stsp static const struct got_error *
9013 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
9014 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
9015 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9016 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9017 ad493afc 2019-08-03 stsp {
9018 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
9019 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
9020 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
9021 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9022 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
9023 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
9024 ad493afc 2019-08-03 stsp int local_changes_subsumed;
9025 9bc94a15 2019-08-03 stsp struct stat sb;
9026 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
9027 ad493afc 2019-08-03 stsp
9028 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
9029 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
9030 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
9031 2e1f37b0 2019-08-08 stsp return NULL;
9032 2e1f37b0 2019-08-08 stsp
9033 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9034 ad493afc 2019-08-03 stsp if (ie == NULL)
9035 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9036 9bc94a15 2019-08-03 stsp
9037 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9038 9bc94a15 2019-08-03 stsp == -1)
9039 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
9040 ad493afc 2019-08-03 stsp
9041 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
9042 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
9043 f69721c3 2019-10-21 stsp if (err)
9044 f69721c3 2019-10-21 stsp goto done;
9045 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9046 f69721c3 2019-10-21 stsp id_str) == -1) {
9047 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
9048 eb81bc23 2022-06-28 tracey goto done;
9049 eb81bc23 2022-06-28 tracey }
9050 eb81bc23 2022-06-28 tracey
9051 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
9052 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
9053 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9054 eb81bc23 2022-06-28 tracey goto done;
9055 eb81bc23 2022-06-28 tracey }
9056 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
9057 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
9058 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
9059 f69721c3 2019-10-21 stsp goto done;
9060 f69721c3 2019-10-21 stsp }
9061 f69721c3 2019-10-21 stsp
9062 ad493afc 2019-08-03 stsp switch (staged_status) {
9063 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
9064 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
9065 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
9066 ad493afc 2019-08-03 stsp if (err)
9067 ad493afc 2019-08-03 stsp break;
9068 ad493afc 2019-08-03 stsp /* fall through */
9069 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
9070 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9071 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
9072 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9073 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9074 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9075 2e1f37b0 2019-08-08 stsp if (err)
9076 2e1f37b0 2019-08-08 stsp break;
9077 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
9078 2e1f37b0 2019-08-08 stsp break;
9079 2e1f37b0 2019-08-08 stsp } else {
9080 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
9081 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
9082 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
9083 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
9084 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
9085 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
9086 2e1f37b0 2019-08-08 stsp }
9087 2e1f37b0 2019-08-08 stsp }
9088 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
9089 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
9090 ad493afc 2019-08-03 stsp if (err)
9091 ad493afc 2019-08-03 stsp break;
9092 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
9093 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
9094 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
9095 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
9096 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
9097 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
9098 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
9099 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9100 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
9101 ea7786be 2020-07-23 stsp break;
9102 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
9103 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9104 36bf999c 2020-07-23 stsp char *staged_target;
9105 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
9106 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
9107 36bf999c 2020-07-23 stsp if (err)
9108 36bf999c 2020-07-23 stsp goto done;
9109 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
9110 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
9111 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
9112 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
9113 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
9114 36bf999c 2020-07-23 stsp free(staged_target);
9115 dfe9fba0 2020-07-23 stsp } else {
9116 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
9117 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
9118 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9119 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9120 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9121 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9122 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9123 dfe9fba0 2020-07-23 stsp }
9124 ea7786be 2020-07-23 stsp break;
9125 ea7786be 2020-07-23 stsp default:
9126 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9127 ea7786be 2020-07-23 stsp break;
9128 ea7786be 2020-07-23 stsp }
9129 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9130 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9131 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9132 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9133 2ac8aa02 2020-11-09 stsp }
9134 ad493afc 2019-08-03 stsp break;
9135 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9136 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9137 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9138 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9139 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9140 2e1f37b0 2019-08-08 stsp if (err)
9141 2e1f37b0 2019-08-08 stsp break;
9142 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9143 2e1f37b0 2019-08-08 stsp break;
9144 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9145 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9146 2e1f37b0 2019-08-08 stsp break;
9147 2e1f37b0 2019-08-08 stsp }
9148 2e1f37b0 2019-08-08 stsp }
9149 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9150 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9151 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9152 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9153 9bc94a15 2019-08-03 stsp if (err)
9154 9bc94a15 2019-08-03 stsp break;
9155 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9156 ad493afc 2019-08-03 stsp break;
9157 ad493afc 2019-08-03 stsp }
9158 f69721c3 2019-10-21 stsp done:
9159 ad493afc 2019-08-03 stsp free(ondisk_path);
9160 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9161 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9162 ad493afc 2019-08-03 stsp if (blob_base)
9163 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9164 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9165 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9166 ad493afc 2019-08-03 stsp if (blob_staged)
9167 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9168 f69721c3 2019-10-21 stsp free(id_str);
9169 f69721c3 2019-10-21 stsp free(label_orig);
9170 ad493afc 2019-08-03 stsp return err;
9171 ad493afc 2019-08-03 stsp }
9172 ad493afc 2019-08-03 stsp
9173 ad493afc 2019-08-03 stsp const struct got_error *
9174 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9175 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9176 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9177 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9178 ad493afc 2019-08-03 stsp struct got_repository *repo)
9179 ad493afc 2019-08-03 stsp {
9180 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9181 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9182 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9183 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9184 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9185 ad493afc 2019-08-03 stsp
9186 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9187 ad493afc 2019-08-03 stsp if (err)
9188 ad493afc 2019-08-03 stsp return err;
9189 ad493afc 2019-08-03 stsp
9190 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9191 ad493afc 2019-08-03 stsp if (err)
9192 ad493afc 2019-08-03 stsp goto done;
9193 ad493afc 2019-08-03 stsp
9194 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9195 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9196 ad493afc 2019-08-03 stsp upa.repo = repo;
9197 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9198 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9199 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9200 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9201 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9202 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9203 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9204 ad493afc 2019-08-03 stsp if (err)
9205 ad493afc 2019-08-03 stsp goto done;
9206 ad493afc 2019-08-03 stsp }
9207 ad493afc 2019-08-03 stsp
9208 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9209 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9210 ad493afc 2019-08-03 stsp err = sync_err;
9211 ad493afc 2019-08-03 stsp done:
9212 ad493afc 2019-08-03 stsp free(fileindex_path);
9213 ad493afc 2019-08-03 stsp if (fileindex)
9214 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9215 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9216 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9217 ad493afc 2019-08-03 stsp err = unlockerr;
9218 ad493afc 2019-08-03 stsp return err;
9219 ad493afc 2019-08-03 stsp }
9220 b2118c49 2020-07-28 stsp
9221 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9222 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9223 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9224 b2118c49 2020-07-28 stsp void *info_arg;
9225 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9226 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9227 b2118c49 2020-07-28 stsp void *cancel_arg;
9228 b2118c49 2020-07-28 stsp };
9229 b2118c49 2020-07-28 stsp
9230 b2118c49 2020-07-28 stsp static const struct got_error *
9231 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9232 b2118c49 2020-07-28 stsp {
9233 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9234 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9235 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9236 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9237 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9238 b2118c49 2020-07-28 stsp int stage;
9239 b2118c49 2020-07-28 stsp
9240 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9241 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9242 b2118c49 2020-07-28 stsp
9243 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9244 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9245 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9246 b2118c49 2020-07-28 stsp break;
9247 b2118c49 2020-07-28 stsp }
9248 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9249 b2118c49 2020-07-28 stsp return NULL;
9250 b2118c49 2020-07-28 stsp
9251 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_blob(ie))
9252 b4b2adf5 2023-02-09 op blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9253 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9254 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9255 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9256 b4b2adf5 2023-02-09 op staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9257 b4b2adf5 2023-02-09 op &staged_blob_id, ie);
9258 b2118c49 2020-07-28 stsp }
9259 b2118c49 2020-07-28 stsp
9260 b4b2adf5 2023-02-09 op if (got_fileindex_entry_has_commit(ie))
9261 b4b2adf5 2023-02-09 op commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9262 b2118c49 2020-07-28 stsp
9263 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9264 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9265 b2118c49 2020-07-28 stsp }
9266 b2118c49 2020-07-28 stsp
9267 b2118c49 2020-07-28 stsp const struct got_error *
9268 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9269 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9270 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9271 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9272 b2118c49 2020-07-28 stsp
9273 b2118c49 2020-07-28 stsp {
9274 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9275 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9276 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9277 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9278 b2118c49 2020-07-28 stsp
9279 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9280 b2118c49 2020-07-28 stsp if (err)
9281 b2118c49 2020-07-28 stsp return err;
9282 b2118c49 2020-07-28 stsp
9283 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9284 b2118c49 2020-07-28 stsp if (err)
9285 b2118c49 2020-07-28 stsp goto done;
9286 b2118c49 2020-07-28 stsp
9287 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9288 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9289 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9290 b2118c49 2020-07-28 stsp arg.paths = paths;
9291 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9292 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9293 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9294 b2118c49 2020-07-28 stsp &arg);
9295 b2118c49 2020-07-28 stsp done:
9296 b2118c49 2020-07-28 stsp free(fileindex_path);
9297 b2118c49 2020-07-28 stsp if (fileindex)
9298 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9299 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9300 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9301 b2118c49 2020-07-28 stsp err = unlockerr;
9302 b2118c49 2020-07-28 stsp return err;
9303 b2118c49 2020-07-28 stsp }
9304 78f5ac24 2022-03-19 op
9305 78f5ac24 2022-03-19 op static const struct got_error *
9306 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9307 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9308 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9309 78f5ac24 2022-03-19 op {
9310 78f5ac24 2022-03-19 op const struct got_error *err;
9311 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9312 78f5ac24 2022-03-19 op struct stat sb;
9313 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9314 78f5ac24 2022-03-19 op
9315 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9316 78f5ac24 2022-03-19 op if (err)
9317 78f5ac24 2022-03-19 op return err;
9318 78f5ac24 2022-03-19 op
9319 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9320 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9321 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9322 78f5ac24 2022-03-19 op
9323 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9324 78f5ac24 2022-03-19 op if (ie) {
9325 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9326 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9327 a05fb460 2022-04-23 op repo);
9328 78f5ac24 2022-03-19 op if (err)
9329 78f5ac24 2022-03-19 op goto done;
9330 78f5ac24 2022-03-19 op } else {
9331 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9332 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9333 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9334 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9335 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9336 78f5ac24 2022-03-19 op ondisk_path);
9337 78f5ac24 2022-03-19 op goto done;
9338 78f5ac24 2022-03-19 op }
9339 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9340 78f5ac24 2022-03-19 op }
9341 78f5ac24 2022-03-19 op }
9342 78f5ac24 2022-03-19 op
9343 78f5ac24 2022-03-19 op done:
9344 78f5ac24 2022-03-19 op free(ondisk_path);
9345 78f5ac24 2022-03-19 op return err;
9346 78f5ac24 2022-03-19 op }
9347 78f5ac24 2022-03-19 op
9348 78f5ac24 2022-03-19 op static const struct got_error *
9349 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9350 78f5ac24 2022-03-19 op unsigned char staged_status)
9351 78f5ac24 2022-03-19 op {
9352 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9353 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9354 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9355 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9356 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9357 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9358 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9359 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9360 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9361 78f5ac24 2022-03-19 op return NULL;
9362 78f5ac24 2022-03-19 op }
9363 78f5ac24 2022-03-19 op
9364 78f5ac24 2022-03-19 op static const struct got_error *
9365 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9366 78f5ac24 2022-03-19 op {
9367 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9368 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9369 78f5ac24 2022-03-19 op return NULL;
9370 78f5ac24 2022-03-19 op }
9371 78f5ac24 2022-03-19 op
9372 78f5ac24 2022-03-19 op static const struct got_error *
9373 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9374 78f5ac24 2022-03-19 op unsigned char staged_status)
9375 78f5ac24 2022-03-19 op {
9376 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9377 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9378 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9379 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9380 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9381 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9382 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9383 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9384 78f5ac24 2022-03-19 op return NULL;
9385 78f5ac24 2022-03-19 op }
9386 78f5ac24 2022-03-19 op
9387 78f5ac24 2022-03-19 op const struct got_error *
9388 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9389 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9390 78f5ac24 2022-03-19 op {
9391 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9392 78f5ac24 2022-03-19 op }
9393 78f5ac24 2022-03-19 op
9394 78f5ac24 2022-03-19 op const struct got_error *
9395 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9396 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9397 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9398 78f5ac24 2022-03-19 op {
9399 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9400 78f5ac24 2022-03-19 op int file_renamed = 0;
9401 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9402 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9403 78f5ac24 2022-03-19 op
9404 78f5ac24 2022-03-19 op *oldpath = NULL;
9405 78f5ac24 2022-03-19 op *newpath = NULL;
9406 78f5ac24 2022-03-19 op
9407 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9408 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9409 78f5ac24 2022-03-19 op if (err)
9410 78f5ac24 2022-03-19 op goto done;
9411 78f5ac24 2022-03-19 op
9412 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9413 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9414 78f5ac24 2022-03-19 op if (err)
9415 78f5ac24 2022-03-19 op goto done;
9416 78f5ac24 2022-03-19 op
9417 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9418 78f5ac24 2022-03-19 op file_renamed = 1;
9419 78f5ac24 2022-03-19 op
9420 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9421 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9422 78f5ac24 2022-03-19 op else if (file_renamed) {
9423 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9424 78f5ac24 2022-03-19 op if (err == NULL)
9425 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9426 78f5ac24 2022-03-19 op } else if (old == NULL)
9427 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9428 78f5ac24 2022-03-19 op else
9429 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9430 78f5ac24 2022-03-19 op
9431 78f5ac24 2022-03-19 op done:
9432 78f5ac24 2022-03-19 op if (err) {
9433 78f5ac24 2022-03-19 op free(*oldpath);
9434 78f5ac24 2022-03-19 op *oldpath = NULL;
9435 78f5ac24 2022-03-19 op free(*newpath);
9436 78f5ac24 2022-03-19 op *newpath = NULL;
9437 78f5ac24 2022-03-19 op }
9438 78f5ac24 2022-03-19 op return err;
9439 78f5ac24 2022-03-19 op }
9440 78f5ac24 2022-03-19 op
9441 78f5ac24 2022-03-19 op const struct got_error *
9442 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9443 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9444 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9445 78f5ac24 2022-03-19 op {
9446 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9447 f2dd7807 2022-05-17 op
9448 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9449 f2dd7807 2022-05-17 op saa.worktree = worktree;
9450 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9451 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9452 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9453 f2dd7807 2022-05-17 op saa.repo = repo;
9454 f2dd7807 2022-05-17 op
9455 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9456 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9457 78f5ac24 2022-03-19 op }
9458 f2dd7807 2022-05-17 op
9459 f2dd7807 2022-05-17 op const struct got_error *
9460 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9461 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9462 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9463 f2dd7807 2022-05-17 op {
9464 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9465 f2dd7807 2022-05-17 op
9466 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9467 f2dd7807 2022-05-17 op sda.worktree = worktree;
9468 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9469 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9470 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9471 f2dd7807 2022-05-17 op sda.repo = repo;
9472 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9473 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9474 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9475 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9476 f2dd7807 2022-05-17 op
9477 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9478 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9479 f2dd7807 2022-05-17 op }
9480 f2dd7807 2022-05-17 op
9481 f2dd7807 2022-05-17 op const struct got_error *
9482 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9483 4bcdc895 2022-05-17 op const char *fileindex_path)
9484 f2dd7807 2022-05-17 op {
9485 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9486 f2dd7807 2022-05-17 op
9487 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9488 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9489 f2dd7807 2022-05-17 op
9490 f2dd7807 2022-05-17 op return err;
9491 f2dd7807 2022-05-17 op }