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 69c6accf 2023-02-04 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 1362b0e3 2023-02-04 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 3093e2d7 2023-02-04 op blob_id->hash, base_commit_id->hash, 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 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1518 6353ad76 2019-02-08 stsp static const struct got_error *
1519 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1520 7154f6ce 2019-03-27 stsp {
1521 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1522 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1523 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1524 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1526 7154f6ce 2019-03-27 stsp };
1527 7154f6ce 2019-03-27 stsp int i = 0;
1528 9bdd68dd 2021-01-02 naddy char *line = NULL;
1529 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1530 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1531 7154f6ce 2019-03-27 stsp
1532 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1533 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1534 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1535 7154f6ce 2019-03-27 stsp if (feof(f))
1536 7154f6ce 2019-03-27 stsp break;
1537 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1538 7154f6ce 2019-03-27 stsp break;
1539 7154f6ce 2019-03-27 stsp }
1540 7154f6ce 2019-03-27 stsp
1541 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1542 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1543 19332e6d 2019-05-13 stsp == 0)
1544 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1545 7154f6ce 2019-03-27 stsp else
1546 7154f6ce 2019-03-27 stsp i++;
1547 7154f6ce 2019-03-27 stsp }
1548 7154f6ce 2019-03-27 stsp }
1549 9bdd68dd 2021-01-02 naddy free(line);
1550 7154f6ce 2019-03-27 stsp
1551 7154f6ce 2019-03-27 stsp return err;
1552 e2b1e152 2019-07-13 stsp }
1553 e2b1e152 2019-07-13 stsp
1554 e2b1e152 2019-07-13 stsp static int
1555 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1556 1ebedb77 2019-10-19 stsp {
1557 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1558 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1559 1ebedb77 2019-10-19 stsp }
1560 1ebedb77 2019-10-19 stsp
1561 1ebedb77 2019-10-19 stsp static int
1562 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1563 e2b1e152 2019-07-13 stsp {
1564 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1565 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1566 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1567 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1568 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1569 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1570 c363b2c1 2019-08-03 stsp }
1571 c363b2c1 2019-08-03 stsp
1572 c363b2c1 2019-08-03 stsp static unsigned char
1573 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1574 c363b2c1 2019-08-03 stsp {
1575 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1576 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1577 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1578 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1579 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1580 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1581 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1582 c363b2c1 2019-08-03 stsp default:
1583 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1584 a919d5c4 2020-07-23 stsp }
1585 a919d5c4 2020-07-23 stsp }
1586 a919d5c4 2020-07-23 stsp
1587 a919d5c4 2020-07-23 stsp static const struct got_error *
1588 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1589 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1590 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1591 a919d5c4 2020-07-23 stsp {
1592 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1593 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1594 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1595 a919d5c4 2020-07-23 stsp ssize_t elen;
1596 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1597 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1598 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1599 a919d5c4 2020-07-23 stsp
1600 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1601 a919d5c4 2020-07-23 stsp
1602 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1603 a919d5c4 2020-07-23 stsp do {
1604 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1605 a919d5c4 2020-07-23 stsp if (err)
1606 a919d5c4 2020-07-23 stsp return err;
1607 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1608 a919d5c4 2020-07-23 stsp /*
1609 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1610 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1611 a919d5c4 2020-07-23 stsp */
1612 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1613 a919d5c4 2020-07-23 stsp }
1614 a919d5c4 2020-07-23 stsp if (len > 0) {
1615 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1616 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1617 a919d5c4 2020-07-23 stsp len - hdrlen);
1618 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1619 a919d5c4 2020-07-23 stsp hdrlen = 0;
1620 a919d5c4 2020-07-23 stsp }
1621 a919d5c4 2020-07-23 stsp } while (len != 0);
1622 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1623 a919d5c4 2020-07-23 stsp
1624 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1625 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1626 a919d5c4 2020-07-23 stsp if (elen == -1)
1627 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1628 a919d5c4 2020-07-23 stsp } else {
1629 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1630 a919d5c4 2020-07-23 stsp if (elen == -1)
1631 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1632 c363b2c1 2019-08-03 stsp }
1633 a919d5c4 2020-07-23 stsp
1634 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1635 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1636 a919d5c4 2020-07-23 stsp
1637 a919d5c4 2020-07-23 stsp return NULL;
1638 7154f6ce 2019-03-27 stsp }
1639 7154f6ce 2019-03-27 stsp
1640 7154f6ce 2019-03-27 stsp static const struct got_error *
1641 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1642 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1643 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1644 6353ad76 2019-02-08 stsp {
1645 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1646 6353ad76 2019-02-08 stsp struct got_object_id id;
1647 6353ad76 2019-02-08 stsp size_t hdrlen;
1648 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1649 6353ad76 2019-02-08 stsp FILE *f = NULL;
1650 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1651 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1652 6353ad76 2019-02-08 stsp size_t flen, blen;
1653 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1654 6353ad76 2019-02-08 stsp
1655 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1656 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1657 6353ad76 2019-02-08 stsp
1658 7f91a133 2019-12-13 stsp /*
1659 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1660 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1661 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1662 7f91a133 2019-12-13 stsp */
1663 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1664 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1666 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1667 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1668 882ef1b9 2019-12-13 stsp else
1669 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1670 882ef1b9 2019-12-13 stsp goto done;
1671 882ef1b9 2019-12-13 stsp }
1672 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1673 3d35a492 2019-12-13 stsp goto done;
1674 3d35a492 2019-12-13 stsp }
1675 7f91a133 2019-12-13 stsp } else {
1676 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1678 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1679 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1680 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1682 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1683 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1685 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1686 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1687 3d35a492 2019-12-13 stsp else
1688 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1689 3d35a492 2019-12-13 stsp goto done;
1690 3d35a492 2019-12-13 stsp }
1691 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1692 1338848f 2019-12-13 stsp goto done;
1693 a378724f 2019-02-10 stsp }
1694 a378724f 2019-02-10 stsp }
1695 6353ad76 2019-02-08 stsp
1696 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1698 1338848f 2019-12-13 stsp goto done;
1699 3f148bc6 2019-07-27 stsp }
1700 efdd40df 2019-07-27 stsp
1701 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1703 1338848f 2019-12-13 stsp goto done;
1704 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1705 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1706 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1707 1338848f 2019-12-13 stsp goto done;
1708 d00136be 2019-03-26 stsp }
1709 b8f41171 2019-02-10 stsp
1710 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1711 f179e66d 2020-07-23 stsp goto done;
1712 f179e66d 2020-07-23 stsp
1713 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1714 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1716 1338848f 2019-12-13 stsp goto done;
1717 f179e66d 2020-07-23 stsp }
1718 6353ad76 2019-02-08 stsp
1719 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1720 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1721 3093e2d7 2023-02-04 op memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1722 c363b2c1 2019-08-03 stsp else
1723 3093e2d7 2023-02-04 op memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1724 90843939 2023-02-04 op id.algo = got_repo_get_hash_algorithm(repo);
1725 c363b2c1 2019-08-03 stsp
1726 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1727 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1728 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1729 eb81bc23 2022-06-28 tracey goto done;
1730 eb81bc23 2022-06-28 tracey }
1731 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1732 6353ad76 2019-02-08 stsp if (err)
1733 1338848f 2019-12-13 stsp goto done;
1734 6353ad76 2019-02-08 stsp
1735 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1736 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1737 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1738 a919d5c4 2020-07-23 stsp goto done;
1739 a919d5c4 2020-07-23 stsp }
1740 a919d5c4 2020-07-23 stsp
1741 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1742 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 ab0d4361 2019-12-13 stsp if (fd == -1) {
1744 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1745 ab0d4361 2019-12-13 stsp goto done;
1746 ab0d4361 2019-12-13 stsp }
1747 3d35a492 2019-12-13 stsp }
1748 3d35a492 2019-12-13 stsp
1749 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1750 6353ad76 2019-02-08 stsp if (f == NULL) {
1751 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1752 6353ad76 2019-02-08 stsp goto done;
1753 6353ad76 2019-02-08 stsp }
1754 1338848f 2019-12-13 stsp fd = -1;
1755 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1756 656b1f76 2019-05-11 jcs for (;;) {
1757 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1759 6353ad76 2019-02-08 stsp if (err)
1760 7154f6ce 2019-03-27 stsp goto done;
1761 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1762 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1764 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1765 7154f6ce 2019-03-27 stsp goto done;
1766 80c5c120 2019-02-19 stsp }
1767 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1768 6353ad76 2019-02-08 stsp if (flen != 0)
1769 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1770 6353ad76 2019-02-08 stsp break;
1771 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1772 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1773 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1774 6353ad76 2019-02-08 stsp break;
1775 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1776 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1777 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1779 6353ad76 2019-02-08 stsp break;
1780 6353ad76 2019-02-08 stsp }
1781 6353ad76 2019-02-08 stsp } else {
1782 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1783 6353ad76 2019-02-08 stsp break;
1784 6353ad76 2019-02-08 stsp }
1785 6353ad76 2019-02-08 stsp hdrlen = 0;
1786 6353ad76 2019-02-08 stsp }
1787 7154f6ce 2019-03-27 stsp
1788 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1789 7154f6ce 2019-03-27 stsp rewind(f);
1790 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1791 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1792 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1793 6353ad76 2019-02-08 stsp done:
1794 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1795 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1796 6353ad76 2019-02-08 stsp if (blob)
1797 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1798 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1799 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1800 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1801 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1802 9d31a1d8 2018-03-11 stsp return err;
1803 e2b1e152 2019-07-13 stsp }
1804 e2b1e152 2019-07-13 stsp
1805 e2b1e152 2019-07-13 stsp /*
1806 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1807 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1808 e2b1e152 2019-07-13 stsp */
1809 e2b1e152 2019-07-13 stsp static const struct got_error *
1810 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1811 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1812 e2b1e152 2019-07-13 stsp {
1813 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1814 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1815 3093e2d7 2023-02-04 op ie->blob_hash, ie->commit_hash, 1);
1816 e2b1e152 2019-07-13 stsp
1817 e2b1e152 2019-07-13 stsp return NULL;
1818 9d31a1d8 2018-03-11 stsp }
1819 9d31a1d8 2018-03-11 stsp
1820 9d31a1d8 2018-03-11 stsp static const struct got_error *
1821 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1822 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1823 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1824 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1825 0584f854 2019-04-06 stsp void *progress_arg)
1826 9d31a1d8 2018-03-11 stsp {
1827 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1828 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1829 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1830 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1831 b8f41171 2019-02-10 stsp struct stat sb;
1832 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1833 9d31a1d8 2018-03-11 stsp
1834 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1835 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1836 6353ad76 2019-02-08 stsp
1837 abb4604f 2019-07-27 stsp if (ie) {
1838 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1839 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1840 a76c42e6 2019-08-03 stsp goto done;
1841 a76c42e6 2019-08-03 stsp }
1842 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1843 7f91a133 2019-12-13 stsp repo);
1844 abb4604f 2019-07-27 stsp if (err)
1845 abb4604f 2019-07-27 stsp goto done;
1846 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1847 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1848 c90c8ce3 2020-07-23 stsp } else {
1849 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1850 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1851 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1852 f6764181 2021-09-24 stsp ondisk_path);
1853 f6764181 2021-09-24 stsp goto done;
1854 f6764181 2021-09-24 stsp }
1855 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1856 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1857 f6764181 2021-09-24 stsp } else {
1858 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1859 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1860 f6764181 2021-09-24 stsp else
1861 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1862 f6764181 2021-09-24 stsp }
1863 c90c8ce3 2020-07-23 stsp }
1864 6353ad76 2019-02-08 stsp
1865 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1866 2c41dce7 2021-06-27 stsp if (ie)
1867 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1868 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1869 b8f41171 2019-02-10 stsp goto done;
1870 b8f41171 2019-02-10 stsp }
1871 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1872 a769b60b 2021-06-27 stsp if (ie)
1873 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1874 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1875 5036ab18 2020-04-18 stsp path);
1876 5036ab18 2020-04-18 stsp goto done;
1877 5036ab18 2020-04-18 stsp }
1878 b8f41171 2019-02-10 stsp
1879 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1880 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1881 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 c6e8a826 2021-04-05 stsp /*
1883 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1884 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1885 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1886 c6e8a826 2021-04-05 stsp * updating contents of this file.
1887 c6e8a826 2021-04-05 stsp */
1888 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1889 3093e2d7 2023-02-04 op memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1890 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1891 c6e8a826 2021-04-05 stsp /* Same commit. */
1892 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1893 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1894 e2b1e152 2019-07-13 stsp if (err)
1895 e2b1e152 2019-07-13 stsp goto done;
1896 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 b8f41171 2019-02-10 stsp path);
1898 6353ad76 2019-02-08 stsp goto done;
1899 a378724f 2019-02-10 stsp }
1900 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1901 3093e2d7 2023-02-04 op memcmp(ie->blob_hash, te->id.hash,
1902 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1903 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1904 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1905 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1906 0f58026f 2021-04-05 stsp if (err)
1907 0f58026f 2021-04-05 stsp goto done;
1908 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1909 0f58026f 2021-04-05 stsp path);
1910 b8f41171 2019-02-10 stsp goto done;
1911 e2b1e152 2019-07-13 stsp }
1912 9d31a1d8 2018-03-11 stsp }
1913 9d31a1d8 2018-03-11 stsp
1914 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1915 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1916 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1917 eb81bc23 2022-06-28 tracey goto done;
1918 eb81bc23 2022-06-28 tracey }
1919 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1920 8da9e5f4 2019-01-12 stsp if (err)
1921 6353ad76 2019-02-08 stsp goto done;
1922 512f0d0e 2019-01-02 stsp
1923 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1924 234035bc 2019-06-01 stsp int update_timestamps;
1925 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1926 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1927 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1928 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1929 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1930 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1931 eb81bc23 2022-06-28 tracey goto done;
1932 eb81bc23 2022-06-28 tracey }
1933 234035bc 2019-06-01 stsp struct got_object_id id2;
1934 3093e2d7 2023-02-04 op memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1935 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1936 eb81bc23 2022-06-28 tracey fd2);
1937 234035bc 2019-06-01 stsp if (err)
1938 f69721c3 2019-10-21 stsp goto done;
1939 f69721c3 2019-10-21 stsp }
1940 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1941 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1942 3093e2d7 2023-02-04 op if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1943 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1944 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1945 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1946 f69721c3 2019-10-21 stsp goto done;
1947 f69721c3 2019-10-21 stsp }
1948 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1949 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1950 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1951 234035bc 2019-06-01 stsp goto done;
1952 f69721c3 2019-10-21 stsp }
1953 234035bc 2019-06-01 stsp }
1954 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1955 36bf999c 2020-07-23 stsp char *link_target;
1956 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1957 36bf999c 2020-07-23 stsp if (err)
1958 36bf999c 2020-07-23 stsp goto done;
1959 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1960 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1961 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1962 36bf999c 2020-07-23 stsp free(link_target);
1963 993e2a1b 2020-07-23 stsp } else {
1964 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1965 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1966 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1967 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1968 993e2a1b 2020-07-23 stsp }
1969 f69721c3 2019-10-21 stsp free(label_orig);
1970 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1971 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1972 eb81bc23 2022-06-28 tracey goto done;
1973 eb81bc23 2022-06-28 tracey }
1974 234035bc 2019-06-01 stsp if (blob2)
1975 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1976 909d120e 2019-09-22 stsp if (err)
1977 909d120e 2019-09-22 stsp goto done;
1978 234035bc 2019-06-01 stsp /*
1979 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1980 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1981 234035bc 2019-06-01 stsp * unmodified files again.
1982 234035bc 2019-06-01 stsp */
1983 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 3093e2d7 2023-02-04 op blob->id.hash, worktree->base_commit_id->hash,
1985 234035bc 2019-06-01 stsp update_timestamps);
1986 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1988 3093e2d7 2023-02-04 op blob->id.hash, worktree->base_commit_id->hash, 0);
1989 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1990 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1991 1ee397ad 2019-07-12 stsp if (err)
1992 1ee397ad 2019-07-12 stsp goto done;
1993 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1994 3093e2d7 2023-02-04 op blob->id.hash, worktree->base_commit_id->hash, 0);
1995 13d9040b 2019-03-27 stsp if (err)
1996 13d9040b 2019-03-27 stsp goto done;
1997 13d9040b 2019-03-27 stsp } else {
1998 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1999 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2000 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2001 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2002 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2003 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2004 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2005 2e1fa222 2020-07-23 stsp } else {
2006 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2007 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2008 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2009 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2010 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2011 2e1fa222 2020-07-23 stsp }
2012 13d9040b 2019-03-27 stsp if (err)
2013 13d9040b 2019-03-27 stsp goto done;
2014 65b05cec 2020-07-23 stsp
2015 054041d0 2020-06-24 stsp if (ie) {
2016 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2017 3093e2d7 2023-02-04 op worktree->root_fd, path, blob->id.hash,
2018 3093e2d7 2023-02-04 op worktree->base_commit_id->hash, 1);
2019 054041d0 2020-06-24 stsp } else {
2020 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2021 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2022 054041d0 2020-06-24 stsp &blob->id);
2023 054041d0 2020-06-24 stsp }
2024 13d9040b 2019-03-27 stsp if (err)
2025 13d9040b 2019-03-27 stsp goto done;
2026 65b05cec 2020-07-23 stsp
2027 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2028 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2029 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2030 2e1fa222 2020-07-23 stsp }
2031 13d9040b 2019-03-27 stsp }
2032 eb81bc23 2022-06-28 tracey
2033 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2034 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2035 eb81bc23 2022-06-28 tracey goto done;
2036 eb81bc23 2022-06-28 tracey }
2037 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2038 6353ad76 2019-02-08 stsp done:
2039 6353ad76 2019-02-08 stsp free(ondisk_path);
2040 8da9e5f4 2019-01-12 stsp return err;
2041 90285c3b 2019-01-08 stsp }
2042 90285c3b 2019-01-08 stsp
2043 90285c3b 2019-01-08 stsp static const struct got_error *
2044 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2045 90285c3b 2019-01-08 stsp {
2046 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2047 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2048 90285c3b 2019-01-08 stsp
2049 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2050 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2051 90285c3b 2019-01-08 stsp
2052 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2053 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2054 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2055 c97665ae 2019-01-13 stsp } else {
2056 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2057 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2058 1c4cdd89 2021-06-20 stsp if (err)
2059 1c4cdd89 2021-06-20 stsp goto done;
2060 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2061 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2062 fddefe3b 2020-10-20 stsp free(ondisk_path);
2063 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2064 1c4cdd89 2021-06-20 stsp parent = NULL;
2065 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2066 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2067 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2068 fddefe3b 2020-10-20 stsp ondisk_path);
2069 90285c3b 2019-01-08 stsp break;
2070 90285c3b 2019-01-08 stsp }
2071 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2072 1c4cdd89 2021-06-20 stsp if (err)
2073 1c4cdd89 2021-06-20 stsp break;
2074 1c4cdd89 2021-06-20 stsp }
2075 90285c3b 2019-01-08 stsp }
2076 1c4cdd89 2021-06-20 stsp done:
2077 90285c3b 2019-01-08 stsp free(ondisk_path);
2078 1c4cdd89 2021-06-20 stsp free(parent);
2079 90285c3b 2019-01-08 stsp return err;
2080 512f0d0e 2019-01-02 stsp }
2081 512f0d0e 2019-01-02 stsp
2082 708d8e67 2019-03-27 stsp static const struct got_error *
2083 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2084 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2085 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 708d8e67 2019-03-27 stsp {
2087 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2088 708d8e67 2019-03-27 stsp unsigned char status;
2089 708d8e67 2019-03-27 stsp struct stat sb;
2090 708d8e67 2019-03-27 stsp char *ondisk_path;
2091 708d8e67 2019-03-27 stsp
2092 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2093 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 a76c42e6 2019-08-03 stsp
2095 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2096 708d8e67 2019-03-27 stsp == -1)
2097 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2098 708d8e67 2019-03-27 stsp
2099 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2100 708d8e67 2019-03-27 stsp if (err)
2101 5a58a424 2020-06-23 stsp goto done;
2102 708d8e67 2019-03-27 stsp
2103 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2104 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2105 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2106 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2107 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2108 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2109 993e2a1b 2020-07-23 stsp goto done;
2110 993e2a1b 2020-07-23 stsp }
2111 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2112 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2113 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2114 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2115 993e2a1b 2020-07-23 stsp if (err)
2116 993e2a1b 2020-07-23 stsp goto done;
2117 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2118 993e2a1b 2020-07-23 stsp ie->path);
2119 993e2a1b 2020-07-23 stsp goto done;
2120 993e2a1b 2020-07-23 stsp }
2121 993e2a1b 2020-07-23 stsp
2122 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2123 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2124 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2125 1ee397ad 2019-07-12 stsp if (err)
2126 5a58a424 2020-06-23 stsp goto done;
2127 fc6346c4 2019-03-27 stsp /*
2128 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2129 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2130 fc6346c4 2019-03-27 stsp */
2131 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2132 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2133 fc6346c4 2019-03-27 stsp } else {
2134 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2135 1ee397ad 2019-07-12 stsp if (err)
2136 5a58a424 2020-06-23 stsp goto done;
2137 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2138 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2139 fc6346c4 2019-03-27 stsp if (err)
2140 5a58a424 2020-06-23 stsp goto done;
2141 fc6346c4 2019-03-27 stsp }
2142 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2143 708d8e67 2019-03-27 stsp }
2144 5a58a424 2020-06-23 stsp done:
2145 5a58a424 2020-06-23 stsp free(ondisk_path);
2146 fc6346c4 2019-03-27 stsp return err;
2147 708d8e67 2019-03-27 stsp }
2148 708d8e67 2019-03-27 stsp
2149 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2150 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2151 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2152 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2153 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2154 8da9e5f4 2019-01-12 stsp void *progress_arg;
2155 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2156 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2157 8da9e5f4 2019-01-12 stsp };
2158 8da9e5f4 2019-01-12 stsp
2159 512f0d0e 2019-01-02 stsp static const struct got_error *
2160 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2161 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2162 512f0d0e 2019-01-02 stsp {
2163 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2164 0584f854 2019-04-06 stsp
2165 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2166 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2167 512f0d0e 2019-01-02 stsp
2168 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2169 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2170 8da9e5f4 2019-01-12 stsp }
2171 512f0d0e 2019-01-02 stsp
2172 8da9e5f4 2019-01-12 stsp static const struct got_error *
2173 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 8da9e5f4 2019-01-12 stsp {
2175 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2176 7a9df742 2019-01-08 stsp
2177 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2178 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2179 0584f854 2019-04-06 stsp
2180 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2181 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2182 9d31a1d8 2018-03-11 stsp }
2183 9d31a1d8 2018-03-11 stsp
2184 9d31a1d8 2018-03-11 stsp static const struct got_error *
2185 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 9d31a1d8 2018-03-11 stsp {
2187 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2188 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2189 8da9e5f4 2019-01-12 stsp char *path;
2190 9d31a1d8 2018-03-11 stsp
2191 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2192 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2193 0584f854 2019-04-06 stsp
2194 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2195 63c5ca5d 2019-08-24 stsp return NULL;
2196 63c5ca5d 2019-08-24 stsp
2197 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2198 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2199 8da9e5f4 2019-01-12 stsp == -1)
2200 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2201 9d31a1d8 2018-03-11 stsp
2202 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2203 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2204 8da9e5f4 2019-01-12 stsp else
2205 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2206 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2207 9d31a1d8 2018-03-11 stsp
2208 8da9e5f4 2019-01-12 stsp free(path);
2209 0cd1c46a 2019-03-11 stsp return err;
2210 0cd1c46a 2019-03-11 stsp }
2211 0cd1c46a 2019-03-11 stsp
2212 b2118c49 2020-07-28 stsp const struct got_error *
2213 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 b2118c49 2020-07-28 stsp {
2215 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2216 b2118c49 2020-07-28 stsp
2217 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2218 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2219 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2220 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2221 b2118c49 2020-07-28 stsp }
2222 b2118c49 2020-07-28 stsp
2223 b2118c49 2020-07-28 stsp return NULL;
2224 b2118c49 2020-07-28 stsp }
2225 b2118c49 2020-07-28 stsp
2226 818c7501 2019-07-11 stsp static const struct got_error *
2227 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 0cd1c46a 2019-03-11 stsp {
2229 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2230 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2231 0cd1c46a 2019-03-11 stsp
2232 517bab73 2019-03-11 stsp *refname = NULL;
2233 517bab73 2019-03-11 stsp
2234 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2235 b2118c49 2020-07-28 stsp if (err)
2236 b2118c49 2020-07-28 stsp return err;
2237 0cd1c46a 2019-03-11 stsp
2238 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2239 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2240 0647c563 2019-03-11 stsp *refname = NULL;
2241 0cd1c46a 2019-03-11 stsp }
2242 517bab73 2019-03-11 stsp free(uuidstr);
2243 517bab73 2019-03-11 stsp return err;
2244 517bab73 2019-03-11 stsp }
2245 517bab73 2019-03-11 stsp
2246 818c7501 2019-07-11 stsp const struct got_error *
2247 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2248 9587e6cc 2023-01-28 mark const char *prefix)
2249 9587e6cc 2023-01-28 mark {
2250 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2251 9587e6cc 2023-01-28 mark }
2252 9587e6cc 2023-01-28 mark
2253 9587e6cc 2023-01-28 mark const struct got_error *
2254 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 818c7501 2019-07-11 stsp {
2256 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2257 818c7501 2019-07-11 stsp }
2258 818c7501 2019-07-11 stsp
2259 818c7501 2019-07-11 stsp static const struct got_error *
2260 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 818c7501 2019-07-11 stsp {
2262 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2263 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2264 818c7501 2019-07-11 stsp }
2265 818c7501 2019-07-11 stsp
2266 818c7501 2019-07-11 stsp static const struct got_error *
2267 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 818c7501 2019-07-11 stsp {
2269 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2270 818c7501 2019-07-11 stsp }
2271 818c7501 2019-07-11 stsp
2272 818c7501 2019-07-11 stsp static const struct got_error *
2273 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 818c7501 2019-07-11 stsp {
2275 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2276 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2277 818c7501 2019-07-11 stsp }
2278 818c7501 2019-07-11 stsp
2279 818c7501 2019-07-11 stsp static const struct got_error *
2280 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 818c7501 2019-07-11 stsp {
2282 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2283 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2284 0ebf8283 2019-07-24 stsp }
2285 0ebf8283 2019-07-24 stsp
2286 0ebf8283 2019-07-24 stsp static const struct got_error *
2287 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 0ebf8283 2019-07-24 stsp {
2289 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2290 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2291 0ebf8283 2019-07-24 stsp }
2292 0ebf8283 2019-07-24 stsp
2293 0ebf8283 2019-07-24 stsp static const struct got_error *
2294 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 0ebf8283 2019-07-24 stsp {
2296 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2297 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2298 0ebf8283 2019-07-24 stsp }
2299 0ebf8283 2019-07-24 stsp
2300 0ebf8283 2019-07-24 stsp static const struct got_error *
2301 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 0ebf8283 2019-07-24 stsp {
2303 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2304 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2305 818c7501 2019-07-11 stsp }
2306 818c7501 2019-07-11 stsp
2307 0ebf8283 2019-07-24 stsp static const struct got_error *
2308 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 0ebf8283 2019-07-24 stsp {
2310 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2311 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2312 0ebf8283 2019-07-24 stsp }
2313 818c7501 2019-07-11 stsp
2314 0ebf8283 2019-07-24 stsp const struct got_error *
2315 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2316 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2317 0ebf8283 2019-07-24 stsp {
2318 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2319 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2320 0ebf8283 2019-07-24 stsp *path = NULL;
2321 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2322 0ebf8283 2019-07-24 stsp }
2323 0ebf8283 2019-07-24 stsp return NULL;
2324 f259c4c1 2021-09-24 stsp }
2325 f259c4c1 2021-09-24 stsp
2326 f259c4c1 2021-09-24 stsp static const struct got_error *
2327 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 f259c4c1 2021-09-24 stsp {
2329 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2330 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2331 0ebf8283 2019-07-24 stsp }
2332 0ebf8283 2019-07-24 stsp
2333 f259c4c1 2021-09-24 stsp static const struct got_error *
2334 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 f259c4c1 2021-09-24 stsp {
2336 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2337 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2338 f259c4c1 2021-09-24 stsp }
2339 f259c4c1 2021-09-24 stsp
2340 517bab73 2019-03-11 stsp /*
2341 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2342 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2343 517bab73 2019-03-11 stsp */
2344 517bab73 2019-03-11 stsp static const struct got_error *
2345 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 517bab73 2019-03-11 stsp {
2347 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2348 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2349 517bab73 2019-03-11 stsp char *refname;
2350 517bab73 2019-03-11 stsp
2351 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2352 517bab73 2019-03-11 stsp if (err)
2353 517bab73 2019-03-11 stsp return err;
2354 0cd1c46a 2019-03-11 stsp
2355 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2356 0cd1c46a 2019-03-11 stsp if (err)
2357 0cd1c46a 2019-03-11 stsp goto done;
2358 0cd1c46a 2019-03-11 stsp
2359 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2360 0cd1c46a 2019-03-11 stsp done:
2361 0cd1c46a 2019-03-11 stsp free(refname);
2362 0cd1c46a 2019-03-11 stsp if (ref)
2363 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2364 3e3a69f1 2019-07-25 stsp return err;
2365 3e3a69f1 2019-07-25 stsp }
2366 3e3a69f1 2019-07-25 stsp
2367 3e3a69f1 2019-07-25 stsp static const struct got_error *
2368 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 3e3a69f1 2019-07-25 stsp {
2370 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2371 3e3a69f1 2019-07-25 stsp
2372 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2373 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2374 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2375 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2376 3e3a69f1 2019-07-25 stsp }
2377 9d31a1d8 2018-03-11 stsp return err;
2378 9d31a1d8 2018-03-11 stsp }
2379 9d31a1d8 2018-03-11 stsp
2380 3e3a69f1 2019-07-25 stsp
2381 ebf99748 2019-05-09 stsp static const struct got_error *
2382 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2383 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2384 ebf99748 2019-05-09 stsp {
2385 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2386 ebf99748 2019-05-09 stsp FILE *index = NULL;
2387 0cd1c46a 2019-03-11 stsp
2388 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2389 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2390 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2391 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2392 ebf99748 2019-05-09 stsp
2393 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2394 3e3a69f1 2019-07-25 stsp if (err)
2395 ebf99748 2019-05-09 stsp goto done;
2396 ebf99748 2019-05-09 stsp
2397 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2398 ebf99748 2019-05-09 stsp if (index == NULL) {
2399 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2400 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2401 ebf99748 2019-05-09 stsp } else {
2402 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2403 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2404 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2405 ebf99748 2019-05-09 stsp }
2406 ebf99748 2019-05-09 stsp done:
2407 ebf99748 2019-05-09 stsp if (err) {
2408 ebf99748 2019-05-09 stsp free(*fileindex_path);
2409 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2410 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2411 ebf99748 2019-05-09 stsp *fileindex = NULL;
2412 ebf99748 2019-05-09 stsp }
2413 ebf99748 2019-05-09 stsp return err;
2414 ebf99748 2019-05-09 stsp }
2415 c932eeeb 2019-05-22 stsp
2416 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2417 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2418 c932eeeb 2019-05-22 stsp const char *path;
2419 c932eeeb 2019-05-22 stsp size_t path_len;
2420 c932eeeb 2019-05-22 stsp const char *entry_name;
2421 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2422 a484d721 2019-06-10 stsp void *progress_arg;
2423 c932eeeb 2019-05-22 stsp };
2424 ebf99748 2019-05-09 stsp
2425 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2426 c932eeeb 2019-05-22 stsp static const struct got_error *
2427 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2428 c932eeeb 2019-05-22 stsp {
2429 1ee397ad 2019-07-12 stsp const struct got_error *err;
2430 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2431 c932eeeb 2019-05-22 stsp
2432 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2433 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2434 c932eeeb 2019-05-22 stsp return NULL;
2435 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2436 102ff934 2019-06-11 stsp return NULL;
2437 102ff934 2019-06-11 stsp
2438 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2439 a769b60b 2021-06-27 stsp return NULL;
2440 a769b60b 2021-06-27 stsp
2441 3093e2d7 2023-02-04 op if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2442 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2443 c932eeeb 2019-05-22 stsp return NULL;
2444 c932eeeb 2019-05-22 stsp
2445 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2446 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2447 edd02c5e 2019-07-11 stsp ie->path);
2448 1ee397ad 2019-07-12 stsp if (err)
2449 1ee397ad 2019-07-12 stsp return err;
2450 1ee397ad 2019-07-12 stsp }
2451 3093e2d7 2023-02-04 op memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2452 c932eeeb 2019-05-22 stsp return NULL;
2453 a615e0e7 2020-12-16 stsp }
2454 a615e0e7 2020-12-16 stsp
2455 a615e0e7 2020-12-16 stsp static const struct got_error *
2456 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2457 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2458 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 a615e0e7 2020-12-16 stsp {
2460 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2461 a615e0e7 2020-12-16 stsp
2462 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2463 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2464 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2465 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2466 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2467 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2468 a615e0e7 2020-12-16 stsp
2469 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2470 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2471 9c6338c4 2019-06-02 stsp }
2472 9c6338c4 2019-06-02 stsp
2473 9c6338c4 2019-06-02 stsp static const struct got_error *
2474 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 9c6338c4 2019-06-02 stsp {
2476 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2477 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2478 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2479 867630bb 2020-01-17 stsp struct timespec timeout;
2480 9c6338c4 2019-06-02 stsp
2481 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2482 b90054ed 2022-11-01 stsp fileindex_path, "");
2483 9c6338c4 2019-06-02 stsp if (err)
2484 9c6338c4 2019-06-02 stsp goto done;
2485 9c6338c4 2019-06-02 stsp
2486 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2487 9c6338c4 2019-06-02 stsp if (err)
2488 9c6338c4 2019-06-02 stsp goto done;
2489 9c6338c4 2019-06-02 stsp
2490 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2491 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2492 9c6338c4 2019-06-02 stsp fileindex_path);
2493 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2494 9c6338c4 2019-06-02 stsp }
2495 867630bb 2020-01-17 stsp
2496 867630bb 2020-01-17 stsp /*
2497 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2498 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2499 867630bb 2020-01-17 stsp * was recorded in the file index.
2500 867630bb 2020-01-17 stsp */
2501 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2502 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2503 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2504 9c6338c4 2019-06-02 stsp done:
2505 9c6338c4 2019-06-02 stsp if (new_index)
2506 9c6338c4 2019-06-02 stsp fclose(new_index);
2507 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2508 9c6338c4 2019-06-02 stsp return err;
2509 c932eeeb 2019-05-22 stsp }
2510 6ced4176 2019-07-12 stsp
2511 6ced4176 2019-07-12 stsp static const struct got_error *
2512 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2513 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2514 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2515 a44927cc 2022-04-07 stsp struct got_repository *repo)
2516 6ced4176 2019-07-12 stsp {
2517 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2518 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2519 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2520 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 6ced4176 2019-07-12 stsp
2522 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2523 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2524 6ced4176 2019-07-12 stsp *tree_id = NULL;
2525 6ced4176 2019-07-12 stsp
2526 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2527 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2528 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2529 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2530 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2531 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2532 6ced4176 2019-07-12 stsp goto done;
2533 6ced4176 2019-07-12 stsp }
2534 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2535 a44927cc 2022-04-07 stsp worktree->path_prefix);
2536 6ced4176 2019-07-12 stsp if (err)
2537 6ced4176 2019-07-12 stsp goto done;
2538 6ced4176 2019-07-12 stsp return NULL;
2539 6ced4176 2019-07-12 stsp }
2540 6ced4176 2019-07-12 stsp
2541 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2542 6ced4176 2019-07-12 stsp
2543 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2544 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2546 6ced4176 2019-07-12 stsp goto done;
2547 6ced4176 2019-07-12 stsp }
2548 6ced4176 2019-07-12 stsp
2549 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2550 6ced4176 2019-07-12 stsp if (err)
2551 6ced4176 2019-07-12 stsp goto done;
2552 6ced4176 2019-07-12 stsp
2553 6ced4176 2019-07-12 stsp free(in_repo_path);
2554 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2555 6ced4176 2019-07-12 stsp
2556 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2557 6ced4176 2019-07-12 stsp if (err)
2558 6ced4176 2019-07-12 stsp goto done;
2559 c932eeeb 2019-05-22 stsp
2560 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2561 6ced4176 2019-07-12 stsp /* Check out a single file. */
2562 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2563 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2564 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2565 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2566 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2567 6ced4176 2019-07-12 stsp goto done;
2568 6ced4176 2019-07-12 stsp }
2569 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2570 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2571 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2572 6ced4176 2019-07-12 stsp goto done;
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp } else {
2575 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2576 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2577 6ced4176 2019-07-12 stsp if (err)
2578 6ced4176 2019-07-12 stsp return err;
2579 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2580 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2581 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2582 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2583 6ced4176 2019-07-12 stsp goto done;
2584 6ced4176 2019-07-12 stsp }
2585 6ced4176 2019-07-12 stsp }
2586 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2587 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2588 6ced4176 2019-07-12 stsp } else {
2589 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2590 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2591 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2592 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2593 6ced4176 2019-07-12 stsp goto done;
2594 6ced4176 2019-07-12 stsp }
2595 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
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 6ced4176 2019-07-12 stsp }
2601 6ced4176 2019-07-12 stsp done:
2602 6ced4176 2019-07-12 stsp free(id);
2603 6ced4176 2019-07-12 stsp free(in_repo_path);
2604 6ced4176 2019-07-12 stsp if (err) {
2605 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2606 6ced4176 2019-07-12 stsp free(*tree_relpath);
2607 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2608 6ced4176 2019-07-12 stsp free(*tree_id);
2609 6ced4176 2019-07-12 stsp *tree_id = NULL;
2610 6ced4176 2019-07-12 stsp }
2611 07ed472d 2019-07-12 stsp return err;
2612 07ed472d 2019-07-12 stsp }
2613 07ed472d 2019-07-12 stsp
2614 07ed472d 2019-07-12 stsp static const struct got_error *
2615 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2616 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2617 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2618 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 07ed472d 2019-07-12 stsp {
2620 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2621 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2622 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2623 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2624 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2625 07ed472d 2019-07-12 stsp
2626 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2627 7f47418f 2019-12-20 stsp if (err) {
2628 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2629 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2630 7f47418f 2019-12-20 stsp goto done;
2631 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2632 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2633 7f47418f 2019-12-20 stsp if (err)
2634 7f47418f 2019-12-20 stsp return err;
2635 7f47418f 2019-12-20 stsp }
2636 07ed472d 2019-07-12 stsp
2637 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2638 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2639 07ed472d 2019-07-12 stsp if (err)
2640 07ed472d 2019-07-12 stsp goto done;
2641 07ed472d 2019-07-12 stsp
2642 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2643 07ed472d 2019-07-12 stsp if (err)
2644 07ed472d 2019-07-12 stsp goto done;
2645 07ed472d 2019-07-12 stsp
2646 07ed472d 2019-07-12 stsp if (entry_name &&
2647 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2648 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2649 07ed472d 2019-07-12 stsp goto done;
2650 07ed472d 2019-07-12 stsp }
2651 07ed472d 2019-07-12 stsp
2652 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2653 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2654 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2655 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2656 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2657 07ed472d 2019-07-12 stsp arg.repo = repo;
2658 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2659 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2660 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2661 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2662 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2663 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2664 07ed472d 2019-07-12 stsp done:
2665 07ed472d 2019-07-12 stsp if (tree)
2666 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2667 07ed472d 2019-07-12 stsp if (commit)
2668 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2669 6ced4176 2019-07-12 stsp return err;
2670 6ced4176 2019-07-12 stsp }
2671 6ced4176 2019-07-12 stsp
2672 9d31a1d8 2018-03-11 stsp const struct got_error *
2673 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2674 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2675 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2676 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2677 9d31a1d8 2018-03-11 stsp {
2678 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2679 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2680 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2681 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2682 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2683 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2684 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2685 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2686 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2687 f2ea84fa 2019-07-27 stsp int entry_type;
2688 f2ea84fa 2019-07-27 stsp char *relpath;
2689 f2ea84fa 2019-07-27 stsp char *entry_name;
2690 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2691 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 9d31a1d8 2018-03-11 stsp
2693 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2694 f2ea84fa 2019-07-27 stsp
2695 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2696 9d31a1d8 2018-03-11 stsp if (err)
2697 9d31a1d8 2018-03-11 stsp return err;
2698 a44927cc 2022-04-07 stsp
2699 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2700 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2701 a44927cc 2022-04-07 stsp if (err)
2702 a44927cc 2022-04-07 stsp goto done;
2703 93a30277 2018-12-24 stsp
2704 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2705 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2706 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2707 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2708 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2709 f2ea84fa 2019-07-27 stsp goto done;
2710 f2ea84fa 2019-07-27 stsp }
2711 6ced4176 2019-07-12 stsp
2712 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2713 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2714 a44927cc 2022-04-07 stsp worktree, repo);
2715 f2ea84fa 2019-07-27 stsp if (err) {
2716 f2ea84fa 2019-07-27 stsp free(tpd);
2717 6ced4176 2019-07-12 stsp goto done;
2718 6ced4176 2019-07-12 stsp }
2719 f2ea84fa 2019-07-27 stsp
2720 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2721 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2722 f2ea84fa 2019-07-27 stsp if (err) {
2723 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2724 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2725 f2ea84fa 2019-07-27 stsp free(tpd);
2726 f2ea84fa 2019-07-27 stsp goto done;
2727 f2ea84fa 2019-07-27 stsp }
2728 f2ea84fa 2019-07-27 stsp } else
2729 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2730 f2ea84fa 2019-07-27 stsp
2731 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2732 6ced4176 2019-07-12 stsp }
2733 6ced4176 2019-07-12 stsp
2734 51514078 2018-12-25 stsp /*
2735 51514078 2018-12-25 stsp * Read the file index.
2736 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2737 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2738 51514078 2018-12-25 stsp */
2739 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2740 8da9e5f4 2019-01-12 stsp if (err)
2741 c4cdcb68 2019-04-03 stsp goto done;
2742 c4cdcb68 2019-04-03 stsp
2743 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2744 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2745 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2746 f2ea84fa 2019-07-27 stsp
2747 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2748 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2749 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2750 f2ea84fa 2019-07-27 stsp if (err)
2751 f2ea84fa 2019-07-27 stsp break;
2752 f2ea84fa 2019-07-27 stsp
2753 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2754 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2755 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2756 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2757 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2758 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2759 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2760 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2761 f2ea84fa 2019-07-27 stsp if (err)
2762 f2ea84fa 2019-07-27 stsp break;
2763 f2ea84fa 2019-07-27 stsp
2764 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2765 711d9cd9 2019-07-12 stsp }
2766 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2767 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2768 af12c6b9 2019-06-04 stsp err = sync_err;
2769 9d31a1d8 2018-03-11 stsp done:
2770 9c6338c4 2019-06-02 stsp free(fileindex_path);
2771 edfa7d7f 2018-09-11 stsp if (tree)
2772 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2773 9d31a1d8 2018-03-11 stsp if (commit)
2774 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2775 5ade8233 2019-07-12 stsp if (fileindex)
2776 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2777 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2778 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2779 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2780 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2781 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2782 f2ea84fa 2019-07-27 stsp free(tpd);
2783 f2ea84fa 2019-07-27 stsp }
2784 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2785 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2786 9d31a1d8 2018-03-11 stsp err = unlockerr;
2787 f8d1f275 2019-02-04 stsp return err;
2788 f8d1f275 2019-02-04 stsp }
2789 f8d1f275 2019-02-04 stsp
2790 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2791 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2792 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2793 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2794 234035bc 2019-06-01 stsp void *progress_arg;
2795 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2796 234035bc 2019-06-01 stsp void *cancel_arg;
2797 f69721c3 2019-10-21 stsp const char *label_orig;
2798 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2799 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2800 234035bc 2019-06-01 stsp };
2801 234035bc 2019-06-01 stsp
2802 234035bc 2019-06-01 stsp static const struct got_error *
2803 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2804 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2805 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2806 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2807 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 234035bc 2019-06-01 stsp {
2809 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2810 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2811 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2812 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2813 234035bc 2019-06-01 stsp struct stat sb;
2814 234035bc 2019-06-01 stsp unsigned char status;
2815 234035bc 2019-06-01 stsp int local_changes_subsumed;
2816 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2817 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2818 234035bc 2019-06-01 stsp
2819 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2820 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2821 d6c87207 2019-08-02 stsp strlen(path2));
2822 1ee397ad 2019-07-12 stsp if (ie == NULL)
2823 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2824 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2825 234035bc 2019-06-01 stsp
2826 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2827 234035bc 2019-06-01 stsp path2) == -1)
2828 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2829 234035bc 2019-06-01 stsp
2830 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2831 7f91a133 2019-12-13 stsp repo);
2832 234035bc 2019-06-01 stsp if (err)
2833 234035bc 2019-06-01 stsp goto done;
2834 234035bc 2019-06-01 stsp
2835 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2836 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2837 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2838 234035bc 2019-06-01 stsp goto done;
2839 234035bc 2019-06-01 stsp }
2840 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2841 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2842 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2843 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2844 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2845 234035bc 2019-06-01 stsp goto done;
2846 234035bc 2019-06-01 stsp }
2847 234035bc 2019-06-01 stsp
2848 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2849 36bf999c 2020-07-23 stsp char *link_target2;
2850 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2851 36bf999c 2020-07-23 stsp if (err)
2852 36bf999c 2020-07-23 stsp goto done;
2853 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2854 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2855 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2856 36bf999c 2020-07-23 stsp free(link_target2);
2857 af57b12a 2020-07-23 stsp } else {
2858 1af628f4 2021-06-03 stsp int fd;
2859 1af628f4 2021-06-03 stsp
2860 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2861 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2862 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2863 1af628f4 2021-06-03 stsp goto done;
2864 1af628f4 2021-06-03 stsp }
2865 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2866 1af628f4 2021-06-03 stsp f_orig, blob1);
2867 1af628f4 2021-06-03 stsp if (err)
2868 1af628f4 2021-06-03 stsp goto done;
2869 1af628f4 2021-06-03 stsp
2870 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2871 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2872 1af628f4 2021-06-03 stsp goto done;
2873 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2874 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2875 1af628f4 2021-06-03 stsp if (err)
2876 1af628f4 2021-06-03 stsp goto done;
2877 1af628f4 2021-06-03 stsp
2878 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2879 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2880 1af628f4 2021-06-03 stsp if (fd == -1) {
2881 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2882 1af628f4 2021-06-03 stsp ondisk_path);
2883 1af628f4 2021-06-03 stsp goto done;
2884 1af628f4 2021-06-03 stsp }
2885 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2886 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2887 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2888 1af628f4 2021-06-03 stsp ondisk_path);
2889 1af628f4 2021-06-03 stsp close(fd);
2890 1af628f4 2021-06-03 stsp goto done;
2891 1af628f4 2021-06-03 stsp }
2892 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2893 1af628f4 2021-06-03 stsp if (err)
2894 1af628f4 2021-06-03 stsp goto done;
2895 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2896 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2897 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2898 1af628f4 2021-06-03 stsp goto done;
2899 1af628f4 2021-06-03 stsp }
2900 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2901 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2902 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
2903 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2904 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2905 af57b12a 2020-07-23 stsp }
2906 234035bc 2019-06-01 stsp } else if (blob1) {
2907 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2908 d6c87207 2019-08-02 stsp strlen(path1));
2909 1ee397ad 2019-07-12 stsp if (ie == NULL)
2910 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2911 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2912 2b92fad7 2019-06-02 stsp
2913 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2914 2b92fad7 2019-06-02 stsp path1) == -1)
2915 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2916 2b92fad7 2019-06-02 stsp
2917 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2918 7f91a133 2019-12-13 stsp repo);
2919 2b92fad7 2019-06-02 stsp if (err)
2920 2b92fad7 2019-06-02 stsp goto done;
2921 2b92fad7 2019-06-02 stsp
2922 2b92fad7 2019-06-02 stsp switch (status) {
2923 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2924 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2925 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2926 1ee397ad 2019-07-12 stsp if (err)
2927 1ee397ad 2019-07-12 stsp goto done;
2928 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2929 2b92fad7 2019-06-02 stsp if (err)
2930 2b92fad7 2019-06-02 stsp goto done;
2931 2b92fad7 2019-06-02 stsp if (ie)
2932 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2933 2b92fad7 2019-06-02 stsp break;
2934 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2935 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2936 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2937 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2938 1ee397ad 2019-07-12 stsp if (err)
2939 1ee397ad 2019-07-12 stsp goto done;
2940 2b92fad7 2019-06-02 stsp if (ie)
2941 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2942 2b92fad7 2019-06-02 stsp break;
2943 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2944 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2945 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2946 72840534 2022-01-19 stsp off_t blob1_size;
2947 0a22ca1a 2020-09-23 stsp /*
2948 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2949 0a22ca1a 2020-09-23 stsp * exists in the repository.
2950 0a22ca1a 2020-09-23 stsp */
2951 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2952 72840534 2022-01-19 stsp &blob1_size, path1);
2953 0a22ca1a 2020-09-23 stsp if (err)
2954 0a22ca1a 2020-09-23 stsp goto done;
2955 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2956 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2957 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2958 0a22ca1a 2020-09-23 stsp if (err)
2959 0a22ca1a 2020-09-23 stsp goto done;
2960 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2961 0a22ca1a 2020-09-23 stsp path1);
2962 0a22ca1a 2020-09-23 stsp if (err)
2963 0a22ca1a 2020-09-23 stsp goto done;
2964 0a22ca1a 2020-09-23 stsp if (ie)
2965 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2966 0a22ca1a 2020-09-23 stsp ie);
2967 0a22ca1a 2020-09-23 stsp } else {
2968 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2969 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2970 0a22ca1a 2020-09-23 stsp }
2971 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2972 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2973 0a22ca1a 2020-09-23 stsp free(id);
2974 0a22ca1a 2020-09-23 stsp if (err)
2975 0a22ca1a 2020-09-23 stsp goto done;
2976 0a22ca1a 2020-09-23 stsp break;
2977 0a22ca1a 2020-09-23 stsp }
2978 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2979 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2980 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2981 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2982 1ee397ad 2019-07-12 stsp if (err)
2983 1ee397ad 2019-07-12 stsp goto done;
2984 2b92fad7 2019-06-02 stsp break;
2985 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2986 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2987 1ee397ad 2019-07-12 stsp if (err)
2988 1ee397ad 2019-07-12 stsp goto done;
2989 2b92fad7 2019-06-02 stsp break;
2990 2b92fad7 2019-06-02 stsp default:
2991 2b92fad7 2019-06-02 stsp break;
2992 2b92fad7 2019-06-02 stsp }
2993 234035bc 2019-06-01 stsp } else if (blob2) {
2994 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2995 234035bc 2019-06-01 stsp path2) == -1)
2996 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2997 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2998 d6c87207 2019-08-02 stsp strlen(path2));
2999 234035bc 2019-06-01 stsp if (ie) {
3000 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3001 7f91a133 2019-12-13 stsp -1, NULL, repo);
3002 234035bc 2019-06-01 stsp if (err)
3003 234035bc 2019-06-01 stsp goto done;
3004 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3005 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3006 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3007 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3008 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3009 1ee397ad 2019-07-12 stsp status, path2);
3010 234035bc 2019-06-01 stsp goto done;
3011 234035bc 2019-06-01 stsp }
3012 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3013 36bf999c 2020-07-23 stsp char *link_target2;
3014 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3015 36bf999c 2020-07-23 stsp blob2);
3016 36bf999c 2020-07-23 stsp if (err)
3017 36bf999c 2020-07-23 stsp goto done;
3018 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3019 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3020 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3021 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3022 36bf999c 2020-07-23 stsp free(link_target2);
3023 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3024 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3025 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3026 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3027 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3028 526a746f 2020-07-23 stsp a->progress_arg);
3029 dfe9fba0 2020-07-23 stsp } else {
3030 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3031 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3032 526a746f 2020-07-23 stsp }
3033 dfe9fba0 2020-07-23 stsp if (err)
3034 dfe9fba0 2020-07-23 stsp goto done;
3035 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3036 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3037 3093e2d7 2023-02-04 op a->worktree->root_fd, path2, blob2->id.hash,
3038 3093e2d7 2023-02-04 op a->worktree->base_commit_id->hash, 0);
3039 234035bc 2019-06-01 stsp if (err)
3040 234035bc 2019-06-01 stsp goto done;
3041 234035bc 2019-06-01 stsp }
3042 234035bc 2019-06-01 stsp } else {
3043 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3044 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3045 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3046 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3047 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3048 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3049 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3050 2e1fa222 2020-07-23 stsp } else {
3051 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3052 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3053 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3054 2e1fa222 2020-07-23 stsp }
3055 234035bc 2019-06-01 stsp if (err)
3056 234035bc 2019-06-01 stsp goto done;
3057 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3058 234035bc 2019-06-01 stsp if (err)
3059 234035bc 2019-06-01 stsp goto done;
3060 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3061 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3062 3969253a 2020-03-07 stsp if (err) {
3063 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3064 3969253a 2020-03-07 stsp goto done;
3065 3969253a 2020-03-07 stsp }
3066 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3067 2b92fad7 2019-06-02 stsp if (err) {
3068 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3069 2b92fad7 2019-06-02 stsp goto done;
3070 2b92fad7 2019-06-02 stsp }
3071 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3072 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3073 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3074 2e1fa222 2020-07-23 stsp }
3075 234035bc 2019-06-01 stsp }
3076 234035bc 2019-06-01 stsp }
3077 234035bc 2019-06-01 stsp done:
3078 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3079 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3080 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3081 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3082 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3083 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3084 1af628f4 2021-06-03 stsp free(id_str);
3085 54d5be07 2021-06-03 stsp free(label_deriv2);
3086 234035bc 2019-06-01 stsp free(ondisk_path);
3087 234035bc 2019-06-01 stsp return err;
3088 234035bc 2019-06-01 stsp }
3089 234035bc 2019-06-01 stsp
3090 69de9dd4 2021-09-03 stsp static const struct got_error *
3091 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 69de9dd4 2021-09-03 stsp {
3093 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3094 69de9dd4 2021-09-03 stsp
3095 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3096 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3097 3093e2d7 2023-02-04 op memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3098 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3099 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3100 69de9dd4 2021-09-03 stsp
3101 69de9dd4 2021-09-03 stsp return NULL;
3102 69de9dd4 2021-09-03 stsp }
3103 69de9dd4 2021-09-03 stsp
3104 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3105 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3106 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3107 234035bc 2019-06-01 stsp struct got_repository *repo;
3108 234035bc 2019-06-01 stsp };
3109 234035bc 2019-06-01 stsp
3110 234035bc 2019-06-01 stsp static const struct got_error *
3111 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3112 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3113 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3114 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3115 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 234035bc 2019-06-01 stsp {
3117 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3118 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3119 234035bc 2019-06-01 stsp unsigned char status;
3120 234035bc 2019-06-01 stsp struct stat sb;
3121 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3122 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3123 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3124 234035bc 2019-06-01 stsp char *ondisk_path;
3125 234035bc 2019-06-01 stsp
3126 69de9dd4 2021-09-03 stsp if (id == NULL)
3127 69de9dd4 2021-09-03 stsp return NULL;
3128 234035bc 2019-06-01 stsp
3129 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3130 69de9dd4 2021-09-03 stsp if (ie == NULL)
3131 69de9dd4 2021-09-03 stsp return NULL;
3132 69de9dd4 2021-09-03 stsp
3133 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3134 234035bc 2019-06-01 stsp == -1)
3135 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3136 234035bc 2019-06-01 stsp
3137 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3138 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3139 5546d466 2021-09-02 stsp free(ondisk_path);
3140 234035bc 2019-06-01 stsp if (err)
3141 234035bc 2019-06-01 stsp return err;
3142 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3143 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3144 234035bc 2019-06-01 stsp
3145 234035bc 2019-06-01 stsp return NULL;
3146 234035bc 2019-06-01 stsp }
3147 234035bc 2019-06-01 stsp
3148 818c7501 2019-07-11 stsp static const struct got_error *
3149 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3150 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3151 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3152 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3153 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3154 234035bc 2019-06-01 stsp {
3155 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3156 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3157 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3158 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3159 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3160 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3161 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3162 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3163 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3164 234035bc 2019-06-01 stsp
3165 03415a1a 2019-06-02 stsp if (commit_id1) {
3166 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3167 a44927cc 2022-04-07 stsp if (err)
3168 a44927cc 2022-04-07 stsp goto done;
3169 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3170 03415a1a 2019-06-02 stsp worktree->path_prefix);
3171 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3172 03415a1a 2019-06-02 stsp goto done;
3173 69d57f3d 2020-07-31 stsp }
3174 69d57f3d 2020-07-31 stsp if (tree_id1) {
3175 69d57f3d 2020-07-31 stsp char *id_str;
3176 03415a1a 2019-06-02 stsp
3177 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3178 03415a1a 2019-06-02 stsp if (err)
3179 03415a1a 2019-06-02 stsp goto done;
3180 f69721c3 2019-10-21 stsp
3181 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3182 f69721c3 2019-10-21 stsp if (err)
3183 f69721c3 2019-10-21 stsp goto done;
3184 f69721c3 2019-10-21 stsp
3185 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3186 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3187 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3188 f69721c3 2019-10-21 stsp free(id_str);
3189 f69721c3 2019-10-21 stsp goto done;
3190 f69721c3 2019-10-21 stsp }
3191 f69721c3 2019-10-21 stsp free(id_str);
3192 b72706c3 2022-06-01 stsp
3193 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3194 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3195 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3196 b72706c3 2022-06-01 stsp goto done;
3197 b72706c3 2022-06-01 stsp }
3198 03415a1a 2019-06-02 stsp }
3199 234035bc 2019-06-01 stsp
3200 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3201 a44927cc 2022-04-07 stsp if (err)
3202 a44927cc 2022-04-07 stsp goto done;
3203 a44927cc 2022-04-07 stsp
3204 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3205 234035bc 2019-06-01 stsp worktree->path_prefix);
3206 234035bc 2019-06-01 stsp if (err)
3207 234035bc 2019-06-01 stsp goto done;
3208 234035bc 2019-06-01 stsp
3209 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3210 234035bc 2019-06-01 stsp if (err)
3211 234035bc 2019-06-01 stsp goto done;
3212 234035bc 2019-06-01 stsp
3213 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3214 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3215 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3216 f9d37699 2022-06-28 stsp goto done;
3217 f9d37699 2022-06-28 stsp }
3218 f9d37699 2022-06-28 stsp
3219 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3220 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3221 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3222 b72706c3 2022-06-01 stsp goto done;
3223 b72706c3 2022-06-01 stsp }
3224 b72706c3 2022-06-01 stsp
3225 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3226 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3227 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3228 f9d37699 2022-06-28 stsp goto done;
3229 f9d37699 2022-06-28 stsp }
3230 f9d37699 2022-06-28 stsp
3231 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3232 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3233 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3234 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3235 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3236 69de9dd4 2021-09-03 stsp if (err)
3237 69de9dd4 2021-09-03 stsp goto done;
3238 69de9dd4 2021-09-03 stsp
3239 234035bc 2019-06-01 stsp arg.worktree = worktree;
3240 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3241 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3242 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3243 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3244 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3245 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3246 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3247 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3248 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3249 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3250 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3251 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3252 af12c6b9 2019-06-04 stsp err = sync_err;
3253 234035bc 2019-06-01 stsp done:
3254 a44927cc 2022-04-07 stsp if (commit1)
3255 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3256 a44927cc 2022-04-07 stsp if (commit2)
3257 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3258 234035bc 2019-06-01 stsp if (tree1)
3259 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3260 234035bc 2019-06-01 stsp if (tree2)
3261 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3262 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3263 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3264 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3265 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3266 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3267 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3268 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3269 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3270 f69721c3 2019-10-21 stsp free(label_orig);
3271 818c7501 2019-07-11 stsp return err;
3272 818c7501 2019-07-11 stsp }
3273 234035bc 2019-06-01 stsp
3274 818c7501 2019-07-11 stsp const struct got_error *
3275 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3276 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3277 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3278 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 818c7501 2019-07-11 stsp {
3280 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3281 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3282 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3283 818c7501 2019-07-11 stsp
3284 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3285 818c7501 2019-07-11 stsp if (err)
3286 818c7501 2019-07-11 stsp return err;
3287 818c7501 2019-07-11 stsp
3288 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3289 818c7501 2019-07-11 stsp if (err)
3290 818c7501 2019-07-11 stsp goto done;
3291 818c7501 2019-07-11 stsp
3292 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3293 69de9dd4 2021-09-03 stsp worktree);
3294 818c7501 2019-07-11 stsp if (err)
3295 818c7501 2019-07-11 stsp goto done;
3296 818c7501 2019-07-11 stsp
3297 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3298 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3299 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3300 818c7501 2019-07-11 stsp done:
3301 818c7501 2019-07-11 stsp if (fileindex)
3302 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3303 818c7501 2019-07-11 stsp free(fileindex_path);
3304 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3305 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3306 234035bc 2019-06-01 stsp err = unlockerr;
3307 234035bc 2019-06-01 stsp return err;
3308 234035bc 2019-06-01 stsp }
3309 234035bc 2019-06-01 stsp
3310 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3311 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3312 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3313 927df6b7 2019-02-10 stsp const char *status_path;
3314 927df6b7 2019-02-10 stsp size_t status_path_len;
3315 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3316 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3317 f8d1f275 2019-02-04 stsp void *status_arg;
3318 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3319 f8d1f275 2019-02-04 stsp void *cancel_arg;
3320 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3321 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3322 f2a9dc41 2019-12-13 tracey int report_unchanged;
3323 3143d852 2020-06-25 stsp int no_ignores;
3324 f8d1f275 2019-02-04 stsp };
3325 88d0e355 2019-08-03 stsp
3326 f8d1f275 2019-02-04 stsp static const struct got_error *
3327 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3328 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3329 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3330 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3331 927df6b7 2019-02-10 stsp {
3332 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3333 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3334 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3335 927df6b7 2019-02-10 stsp struct stat sb;
3336 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3337 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3338 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3339 927df6b7 2019-02-10 stsp
3340 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3341 98eaaa12 2019-08-03 stsp if (err)
3342 98eaaa12 2019-08-03 stsp return err;
3343 98eaaa12 2019-08-03 stsp
3344 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3345 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3346 98eaaa12 2019-08-03 stsp return NULL;
3347 98eaaa12 2019-08-03 stsp
3348 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3349 3093e2d7 2023-02-04 op memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3350 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3351 98eaaa12 2019-08-03 stsp }
3352 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3353 3093e2d7 2023-02-04 op memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3354 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3355 927df6b7 2019-02-10 stsp }
3356 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3357 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3358 3093e2d7 2023-02-04 op memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3359 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3360 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3361 98eaaa12 2019-08-03 stsp }
3362 98eaaa12 2019-08-03 stsp
3363 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3364 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3365 927df6b7 2019-02-10 stsp }
3366 927df6b7 2019-02-10 stsp
3367 927df6b7 2019-02-10 stsp static const struct got_error *
3368 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3369 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3370 f8d1f275 2019-02-04 stsp {
3371 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3372 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3373 f8d1f275 2019-02-04 stsp char *abspath;
3374 f8d1f275 2019-02-04 stsp
3375 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3376 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3377 0584f854 2019-04-06 stsp
3378 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3379 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3380 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3381 927df6b7 2019-02-10 stsp return NULL;
3382 927df6b7 2019-02-10 stsp
3383 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3384 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3385 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3386 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3387 f8d1f275 2019-02-04 stsp } else {
3388 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3389 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3391 f8d1f275 2019-02-04 stsp }
3392 f8d1f275 2019-02-04 stsp
3393 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3394 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3395 f8d1f275 2019-02-04 stsp free(abspath);
3396 9d31a1d8 2018-03-11 stsp return err;
3397 9d31a1d8 2018-03-11 stsp }
3398 f8d1f275 2019-02-04 stsp
3399 f8d1f275 2019-02-04 stsp static const struct got_error *
3400 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3401 f8d1f275 2019-02-04 stsp {
3402 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3403 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3404 2ec1f75b 2019-03-26 stsp unsigned char status;
3405 927df6b7 2019-02-10 stsp
3406 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3407 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3408 0584f854 2019-04-06 stsp
3409 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3410 927df6b7 2019-02-10 stsp return NULL;
3411 927df6b7 2019-02-10 stsp
3412 3093e2d7 2023-02-04 op memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3413 3093e2d7 2023-02-04 op memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3414 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3415 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3416 2ec1f75b 2019-03-26 stsp else
3417 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3418 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3419 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3420 6841da00 2019-08-08 stsp }
3421 6841da00 2019-08-08 stsp
3422 336075a4 2022-06-25 op static void
3423 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3424 6841da00 2019-08-08 stsp {
3425 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3426 6841da00 2019-08-08 stsp
3427 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3428 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3429 d8bacb93 2023-01-10 mark
3430 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3431 6841da00 2019-08-08 stsp }
3432 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3433 6841da00 2019-08-08 stsp }
3434 6841da00 2019-08-08 stsp
3435 6841da00 2019-08-08 stsp static const struct got_error *
3436 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3437 6841da00 2019-08-08 stsp {
3438 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3439 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3440 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3441 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3442 6841da00 2019-08-08 stsp size_t linesize = 0;
3443 6841da00 2019-08-08 stsp ssize_t linelen;
3444 6841da00 2019-08-08 stsp
3445 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3446 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3447 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3448 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3449 6841da00 2019-08-08 stsp
3450 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3452 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3453 bd8de430 2019-10-04 stsp
3454 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3455 bd8de430 2019-10-04 stsp if (line[0] == '#')
3456 bd8de430 2019-10-04 stsp continue;
3457 bd8de430 2019-10-04 stsp
3458 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3459 bd8de430 2019-10-04 stsp if (line[0] == '!')
3460 bd8de430 2019-10-04 stsp continue;
3461 bd8de430 2019-10-04 stsp
3462 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3463 6841da00 2019-08-08 stsp line) == -1) {
3464 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3465 6841da00 2019-08-08 stsp goto done;
3466 6841da00 2019-08-08 stsp }
3467 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3468 6841da00 2019-08-08 stsp if (err)
3469 6841da00 2019-08-08 stsp goto done;
3470 6841da00 2019-08-08 stsp }
3471 6841da00 2019-08-08 stsp if (ferror(f)) {
3472 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3473 6841da00 2019-08-08 stsp goto done;
3474 6841da00 2019-08-08 stsp }
3475 6841da00 2019-08-08 stsp
3476 6841da00 2019-08-08 stsp dirpath = strdup(path);
3477 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3478 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3479 6841da00 2019-08-08 stsp goto done;
3480 6841da00 2019-08-08 stsp }
3481 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3482 6841da00 2019-08-08 stsp done:
3483 6841da00 2019-08-08 stsp free(line);
3484 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3485 6841da00 2019-08-08 stsp free(dirpath);
3486 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3487 6841da00 2019-08-08 stsp }
3488 6841da00 2019-08-08 stsp return err;
3489 6841da00 2019-08-08 stsp }
3490 6841da00 2019-08-08 stsp
3491 336075a4 2022-06-25 op static int
3492 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3493 6841da00 2019-08-08 stsp {
3494 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3495 bd8de430 2019-10-04 stsp
3496 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3497 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3498 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3499 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3500 bd8de430 2019-10-04 stsp
3501 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3502 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3503 6841da00 2019-08-08 stsp
3504 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3505 bd8de430 2019-10-04 stsp continue;
3506 bd8de430 2019-10-04 stsp pattern += 3;
3507 bd8de430 2019-10-04 stsp p = path;
3508 bd8de430 2019-10-04 stsp while (*p) {
3509 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3510 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3512 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3513 bd8de430 2019-10-04 stsp p++;
3514 bd8de430 2019-10-04 stsp while (*p == '/')
3515 bd8de430 2019-10-04 stsp p++;
3516 bd8de430 2019-10-04 stsp continue;
3517 bd8de430 2019-10-04 stsp }
3518 bd8de430 2019-10-04 stsp return 1;
3519 bd8de430 2019-10-04 stsp }
3520 bd8de430 2019-10-04 stsp }
3521 bd8de430 2019-10-04 stsp }
3522 bd8de430 2019-10-04 stsp
3523 6841da00 2019-08-08 stsp /*
3524 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3525 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3526 6841da00 2019-08-08 stsp * ignores backwards.
3527 6841da00 2019-08-08 stsp */
3528 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3529 6841da00 2019-08-08 stsp while (pe) {
3530 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3532 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3533 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3534 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3535 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3536 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3537 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3538 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3539 6841da00 2019-08-08 stsp continue;
3540 6841da00 2019-08-08 stsp return 1;
3541 6841da00 2019-08-08 stsp }
3542 6841da00 2019-08-08 stsp }
3543 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3544 6841da00 2019-08-08 stsp }
3545 6841da00 2019-08-08 stsp
3546 6841da00 2019-08-08 stsp return 0;
3547 6841da00 2019-08-08 stsp }
3548 6841da00 2019-08-08 stsp
3549 6841da00 2019-08-08 stsp static const struct got_error *
3550 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3552 6841da00 2019-08-08 stsp {
3553 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3554 6841da00 2019-08-08 stsp char *ignorespath;
3555 886cec17 2019-12-15 stsp int fd = -1;
3556 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3557 6841da00 2019-08-08 stsp
3558 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3560 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3561 6841da00 2019-08-08 stsp
3562 886cec17 2019-12-15 stsp if (dirfd != -1) {
3563 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3564 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3565 886cec17 2019-12-15 stsp if (fd == -1) {
3566 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3567 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3568 886cec17 2019-12-15 stsp ignorespath);
3569 886cec17 2019-12-15 stsp } else {
3570 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3571 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3572 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3573 886cec17 2019-12-15 stsp ignorespath);
3574 886cec17 2019-12-15 stsp else {
3575 886cec17 2019-12-15 stsp fd = -1;
3576 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3577 886cec17 2019-12-15 stsp }
3578 886cec17 2019-12-15 stsp }
3579 886cec17 2019-12-15 stsp } else {
3580 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3581 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3582 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3583 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3584 886cec17 2019-12-15 stsp ignorespath);
3585 886cec17 2019-12-15 stsp } else
3586 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3587 886cec17 2019-12-15 stsp }
3588 6841da00 2019-08-08 stsp
3589 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3591 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3592 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3593 6841da00 2019-08-08 stsp free(ignorespath);
3594 6841da00 2019-08-08 stsp return err;
3595 f8d1f275 2019-02-04 stsp }
3596 f8d1f275 2019-02-04 stsp
3597 f8d1f275 2019-02-04 stsp static const struct got_error *
3598 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3599 62da3196 2021-10-01 stsp int dirfd)
3600 f8d1f275 2019-02-04 stsp {
3601 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3602 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3603 f8d1f275 2019-02-04 stsp char *path = NULL;
3604 62da3196 2021-10-01 stsp
3605 62da3196 2021-10-01 stsp if (ignore != NULL)
3606 62da3196 2021-10-01 stsp *ignore = 0;
3607 f8d1f275 2019-02-04 stsp
3608 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3610 0584f854 2019-04-06 stsp
3611 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3612 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3614 f8d1f275 2019-02-04 stsp } else {
3615 f8d1f275 2019-02-04 stsp path = de->d_name;
3616 f8d1f275 2019-02-04 stsp }
3617 f8d1f275 2019-02-04 stsp
3618 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3619 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3620 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3621 62da3196 2021-10-01 stsp *ignore = 1;
3622 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3623 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3624 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 f8d1f275 2019-02-04 stsp if (parent_path[0])
3627 f8d1f275 2019-02-04 stsp free(path);
3628 b72f483a 2019-02-05 stsp return err;
3629 f8d1f275 2019-02-04 stsp }
3630 f8d1f275 2019-02-04 stsp
3631 347d1d3e 2019-07-12 stsp static const struct got_error *
3632 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3633 3143d852 2020-06-25 stsp {
3634 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3635 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3636 3143d852 2020-06-25 stsp
3637 3143d852 2020-06-25 stsp if (a->no_ignores)
3638 3143d852 2020-06-25 stsp return NULL;
3639 3143d852 2020-06-25 stsp
3640 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3641 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3642 3143d852 2020-06-25 stsp if (err)
3643 3143d852 2020-06-25 stsp return err;
3644 3143d852 2020-06-25 stsp
3645 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3647 3143d852 2020-06-25 stsp
3648 3143d852 2020-06-25 stsp return err;
3649 3143d852 2020-06-25 stsp }
3650 3143d852 2020-06-25 stsp
3651 3143d852 2020-06-25 stsp static const struct got_error *
3652 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3653 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3655 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3656 abb4604f 2019-07-27 stsp {
3657 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3658 abb4604f 2019-07-27 stsp struct stat sb;
3659 ff56836b 2021-07-08 stsp
3660 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3661 abb4604f 2019-07-27 stsp if (ie)
3662 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3663 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3664 abb4604f 2019-07-27 stsp
3665 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3666 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3667 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3668 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3670 abb4604f 2019-07-27 stsp }
3671 abb4604f 2019-07-27 stsp
3672 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3673 916237f3 2022-02-11 stsp return NULL;
3674 916237f3 2022-02-11 stsp
3675 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3678 abb4604f 2019-07-27 stsp
3679 abb4604f 2019-07-27 stsp return NULL;
3680 3143d852 2020-06-25 stsp }
3681 3143d852 2020-06-25 stsp
3682 3143d852 2020-06-25 stsp static const struct got_error *
3683 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3685 3143d852 2020-06-25 stsp {
3686 3143d852 2020-06-25 stsp const struct got_error *err;
3687 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3688 3143d852 2020-06-25 stsp
3689 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3690 3143d852 2020-06-25 stsp ".cvsignore");
3691 3143d852 2020-06-25 stsp if (err)
3692 3143d852 2020-06-25 stsp return err;
3693 3143d852 2020-06-25 stsp
3694 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3695 3143d852 2020-06-25 stsp ".gitignore");
3696 3143d852 2020-06-25 stsp if (err)
3697 3143d852 2020-06-25 stsp return err;
3698 3143d852 2020-06-25 stsp
3699 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3700 3143d852 2020-06-25 stsp if (err) {
3701 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3702 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3703 3143d852 2020-06-25 stsp return err;
3704 3143d852 2020-06-25 stsp }
3705 3143d852 2020-06-25 stsp for (;;) {
3706 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3707 3143d852 2020-06-25 stsp ".cvsignore");
3708 3143d852 2020-06-25 stsp if (err)
3709 3143d852 2020-06-25 stsp break;
3710 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3711 3143d852 2020-06-25 stsp ".gitignore");
3712 3143d852 2020-06-25 stsp if (err)
3713 3143d852 2020-06-25 stsp break;
3714 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3715 3143d852 2020-06-25 stsp if (err) {
3716 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3717 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3718 3143d852 2020-06-25 stsp break;
3719 3143d852 2020-06-25 stsp }
3720 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3721 ff56836b 2021-07-08 stsp break;
3722 b737c85a 2020-06-26 stsp free(parent_path);
3723 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3724 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3725 3143d852 2020-06-25 stsp }
3726 3143d852 2020-06-25 stsp
3727 b737c85a 2020-06-26 stsp free(parent_path);
3728 b737c85a 2020-06-26 stsp free(next_parent_path);
3729 3143d852 2020-06-25 stsp return err;
3730 abb4604f 2019-07-27 stsp }
3731 abb4604f 2019-07-27 stsp
3732 abb4604f 2019-07-27 stsp static const struct got_error *
3733 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3734 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3735 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3736 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 f2a9dc41 2019-12-13 tracey int report_unchanged)
3738 f8d1f275 2019-02-04 stsp {
3739 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3740 6fc93f37 2019-12-13 stsp int fd = -1;
3741 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3742 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3743 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3744 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3745 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3746 3143d852 2020-06-25 stsp
3747 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3748 f8d1f275 2019-02-04 stsp
3749 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3750 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3752 8dc303cc 2019-07-27 stsp
3753 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3754 a84c0d30 2022-03-12 stsp if (ie) {
3755 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3756 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3757 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3758 a84c0d30 2022-03-12 stsp goto done;
3759 a84c0d30 2022-03-12 stsp }
3760 a84c0d30 2022-03-12 stsp
3761 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3762 6fc93f37 2019-12-13 stsp if (fd == -1) {
3763 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3765 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3766 ff56836b 2021-07-08 stsp else {
3767 ff56836b 2021-07-08 stsp if (!no_ignores) {
3768 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3769 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3770 ff56836b 2021-07-08 stsp if (err)
3771 ff56836b 2021-07-08 stsp goto done;
3772 ff56836b 2021-07-08 stsp }
3773 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3774 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3775 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3776 ff56836b 2021-07-08 stsp }
3777 abb4604f 2019-07-27 stsp } else {
3778 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3779 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3780 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3781 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3782 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3783 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3784 abb4604f 2019-07-27 stsp arg.status_path = path;
3785 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3786 abb4604f 2019-07-27 stsp arg.repo = repo;
3787 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3788 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3789 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3790 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3791 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3792 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3793 022fae89 2019-12-06 tracey if (!no_ignores) {
3794 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3795 3143d852 2020-06-25 stsp worktree->root_path, path);
3796 3143d852 2020-06-25 stsp if (err)
3797 3143d852 2020-06-25 stsp goto done;
3798 022fae89 2019-12-06 tracey }
3799 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3800 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3801 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3802 927df6b7 2019-02-10 stsp }
3803 3143d852 2020-06-25 stsp done:
3804 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3805 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3806 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3807 927df6b7 2019-02-10 stsp free(ondisk_path);
3808 347d1d3e 2019-07-12 stsp return err;
3809 347d1d3e 2019-07-12 stsp }
3810 347d1d3e 2019-07-12 stsp
3811 347d1d3e 2019-07-12 stsp const struct got_error *
3812 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3813 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3814 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3816 347d1d3e 2019-07-12 stsp {
3817 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3818 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3819 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3820 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3821 347d1d3e 2019-07-12 stsp
3822 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3823 347d1d3e 2019-07-12 stsp if (err)
3824 347d1d3e 2019-07-12 stsp return err;
3825 347d1d3e 2019-07-12 stsp
3826 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3827 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3828 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3829 f6343036 2021-06-22 stsp no_ignores, 0);
3830 72ea6654 2019-07-27 stsp if (err)
3831 72ea6654 2019-07-27 stsp break;
3832 72ea6654 2019-07-27 stsp }
3833 f8d1f275 2019-02-04 stsp free(fileindex_path);
3834 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3835 f8d1f275 2019-02-04 stsp return err;
3836 f8d1f275 2019-02-04 stsp }
3837 6c7ab921 2019-03-18 stsp
3838 6c7ab921 2019-03-18 stsp const struct got_error *
3839 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 6c7ab921 2019-03-18 stsp const char *arg)
3841 6c7ab921 2019-03-18 stsp {
3842 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3843 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 6c7ab921 2019-03-18 stsp size_t len;
3845 00bb5ea0 2020-07-23 stsp struct stat sb;
3846 01740607 2020-11-04 stsp char *abspath = NULL;
3847 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3848 6c7ab921 2019-03-18 stsp
3849 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3850 6c7ab921 2019-03-18 stsp
3851 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3852 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3853 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3854 00bb5ea0 2020-07-23 stsp
3855 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3856 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3857 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3858 00bb5ea0 2020-07-23 stsp goto done;
3859 00bb5ea0 2020-07-23 stsp }
3860 727173c3 2020-11-06 stsp sb.st_mode = 0;
3861 00bb5ea0 2020-07-23 stsp }
3862 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3863 00bb5ea0 2020-07-23 stsp /*
3864 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3865 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3866 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3867 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3868 00bb5ea0 2020-07-23 stsp */
3869 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3870 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3872 00bb5ea0 2020-07-23 stsp goto done;
3873 00bb5ea0 2020-07-23 stsp }
3874 00bb5ea0 2020-07-23 stsp
3875 00bb5ea0 2020-07-23 stsp }
3876 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3877 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3878 00bb5ea0 2020-07-23 stsp if (err)
3879 d0710d08 2019-07-22 stsp goto done;
3880 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3881 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3882 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3883 00bb5ea0 2020-07-23 stsp goto done;
3884 00bb5ea0 2020-07-23 stsp }
3885 00bb5ea0 2020-07-23 stsp } else {
3886 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3887 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3888 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3889 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3890 00bb5ea0 2020-07-23 stsp goto done;
3891 00bb5ea0 2020-07-23 stsp }
3892 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3894 01740607 2020-11-04 stsp goto done;
3895 01740607 2020-11-04 stsp }
3896 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3897 01740607 2020-11-04 stsp sizeof(canonpath));
3898 01740607 2020-11-04 stsp if (err)
3899 00bb5ea0 2020-07-23 stsp goto done;
3900 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3901 01740607 2020-11-04 stsp if (resolved == NULL) {
3902 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3903 01740607 2020-11-04 stsp goto done;
3904 00bb5ea0 2020-07-23 stsp }
3905 d0710d08 2019-07-22 stsp }
3906 d0710d08 2019-07-22 stsp }
3907 6c7ab921 2019-03-18 stsp
3908 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3910 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3911 6c7ab921 2019-03-18 stsp goto done;
3912 6c7ab921 2019-03-18 stsp }
3913 6c7ab921 2019-03-18 stsp
3914 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3916 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3917 f6d88e1a 2019-05-29 stsp if (err)
3918 f6d88e1a 2019-05-29 stsp goto done;
3919 f6d88e1a 2019-05-29 stsp } else {
3920 f6d88e1a 2019-05-29 stsp path = strdup("");
3921 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3922 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3923 f6d88e1a 2019-05-29 stsp goto done;
3924 f6d88e1a 2019-05-29 stsp }
3925 6c7ab921 2019-03-18 stsp }
3926 6c7ab921 2019-03-18 stsp
3927 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3928 6c7ab921 2019-03-18 stsp len = strlen(path);
3929 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3930 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3931 6c7ab921 2019-03-18 stsp len--;
3932 6c7ab921 2019-03-18 stsp }
3933 6c7ab921 2019-03-18 stsp done:
3934 01740607 2020-11-04 stsp free(abspath);
3935 6c7ab921 2019-03-18 stsp free(resolved);
3936 d0710d08 2019-07-22 stsp free(cwd);
3937 6c7ab921 2019-03-18 stsp if (err == NULL)
3938 6c7ab921 2019-03-18 stsp *wt_path = path;
3939 6c7ab921 2019-03-18 stsp else
3940 6c7ab921 2019-03-18 stsp free(path);
3941 d00136be 2019-03-26 stsp return err;
3942 d00136be 2019-03-26 stsp }
3943 d00136be 2019-03-26 stsp
3944 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3945 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3946 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3947 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3948 4e68cba3 2019-11-23 stsp void *progress_arg;
3949 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3950 4e68cba3 2019-11-23 stsp };
3951 4e68cba3 2019-11-23 stsp
3952 4e68cba3 2019-11-23 stsp static const struct got_error *
3953 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3955 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3957 07f5b47a 2019-06-02 stsp {
3958 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3959 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3960 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3961 6d022e97 2019-08-04 stsp struct stat sb;
3962 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3963 07f5b47a 2019-06-02 stsp
3964 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 dbb83fbd 2019-12-12 stsp relpath) == -1)
3966 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3967 dbb83fbd 2019-12-12 stsp
3968 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 6d022e97 2019-08-04 stsp if (ie) {
3970 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3971 12463d8b 2019-12-13 stsp de_name, a->repo);
3972 6d022e97 2019-08-04 stsp if (err)
3973 dbb83fbd 2019-12-12 stsp goto done;
3974 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3975 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3976 dbb83fbd 2019-12-12 stsp goto done;
3977 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3978 dbb83fbd 2019-12-12 stsp if (err)
3979 dbb83fbd 2019-12-12 stsp goto done;
3980 6d022e97 2019-08-04 stsp }
3981 07f5b47a 2019-06-02 stsp
3982 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3983 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3984 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3985 9b4603c0 2022-01-31 stsp else
3986 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3987 dbb83fbd 2019-12-12 stsp goto done;
3988 4e68cba3 2019-11-23 stsp }
3989 07f5b47a 2019-06-02 stsp
3990 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3991 4e68cba3 2019-11-23 stsp if (err)
3992 4e68cba3 2019-11-23 stsp goto done;
3993 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3995 3969253a 2020-03-07 stsp if (err) {
3996 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3997 3969253a 2020-03-07 stsp goto done;
3998 3969253a 2020-03-07 stsp }
3999 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4000 07f5b47a 2019-06-02 stsp if (err) {
4001 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4002 4e68cba3 2019-11-23 stsp goto done;
4003 07f5b47a 2019-06-02 stsp }
4004 4e68cba3 2019-11-23 stsp done:
4005 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4006 dbb83fbd 2019-12-12 stsp if (err)
4007 dbb83fbd 2019-12-12 stsp return err;
4008 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4009 dbb83fbd 2019-12-12 stsp return NULL;
4010 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4011 07f5b47a 2019-06-02 stsp }
4012 07f5b47a 2019-06-02 stsp
4013 d00136be 2019-03-26 stsp const struct got_error *
4014 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4015 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4016 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4018 d00136be 2019-03-26 stsp {
4019 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4020 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4021 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4023 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4024 d00136be 2019-03-26 stsp
4025 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4026 d00136be 2019-03-26 stsp if (err)
4027 d00136be 2019-03-26 stsp return err;
4028 d00136be 2019-03-26 stsp
4029 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4030 d00136be 2019-03-26 stsp if (err)
4031 d00136be 2019-03-26 stsp goto done;
4032 d00136be 2019-03-26 stsp
4033 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4034 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4035 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4036 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4037 4e68cba3 2019-11-23 stsp saa.repo = repo;
4038 4e68cba3 2019-11-23 stsp
4039 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4040 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4041 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4042 1dd54920 2019-05-11 stsp if (err)
4043 af12c6b9 2019-06-04 stsp break;
4044 1dd54920 2019-05-11 stsp }
4045 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4046 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4047 af12c6b9 2019-06-04 stsp err = sync_err;
4048 d00136be 2019-03-26 stsp done:
4049 fb399478 2019-07-12 stsp free(fileindex_path);
4050 d00136be 2019-03-26 stsp if (fileindex)
4051 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4052 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4053 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4054 d00136be 2019-03-26 stsp err = unlockerr;
4055 6c7ab921 2019-03-18 stsp return err;
4056 6c7ab921 2019-03-18 stsp }
4057 17ed4618 2019-06-02 stsp
4058 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4059 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4060 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4061 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4062 f2a9dc41 2019-12-13 tracey void *progress_arg;
4063 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4064 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4065 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4066 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4067 766841c2 2020-08-13 stsp const char *status_codes;
4068 f2a9dc41 2019-12-13 tracey };
4069 f2a9dc41 2019-12-13 tracey
4070 f2a9dc41 2019-12-13 tracey static const struct got_error *
4071 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4072 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4073 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4075 17ed4618 2019-06-02 stsp {
4076 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4077 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4078 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4079 17ed4618 2019-06-02 stsp struct stat sb;
4080 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4081 4e12cd97 2022-01-25 stsp
4082 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4083 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4084 4e12cd97 2022-01-25 stsp return NULL;
4085 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4086 4e12cd97 2022-01-25 stsp }
4087 17ed4618 2019-06-02 stsp
4088 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 17ed4618 2019-06-02 stsp if (ie == NULL)
4090 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4091 2ec1f75b 2019-03-26 stsp
4092 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4093 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4095 9acbc4fa 2019-08-03 stsp return NULL;
4096 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4097 9acbc4fa 2019-08-03 stsp }
4098 9acbc4fa 2019-08-03 stsp
4099 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4100 f2a9dc41 2019-12-13 tracey relpath) == -1)
4101 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4102 f2a9dc41 2019-12-13 tracey
4103 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4104 12463d8b 2019-12-13 stsp a->repo);
4105 17ed4618 2019-06-02 stsp if (err)
4106 f2a9dc41 2019-12-13 tracey goto done;
4107 17ed4618 2019-06-02 stsp
4108 766841c2 2020-08-13 stsp if (a->status_codes) {
4109 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4110 766841c2 2020-08-13 stsp int i;
4111 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4112 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4113 766841c2 2020-08-13 stsp break;
4114 766841c2 2020-08-13 stsp }
4115 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4116 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4117 766841c2 2020-08-13 stsp free(ondisk_path);
4118 766841c2 2020-08-13 stsp return NULL;
4119 766841c2 2020-08-13 stsp }
4120 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4122 766841c2 2020-08-13 stsp static char msg[64];
4123 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4124 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4125 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4126 766841c2 2020-08-13 stsp goto done;
4127 766841c2 2020-08-13 stsp }
4128 766841c2 2020-08-13 stsp }
4129 766841c2 2020-08-13 stsp
4130 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4131 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4132 f2a9dc41 2019-12-13 tracey goto done;
4133 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4135 f2a9dc41 2019-12-13 tracey goto done;
4136 f2a9dc41 2019-12-13 tracey }
4137 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4139 4e12cd97 2022-01-25 stsp goto done;
4140 4e12cd97 2022-01-25 stsp }
4141 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4142 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4143 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 f2a9dc41 2019-12-13 tracey goto done;
4145 f2a9dc41 2019-12-13 tracey }
4146 17ed4618 2019-06-02 stsp }
4147 17ed4618 2019-06-02 stsp
4148 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 20a2ad1c 2020-10-20 stsp size_t root_len;
4150 20a2ad1c 2020-10-20 stsp
4151 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4152 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4153 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4154 12463d8b 2019-12-13 stsp ondisk_path);
4155 12463d8b 2019-12-13 stsp goto done;
4156 12463d8b 2019-12-13 stsp }
4157 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4158 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4159 12463d8b 2019-12-13 stsp goto done;
4160 15341bfd 2020-03-05 tracey }
4161 15341bfd 2020-03-05 tracey
4162 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4163 20a2ad1c 2020-10-20 stsp do {
4164 20a2ad1c 2020-10-20 stsp char *parent;
4165 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4166 20a2ad1c 2020-10-20 stsp if (err)
4167 2513f20a 2020-10-20 stsp goto done;
4168 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4169 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4170 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4171 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4172 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4173 20a2ad1c 2020-10-20 stsp ondisk_path);
4174 15341bfd 2020-03-05 tracey break;
4175 15341bfd 2020-03-05 tracey }
4176 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4178 f2a9dc41 2019-12-13 tracey }
4179 17ed4618 2019-06-02 stsp
4180 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4181 f2a9dc41 2019-12-13 tracey done:
4182 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4183 f2a9dc41 2019-12-13 tracey if (err)
4184 f2a9dc41 2019-12-13 tracey return err;
4185 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4186 f2a9dc41 2019-12-13 tracey return NULL;
4187 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4189 17ed4618 2019-06-02 stsp }
4190 17ed4618 2019-06-02 stsp
4191 2ec1f75b 2019-03-26 stsp const struct got_error *
4192 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4193 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4194 766841c2 2020-08-13 stsp const char *status_codes,
4195 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4196 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4197 2ec1f75b 2019-03-26 stsp {
4198 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4199 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4200 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4202 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4203 2ec1f75b 2019-03-26 stsp
4204 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4205 2ec1f75b 2019-03-26 stsp if (err)
4206 2ec1f75b 2019-03-26 stsp return err;
4207 2ec1f75b 2019-03-26 stsp
4208 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 2ec1f75b 2019-03-26 stsp if (err)
4210 2ec1f75b 2019-03-26 stsp goto done;
4211 f2a9dc41 2019-12-13 tracey
4212 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4213 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4214 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4215 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4216 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4217 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4218 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4219 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4220 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4221 2ec1f75b 2019-03-26 stsp
4222 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4223 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4224 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4225 17ed4618 2019-06-02 stsp if (err)
4226 af12c6b9 2019-06-04 stsp break;
4227 2ec1f75b 2019-03-26 stsp }
4228 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4229 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4230 af12c6b9 2019-06-04 stsp err = sync_err;
4231 2ec1f75b 2019-03-26 stsp done:
4232 fb399478 2019-07-12 stsp free(fileindex_path);
4233 2ec1f75b 2019-03-26 stsp if (fileindex)
4234 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4235 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4236 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4237 2ec1f75b 2019-03-26 stsp err = unlockerr;
4238 33aa809d 2019-08-08 stsp return err;
4239 33aa809d 2019-08-08 stsp }
4240 33aa809d 2019-08-08 stsp
4241 33aa809d 2019-08-08 stsp static const struct got_error *
4242 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4243 33aa809d 2019-08-08 stsp {
4244 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4245 33aa809d 2019-08-08 stsp char *line = NULL;
4246 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4247 33aa809d 2019-08-08 stsp ssize_t linelen;
4248 33aa809d 2019-08-08 stsp
4249 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4250 33aa809d 2019-08-08 stsp if (linelen == -1) {
4251 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4252 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4253 33aa809d 2019-08-08 stsp goto done;
4254 33aa809d 2019-08-08 stsp }
4255 33aa809d 2019-08-08 stsp return NULL;
4256 33aa809d 2019-08-08 stsp }
4257 33aa809d 2019-08-08 stsp if (outfile) {
4258 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4259 33aa809d 2019-08-08 stsp if (n != linelen) {
4260 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4261 33aa809d 2019-08-08 stsp goto done;
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp }
4264 33aa809d 2019-08-08 stsp if (rejectfile) {
4265 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4266 33aa809d 2019-08-08 stsp if (n != linelen)
4267 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4268 33aa809d 2019-08-08 stsp }
4269 33aa809d 2019-08-08 stsp done:
4270 33aa809d 2019-08-08 stsp free(line);
4271 2ec1f75b 2019-03-26 stsp return err;
4272 2ec1f75b 2019-03-26 stsp }
4273 1f1abb7e 2019-08-08 stsp
4274 33aa809d 2019-08-08 stsp static const struct got_error *
4275 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4276 33aa809d 2019-08-08 stsp {
4277 33aa809d 2019-08-08 stsp char *line = NULL;
4278 33aa809d 2019-08-08 stsp size_t linesize = 0;
4279 33aa809d 2019-08-08 stsp ssize_t linelen;
4280 33aa809d 2019-08-08 stsp
4281 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4282 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4283 500467ff 2019-09-25 hiltjo if (ferror(f))
4284 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4285 500467ff 2019-09-25 hiltjo return NULL;
4286 500467ff 2019-09-25 hiltjo }
4287 33aa809d 2019-08-08 stsp free(line);
4288 33aa809d 2019-08-08 stsp return NULL;
4289 33aa809d 2019-08-08 stsp }
4290 33aa809d 2019-08-08 stsp
4291 33aa809d 2019-08-08 stsp static const struct got_error *
4292 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4294 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4295 abc59930 2021-09-05 naddy {
4296 33aa809d 2019-08-08 stsp const struct got_error *err;
4297 33aa809d 2019-08-08 stsp
4298 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4299 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4300 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4301 33aa809d 2019-08-08 stsp if (err)
4302 33aa809d 2019-08-08 stsp return err;
4303 33aa809d 2019-08-08 stsp (*line_cur1)++;
4304 33aa809d 2019-08-08 stsp }
4305 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4306 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4307 33aa809d 2019-08-08 stsp if (rejectfile)
4308 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4309 33aa809d 2019-08-08 stsp else
4310 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4311 33aa809d 2019-08-08 stsp if (err)
4312 33aa809d 2019-08-08 stsp return err;
4313 33aa809d 2019-08-08 stsp (*line_cur2)++;
4314 33aa809d 2019-08-08 stsp }
4315 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4316 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4317 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4318 33aa809d 2019-08-08 stsp if (err)
4319 33aa809d 2019-08-08 stsp return err;
4320 33aa809d 2019-08-08 stsp (*line_cur2)++;
4321 33aa809d 2019-08-08 stsp }
4322 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4323 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4324 33aa809d 2019-08-08 stsp if (rejectfile)
4325 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4326 33aa809d 2019-08-08 stsp else
4327 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4328 33aa809d 2019-08-08 stsp if (err)
4329 33aa809d 2019-08-08 stsp return err;
4330 33aa809d 2019-08-08 stsp (*line_cur1)++;
4331 33aa809d 2019-08-08 stsp }
4332 f1e81a05 2019-08-10 stsp
4333 f1e81a05 2019-08-10 stsp return NULL;
4334 f1e81a05 2019-08-10 stsp }
4335 f1e81a05 2019-08-10 stsp
4336 f1e81a05 2019-08-10 stsp static const struct got_error *
4337 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4339 f1e81a05 2019-08-10 stsp {
4340 f1e81a05 2019-08-10 stsp const struct got_error *err;
4341 f1e81a05 2019-08-10 stsp
4342 f1e81a05 2019-08-10 stsp if (outfile) {
4343 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4344 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4345 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4346 f1e81a05 2019-08-10 stsp if (err)
4347 f1e81a05 2019-08-10 stsp return err;
4348 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4349 f1e81a05 2019-08-10 stsp }
4350 f1e81a05 2019-08-10 stsp }
4351 f1e81a05 2019-08-10 stsp if (rejectfile) {
4352 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4353 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4354 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4355 f1e81a05 2019-08-10 stsp if (err)
4356 f1e81a05 2019-08-10 stsp return err;
4357 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4358 f1e81a05 2019-08-10 stsp }
4359 33aa809d 2019-08-08 stsp }
4360 33aa809d 2019-08-08 stsp
4361 33aa809d 2019-08-08 stsp return NULL;
4362 33aa809d 2019-08-08 stsp }
4363 33aa809d 2019-08-08 stsp
4364 33aa809d 2019-08-08 stsp static const struct got_error *
4365 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4366 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4367 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4370 33aa809d 2019-08-08 stsp {
4371 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4372 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4373 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4374 33aa809d 2019-08-08 stsp FILE *hunkfile;
4375 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4376 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4377 fe621944 2020-11-10 stsp int rc;
4378 33aa809d 2019-08-08 stsp
4379 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4380 33aa809d 2019-08-08 stsp
4381 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4382 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 fe621944 2020-11-10 stsp start_old = cc.left.start;
4384 fe621944 2020-11-10 stsp end_old = cc.left.end;
4385 fe621944 2020-11-10 stsp start_new = cc.right.start;
4386 fe621944 2020-11-10 stsp end_new = cc.right.end;
4387 33aa809d 2019-08-08 stsp
4388 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4389 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4390 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4391 33aa809d 2019-08-08 stsp
4392 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4393 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4394 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4395 33aa809d 2019-08-08 stsp
4396 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4397 fe621944 2020-11-10 stsp if (diff_state == NULL)
4398 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4399 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4400 fe621944 2020-11-10 stsp
4401 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4402 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4403 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4404 33aa809d 2019-08-08 stsp goto done;
4405 33aa809d 2019-08-08 stsp }
4406 fe621944 2020-11-10 stsp
4407 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 fe621944 2020-11-10 stsp diff_result, &cc);
4409 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4410 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4411 33aa809d 2019-08-08 stsp goto done;
4412 33aa809d 2019-08-08 stsp }
4413 fe621944 2020-11-10 stsp
4414 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4416 33aa809d 2019-08-08 stsp goto done;
4417 33aa809d 2019-08-08 stsp }
4418 33aa809d 2019-08-08 stsp
4419 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4421 33aa809d 2019-08-08 stsp if (err)
4422 33aa809d 2019-08-08 stsp goto done;
4423 33aa809d 2019-08-08 stsp
4424 33aa809d 2019-08-08 stsp switch (*choice) {
4425 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4426 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4428 33aa809d 2019-08-08 stsp break;
4429 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4430 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4432 33aa809d 2019-08-08 stsp break;
4433 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4434 33aa809d 2019-08-08 stsp break;
4435 33aa809d 2019-08-08 stsp default:
4436 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4437 33aa809d 2019-08-08 stsp break;
4438 33aa809d 2019-08-08 stsp }
4439 33aa809d 2019-08-08 stsp done:
4440 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4441 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4443 33aa809d 2019-08-08 stsp return err;
4444 33aa809d 2019-08-08 stsp }
4445 33aa809d 2019-08-08 stsp
4446 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4447 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4448 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4449 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4450 1f1abb7e 2019-08-08 stsp void *progress_arg;
4451 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4452 33aa809d 2019-08-08 stsp void *patch_arg;
4453 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4454 f259c4c1 2021-09-24 stsp int unlink_added_files;
4455 1f1abb7e 2019-08-08 stsp };
4456 a129376b 2019-03-28 stsp
4457 e20a8b6f 2019-06-04 stsp static const struct got_error *
4458 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4459 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4460 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4461 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4462 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4463 33aa809d 2019-08-08 stsp {
4464 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4465 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4466 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4468 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4469 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4470 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4471 fe621944 2020-11-10 stsp struct stat sb2;
4472 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4473 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4475 33aa809d 2019-08-08 stsp
4476 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4477 33aa809d 2019-08-08 stsp
4478 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4479 33aa809d 2019-08-08 stsp if (err)
4480 33aa809d 2019-08-08 stsp return err;
4481 33aa809d 2019-08-08 stsp
4482 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4483 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4484 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4486 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4487 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4488 fa3cef63 2020-07-23 stsp goto done;
4489 fa3cef63 2020-07-23 stsp }
4490 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4491 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4492 c0df5966 2021-12-31 stsp if (link_len == -1) {
4493 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4494 c0df5966 2021-12-31 stsp path2);
4495 c0df5966 2021-12-31 stsp }
4496 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4497 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4498 12463d8b 2019-12-13 stsp }
4499 12463d8b 2019-12-13 stsp } else {
4500 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4501 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4502 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4503 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4504 fa3cef63 2020-07-23 stsp goto done;
4505 fa3cef63 2020-07-23 stsp }
4506 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4507 fa3cef63 2020-07-23 stsp sizeof(link_target));
4508 fa3cef63 2020-07-23 stsp if (link_len == -1)
4509 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4510 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4511 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4512 12463d8b 2019-12-13 stsp }
4513 1ebedb77 2019-10-19 stsp }
4514 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4515 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4516 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4517 fa3cef63 2020-07-23 stsp goto done;
4518 fa3cef63 2020-07-23 stsp }
4519 1ebedb77 2019-10-19 stsp
4520 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4521 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4522 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4523 fa3cef63 2020-07-23 stsp goto done;
4524 fa3cef63 2020-07-23 stsp }
4525 fa3cef63 2020-07-23 stsp fd2 = -1;
4526 fa3cef63 2020-07-23 stsp } else {
4527 fa3cef63 2020-07-23 stsp size_t n;
4528 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4529 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4530 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4531 fa3cef63 2020-07-23 stsp goto done;
4532 fa3cef63 2020-07-23 stsp }
4533 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4534 fa3cef63 2020-07-23 stsp if (n != link_len) {
4535 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4536 fa3cef63 2020-07-23 stsp goto done;
4537 fa3cef63 2020-07-23 stsp }
4538 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4539 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4540 fa3cef63 2020-07-23 stsp goto done;
4541 fa3cef63 2020-07-23 stsp }
4542 fa3cef63 2020-07-23 stsp rewind(f2);
4543 33aa809d 2019-08-08 stsp }
4544 33aa809d 2019-08-08 stsp
4545 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4546 eb81bc23 2022-06-28 tracey if (fd == -1) {
4547 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4548 eb81bc23 2022-06-28 tracey goto done;
4549 eb81bc23 2022-06-28 tracey }
4550 eb81bc23 2022-06-28 tracey
4551 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4552 33aa809d 2019-08-08 stsp if (err)
4553 33aa809d 2019-08-08 stsp goto done;
4554 33aa809d 2019-08-08 stsp
4555 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4556 33aa809d 2019-08-08 stsp if (err)
4557 33aa809d 2019-08-08 stsp goto done;
4558 33aa809d 2019-08-08 stsp
4559 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4560 33aa809d 2019-08-08 stsp if (err)
4561 33aa809d 2019-08-08 stsp goto done;
4562 33aa809d 2019-08-08 stsp
4563 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4565 33aa809d 2019-08-08 stsp if (err)
4566 33aa809d 2019-08-08 stsp goto done;
4567 33aa809d 2019-08-08 stsp
4568 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4569 b90054ed 2022-11-01 stsp "");
4570 33aa809d 2019-08-08 stsp if (err)
4571 33aa809d 2019-08-08 stsp goto done;
4572 33aa809d 2019-08-08 stsp
4573 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4574 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4575 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4576 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4577 fe621944 2020-11-10 stsp
4578 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4579 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4580 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4581 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4582 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4583 fe621944 2020-11-10 stsp nchanges++;
4584 fe621944 2020-11-10 stsp }
4585 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4586 33aa809d 2019-08-08 stsp int choice;
4587 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4588 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4589 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4590 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4591 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4592 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4593 33aa809d 2019-08-08 stsp if (err)
4594 33aa809d 2019-08-08 stsp goto done;
4595 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4596 33aa809d 2019-08-08 stsp have_content = 1;
4597 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4598 33aa809d 2019-08-08 stsp break;
4599 33aa809d 2019-08-08 stsp }
4600 1ebedb77 2019-10-19 stsp if (have_content) {
4601 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4602 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4603 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4604 1ebedb77 2019-10-19 stsp if (err)
4605 1ebedb77 2019-10-19 stsp goto done;
4606 1ebedb77 2019-10-19 stsp
4607 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4608 b2b3fce1 2022-10-29 op mode_t mode;
4609 b2b3fce1 2022-10-29 op
4610 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4611 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4612 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4613 fa3cef63 2020-07-23 stsp goto done;
4614 fa3cef63 2020-07-23 stsp }
4615 1ebedb77 2019-10-19 stsp }
4616 1ebedb77 2019-10-19 stsp }
4617 33aa809d 2019-08-08 stsp done:
4618 33aa809d 2019-08-08 stsp free(id_str);
4619 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4620 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4621 33aa809d 2019-08-08 stsp if (blob)
4622 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4623 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4624 fe621944 2020-11-10 stsp if (err == NULL)
4625 fe621944 2020-11-10 stsp err = free_err;
4626 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4627 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4628 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4629 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4630 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4631 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4632 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4633 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4634 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4635 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4636 33aa809d 2019-08-08 stsp if (err || !have_content) {
4637 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4638 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4639 33aa809d 2019-08-08 stsp free(*path_outfile);
4640 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4641 33aa809d 2019-08-08 stsp }
4642 33aa809d 2019-08-08 stsp free(path1);
4643 33aa809d 2019-08-08 stsp return err;
4644 33aa809d 2019-08-08 stsp }
4645 33aa809d 2019-08-08 stsp
4646 33aa809d 2019-08-08 stsp static const struct got_error *
4647 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4648 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4649 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4650 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4651 a129376b 2019-03-28 stsp {
4652 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4653 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4654 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4655 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4656 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4657 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4658 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4659 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4660 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4661 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4662 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4663 eb81bc23 2022-06-28 tracey int fd = -1;
4664 a129376b 2019-03-28 stsp
4665 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4666 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4667 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4668 d3bcc3d1 2019-08-08 stsp return NULL;
4669 3d69ad8d 2019-08-17 semarie
4670 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4671 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4672 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4673 d3bcc3d1 2019-08-08 stsp
4674 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4675 65084dad 2019-08-08 stsp if (ie == NULL)
4676 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 a129376b 2019-03-28 stsp
4678 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4679 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4680 a129376b 2019-03-28 stsp if (err) {
4681 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4682 a129376b 2019-03-28 stsp goto done;
4683 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4684 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4685 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4686 e20a8b6f 2019-06-04 stsp goto done;
4687 e20a8b6f 2019-06-04 stsp }
4688 a129376b 2019-03-28 stsp }
4689 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4690 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4691 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4692 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4693 a129376b 2019-03-28 stsp goto done;
4694 a129376b 2019-03-28 stsp }
4695 a129376b 2019-03-28 stsp } else {
4696 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4697 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4698 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4699 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4700 a129376b 2019-03-28 stsp goto done;
4701 a129376b 2019-03-28 stsp }
4702 a129376b 2019-03-28 stsp } else {
4703 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4704 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4705 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4706 a129376b 2019-03-28 stsp goto done;
4707 a129376b 2019-03-28 stsp }
4708 a129376b 2019-03-28 stsp }
4709 a129376b 2019-03-28 stsp }
4710 a129376b 2019-03-28 stsp
4711 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4712 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4713 a44927cc 2022-04-07 stsp if (err)
4714 a44927cc 2022-04-07 stsp goto done;
4715 a44927cc 2022-04-07 stsp
4716 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4717 a9fa2909 2019-07-27 stsp if (err) {
4718 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4719 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4720 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4721 a9fa2909 2019-07-27 stsp goto done;
4722 a9fa2909 2019-07-27 stsp } else {
4723 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4724 a9fa2909 2019-07-27 stsp if (err)
4725 a9fa2909 2019-07-27 stsp goto done;
4726 a9fa2909 2019-07-27 stsp
4727 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4728 1233e6b6 2020-10-19 stsp if (err)
4729 a9fa2909 2019-07-27 stsp goto done;
4730 a9fa2909 2019-07-27 stsp
4731 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4732 1233e6b6 2020-10-19 stsp free(te_name);
4733 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4734 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4735 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4736 a9fa2909 2019-07-27 stsp goto done;
4737 a9fa2909 2019-07-27 stsp }
4738 a129376b 2019-03-28 stsp }
4739 a129376b 2019-03-28 stsp
4740 a129376b 2019-03-28 stsp switch (status) {
4741 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4742 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4743 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4744 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4745 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4746 33aa809d 2019-08-08 stsp if (err)
4747 33aa809d 2019-08-08 stsp goto done;
4748 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4749 33aa809d 2019-08-08 stsp break;
4750 33aa809d 2019-08-08 stsp }
4751 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4752 1f1abb7e 2019-08-08 stsp ie->path);
4753 1ee397ad 2019-07-12 stsp if (err)
4754 1ee397ad 2019-07-12 stsp goto done;
4755 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4756 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4757 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4758 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4759 f259c4c1 2021-09-24 stsp relpath) == -1) {
4760 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4761 f259c4c1 2021-09-24 stsp goto done;
4762 f259c4c1 2021-09-24 stsp }
4763 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4764 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4765 f259c4c1 2021-09-24 stsp ondisk_path);
4766 f259c4c1 2021-09-24 stsp break;
4767 f259c4c1 2021-09-24 stsp }
4768 f259c4c1 2021-09-24 stsp }
4769 a129376b 2019-03-28 stsp break;
4770 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4771 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4772 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4773 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4774 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4775 33aa809d 2019-08-08 stsp if (err)
4776 33aa809d 2019-08-08 stsp goto done;
4777 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4778 33aa809d 2019-08-08 stsp break;
4779 33aa809d 2019-08-08 stsp }
4780 33aa809d 2019-08-08 stsp /* fall through */
4781 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4782 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4783 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4784 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4785 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4786 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4787 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4788 3093e2d7 2023-02-04 op memcpy(id.hash, ie->staged_blob_hash,
4789 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4790 24278f30 2019-08-03 stsp } else
4791 3093e2d7 2023-02-04 op memcpy(id.hash, ie->blob_hash,
4792 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4793 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4794 eb81bc23 2022-06-28 tracey if (fd == -1) {
4795 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4796 eb81bc23 2022-06-28 tracey goto done;
4797 eb81bc23 2022-06-28 tracey }
4798 eb81bc23 2022-06-28 tracey
4799 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4800 a129376b 2019-03-28 stsp if (err)
4801 65084dad 2019-08-08 stsp goto done;
4802 65084dad 2019-08-08 stsp
4803 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4804 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4805 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4806 a129376b 2019-03-28 stsp goto done;
4807 65084dad 2019-08-08 stsp }
4808 65084dad 2019-08-08 stsp
4809 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4810 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4811 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4812 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4813 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4814 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4815 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4816 33aa809d 2019-08-08 stsp break;
4817 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4818 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4819 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4820 369fd7e5 2020-07-23 stsp path_content);
4821 369fd7e5 2020-07-23 stsp break;
4822 369fd7e5 2020-07-23 stsp }
4823 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4824 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4825 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4826 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4827 369fd7e5 2020-07-23 stsp } else {
4828 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4829 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4830 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4831 369fd7e5 2020-07-23 stsp goto done;
4832 369fd7e5 2020-07-23 stsp }
4833 33aa809d 2019-08-08 stsp }
4834 33aa809d 2019-08-08 stsp } else {
4835 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4836 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4837 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4838 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4839 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4840 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4841 2e1fa222 2020-07-23 stsp } else {
4842 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4843 2e1fa222 2020-07-23 stsp ie->path,
4844 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4845 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4846 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4847 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4848 2e1fa222 2020-07-23 stsp }
4849 a129376b 2019-03-28 stsp if (err)
4850 a129376b 2019-03-28 stsp goto done;
4851 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4852 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4853 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4854 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4855 3093e2d7 2023-02-04 op blob->id.hash,
4856 3093e2d7 2023-02-04 op a->worktree->base_commit_id->hash, 1);
4857 2e1fa222 2020-07-23 stsp if (err)
4858 2e1fa222 2020-07-23 stsp goto done;
4859 2e1fa222 2020-07-23 stsp }
4860 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4861 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4862 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4863 33aa809d 2019-08-08 stsp }
4864 a129376b 2019-03-28 stsp }
4865 a129376b 2019-03-28 stsp break;
4866 e20a8b6f 2019-06-04 stsp }
4867 a129376b 2019-03-28 stsp default:
4868 1f1abb7e 2019-08-08 stsp break;
4869 a129376b 2019-03-28 stsp }
4870 a129376b 2019-03-28 stsp done:
4871 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4872 33aa809d 2019-08-08 stsp free(path_content);
4873 e20a8b6f 2019-06-04 stsp free(parent_path);
4874 a129376b 2019-03-28 stsp free(tree_path);
4875 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4876 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4877 a129376b 2019-03-28 stsp if (blob)
4878 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4879 a129376b 2019-03-28 stsp if (tree)
4880 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4881 a129376b 2019-03-28 stsp free(tree_id);
4882 a44927cc 2022-04-07 stsp if (base_commit)
4883 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4884 e20a8b6f 2019-06-04 stsp return err;
4885 e20a8b6f 2019-06-04 stsp }
4886 e20a8b6f 2019-06-04 stsp
4887 e20a8b6f 2019-06-04 stsp const struct got_error *
4888 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4889 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4890 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4891 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4892 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4893 e20a8b6f 2019-06-04 stsp {
4894 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4895 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4896 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4897 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4898 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4899 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4900 e20a8b6f 2019-06-04 stsp
4901 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4902 e20a8b6f 2019-06-04 stsp if (err)
4903 e20a8b6f 2019-06-04 stsp return err;
4904 e20a8b6f 2019-06-04 stsp
4905 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4906 e20a8b6f 2019-06-04 stsp if (err)
4907 e20a8b6f 2019-06-04 stsp goto done;
4908 e20a8b6f 2019-06-04 stsp
4909 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4910 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4911 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4912 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4913 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4914 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4915 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4916 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4917 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4918 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4919 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4920 e20a8b6f 2019-06-04 stsp if (err)
4921 af12c6b9 2019-06-04 stsp break;
4922 e20a8b6f 2019-06-04 stsp }
4923 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4924 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4925 e20a8b6f 2019-06-04 stsp err = sync_err;
4926 af12c6b9 2019-06-04 stsp done:
4927 fb399478 2019-07-12 stsp free(fileindex_path);
4928 a129376b 2019-03-28 stsp if (fileindex)
4929 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4930 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4931 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4932 a129376b 2019-03-28 stsp err = unlockerr;
4933 c4296144 2019-05-09 stsp return err;
4934 c4296144 2019-05-09 stsp }
4935 c4296144 2019-05-09 stsp
4936 cf066bf8 2019-05-09 stsp static void
4937 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4938 cf066bf8 2019-05-09 stsp {
4939 24519714 2019-05-09 stsp free(ct->path);
4940 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4941 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4942 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4943 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4944 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4945 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4946 cf066bf8 2019-05-09 stsp free(ct);
4947 cf066bf8 2019-05-09 stsp }
4948 24519714 2019-05-09 stsp
4949 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4950 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4951 24519714 2019-05-09 stsp struct got_repository *repo;
4952 24519714 2019-05-09 stsp struct got_worktree *worktree;
4953 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4954 5f8a88c6 2019-08-03 stsp int have_staged_files;
4955 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4956 2a47b1e5 2022-11-01 stsp int diff_header_shown;
4957 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
4958 2a47b1e5 2022-11-01 stsp FILE *f1;
4959 2a47b1e5 2022-11-01 stsp FILE *f2;
4960 24519714 2019-05-09 stsp };
4961 2a47b1e5 2022-11-01 stsp
4962 2a47b1e5 2022-11-01 stsp /*
4963 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
4964 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
4965 2a47b1e5 2022-11-01 stsp */
4966 2a47b1e5 2022-11-01 stsp static const struct got_error *
4967 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4968 2a47b1e5 2022-11-01 stsp const char *abspath)
4969 2a47b1e5 2022-11-01 stsp {
4970 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
4971 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
4972 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
4973 2a47b1e5 2022-11-01 stsp
4974 2a47b1e5 2022-11-01 stsp *fd = -1;
4975 2a47b1e5 2022-11-01 stsp
4976 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
4977 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4978 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4979 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
4980 2a47b1e5 2022-11-01 stsp } else {
4981 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4982 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4983 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
4984 2a47b1e5 2022-11-01 stsp }
4985 2a47b1e5 2022-11-01 stsp
4986 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
4987 2a47b1e5 2022-11-01 stsp if (*fd == -1)
4988 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
4989 2a47b1e5 2022-11-01 stsp
4990 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
4991 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
4992 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
4993 2a47b1e5 2022-11-01 stsp goto done;
4994 2a47b1e5 2022-11-01 stsp }
4995 2a47b1e5 2022-11-01 stsp
4996 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4997 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
4998 2a47b1e5 2022-11-01 stsp goto done;
4999 2a47b1e5 2022-11-01 stsp }
5000 2a47b1e5 2022-11-01 stsp done:
5001 2a47b1e5 2022-11-01 stsp if (err) {
5002 2a47b1e5 2022-11-01 stsp close(*fd);
5003 2a47b1e5 2022-11-01 stsp *fd = -1;
5004 2a47b1e5 2022-11-01 stsp }
5005 2a47b1e5 2022-11-01 stsp return err;
5006 2a47b1e5 2022-11-01 stsp }
5007 2a47b1e5 2022-11-01 stsp
5008 2a47b1e5 2022-11-01 stsp static const struct got_error *
5009 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5010 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5011 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5012 2a47b1e5 2022-11-01 stsp {
5013 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5014 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5015 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5016 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5017 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5018 2a47b1e5 2022-11-01 stsp struct stat sb;
5019 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5020 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5021 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5022 2a47b1e5 2022-11-01 stsp
5023 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5024 4ba5cca9 2022-11-01 stsp
5025 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5026 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5027 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5028 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5029 4ba5cca9 2022-11-01 stsp return NULL;
5030 4ba5cca9 2022-11-01 stsp } else {
5031 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5032 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5033 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_DELETE)
5034 4ba5cca9 2022-11-01 stsp return NULL;
5035 4ba5cca9 2022-11-01 stsp }
5036 2a47b1e5 2022-11-01 stsp
5037 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5038 2a47b1e5 2022-11-01 stsp if (err)
5039 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5040 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5041 2a47b1e5 2022-11-01 stsp if (err)
5042 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5043 2a47b1e5 2022-11-01 stsp
5044 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5045 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5046 2a47b1e5 2022-11-01 stsp if (err)
5047 2a47b1e5 2022-11-01 stsp return err;
5048 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5049 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5050 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5051 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5052 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5053 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5054 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5055 2a47b1e5 2022-11-01 stsp }
5056 2a47b1e5 2022-11-01 stsp
5057 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5058 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5059 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5060 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5061 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5062 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5063 2a47b1e5 2022-11-01 stsp break;
5064 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5065 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5066 2a47b1e5 2022-11-01 stsp break;
5067 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5068 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5069 2a47b1e5 2022-11-01 stsp break;
5070 2a47b1e5 2022-11-01 stsp default:
5071 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5072 2a47b1e5 2022-11-01 stsp }
5073 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5074 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5075 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5076 2a47b1e5 2022-11-01 stsp goto done;
5077 2a47b1e5 2022-11-01 stsp }
5078 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5079 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5080 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5081 2a47b1e5 2022-11-01 stsp goto done;
5082 2a47b1e5 2022-11-01 stsp }
5083 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5084 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5085 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5086 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5087 2a47b1e5 2022-11-01 stsp goto done;
5088 2a47b1e5 2022-11-01 stsp }
5089 2a47b1e5 2022-11-01 stsp
5090 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5091 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5092 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5093 2a47b1e5 2022-11-01 stsp goto done;
5094 2a47b1e5 2022-11-01 stsp }
5095 2a47b1e5 2022-11-01 stsp
5096 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5097 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5098 2a47b1e5 2022-11-01 stsp 8192, fd1);
5099 2a47b1e5 2022-11-01 stsp if (err)
5100 2a47b1e5 2022-11-01 stsp goto done;
5101 2a47b1e5 2022-11-01 stsp }
5102 2a47b1e5 2022-11-01 stsp
5103 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5104 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5105 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5106 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5107 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5108 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5109 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5110 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5111 2a47b1e5 2022-11-01 stsp goto done;
5112 2a47b1e5 2022-11-01 stsp }
5113 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5114 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5115 2a47b1e5 2022-11-01 stsp if (err)
5116 2a47b1e5 2022-11-01 stsp goto done;
5117 2a47b1e5 2022-11-01 stsp }
5118 2a47b1e5 2022-11-01 stsp } else {
5119 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5120 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5121 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5122 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5123 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5124 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5125 2a47b1e5 2022-11-01 stsp goto done;
5126 2a47b1e5 2022-11-01 stsp }
5127 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5128 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5129 2a47b1e5 2022-11-01 stsp if (err)
5130 2a47b1e5 2022-11-01 stsp goto done;
5131 2a47b1e5 2022-11-01 stsp }
5132 2a47b1e5 2022-11-01 stsp }
5133 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5134 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5135 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5136 2a47b1e5 2022-11-01 stsp goto done;
5137 2a47b1e5 2022-11-01 stsp }
5138 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5139 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5140 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5141 2a47b1e5 2022-11-01 stsp goto done;
5142 2a47b1e5 2022-11-01 stsp }
5143 2a47b1e5 2022-11-01 stsp fd = -1;
5144 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5145 2a47b1e5 2022-11-01 stsp }
5146 2a47b1e5 2022-11-01 stsp
5147 2a47b1e5 2022-11-01 stsp if (blob1) {
5148 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5149 2a47b1e5 2022-11-01 stsp f1, blob1);
5150 2a47b1e5 2022-11-01 stsp if (err)
5151 2a47b1e5 2022-11-01 stsp goto done;
5152 2a47b1e5 2022-11-01 stsp }
5153 2a47b1e5 2022-11-01 stsp
5154 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5155 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5156 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5157 2a47b1e5 2022-11-01 stsp done:
5158 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5159 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5160 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5161 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5162 2a47b1e5 2022-11-01 stsp if (blob1)
5163 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5164 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5165 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5166 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5167 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5168 2a47b1e5 2022-11-01 stsp return err;
5169 2a47b1e5 2022-11-01 stsp }
5170 cf066bf8 2019-05-09 stsp
5171 c4296144 2019-05-09 stsp static const struct got_error *
5172 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5173 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5174 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5175 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5176 c4296144 2019-05-09 stsp {
5177 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5178 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5179 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5180 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5181 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5182 768aea60 2019-05-09 stsp struct stat sb;
5183 c4296144 2019-05-09 stsp
5184 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5185 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5186 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5187 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5188 5f8a88c6 2019-08-03 stsp return NULL;
5189 5f8a88c6 2019-08-03 stsp } else {
5190 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
5191 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5192 c4296144 2019-05-09 stsp
5193 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5194 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5195 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5196 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
5197 5f8a88c6 2019-08-03 stsp return NULL;
5198 5f8a88c6 2019-08-03 stsp }
5199 0b5cc0d6 2019-05-09 stsp
5200 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5201 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5202 036813ee 2019-05-09 stsp goto done;
5203 036813ee 2019-05-09 stsp }
5204 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5205 036813ee 2019-05-09 stsp parent_path = strdup("");
5206 69960a46 2019-05-09 stsp if (parent_path == NULL)
5207 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5208 69960a46 2019-05-09 stsp } else {
5209 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5210 69960a46 2019-05-09 stsp if (err)
5211 69960a46 2019-05-09 stsp return err;
5212 69960a46 2019-05-09 stsp }
5213 c4296144 2019-05-09 stsp
5214 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5215 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5216 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5217 c4296144 2019-05-09 stsp goto done;
5218 768aea60 2019-05-09 stsp }
5219 768aea60 2019-05-09 stsp
5220 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5221 768aea60 2019-05-09 stsp relpath) == -1) {
5222 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5223 768aea60 2019-05-09 stsp goto done;
5224 768aea60 2019-05-09 stsp }
5225 0aeb8099 2020-07-23 stsp
5226 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5227 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5228 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5229 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5230 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5231 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5232 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5233 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5234 0aeb8099 2020-07-23 stsp break;
5235 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5236 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5237 0aeb8099 2020-07-23 stsp break;
5238 0aeb8099 2020-07-23 stsp default:
5239 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5240 0aeb8099 2020-07-23 stsp goto done;
5241 0aeb8099 2020-07-23 stsp }
5242 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5243 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5244 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5245 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5246 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5247 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5248 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5249 12463d8b 2019-12-13 stsp ct->ondisk_path);
5250 12463d8b 2019-12-13 stsp goto done;
5251 12463d8b 2019-12-13 stsp }
5252 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5253 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5254 768aea60 2019-05-09 stsp goto done;
5255 768aea60 2019-05-09 stsp }
5256 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5257 c4296144 2019-05-09 stsp }
5258 c4296144 2019-05-09 stsp
5259 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5260 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5261 44d03001 2019-05-09 stsp relpath) == -1) {
5262 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5263 44d03001 2019-05-09 stsp goto done;
5264 44d03001 2019-05-09 stsp }
5265 44d03001 2019-05-09 stsp
5266 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5267 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5268 35213c7c 2020-07-23 stsp int is_bad_symlink;
5269 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5270 35213c7c 2020-07-23 stsp ssize_t target_len;
5271 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5272 35213c7c 2020-07-23 stsp sizeof(target_path));
5273 35213c7c 2020-07-23 stsp if (target_len == -1) {
5274 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5275 35213c7c 2020-07-23 stsp ct->ondisk_path);
5276 35213c7c 2020-07-23 stsp goto done;
5277 35213c7c 2020-07-23 stsp }
5278 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5279 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5280 35213c7c 2020-07-23 stsp if (err)
5281 35213c7c 2020-07-23 stsp goto done;
5282 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5283 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5284 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5285 35213c7c 2020-07-23 stsp goto done;
5286 35213c7c 2020-07-23 stsp }
5287 35213c7c 2020-07-23 stsp }
5288 35213c7c 2020-07-23 stsp
5289 35213c7c 2020-07-23 stsp
5290 cf066bf8 2019-05-09 stsp ct->status = status;
5291 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5292 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5293 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5294 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5295 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5296 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5297 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5298 b416585c 2019-05-13 stsp goto done;
5299 b416585c 2019-05-13 stsp }
5300 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5301 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5302 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5303 f0b75401 2019-08-03 stsp goto done;
5304 f0b75401 2019-08-03 stsp }
5305 f0b75401 2019-08-03 stsp }
5306 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5307 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5308 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5309 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5310 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5311 036813ee 2019-05-09 stsp goto done;
5312 036813ee 2019-05-09 stsp }
5313 ca2503ea 2019-05-09 stsp }
5314 24519714 2019-05-09 stsp ct->path = strdup(path);
5315 24519714 2019-05-09 stsp if (ct->path == NULL) {
5316 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5317 24519714 2019-05-09 stsp goto done;
5318 24519714 2019-05-09 stsp }
5319 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5320 2a47b1e5 2022-11-01 stsp if (err)
5321 2a47b1e5 2022-11-01 stsp goto done;
5322 2a47b1e5 2022-11-01 stsp
5323 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5324 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5325 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5326 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5327 2a47b1e5 2022-11-01 stsp if (err)
5328 2a47b1e5 2022-11-01 stsp goto done;
5329 2a47b1e5 2022-11-01 stsp }
5330 c4296144 2019-05-09 stsp done:
5331 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5332 ed175427 2019-05-09 stsp free_commitable(ct);
5333 24519714 2019-05-09 stsp free(parent_path);
5334 036813ee 2019-05-09 stsp free(path);
5335 a129376b 2019-03-28 stsp return err;
5336 a129376b 2019-03-28 stsp }
5337 c4296144 2019-05-09 stsp
5338 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5339 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5340 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5341 036813ee 2019-05-09 stsp struct got_repository *);
5342 ed175427 2019-05-09 stsp
5343 ed175427 2019-05-09 stsp static const struct got_error *
5344 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5345 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5346 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5347 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5348 afa376bf 2019-05-09 stsp struct got_repository *repo)
5349 ed175427 2019-05-09 stsp {
5350 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5351 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5352 036813ee 2019-05-09 stsp char *subpath;
5353 ed175427 2019-05-09 stsp
5354 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5355 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5356 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5357 ed175427 2019-05-09 stsp
5358 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5359 ed175427 2019-05-09 stsp if (err)
5360 ed175427 2019-05-09 stsp return err;
5361 ed175427 2019-05-09 stsp
5362 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5363 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5364 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5365 036813ee 2019-05-09 stsp free(subpath);
5366 036813ee 2019-05-09 stsp return err;
5367 036813ee 2019-05-09 stsp }
5368 ed175427 2019-05-09 stsp
5369 036813ee 2019-05-09 stsp static const struct got_error *
5370 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5371 036813ee 2019-05-09 stsp {
5372 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5373 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5374 ed175427 2019-05-09 stsp
5375 036813ee 2019-05-09 stsp *match = 0;
5376 ed175427 2019-05-09 stsp
5377 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5378 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5379 0f63689d 2019-05-10 stsp return NULL;
5380 036813ee 2019-05-09 stsp }
5381 0b5cc0d6 2019-05-09 stsp
5382 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5383 0f63689d 2019-05-10 stsp if (err)
5384 0f63689d 2019-05-10 stsp return err;
5385 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5386 036813ee 2019-05-09 stsp free(ct_parent_path);
5387 036813ee 2019-05-09 stsp return err;
5388 036813ee 2019-05-09 stsp }
5389 0b5cc0d6 2019-05-09 stsp
5390 768aea60 2019-05-09 stsp static mode_t
5391 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5392 768aea60 2019-05-09 stsp {
5393 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5394 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5395 3d9a4ec4 2020-07-23 stsp
5396 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5397 768aea60 2019-05-09 stsp }
5398 768aea60 2019-05-09 stsp
5399 036813ee 2019-05-09 stsp static const struct got_error *
5400 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5401 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5402 036813ee 2019-05-09 stsp {
5403 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5404 ca2503ea 2019-05-09 stsp
5405 036813ee 2019-05-09 stsp *new_te = NULL;
5406 0b5cc0d6 2019-05-09 stsp
5407 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5408 036813ee 2019-05-09 stsp if (err)
5409 036813ee 2019-05-09 stsp goto done;
5410 0b5cc0d6 2019-05-09 stsp
5411 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5412 036813ee 2019-05-09 stsp
5413 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5414 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5415 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5416 5f8a88c6 2019-08-03 stsp else
5417 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5418 036813ee 2019-05-09 stsp done:
5419 036813ee 2019-05-09 stsp if (err && *new_te) {
5420 56e0773d 2019-11-28 stsp free(*new_te);
5421 036813ee 2019-05-09 stsp *new_te = NULL;
5422 036813ee 2019-05-09 stsp }
5423 036813ee 2019-05-09 stsp return err;
5424 036813ee 2019-05-09 stsp }
5425 036813ee 2019-05-09 stsp
5426 036813ee 2019-05-09 stsp static const struct got_error *
5427 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5428 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5429 036813ee 2019-05-09 stsp {
5430 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5431 102b254e 2020-10-19 stsp char *ct_name = NULL;
5432 036813ee 2019-05-09 stsp
5433 036813ee 2019-05-09 stsp *new_te = NULL;
5434 036813ee 2019-05-09 stsp
5435 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5436 036813ee 2019-05-09 stsp if (*new_te == NULL)
5437 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5438 036813ee 2019-05-09 stsp
5439 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5440 102b254e 2020-10-19 stsp if (err)
5441 036813ee 2019-05-09 stsp goto done;
5442 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5443 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5444 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5445 036813ee 2019-05-09 stsp goto done;
5446 036813ee 2019-05-09 stsp }
5447 036813ee 2019-05-09 stsp
5448 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5449 036813ee 2019-05-09 stsp
5450 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5451 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5452 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5453 5f8a88c6 2019-08-03 stsp else
5454 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5455 036813ee 2019-05-09 stsp done:
5456 102b254e 2020-10-19 stsp free(ct_name);
5457 036813ee 2019-05-09 stsp if (err && *new_te) {
5458 56e0773d 2019-11-28 stsp free(*new_te);
5459 036813ee 2019-05-09 stsp *new_te = NULL;
5460 036813ee 2019-05-09 stsp }
5461 036813ee 2019-05-09 stsp return err;
5462 036813ee 2019-05-09 stsp }
5463 036813ee 2019-05-09 stsp
5464 036813ee 2019-05-09 stsp static const struct got_error *
5465 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5466 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5467 036813ee 2019-05-09 stsp {
5468 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5469 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5470 036813ee 2019-05-09 stsp
5471 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5472 036813ee 2019-05-09 stsp if (err)
5473 036813ee 2019-05-09 stsp return err;
5474 036813ee 2019-05-09 stsp if (new_pe == NULL)
5475 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5476 036813ee 2019-05-09 stsp return NULL;
5477 afa376bf 2019-05-09 stsp }
5478 afa376bf 2019-05-09 stsp
5479 afa376bf 2019-05-09 stsp static const struct got_error *
5480 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5481 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5482 afa376bf 2019-05-09 stsp {
5483 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5484 5f8a88c6 2019-08-03 stsp unsigned char status;
5485 0ff8d236 2021-09-28 stsp
5486 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5487 0ff8d236 2021-09-28 stsp return NULL;
5488 5f8a88c6 2019-08-03 stsp
5489 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5490 afa376bf 2019-05-09 stsp ct_path++;
5491 5f8a88c6 2019-08-03 stsp
5492 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5493 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5494 5f8a88c6 2019-08-03 stsp else
5495 5f8a88c6 2019-08-03 stsp status = ct->status;
5496 5f8a88c6 2019-08-03 stsp
5497 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5498 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5499 036813ee 2019-05-09 stsp }
5500 036813ee 2019-05-09 stsp
5501 036813ee 2019-05-09 stsp static const struct got_error *
5502 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5503 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5504 44d03001 2019-05-09 stsp {
5505 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5506 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5507 44d03001 2019-05-09 stsp char *te_path;
5508 44d03001 2019-05-09 stsp
5509 44d03001 2019-05-09 stsp *modified = 0;
5510 44d03001 2019-05-09 stsp
5511 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5512 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5513 44d03001 2019-05-09 stsp te->name) == -1)
5514 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5515 44d03001 2019-05-09 stsp
5516 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5517 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5518 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5519 44d03001 2019-05-09 stsp strlen(te_path));
5520 62d463ca 2020-10-20 naddy if (*modified)
5521 44d03001 2019-05-09 stsp break;
5522 44d03001 2019-05-09 stsp }
5523 44d03001 2019-05-09 stsp
5524 44d03001 2019-05-09 stsp free(te_path);
5525 44d03001 2019-05-09 stsp return err;
5526 44d03001 2019-05-09 stsp }
5527 44d03001 2019-05-09 stsp
5528 44d03001 2019-05-09 stsp static const struct got_error *
5529 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5530 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5531 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5532 036813ee 2019-05-09 stsp {
5533 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5534 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5535 036813ee 2019-05-09 stsp
5536 036813ee 2019-05-09 stsp *ctp = NULL;
5537 036813ee 2019-05-09 stsp
5538 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5539 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5540 036813ee 2019-05-09 stsp char *ct_name = NULL;
5541 036813ee 2019-05-09 stsp int path_matches;
5542 036813ee 2019-05-09 stsp
5543 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5544 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5545 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5546 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5547 5f8a88c6 2019-08-03 stsp continue;
5548 5f8a88c6 2019-08-03 stsp } else {
5549 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5550 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5551 5f8a88c6 2019-08-03 stsp continue;
5552 5f8a88c6 2019-08-03 stsp }
5553 036813ee 2019-05-09 stsp
5554 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5555 036813ee 2019-05-09 stsp continue;
5556 036813ee 2019-05-09 stsp
5557 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5558 62d463ca 2020-10-20 naddy if (err)
5559 036813ee 2019-05-09 stsp return err;
5560 036813ee 2019-05-09 stsp if (!path_matches)
5561 036813ee 2019-05-09 stsp continue;
5562 036813ee 2019-05-09 stsp
5563 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5564 d34b633e 2020-10-19 stsp if (err)
5565 d34b633e 2020-10-19 stsp return err;
5566 d34b633e 2020-10-19 stsp
5567 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5568 d34b633e 2020-10-19 stsp free(ct_name);
5569 036813ee 2019-05-09 stsp continue;
5570 d34b633e 2020-10-19 stsp }
5571 d34b633e 2020-10-19 stsp free(ct_name);
5572 036813ee 2019-05-09 stsp
5573 036813ee 2019-05-09 stsp *ctp = ct;
5574 036813ee 2019-05-09 stsp break;
5575 036813ee 2019-05-09 stsp }
5576 2b496619 2019-07-10 stsp
5577 2b496619 2019-07-10 stsp return err;
5578 2b496619 2019-07-10 stsp }
5579 2b496619 2019-07-10 stsp
5580 2b496619 2019-07-10 stsp static const struct got_error *
5581 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5582 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5583 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5584 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5585 2b496619 2019-07-10 stsp struct got_repository *repo)
5586 2b496619 2019-07-10 stsp {
5587 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5588 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5589 2b496619 2019-07-10 stsp char *subtree_path;
5590 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5591 ba580f68 2020-03-22 stsp int nentries;
5592 2b496619 2019-07-10 stsp
5593 2b496619 2019-07-10 stsp *new_tep = NULL;
5594 2b496619 2019-07-10 stsp
5595 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5596 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5597 2b496619 2019-07-10 stsp child_path) == -1)
5598 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5599 036813ee 2019-05-09 stsp
5600 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5601 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5602 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5603 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5604 56e0773d 2019-11-28 stsp
5605 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5606 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5607 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5608 2b496619 2019-07-10 stsp goto done;
5609 2b496619 2019-07-10 stsp }
5610 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5611 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5612 2b496619 2019-07-10 stsp if (err) {
5613 56e0773d 2019-11-28 stsp free(new_te);
5614 2b496619 2019-07-10 stsp goto done;
5615 2b496619 2019-07-10 stsp }
5616 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5617 2b496619 2019-07-10 stsp done:
5618 56e0773d 2019-11-28 stsp free(id);
5619 2b496619 2019-07-10 stsp free(subtree_path);
5620 2b496619 2019-07-10 stsp if (err == NULL)
5621 2b496619 2019-07-10 stsp *new_tep = new_te;
5622 036813ee 2019-05-09 stsp return err;
5623 036813ee 2019-05-09 stsp }
5624 036813ee 2019-05-09 stsp
5625 036813ee 2019-05-09 stsp static const struct got_error *
5626 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5627 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5628 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5629 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5630 036813ee 2019-05-09 stsp struct got_repository *repo)
5631 036813ee 2019-05-09 stsp {
5632 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5633 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5634 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5635 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5636 036813ee 2019-05-09 stsp
5637 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5638 ba580f68 2020-03-22 stsp *nentries = 0;
5639 036813ee 2019-05-09 stsp
5640 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5641 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5642 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5643 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5644 036813ee 2019-05-09 stsp
5645 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5646 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5647 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5648 036813ee 2019-05-09 stsp continue;
5649 036813ee 2019-05-09 stsp
5650 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5651 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5652 036813ee 2019-05-09 stsp continue;
5653 036813ee 2019-05-09 stsp
5654 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5655 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5656 036813ee 2019-05-09 stsp if (err)
5657 036813ee 2019-05-09 stsp goto done;
5658 036813ee 2019-05-09 stsp
5659 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5660 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5661 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5662 036813ee 2019-05-09 stsp if (err)
5663 036813ee 2019-05-09 stsp goto done;
5664 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5665 afa376bf 2019-05-09 stsp if (err)
5666 afa376bf 2019-05-09 stsp goto done;
5667 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5668 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5669 2b496619 2019-07-10 stsp if (err)
5670 2f51b5b3 2019-05-09 stsp goto done;
5671 ba580f68 2020-03-22 stsp (*nentries)++;
5672 2b496619 2019-07-10 stsp } else {
5673 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5674 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5675 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5676 2b496619 2019-07-10 stsp == NULL) {
5677 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5678 2b496619 2019-07-10 stsp child_path, path_base_tree,
5679 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5680 2b496619 2019-07-10 stsp repo);
5681 2b496619 2019-07-10 stsp if (err)
5682 2b496619 2019-07-10 stsp goto done;
5683 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5684 2b496619 2019-07-10 stsp if (err)
5685 2b496619 2019-07-10 stsp goto done;
5686 ba580f68 2020-03-22 stsp (*nentries)++;
5687 9ba0479c 2019-05-10 stsp }
5688 ed175427 2019-05-09 stsp }
5689 2f51b5b3 2019-05-09 stsp }
5690 2f51b5b3 2019-05-09 stsp
5691 2f51b5b3 2019-05-09 stsp if (base_tree) {
5692 56e0773d 2019-11-28 stsp int i, nbase_entries;
5693 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5694 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5695 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5696 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5697 63c5ca5d 2019-08-24 stsp
5698 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5699 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5700 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5701 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5702 63c5ca5d 2019-08-24 stsp if (err)
5703 63c5ca5d 2019-08-24 stsp goto done;
5704 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5705 63c5ca5d 2019-08-24 stsp if (err)
5706 63c5ca5d 2019-08-24 stsp goto done;
5707 ba580f68 2020-03-22 stsp (*nentries)++;
5708 63c5ca5d 2019-08-24 stsp continue;
5709 63c5ca5d 2019-08-24 stsp }
5710 2f51b5b3 2019-05-09 stsp
5711 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5712 44d03001 2019-05-09 stsp int modified;
5713 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5714 036813ee 2019-05-09 stsp if (err)
5715 036813ee 2019-05-09 stsp goto done;
5716 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5717 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5718 2f51b5b3 2019-05-09 stsp if (err)
5719 2f51b5b3 2019-05-09 stsp goto done;
5720 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5721 44d03001 2019-05-09 stsp if (modified) {
5722 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5723 ba580f68 2020-03-22 stsp int nsubentries;
5724 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5725 ba580f68 2020-03-22 stsp &nsubentries, te,
5726 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5727 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5728 44d03001 2019-05-09 stsp if (err)
5729 44d03001 2019-05-09 stsp goto done;
5730 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5731 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5732 ba580f68 2020-03-22 stsp free(new_id);
5733 ba580f68 2020-03-22 stsp continue;
5734 ba580f68 2020-03-22 stsp }
5735 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5736 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5737 56e0773d 2019-11-28 stsp free(new_id);
5738 44d03001 2019-05-09 stsp }
5739 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5740 036813ee 2019-05-09 stsp if (err)
5741 036813ee 2019-05-09 stsp goto done;
5742 ba580f68 2020-03-22 stsp (*nentries)++;
5743 2f51b5b3 2019-05-09 stsp continue;
5744 036813ee 2019-05-09 stsp }
5745 2f51b5b3 2019-05-09 stsp
5746 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5747 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5748 f66c734c 2019-09-22 stsp if (err)
5749 f66c734c 2019-09-22 stsp goto done;
5750 2f51b5b3 2019-05-09 stsp if (ct) {
5751 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5752 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5753 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5754 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5755 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5756 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5757 2f51b5b3 2019-05-09 stsp if (err)
5758 2f51b5b3 2019-05-09 stsp goto done;
5759 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5760 2f51b5b3 2019-05-09 stsp if (err)
5761 2f51b5b3 2019-05-09 stsp goto done;
5762 ba580f68 2020-03-22 stsp (*nentries)++;
5763 2f51b5b3 2019-05-09 stsp }
5764 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5765 b416585c 2019-05-13 stsp status_arg);
5766 afa376bf 2019-05-09 stsp if (err)
5767 afa376bf 2019-05-09 stsp goto done;
5768 2f51b5b3 2019-05-09 stsp } else {
5769 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5770 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5771 2f51b5b3 2019-05-09 stsp if (err)
5772 2f51b5b3 2019-05-09 stsp goto done;
5773 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5774 2f51b5b3 2019-05-09 stsp if (err)
5775 2f51b5b3 2019-05-09 stsp goto done;
5776 ba580f68 2020-03-22 stsp (*nentries)++;
5777 2f51b5b3 2019-05-09 stsp }
5778 ca2503ea 2019-05-09 stsp }
5779 ed175427 2019-05-09 stsp }
5780 0b5cc0d6 2019-05-09 stsp
5781 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5782 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5783 ed175427 2019-05-09 stsp done:
5784 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5785 2e1fa222 2020-07-23 stsp return err;
5786 2e1fa222 2020-07-23 stsp }
5787 2e1fa222 2020-07-23 stsp
5788 2e1fa222 2020-07-23 stsp static const struct got_error *
5789 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5790 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5791 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5792 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5793 ebf99748 2019-05-09 stsp {
5794 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5795 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5796 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5797 ebf99748 2019-05-09 stsp
5798 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5799 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5800 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5801 ebf99748 2019-05-09 stsp
5802 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5803 437adc9d 2020-12-10 yzhong
5804 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5805 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5806 437adc9d 2020-12-10 yzhong if (err)
5807 437adc9d 2020-12-10 yzhong goto done;
5808 437adc9d 2020-12-10 yzhong
5809 ebf99748 2019-05-09 stsp if (ie) {
5810 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5811 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5812 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5813 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5814 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5815 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5816 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5817 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5818 437adc9d 2020-12-10 yzhong
5819 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5820 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5821 3093e2d7 2023-02-04 op ct->staged_blob_id->hash,
5822 3093e2d7 2023-02-04 op new_base_commit_id->hash,
5823 72fd46fa 2019-09-06 stsp !have_staged_files);
5824 ebf99748 2019-05-09 stsp } else
5825 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5826 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5827 3093e2d7 2023-02-04 op ct->blob_id->hash,
5828 3093e2d7 2023-02-04 op new_base_commit_id->hash,
5829 72fd46fa 2019-09-06 stsp !have_staged_files);
5830 ebf99748 2019-05-09 stsp } else {
5831 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5832 ebf99748 2019-05-09 stsp if (err)
5833 437adc9d 2020-12-10 yzhong goto done;
5834 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5835 3093e2d7 2023-02-04 op worktree->root_fd, relpath, ct->blob_id->hash,
5836 3093e2d7 2023-02-04 op new_base_commit_id->hash, 1);
5837 3969253a 2020-03-07 stsp if (err) {
5838 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5839 437adc9d 2020-12-10 yzhong goto done;
5840 3969253a 2020-03-07 stsp }
5841 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5842 3969253a 2020-03-07 stsp if (err) {
5843 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5844 437adc9d 2020-12-10 yzhong goto done;
5845 3969253a 2020-03-07 stsp }
5846 ebf99748 2019-05-09 stsp }
5847 437adc9d 2020-12-10 yzhong free(relpath);
5848 437adc9d 2020-12-10 yzhong relpath = NULL;
5849 ebf99748 2019-05-09 stsp }
5850 437adc9d 2020-12-10 yzhong done:
5851 437adc9d 2020-12-10 yzhong free(relpath);
5852 d56d26ce 2019-05-10 stsp return err;
5853 d56d26ce 2019-05-10 stsp }
5854 735ef5ac 2019-08-03 stsp
5855 d56d26ce 2019-05-10 stsp
5856 d56d26ce 2019-05-10 stsp static const struct got_error *
5857 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5858 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5859 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5860 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5861 735ef5ac 2019-08-03 stsp int ood_errcode)
5862 d56d26ce 2019-05-10 stsp {
5863 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5864 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5865 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5866 1a36436d 2019-06-10 stsp
5867 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5868 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5869 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5870 1a36436d 2019-06-10 stsp return NULL;
5871 9bead371 2019-07-28 stsp /*
5872 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5873 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5874 9bead371 2019-07-28 stsp */
5875 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5876 a44927cc 2022-04-07 stsp if (err)
5877 a44927cc 2022-04-07 stsp goto done;
5878 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5879 9bead371 2019-07-28 stsp if (err) {
5880 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5881 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5882 1a36436d 2019-06-10 stsp goto done;
5883 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5884 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5885 1a36436d 2019-06-10 stsp } else {
5886 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5887 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5888 a44927cc 2022-04-07 stsp if (err)
5889 a44927cc 2022-04-07 stsp goto done;
5890 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5891 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5892 1a36436d 2019-06-10 stsp goto done;
5893 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5894 1a36436d 2019-06-10 stsp }
5895 1a36436d 2019-06-10 stsp done:
5896 1a36436d 2019-06-10 stsp free(id);
5897 a44927cc 2022-04-07 stsp if (commit)
5898 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5899 1a36436d 2019-06-10 stsp return err;
5900 ebf99748 2019-05-09 stsp }
5901 ebf99748 2019-05-09 stsp
5902 336075a4 2022-06-25 op static const struct got_error *
5903 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5904 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5905 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5906 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5907 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5908 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5909 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5910 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5911 afa376bf 2019-05-09 stsp struct got_repository *repo)
5912 c4296144 2019-05-09 stsp {
5913 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5914 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5915 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5916 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5917 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5918 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5919 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5920 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5921 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5922 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5923 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5924 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5925 f8399b8f 2022-07-22 stsp time_t timestamp;
5926 c4296144 2019-05-09 stsp
5927 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5928 c4296144 2019-05-09 stsp
5929 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5930 675c7539 2019-05-09 stsp
5931 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5932 588edf97 2019-05-10 stsp if (err)
5933 588edf97 2019-05-10 stsp goto done;
5934 de18fc63 2019-05-09 stsp
5935 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5936 588edf97 2019-05-10 stsp if (err)
5937 588edf97 2019-05-10 stsp goto done;
5938 588edf97 2019-05-10 stsp
5939 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5940 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
5941 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
5942 33ad4cbe 2019-05-12 jcs if (err)
5943 33ad4cbe 2019-05-12 jcs goto done;
5944 33ad4cbe 2019-05-12 jcs }
5945 c4296144 2019-05-09 stsp
5946 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5947 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5948 33ad4cbe 2019-05-12 jcs goto done;
5949 33ad4cbe 2019-05-12 jcs }
5950 33ad4cbe 2019-05-12 jcs
5951 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5952 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5953 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5954 cf066bf8 2019-05-09 stsp char *ondisk_path;
5955 cf066bf8 2019-05-09 stsp
5956 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5957 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5958 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5959 5f8a88c6 2019-08-03 stsp continue;
5960 5f8a88c6 2019-08-03 stsp
5961 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5962 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5963 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5964 cf066bf8 2019-05-09 stsp continue;
5965 cf066bf8 2019-05-09 stsp
5966 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5967 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5968 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5969 cf066bf8 2019-05-09 stsp goto done;
5970 cf066bf8 2019-05-09 stsp }
5971 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5972 2e1fa222 2020-07-23 stsp free(ondisk_path);
5973 2e1fa222 2020-07-23 stsp if (err)
5974 cf066bf8 2019-05-09 stsp goto done;
5975 cf066bf8 2019-05-09 stsp }
5976 036813ee 2019-05-09 stsp
5977 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5978 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5979 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5980 036813ee 2019-05-09 stsp if (err)
5981 036813ee 2019-05-09 stsp goto done;
5982 036813ee 2019-05-09 stsp
5983 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5984 de18fc63 2019-05-09 stsp if (err)
5985 de18fc63 2019-05-09 stsp goto done;
5986 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5987 f259c4c1 2021-09-24 stsp nparents++;
5988 f259c4c1 2021-09-24 stsp if (parent_id2) {
5989 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5990 f259c4c1 2021-09-24 stsp if (err)
5991 f259c4c1 2021-09-24 stsp goto done;
5992 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5993 f259c4c1 2021-09-24 stsp nparents++;
5994 f259c4c1 2021-09-24 stsp }
5995 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
5996 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5997 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
5998 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5999 33ad4cbe 2019-05-12 jcs free(logmsg);
6000 9d40349a 2019-05-09 stsp if (err)
6001 9d40349a 2019-05-09 stsp goto done;
6002 9d40349a 2019-05-09 stsp
6003 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6004 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6005 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6006 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6007 09f5bd90 2019-05-09 stsp goto done;
6008 09f5bd90 2019-05-09 stsp }
6009 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6010 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6011 09f5bd90 2019-05-09 stsp if (err)
6012 09f5bd90 2019-05-09 stsp goto done;
6013 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6014 ebf99748 2019-05-09 stsp if (err)
6015 ebf99748 2019-05-09 stsp goto done;
6016 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6017 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6018 09f5bd90 2019-05-09 stsp goto done;
6019 09f5bd90 2019-05-09 stsp }
6020 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6021 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6022 f2c16586 2019-05-09 stsp if (err)
6023 f2c16586 2019-05-09 stsp goto done;
6024 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6025 f2c16586 2019-05-09 stsp if (err)
6026 f2c16586 2019-05-09 stsp goto done;
6027 f2c16586 2019-05-09 stsp
6028 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6029 09f5bd90 2019-05-09 stsp if (err)
6030 09f5bd90 2019-05-09 stsp goto done;
6031 09f5bd90 2019-05-09 stsp
6032 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6033 ebf99748 2019-05-09 stsp if (err)
6034 ebf99748 2019-05-09 stsp goto done;
6035 c4296144 2019-05-09 stsp done:
6036 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6037 588edf97 2019-05-10 stsp if (head_tree)
6038 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6039 588edf97 2019-05-10 stsp if (head_commit)
6040 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6041 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6042 2f17228e 2019-05-12 stsp if (head_ref2) {
6043 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6044 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6045 2f17228e 2019-05-12 stsp err = unlockerr;
6046 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6047 39cd0ff6 2019-07-12 stsp }
6048 39cd0ff6 2019-07-12 stsp return err;
6049 39cd0ff6 2019-07-12 stsp }
6050 5c1e53bc 2019-07-28 stsp
6051 5c1e53bc 2019-07-28 stsp static const struct got_error *
6052 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6053 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6054 5c1e53bc 2019-07-28 stsp {
6055 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6056 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6057 39cd0ff6 2019-07-12 stsp
6058 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6059 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6060 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6061 5c1e53bc 2019-07-28 stsp
6062 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6063 5c1e53bc 2019-07-28 stsp ct_path++;
6064 5c1e53bc 2019-07-28 stsp
6065 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6066 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6067 5c1e53bc 2019-07-28 stsp break;
6068 5c1e53bc 2019-07-28 stsp }
6069 5c1e53bc 2019-07-28 stsp
6070 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6071 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6072 5c1e53bc 2019-07-28 stsp
6073 5c1e53bc 2019-07-28 stsp return NULL;
6074 5c1e53bc 2019-07-28 stsp }
6075 5c1e53bc 2019-07-28 stsp
6076 f0b75401 2019-08-03 stsp static const struct got_error *
6077 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6078 f0b75401 2019-08-03 stsp {
6079 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6080 f0b75401 2019-08-03 stsp
6081 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6082 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6083 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6084 f0b75401 2019-08-03 stsp }
6085 f0b75401 2019-08-03 stsp
6086 f0b75401 2019-08-03 stsp return NULL;
6087 f0b75401 2019-08-03 stsp }
6088 f0b75401 2019-08-03 stsp
6089 5f8a88c6 2019-08-03 stsp static const struct got_error *
6090 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6091 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6092 5f8a88c6 2019-08-03 stsp {
6093 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6094 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6095 5f8a88c6 2019-08-03 stsp
6096 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6097 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6098 5f8a88c6 2019-08-03 stsp continue;
6099 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6100 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6101 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6102 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6103 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6104 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6105 5f8a88c6 2019-08-03 stsp }
6106 5f8a88c6 2019-08-03 stsp
6107 5f8a88c6 2019-08-03 stsp return NULL;
6108 5f8a88c6 2019-08-03 stsp }
6109 5f8a88c6 2019-08-03 stsp
6110 39cd0ff6 2019-07-12 stsp const struct got_error *
6111 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6112 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6113 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6114 2a47b1e5 2022-11-01 stsp int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6115 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6116 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6117 39cd0ff6 2019-07-12 stsp {
6118 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6119 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6120 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6121 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6122 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6123 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6124 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6125 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6126 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6127 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6128 39cd0ff6 2019-07-12 stsp
6129 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6130 39cd0ff6 2019-07-12 stsp
6131 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6132 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6133 39cd0ff6 2019-07-12 stsp
6134 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6135 39cd0ff6 2019-07-12 stsp if (err)
6136 39cd0ff6 2019-07-12 stsp goto done;
6137 39cd0ff6 2019-07-12 stsp
6138 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6139 39cd0ff6 2019-07-12 stsp if (err)
6140 39cd0ff6 2019-07-12 stsp goto done;
6141 39cd0ff6 2019-07-12 stsp
6142 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6143 39cd0ff6 2019-07-12 stsp if (err)
6144 39cd0ff6 2019-07-12 stsp goto done;
6145 39cd0ff6 2019-07-12 stsp
6146 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6147 39cd0ff6 2019-07-12 stsp if (err)
6148 39cd0ff6 2019-07-12 stsp goto done;
6149 39cd0ff6 2019-07-12 stsp
6150 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6151 5f8a88c6 2019-08-03 stsp &have_staged_files);
6152 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6153 5f8a88c6 2019-08-03 stsp goto done;
6154 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6155 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6156 5f8a88c6 2019-08-03 stsp if (err)
6157 5f8a88c6 2019-08-03 stsp goto done;
6158 5f8a88c6 2019-08-03 stsp }
6159 5f8a88c6 2019-08-03 stsp
6160 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6161 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6162 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6163 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6164 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6165 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6166 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6167 2a47b1e5 2022-11-01 stsp if (show_diff) {
6168 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6169 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6170 2a47b1e5 2022-11-01 stsp if (err)
6171 2a47b1e5 2022-11-01 stsp goto done;
6172 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6173 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6174 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6175 2a47b1e5 2022-11-01 stsp goto done;
6176 2a47b1e5 2022-11-01 stsp }
6177 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6178 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6179 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6180 2a47b1e5 2022-11-01 stsp goto done;
6181 2a47b1e5 2022-11-01 stsp }
6182 2a47b1e5 2022-11-01 stsp }
6183 2a47b1e5 2022-11-01 stsp
6184 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6185 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6186 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6187 5c1e53bc 2019-07-28 stsp if (err)
6188 5c1e53bc 2019-07-28 stsp goto done;
6189 5c1e53bc 2019-07-28 stsp }
6190 39cd0ff6 2019-07-12 stsp
6191 2a47b1e5 2022-11-01 stsp if (show_diff) {
6192 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6193 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6194 2a47b1e5 2022-11-01 stsp goto done;
6195 2a47b1e5 2022-11-01 stsp }
6196 2a47b1e5 2022-11-01 stsp }
6197 2a47b1e5 2022-11-01 stsp
6198 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6199 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6200 39cd0ff6 2019-07-12 stsp goto done;
6201 5c1e53bc 2019-07-28 stsp }
6202 5c1e53bc 2019-07-28 stsp
6203 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6204 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6205 5c1e53bc 2019-07-28 stsp if (err)
6206 5c1e53bc 2019-07-28 stsp goto done;
6207 39cd0ff6 2019-07-12 stsp }
6208 f0b75401 2019-08-03 stsp
6209 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6210 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6211 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6212 f0b75401 2019-08-03 stsp
6213 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6214 f0b75401 2019-08-03 stsp ct_path++;
6215 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6216 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6217 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6218 b50cabdf 2019-07-12 stsp if (err)
6219 b50cabdf 2019-07-12 stsp goto done;
6220 f0b75401 2019-08-03 stsp
6221 b50cabdf 2019-07-12 stsp }
6222 b50cabdf 2019-07-12 stsp
6223 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6224 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6225 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6226 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6227 39cd0ff6 2019-07-12 stsp if (err)
6228 39cd0ff6 2019-07-12 stsp goto done;
6229 39cd0ff6 2019-07-12 stsp
6230 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6231 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6232 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6233 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6234 39cd0ff6 2019-07-12 stsp err = sync_err;
6235 39cd0ff6 2019-07-12 stsp done:
6236 39cd0ff6 2019-07-12 stsp if (fileindex)
6237 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6238 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6239 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6240 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6241 39cd0ff6 2019-07-12 stsp err = unlockerr;
6242 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6243 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6244 d8bacb93 2023-01-10 mark
6245 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6246 39cd0ff6 2019-07-12 stsp }
6247 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6248 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6249 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6250 2a47b1e5 2022-11-01 stsp free(diff_path);
6251 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6252 2a47b1e5 2022-11-01 stsp err == NULL)
6253 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6254 c4296144 2019-05-09 stsp return err;
6255 8656d6c4 2019-05-20 stsp }
6256 8656d6c4 2019-05-20 stsp
6257 8656d6c4 2019-05-20 stsp const char *
6258 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6259 8656d6c4 2019-05-20 stsp {
6260 8656d6c4 2019-05-20 stsp return ct->path;
6261 8656d6c4 2019-05-20 stsp }
6262 8656d6c4 2019-05-20 stsp
6263 8656d6c4 2019-05-20 stsp unsigned int
6264 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6265 8656d6c4 2019-05-20 stsp {
6266 8656d6c4 2019-05-20 stsp return ct->status;
6267 818c7501 2019-07-11 stsp }
6268 818c7501 2019-07-11 stsp
6269 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6270 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6271 818c7501 2019-07-11 stsp struct got_repository *repo;
6272 818c7501 2019-07-11 stsp };
6273 818c7501 2019-07-11 stsp
6274 818c7501 2019-07-11 stsp static const struct got_error *
6275 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6276 818c7501 2019-07-11 stsp {
6277 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6278 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6279 818c7501 2019-07-11 stsp unsigned char status;
6280 818c7501 2019-07-11 stsp struct stat sb;
6281 818c7501 2019-07-11 stsp char *ondisk_path;
6282 818c7501 2019-07-11 stsp
6283 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6284 3093e2d7 2023-02-04 op if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6285 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6286 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6287 818c7501 2019-07-11 stsp
6288 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6289 818c7501 2019-07-11 stsp == -1)
6290 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6291 818c7501 2019-07-11 stsp
6292 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6293 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6294 818c7501 2019-07-11 stsp free(ondisk_path);
6295 818c7501 2019-07-11 stsp if (err)
6296 818c7501 2019-07-11 stsp return err;
6297 818c7501 2019-07-11 stsp
6298 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6299 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6300 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6301 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6302 818c7501 2019-07-11 stsp
6303 818c7501 2019-07-11 stsp return NULL;
6304 818c7501 2019-07-11 stsp }
6305 818c7501 2019-07-11 stsp
6306 818c7501 2019-07-11 stsp const struct got_error *
6307 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6308 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6309 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6310 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6311 818c7501 2019-07-11 stsp {
6312 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6313 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6314 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6315 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6316 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6317 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6318 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6319 818c7501 2019-07-11 stsp
6320 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6321 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6322 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6323 818c7501 2019-07-11 stsp
6324 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6325 818c7501 2019-07-11 stsp if (err)
6326 818c7501 2019-07-11 stsp return err;
6327 818c7501 2019-07-11 stsp
6328 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6329 818c7501 2019-07-11 stsp if (err)
6330 818c7501 2019-07-11 stsp goto done;
6331 818c7501 2019-07-11 stsp
6332 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6333 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6334 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6335 818c7501 2019-07-11 stsp &ok_arg);
6336 818c7501 2019-07-11 stsp if (err)
6337 818c7501 2019-07-11 stsp goto done;
6338 818c7501 2019-07-11 stsp
6339 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6340 818c7501 2019-07-11 stsp if (err)
6341 818c7501 2019-07-11 stsp goto done;
6342 818c7501 2019-07-11 stsp
6343 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6344 818c7501 2019-07-11 stsp if (err)
6345 818c7501 2019-07-11 stsp goto done;
6346 818c7501 2019-07-11 stsp
6347 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6348 818c7501 2019-07-11 stsp if (err)
6349 818c7501 2019-07-11 stsp goto done;
6350 818c7501 2019-07-11 stsp
6351 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6352 818c7501 2019-07-11 stsp 0);
6353 e51d7b55 2020-01-04 stsp if (err)
6354 e51d7b55 2020-01-04 stsp goto done;
6355 e51d7b55 2020-01-04 stsp
6356 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6357 818c7501 2019-07-11 stsp if (err)
6358 818c7501 2019-07-11 stsp goto done;
6359 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6360 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6361 e51d7b55 2020-01-04 stsp goto done;
6362 e51d7b55 2020-01-04 stsp }
6363 818c7501 2019-07-11 stsp
6364 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6365 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6366 818c7501 2019-07-11 stsp if (err)
6367 818c7501 2019-07-11 stsp goto done;
6368 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6369 818c7501 2019-07-11 stsp if (err)
6370 818c7501 2019-07-11 stsp goto done;
6371 818c7501 2019-07-11 stsp
6372 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6373 818c7501 2019-07-11 stsp
6374 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6375 818c7501 2019-07-11 stsp if (err)
6376 818c7501 2019-07-11 stsp goto done;
6377 818c7501 2019-07-11 stsp
6378 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6379 818c7501 2019-07-11 stsp if (err)
6380 818c7501 2019-07-11 stsp goto done;
6381 818c7501 2019-07-11 stsp
6382 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6383 818c7501 2019-07-11 stsp worktree->base_commit_id);
6384 818c7501 2019-07-11 stsp if (err)
6385 818c7501 2019-07-11 stsp goto done;
6386 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6387 818c7501 2019-07-11 stsp if (err)
6388 818c7501 2019-07-11 stsp goto done;
6389 818c7501 2019-07-11 stsp
6390 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6391 818c7501 2019-07-11 stsp if (err)
6392 818c7501 2019-07-11 stsp goto done;
6393 818c7501 2019-07-11 stsp done:
6394 818c7501 2019-07-11 stsp free(fileindex_path);
6395 818c7501 2019-07-11 stsp free(tmp_branch_name);
6396 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6397 818c7501 2019-07-11 stsp free(branch_ref_name);
6398 818c7501 2019-07-11 stsp if (branch_ref)
6399 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6400 818c7501 2019-07-11 stsp if (wt_branch)
6401 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6402 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6403 818c7501 2019-07-11 stsp if (err) {
6404 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6405 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6406 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6407 818c7501 2019-07-11 stsp }
6408 818c7501 2019-07-11 stsp if (*tmp_branch) {
6409 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6410 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6411 818c7501 2019-07-11 stsp }
6412 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6413 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6414 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6415 3e3a69f1 2019-07-25 stsp }
6416 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6417 818c7501 2019-07-11 stsp }
6418 818c7501 2019-07-11 stsp return err;
6419 818c7501 2019-07-11 stsp }
6420 818c7501 2019-07-11 stsp
6421 818c7501 2019-07-11 stsp const struct got_error *
6422 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6423 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6424 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6425 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6426 818c7501 2019-07-11 stsp {
6427 818c7501 2019-07-11 stsp const struct got_error *err;
6428 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6429 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6430 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6431 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6432 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6433 818c7501 2019-07-11 stsp
6434 818c7501 2019-07-11 stsp *commit_id = NULL;
6435 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6436 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6437 3e3a69f1 2019-07-25 stsp *branch = NULL;
6438 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6439 3e3a69f1 2019-07-25 stsp
6440 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6441 3e3a69f1 2019-07-25 stsp if (err)
6442 3e3a69f1 2019-07-25 stsp return err;
6443 818c7501 2019-07-11 stsp
6444 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6445 3e3a69f1 2019-07-25 stsp if (err)
6446 f032f1f7 2019-08-04 stsp goto done;
6447 f032f1f7 2019-08-04 stsp
6448 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6449 f032f1f7 2019-08-04 stsp &have_staged_files);
6450 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6451 f032f1f7 2019-08-04 stsp goto done;
6452 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6453 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6454 3e3a69f1 2019-07-25 stsp goto done;
6455 f032f1f7 2019-08-04 stsp }
6456 3e3a69f1 2019-07-25 stsp
6457 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6458 818c7501 2019-07-11 stsp if (err)
6459 f032f1f7 2019-08-04 stsp goto done;
6460 818c7501 2019-07-11 stsp
6461 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6462 818c7501 2019-07-11 stsp if (err)
6463 818c7501 2019-07-11 stsp goto done;
6464 818c7501 2019-07-11 stsp
6465 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6466 818c7501 2019-07-11 stsp if (err)
6467 818c7501 2019-07-11 stsp goto done;
6468 818c7501 2019-07-11 stsp
6469 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6470 818c7501 2019-07-11 stsp if (err)
6471 818c7501 2019-07-11 stsp goto done;
6472 818c7501 2019-07-11 stsp
6473 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6474 818c7501 2019-07-11 stsp if (err)
6475 818c7501 2019-07-11 stsp goto done;
6476 818c7501 2019-07-11 stsp
6477 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6478 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6479 818c7501 2019-07-11 stsp if (err)
6480 818c7501 2019-07-11 stsp goto done;
6481 818c7501 2019-07-11 stsp
6482 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6483 818c7501 2019-07-11 stsp if (err)
6484 818c7501 2019-07-11 stsp goto done;
6485 818c7501 2019-07-11 stsp
6486 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6487 818c7501 2019-07-11 stsp if (err)
6488 818c7501 2019-07-11 stsp goto done;
6489 818c7501 2019-07-11 stsp
6490 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6491 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6492 818c7501 2019-07-11 stsp if (err)
6493 818c7501 2019-07-11 stsp goto done;
6494 818c7501 2019-07-11 stsp
6495 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6496 818c7501 2019-07-11 stsp if (err)
6497 818c7501 2019-07-11 stsp goto done;
6498 818c7501 2019-07-11 stsp done:
6499 818c7501 2019-07-11 stsp free(commit_ref_name);
6500 818c7501 2019-07-11 stsp free(branch_ref_name);
6501 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6502 818c7501 2019-07-11 stsp if (commit_ref)
6503 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6504 818c7501 2019-07-11 stsp if (branch_ref)
6505 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6506 818c7501 2019-07-11 stsp if (err) {
6507 818c7501 2019-07-11 stsp free(*commit_id);
6508 818c7501 2019-07-11 stsp *commit_id = NULL;
6509 818c7501 2019-07-11 stsp if (*tmp_branch) {
6510 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6511 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6512 818c7501 2019-07-11 stsp }
6513 818c7501 2019-07-11 stsp if (*new_base_branch) {
6514 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6515 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6516 818c7501 2019-07-11 stsp }
6517 818c7501 2019-07-11 stsp if (*branch) {
6518 818c7501 2019-07-11 stsp got_ref_close(*branch);
6519 818c7501 2019-07-11 stsp *branch = NULL;
6520 818c7501 2019-07-11 stsp }
6521 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6522 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6523 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6524 3e3a69f1 2019-07-25 stsp }
6525 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6526 818c7501 2019-07-11 stsp }
6527 818c7501 2019-07-11 stsp return err;
6528 c4296144 2019-05-09 stsp }
6529 818c7501 2019-07-11 stsp
6530 818c7501 2019-07-11 stsp const struct got_error *
6531 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6532 818c7501 2019-07-11 stsp {
6533 818c7501 2019-07-11 stsp const struct got_error *err;
6534 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6535 818c7501 2019-07-11 stsp
6536 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6537 818c7501 2019-07-11 stsp if (err)
6538 818c7501 2019-07-11 stsp return err;
6539 818c7501 2019-07-11 stsp
6540 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6541 818c7501 2019-07-11 stsp free(tmp_branch_name);
6542 818c7501 2019-07-11 stsp return NULL;
6543 818c7501 2019-07-11 stsp }
6544 818c7501 2019-07-11 stsp
6545 818c7501 2019-07-11 stsp static const struct got_error *
6546 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6547 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6548 818c7501 2019-07-11 stsp {
6549 0ebf8283 2019-07-24 stsp *logmsg = arg;
6550 818c7501 2019-07-11 stsp return NULL;
6551 818c7501 2019-07-11 stsp }
6552 818c7501 2019-07-11 stsp
6553 818c7501 2019-07-11 stsp static const struct got_error *
6554 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6555 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6556 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6557 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6558 818c7501 2019-07-11 stsp {
6559 818c7501 2019-07-11 stsp return NULL;
6560 01757395 2019-07-12 stsp }
6561 01757395 2019-07-12 stsp
6562 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6563 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6564 01757395 2019-07-12 stsp void *progress_arg;
6565 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6566 01757395 2019-07-12 stsp };
6567 01757395 2019-07-12 stsp
6568 01757395 2019-07-12 stsp static const struct got_error *
6569 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6570 01757395 2019-07-12 stsp {
6571 01757395 2019-07-12 stsp const struct got_error *err;
6572 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6573 01757395 2019-07-12 stsp char *p;
6574 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6575 01757395 2019-07-12 stsp
6576 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6577 01757395 2019-07-12 stsp if (err)
6578 01757395 2019-07-12 stsp return err;
6579 01757395 2019-07-12 stsp
6580 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6581 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6582 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6583 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6584 01757395 2019-07-12 stsp return NULL;
6585 01757395 2019-07-12 stsp
6586 01757395 2019-07-12 stsp p = strdup(path);
6587 01757395 2019-07-12 stsp if (p == NULL)
6588 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6589 01757395 2019-07-12 stsp
6590 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6591 01757395 2019-07-12 stsp if (err || new == NULL)
6592 01757395 2019-07-12 stsp free(p);
6593 01757395 2019-07-12 stsp return err;
6594 818c7501 2019-07-11 stsp }
6595 818c7501 2019-07-11 stsp
6596 0ebf8283 2019-07-24 stsp static const struct got_error *
6597 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6598 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6599 818c7501 2019-07-11 stsp {
6600 818c7501 2019-07-11 stsp const struct got_error *err;
6601 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6602 818c7501 2019-07-11 stsp
6603 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6604 818c7501 2019-07-11 stsp if (err) {
6605 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6606 818c7501 2019-07-11 stsp goto done;
6607 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6608 818c7501 2019-07-11 stsp if (err)
6609 818c7501 2019-07-11 stsp goto done;
6610 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6611 818c7501 2019-07-11 stsp if (err)
6612 818c7501 2019-07-11 stsp goto done;
6613 de05890f 2020-03-05 stsp } else if (is_rebase) {
6614 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6615 818c7501 2019-07-11 stsp int cmp;
6616 818c7501 2019-07-11 stsp
6617 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6618 818c7501 2019-07-11 stsp if (err)
6619 818c7501 2019-07-11 stsp goto done;
6620 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6621 818c7501 2019-07-11 stsp free(stored_id);
6622 818c7501 2019-07-11 stsp if (cmp != 0) {
6623 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6624 818c7501 2019-07-11 stsp goto done;
6625 818c7501 2019-07-11 stsp }
6626 818c7501 2019-07-11 stsp }
6627 0ebf8283 2019-07-24 stsp done:
6628 0ebf8283 2019-07-24 stsp if (commit_ref)
6629 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6630 0ebf8283 2019-07-24 stsp return err;
6631 0ebf8283 2019-07-24 stsp }
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp static const struct got_error *
6634 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6635 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6636 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6637 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6638 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6639 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6640 0ebf8283 2019-07-24 stsp {
6641 0ebf8283 2019-07-24 stsp const struct got_error *err;
6642 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6643 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6644 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6645 818c7501 2019-07-11 stsp
6646 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6647 0ebf8283 2019-07-24 stsp
6648 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6649 0ebf8283 2019-07-24 stsp if (err)
6650 0ebf8283 2019-07-24 stsp return err;
6651 0ebf8283 2019-07-24 stsp
6652 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6653 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6654 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6655 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6656 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6657 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6658 818c7501 2019-07-11 stsp if (commit_ref)
6659 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6660 818c7501 2019-07-11 stsp return err;
6661 818c7501 2019-07-11 stsp }
6662 818c7501 2019-07-11 stsp
6663 818c7501 2019-07-11 stsp const struct got_error *
6664 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6665 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6666 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6667 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6668 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6669 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6670 818c7501 2019-07-11 stsp {
6671 0ebf8283 2019-07-24 stsp const struct got_error *err;
6672 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6673 0ebf8283 2019-07-24 stsp
6674 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6675 0ebf8283 2019-07-24 stsp if (err)
6676 0ebf8283 2019-07-24 stsp return err;
6677 0ebf8283 2019-07-24 stsp
6678 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6679 0ebf8283 2019-07-24 stsp if (err)
6680 0ebf8283 2019-07-24 stsp goto done;
6681 0ebf8283 2019-07-24 stsp
6682 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6683 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6684 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6685 0ebf8283 2019-07-24 stsp done:
6686 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6687 0ebf8283 2019-07-24 stsp return err;
6688 0ebf8283 2019-07-24 stsp }
6689 0ebf8283 2019-07-24 stsp
6690 0ebf8283 2019-07-24 stsp const struct got_error *
6691 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6692 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6693 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6694 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6695 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6696 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6697 0ebf8283 2019-07-24 stsp {
6698 0ebf8283 2019-07-24 stsp const struct got_error *err;
6699 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6700 0ebf8283 2019-07-24 stsp
6701 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6702 0ebf8283 2019-07-24 stsp if (err)
6703 0ebf8283 2019-07-24 stsp return err;
6704 0ebf8283 2019-07-24 stsp
6705 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6706 0ebf8283 2019-07-24 stsp if (err)
6707 0ebf8283 2019-07-24 stsp goto done;
6708 0ebf8283 2019-07-24 stsp
6709 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6710 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6711 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6712 0ebf8283 2019-07-24 stsp done:
6713 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6714 0ebf8283 2019-07-24 stsp return err;
6715 0ebf8283 2019-07-24 stsp }
6716 0ebf8283 2019-07-24 stsp
6717 0ebf8283 2019-07-24 stsp static const struct got_error *
6718 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6719 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6720 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6721 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6722 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6723 598eac43 2022-07-22 stsp struct got_repository *repo)
6724 0ebf8283 2019-07-24 stsp {
6725 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6726 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6727 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6728 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6729 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6730 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6731 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6732 818c7501 2019-07-11 stsp
6733 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6734 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6735 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6736 a0e95631 2019-07-12 stsp
6737 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6738 818c7501 2019-07-11 stsp
6739 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6740 a0e95631 2019-07-12 stsp if (err)
6741 3e3a69f1 2019-07-25 stsp return err;
6742 a0e95631 2019-07-12 stsp
6743 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6744 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6745 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6746 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6747 01757395 2019-07-12 stsp /*
6748 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6749 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6750 6c13b005 2021-09-02 stsp *
6751 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6752 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6753 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6754 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6755 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6756 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6757 01757395 2019-07-12 stsp */
6758 01757395 2019-07-12 stsp if (merged_paths) {
6759 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6760 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6761 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6762 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6763 f2a9dc41 2019-12-13 tracey 0);
6764 01757395 2019-07-12 stsp if (err)
6765 01757395 2019-07-12 stsp goto done;
6766 01757395 2019-07-12 stsp }
6767 01757395 2019-07-12 stsp } else {
6768 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6769 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6770 01757395 2019-07-12 stsp if (err)
6771 01757395 2019-07-12 stsp goto done;
6772 01757395 2019-07-12 stsp }
6773 a0e95631 2019-07-12 stsp
6774 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6775 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6776 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6777 a0e95631 2019-07-12 stsp if (err)
6778 a0e95631 2019-07-12 stsp goto done;
6779 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6780 a0e95631 2019-07-12 stsp goto done;
6781 ff0d2220 2019-07-11 stsp }
6782 818c7501 2019-07-11 stsp
6783 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6784 edd02c5e 2019-07-11 stsp if (err)
6785 edd02c5e 2019-07-11 stsp goto done;
6786 edd02c5e 2019-07-11 stsp
6787 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6788 818c7501 2019-07-11 stsp if (err)
6789 818c7501 2019-07-11 stsp goto done;
6790 818c7501 2019-07-11 stsp
6791 5943eee2 2019-08-13 stsp if (new_logmsg) {
6792 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6793 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6794 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6795 5943eee2 2019-08-13 stsp goto done;
6796 5943eee2 2019-08-13 stsp }
6797 5943eee2 2019-08-13 stsp } else {
6798 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6799 5943eee2 2019-08-13 stsp if (err)
6800 5943eee2 2019-08-13 stsp goto done;
6801 5943eee2 2019-08-13 stsp }
6802 0ebf8283 2019-07-24 stsp
6803 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6804 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6805 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6806 50e7a649 2022-07-22 stsp committer ? committer :
6807 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6808 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6809 a0e95631 2019-07-12 stsp if (err)
6810 a0e95631 2019-07-12 stsp goto done;
6811 a0e95631 2019-07-12 stsp
6812 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6813 a0e95631 2019-07-12 stsp if (err)
6814 a0e95631 2019-07-12 stsp goto done;
6815 a0e95631 2019-07-12 stsp
6816 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6817 a0e95631 2019-07-12 stsp if (err)
6818 a0e95631 2019-07-12 stsp goto done;
6819 a0e95631 2019-07-12 stsp
6820 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6821 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6822 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6823 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6824 a0e95631 2019-07-12 stsp err = sync_err;
6825 818c7501 2019-07-11 stsp done:
6826 a0e95631 2019-07-12 stsp free(fileindex_path);
6827 a0e95631 2019-07-12 stsp free(head_commit_id);
6828 a0e95631 2019-07-12 stsp if (head_ref)
6829 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6830 818c7501 2019-07-11 stsp if (err) {
6831 818c7501 2019-07-11 stsp free(*new_commit_id);
6832 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6833 818c7501 2019-07-11 stsp }
6834 818c7501 2019-07-11 stsp return err;
6835 818c7501 2019-07-11 stsp }
6836 818c7501 2019-07-11 stsp
6837 818c7501 2019-07-11 stsp const struct got_error *
6838 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6839 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6840 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6841 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6842 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6843 0ebf8283 2019-07-24 stsp {
6844 0ebf8283 2019-07-24 stsp const struct got_error *err;
6845 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6846 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6847 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6848 0ebf8283 2019-07-24 stsp
6849 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6850 0ebf8283 2019-07-24 stsp if (err)
6851 0ebf8283 2019-07-24 stsp return err;
6852 0ebf8283 2019-07-24 stsp
6853 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6854 0ebf8283 2019-07-24 stsp if (err)
6855 0ebf8283 2019-07-24 stsp goto done;
6856 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6857 0ebf8283 2019-07-24 stsp if (err)
6858 0ebf8283 2019-07-24 stsp goto done;
6859 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6860 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6861 0ebf8283 2019-07-24 stsp goto done;
6862 0ebf8283 2019-07-24 stsp }
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6865 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6866 598eac43 2022-07-22 stsp NULL, repo);
6867 0ebf8283 2019-07-24 stsp done:
6868 0ebf8283 2019-07-24 stsp if (commit_ref)
6869 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6870 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6871 0ebf8283 2019-07-24 stsp free(commit_id);
6872 0ebf8283 2019-07-24 stsp return err;
6873 0ebf8283 2019-07-24 stsp }
6874 0ebf8283 2019-07-24 stsp
6875 0ebf8283 2019-07-24 stsp const struct got_error *
6876 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6877 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6878 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6879 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6880 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6881 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6882 0ebf8283 2019-07-24 stsp {
6883 0ebf8283 2019-07-24 stsp const struct got_error *err;
6884 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6885 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6886 0ebf8283 2019-07-24 stsp
6887 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6888 0ebf8283 2019-07-24 stsp if (err)
6889 0ebf8283 2019-07-24 stsp return err;
6890 0ebf8283 2019-07-24 stsp
6891 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6892 0ebf8283 2019-07-24 stsp if (err)
6893 0ebf8283 2019-07-24 stsp goto done;
6894 0ebf8283 2019-07-24 stsp
6895 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6896 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6897 598eac43 2022-07-22 stsp new_logmsg, repo);
6898 0ebf8283 2019-07-24 stsp done:
6899 0ebf8283 2019-07-24 stsp if (commit_ref)
6900 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6901 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6902 0ebf8283 2019-07-24 stsp return err;
6903 0ebf8283 2019-07-24 stsp }
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp const struct got_error *
6906 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6907 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6908 818c7501 2019-07-11 stsp {
6909 3e3a69f1 2019-07-25 stsp if (fileindex)
6910 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6911 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6912 69844fba 2019-07-11 stsp }
6913 69844fba 2019-07-11 stsp
6914 69844fba 2019-07-11 stsp static const struct got_error *
6915 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6916 69844fba 2019-07-11 stsp {
6917 69844fba 2019-07-11 stsp const struct got_error *err;
6918 69844fba 2019-07-11 stsp struct got_reference *ref;
6919 69844fba 2019-07-11 stsp
6920 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6921 69844fba 2019-07-11 stsp if (err) {
6922 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6923 69844fba 2019-07-11 stsp return NULL;
6924 69844fba 2019-07-11 stsp return err;
6925 69844fba 2019-07-11 stsp }
6926 69844fba 2019-07-11 stsp
6927 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6928 69844fba 2019-07-11 stsp got_ref_close(ref);
6929 69844fba 2019-07-11 stsp return err;
6930 818c7501 2019-07-11 stsp }
6931 818c7501 2019-07-11 stsp
6932 69844fba 2019-07-11 stsp static const struct got_error *
6933 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6934 69844fba 2019-07-11 stsp {
6935 69844fba 2019-07-11 stsp const struct got_error *err;
6936 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6937 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6938 69844fba 2019-07-11 stsp
6939 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6940 69844fba 2019-07-11 stsp if (err)
6941 69844fba 2019-07-11 stsp goto done;
6942 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6943 69844fba 2019-07-11 stsp if (err)
6944 69844fba 2019-07-11 stsp goto done;
6945 69844fba 2019-07-11 stsp
6946 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6947 69844fba 2019-07-11 stsp if (err)
6948 69844fba 2019-07-11 stsp goto done;
6949 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6950 69844fba 2019-07-11 stsp if (err)
6951 69844fba 2019-07-11 stsp goto done;
6952 69844fba 2019-07-11 stsp
6953 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6954 69844fba 2019-07-11 stsp if (err)
6955 69844fba 2019-07-11 stsp goto done;
6956 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6957 69844fba 2019-07-11 stsp if (err)
6958 69844fba 2019-07-11 stsp goto done;
6959 69844fba 2019-07-11 stsp
6960 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6961 69844fba 2019-07-11 stsp if (err)
6962 69844fba 2019-07-11 stsp goto done;
6963 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6964 69844fba 2019-07-11 stsp if (err)
6965 69844fba 2019-07-11 stsp goto done;
6966 69844fba 2019-07-11 stsp
6967 69844fba 2019-07-11 stsp done:
6968 69844fba 2019-07-11 stsp free(tmp_branch_name);
6969 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6970 69844fba 2019-07-11 stsp free(branch_ref_name);
6971 69844fba 2019-07-11 stsp free(commit_ref_name);
6972 e600f124 2021-03-21 stsp return err;
6973 e600f124 2021-03-21 stsp }
6974 e600f124 2021-03-21 stsp
6975 336075a4 2022-06-25 op static const struct got_error *
6976 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6977 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6978 e600f124 2021-03-21 stsp {
6979 e600f124 2021-03-21 stsp const struct got_error *err;
6980 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6981 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6982 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6983 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6984 e600f124 2021-03-21 stsp char *refname = NULL;
6985 e600f124 2021-03-21 stsp
6986 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6987 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6988 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6989 e600f124 2021-03-21 stsp branch_name += 11;
6990 e600f124 2021-03-21 stsp
6991 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6992 e600f124 2021-03-21 stsp if (err)
6993 e600f124 2021-03-21 stsp return err;
6994 e600f124 2021-03-21 stsp
6995 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6996 e600f124 2021-03-21 stsp new_id_str) == -1) {
6997 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6998 e600f124 2021-03-21 stsp goto done;
6999 e600f124 2021-03-21 stsp }
7000 e600f124 2021-03-21 stsp
7001 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7002 e600f124 2021-03-21 stsp if (err)
7003 e600f124 2021-03-21 stsp goto done;
7004 e600f124 2021-03-21 stsp
7005 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7006 e600f124 2021-03-21 stsp if (err)
7007 e600f124 2021-03-21 stsp goto done;
7008 e600f124 2021-03-21 stsp
7009 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7010 e600f124 2021-03-21 stsp done:
7011 e600f124 2021-03-21 stsp free(new_id_str);
7012 e600f124 2021-03-21 stsp free(refname);
7013 e600f124 2021-03-21 stsp free(old_commit_id);
7014 e600f124 2021-03-21 stsp if (ref)
7015 e600f124 2021-03-21 stsp got_ref_close(ref);
7016 69844fba 2019-07-11 stsp return err;
7017 69844fba 2019-07-11 stsp }
7018 69844fba 2019-07-11 stsp
7019 818c7501 2019-07-11 stsp const struct got_error *
7020 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7021 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7022 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7023 ef85a376 2023-02-01 mark int create_backup)
7024 818c7501 2019-07-11 stsp {
7025 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7026 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7027 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7028 818c7501 2019-07-11 stsp
7029 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7030 818c7501 2019-07-11 stsp if (err)
7031 818c7501 2019-07-11 stsp return err;
7032 e600f124 2021-03-21 stsp
7033 e600f124 2021-03-21 stsp if (create_backup) {
7034 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7035 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7036 e600f124 2021-03-21 stsp if (err)
7037 e600f124 2021-03-21 stsp goto done;
7038 e600f124 2021-03-21 stsp }
7039 818c7501 2019-07-11 stsp
7040 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7041 818c7501 2019-07-11 stsp if (err)
7042 818c7501 2019-07-11 stsp goto done;
7043 818c7501 2019-07-11 stsp
7044 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7045 818c7501 2019-07-11 stsp if (err)
7046 818c7501 2019-07-11 stsp goto done;
7047 818c7501 2019-07-11 stsp
7048 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7049 818c7501 2019-07-11 stsp if (err)
7050 818c7501 2019-07-11 stsp goto done;
7051 818c7501 2019-07-11 stsp
7052 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7053 a615e0e7 2020-12-16 stsp if (err)
7054 a615e0e7 2020-12-16 stsp goto done;
7055 a615e0e7 2020-12-16 stsp
7056 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7057 a615e0e7 2020-12-16 stsp if (err)
7058 a615e0e7 2020-12-16 stsp goto done;
7059 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7060 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7061 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7062 a615e0e7 2020-12-16 stsp err = sync_err;
7063 818c7501 2019-07-11 stsp done:
7064 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7065 a615e0e7 2020-12-16 stsp free(fileindex_path);
7066 818c7501 2019-07-11 stsp free(new_head_commit_id);
7067 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7068 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7069 818c7501 2019-07-11 stsp err = unlockerr;
7070 818c7501 2019-07-11 stsp return err;
7071 818c7501 2019-07-11 stsp }
7072 818c7501 2019-07-11 stsp
7073 818c7501 2019-07-11 stsp const struct got_error *
7074 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7075 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7076 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7077 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7078 818c7501 2019-07-11 stsp {
7079 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7080 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7081 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7082 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7083 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7084 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7085 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7086 818c7501 2019-07-11 stsp
7087 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7088 818c7501 2019-07-11 stsp if (err)
7089 818c7501 2019-07-11 stsp return err;
7090 818c7501 2019-07-11 stsp
7091 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7092 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7093 a44927cc 2022-04-07 stsp if (err)
7094 a44927cc 2022-04-07 stsp goto done;
7095 a44927cc 2022-04-07 stsp
7096 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7097 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7098 818c7501 2019-07-11 stsp if (err)
7099 818c7501 2019-07-11 stsp goto done;
7100 818c7501 2019-07-11 stsp
7101 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7102 818c7501 2019-07-11 stsp if (err)
7103 818c7501 2019-07-11 stsp goto done;
7104 818c7501 2019-07-11 stsp
7105 818c7501 2019-07-11 stsp /*
7106 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7107 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7108 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7109 818c7501 2019-07-11 stsp */
7110 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7111 818c7501 2019-07-11 stsp if (err)
7112 818c7501 2019-07-11 stsp goto done;
7113 818c7501 2019-07-11 stsp
7114 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7115 818c7501 2019-07-11 stsp if (err)
7116 818c7501 2019-07-11 stsp goto done;
7117 818c7501 2019-07-11 stsp
7118 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7119 a44927cc 2022-04-07 stsp worktree->path_prefix);
7120 ca355955 2019-07-12 stsp if (err)
7121 ca355955 2019-07-12 stsp goto done;
7122 ca355955 2019-07-12 stsp
7123 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7124 ca355955 2019-07-12 stsp if (err)
7125 ca355955 2019-07-12 stsp goto done;
7126 ca355955 2019-07-12 stsp
7127 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7128 818c7501 2019-07-11 stsp if (err)
7129 818c7501 2019-07-11 stsp goto done;
7130 818c7501 2019-07-11 stsp
7131 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7132 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7133 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7134 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7135 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7136 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7137 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7138 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7139 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7140 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7141 55bd499d 2019-07-12 stsp if (err)
7142 1f1abb7e 2019-08-08 stsp goto sync;
7143 55bd499d 2019-07-12 stsp
7144 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7145 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7146 ca355955 2019-07-12 stsp sync:
7147 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7148 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7149 ca355955 2019-07-12 stsp err = sync_err;
7150 818c7501 2019-07-11 stsp done:
7151 818c7501 2019-07-11 stsp got_ref_close(resolved);
7152 a3a2faf2 2019-07-12 stsp free(tree_id);
7153 818c7501 2019-07-11 stsp free(commit_id);
7154 a44927cc 2022-04-07 stsp if (commit)
7155 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7156 0ebf8283 2019-07-24 stsp if (fileindex)
7157 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7158 0ebf8283 2019-07-24 stsp free(fileindex_path);
7159 0ebf8283 2019-07-24 stsp
7160 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7161 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7162 0ebf8283 2019-07-24 stsp err = unlockerr;
7163 0ebf8283 2019-07-24 stsp return err;
7164 0ebf8283 2019-07-24 stsp }
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp const struct got_error *
7167 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7168 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7169 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7170 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7171 0ebf8283 2019-07-24 stsp {
7172 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7173 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7174 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7175 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7176 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7177 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7178 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7179 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7180 0ebf8283 2019-07-24 stsp
7181 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7182 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7183 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7184 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7185 0ebf8283 2019-07-24 stsp
7186 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7187 0ebf8283 2019-07-24 stsp if (err)
7188 0ebf8283 2019-07-24 stsp return err;
7189 0ebf8283 2019-07-24 stsp
7190 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7191 0ebf8283 2019-07-24 stsp if (err)
7192 0ebf8283 2019-07-24 stsp goto done;
7193 0ebf8283 2019-07-24 stsp
7194 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7195 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7196 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7197 0ebf8283 2019-07-24 stsp &ok_arg);
7198 0ebf8283 2019-07-24 stsp if (err)
7199 0ebf8283 2019-07-24 stsp goto done;
7200 0ebf8283 2019-07-24 stsp
7201 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7202 0ebf8283 2019-07-24 stsp if (err)
7203 0ebf8283 2019-07-24 stsp goto done;
7204 0ebf8283 2019-07-24 stsp
7205 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7206 0ebf8283 2019-07-24 stsp if (err)
7207 0ebf8283 2019-07-24 stsp goto done;
7208 0ebf8283 2019-07-24 stsp
7209 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7210 0ebf8283 2019-07-24 stsp worktree);
7211 0ebf8283 2019-07-24 stsp if (err)
7212 0ebf8283 2019-07-24 stsp goto done;
7213 0ebf8283 2019-07-24 stsp
7214 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7215 0ebf8283 2019-07-24 stsp 0);
7216 0ebf8283 2019-07-24 stsp if (err)
7217 0ebf8283 2019-07-24 stsp goto done;
7218 0ebf8283 2019-07-24 stsp
7219 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7220 0ebf8283 2019-07-24 stsp if (err)
7221 0ebf8283 2019-07-24 stsp goto done;
7222 0ebf8283 2019-07-24 stsp
7223 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7224 0ebf8283 2019-07-24 stsp if (err)
7225 0ebf8283 2019-07-24 stsp goto done;
7226 0ebf8283 2019-07-24 stsp
7227 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7228 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7229 0ebf8283 2019-07-24 stsp if (err)
7230 0ebf8283 2019-07-24 stsp goto done;
7231 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7232 0ebf8283 2019-07-24 stsp if (err)
7233 0ebf8283 2019-07-24 stsp goto done;
7234 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7235 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7236 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7237 0ebf8283 2019-07-24 stsp goto done;
7238 0ebf8283 2019-07-24 stsp }
7239 0ebf8283 2019-07-24 stsp
7240 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7241 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7242 0ebf8283 2019-07-24 stsp if (err)
7243 0ebf8283 2019-07-24 stsp goto done;
7244 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7245 0ebf8283 2019-07-24 stsp if (err)
7246 0ebf8283 2019-07-24 stsp goto done;
7247 0ebf8283 2019-07-24 stsp
7248 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7249 0ebf8283 2019-07-24 stsp if (err)
7250 0ebf8283 2019-07-24 stsp goto done;
7251 0ebf8283 2019-07-24 stsp done:
7252 0ebf8283 2019-07-24 stsp free(fileindex_path);
7253 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7254 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7255 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7256 0ebf8283 2019-07-24 stsp if (wt_branch)
7257 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7258 0ebf8283 2019-07-24 stsp if (err) {
7259 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7260 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7261 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7262 0ebf8283 2019-07-24 stsp }
7263 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7264 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7265 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7266 0ebf8283 2019-07-24 stsp }
7267 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7268 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7269 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7270 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7271 3e3a69f1 2019-07-25 stsp }
7272 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7273 0ebf8283 2019-07-24 stsp }
7274 0ebf8283 2019-07-24 stsp return err;
7275 0ebf8283 2019-07-24 stsp }
7276 0ebf8283 2019-07-24 stsp
7277 0ebf8283 2019-07-24 stsp const struct got_error *
7278 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7279 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7280 0ebf8283 2019-07-24 stsp {
7281 3e3a69f1 2019-07-25 stsp if (fileindex)
7282 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7283 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7284 0ebf8283 2019-07-24 stsp }
7285 0ebf8283 2019-07-24 stsp
7286 0ebf8283 2019-07-24 stsp const struct got_error *
7287 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7288 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7289 0ebf8283 2019-07-24 stsp {
7290 0ebf8283 2019-07-24 stsp const struct got_error *err;
7291 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7292 0ebf8283 2019-07-24 stsp
7293 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7294 0ebf8283 2019-07-24 stsp if (err)
7295 0ebf8283 2019-07-24 stsp return err;
7296 0ebf8283 2019-07-24 stsp
7297 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7298 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7299 0ebf8283 2019-07-24 stsp return NULL;
7300 0ebf8283 2019-07-24 stsp }
7301 0ebf8283 2019-07-24 stsp
7302 0ebf8283 2019-07-24 stsp const struct got_error *
7303 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7304 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7305 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7306 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7307 0ebf8283 2019-07-24 stsp {
7308 0ebf8283 2019-07-24 stsp const struct got_error *err;
7309 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7310 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7311 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7312 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7313 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7314 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7315 0ebf8283 2019-07-24 stsp
7316 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7317 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7318 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7319 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7320 0ebf8283 2019-07-24 stsp
7321 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7322 3e3a69f1 2019-07-25 stsp if (err)
7323 3e3a69f1 2019-07-25 stsp return err;
7324 3e3a69f1 2019-07-25 stsp
7325 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7326 3e3a69f1 2019-07-25 stsp if (err)
7327 f032f1f7 2019-08-04 stsp goto done;
7328 f032f1f7 2019-08-04 stsp
7329 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7330 f032f1f7 2019-08-04 stsp &have_staged_files);
7331 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7332 f032f1f7 2019-08-04 stsp goto done;
7333 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7334 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7335 3e3a69f1 2019-07-25 stsp goto done;
7336 f032f1f7 2019-08-04 stsp }
7337 3e3a69f1 2019-07-25 stsp
7338 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7339 0ebf8283 2019-07-24 stsp if (err)
7340 f032f1f7 2019-08-04 stsp goto done;
7341 0ebf8283 2019-07-24 stsp
7342 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7343 0ebf8283 2019-07-24 stsp if (err)
7344 0ebf8283 2019-07-24 stsp goto done;
7345 0ebf8283 2019-07-24 stsp
7346 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7347 0ebf8283 2019-07-24 stsp if (err)
7348 0ebf8283 2019-07-24 stsp goto done;
7349 0ebf8283 2019-07-24 stsp
7350 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7351 0ebf8283 2019-07-24 stsp worktree);
7352 0ebf8283 2019-07-24 stsp if (err)
7353 0ebf8283 2019-07-24 stsp goto done;
7354 0ebf8283 2019-07-24 stsp
7355 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7356 0ebf8283 2019-07-24 stsp if (err)
7357 0ebf8283 2019-07-24 stsp goto done;
7358 0ebf8283 2019-07-24 stsp
7359 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7360 0ebf8283 2019-07-24 stsp if (err)
7361 0ebf8283 2019-07-24 stsp goto done;
7362 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7363 0ebf8283 2019-07-24 stsp if (err)
7364 0ebf8283 2019-07-24 stsp goto done;
7365 0ebf8283 2019-07-24 stsp
7366 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7367 0ebf8283 2019-07-24 stsp if (err)
7368 0ebf8283 2019-07-24 stsp goto done;
7369 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7370 0ebf8283 2019-07-24 stsp if (err)
7371 0ebf8283 2019-07-24 stsp goto done;
7372 0ebf8283 2019-07-24 stsp
7373 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7374 0ebf8283 2019-07-24 stsp if (err)
7375 0ebf8283 2019-07-24 stsp goto done;
7376 0ebf8283 2019-07-24 stsp done:
7377 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7378 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7379 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7380 0ebf8283 2019-07-24 stsp if (commit_ref)
7381 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7382 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7383 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7384 0ebf8283 2019-07-24 stsp if (err) {
7385 0ebf8283 2019-07-24 stsp free(*commit_id);
7386 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7387 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7388 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7389 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7390 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7391 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7392 0ebf8283 2019-07-24 stsp }
7393 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7394 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7395 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7396 3e3a69f1 2019-07-25 stsp }
7397 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7398 0ebf8283 2019-07-24 stsp }
7399 0ebf8283 2019-07-24 stsp return err;
7400 0ebf8283 2019-07-24 stsp }
7401 0ebf8283 2019-07-24 stsp
7402 0ebf8283 2019-07-24 stsp static const struct got_error *
7403 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 0ebf8283 2019-07-24 stsp {
7405 0ebf8283 2019-07-24 stsp const struct got_error *err;
7406 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7407 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 0ebf8283 2019-07-24 stsp
7409 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7410 0ebf8283 2019-07-24 stsp if (err)
7411 0ebf8283 2019-07-24 stsp goto done;
7412 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7413 0ebf8283 2019-07-24 stsp if (err)
7414 0ebf8283 2019-07-24 stsp goto done;
7415 0ebf8283 2019-07-24 stsp
7416 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7417 0ebf8283 2019-07-24 stsp worktree);
7418 0ebf8283 2019-07-24 stsp if (err)
7419 0ebf8283 2019-07-24 stsp goto done;
7420 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7421 0ebf8283 2019-07-24 stsp if (err)
7422 0ebf8283 2019-07-24 stsp goto done;
7423 0ebf8283 2019-07-24 stsp
7424 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7425 0ebf8283 2019-07-24 stsp if (err)
7426 0ebf8283 2019-07-24 stsp goto done;
7427 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7428 0ebf8283 2019-07-24 stsp if (err)
7429 0ebf8283 2019-07-24 stsp goto done;
7430 0ebf8283 2019-07-24 stsp
7431 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7432 0ebf8283 2019-07-24 stsp if (err)
7433 0ebf8283 2019-07-24 stsp goto done;
7434 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7435 0ebf8283 2019-07-24 stsp if (err)
7436 0ebf8283 2019-07-24 stsp goto done;
7437 0ebf8283 2019-07-24 stsp done:
7438 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7439 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7440 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7441 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7442 0ebf8283 2019-07-24 stsp return err;
7443 0ebf8283 2019-07-24 stsp }
7444 0ebf8283 2019-07-24 stsp
7445 0ebf8283 2019-07-24 stsp const struct got_error *
7446 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7447 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7448 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7449 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7450 0ebf8283 2019-07-24 stsp {
7451 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7452 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7453 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7454 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7455 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7456 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7457 0ebf8283 2019-07-24 stsp
7458 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7459 0ebf8283 2019-07-24 stsp if (err)
7460 0ebf8283 2019-07-24 stsp return err;
7461 0ebf8283 2019-07-24 stsp
7462 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7463 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7464 a44927cc 2022-04-07 stsp if (err)
7465 a44927cc 2022-04-07 stsp goto done;
7466 a44927cc 2022-04-07 stsp
7467 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7468 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7469 0ebf8283 2019-07-24 stsp if (err)
7470 0ebf8283 2019-07-24 stsp goto done;
7471 0ebf8283 2019-07-24 stsp
7472 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
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 = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7477 0ebf8283 2019-07-24 stsp if (err)
7478 0ebf8283 2019-07-24 stsp goto done;
7479 0ebf8283 2019-07-24 stsp
7480 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7481 0ebf8283 2019-07-24 stsp worktree->path_prefix);
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 = delete_histedit_refs(worktree, repo);
7486 0ebf8283 2019-07-24 stsp if (err)
7487 0ebf8283 2019-07-24 stsp goto done;
7488 0ebf8283 2019-07-24 stsp
7489 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7490 0ebf8283 2019-07-24 stsp if (err)
7491 0ebf8283 2019-07-24 stsp goto done;
7492 0ebf8283 2019-07-24 stsp
7493 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7494 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7495 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7496 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7497 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7498 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7499 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7500 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7501 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7502 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7503 0ebf8283 2019-07-24 stsp if (err)
7504 1f1abb7e 2019-08-08 stsp goto sync;
7505 0ebf8283 2019-07-24 stsp
7506 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7507 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7508 0ebf8283 2019-07-24 stsp sync:
7509 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7510 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7511 0ebf8283 2019-07-24 stsp err = sync_err;
7512 0ebf8283 2019-07-24 stsp done:
7513 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7514 0ebf8283 2019-07-24 stsp free(tree_id);
7515 818c7501 2019-07-11 stsp free(fileindex_path);
7516 818c7501 2019-07-11 stsp
7517 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7518 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7519 818c7501 2019-07-11 stsp err = unlockerr;
7520 818c7501 2019-07-11 stsp return err;
7521 818c7501 2019-07-11 stsp }
7522 0ebf8283 2019-07-24 stsp
7523 0ebf8283 2019-07-24 stsp const struct got_error *
7524 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7525 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7526 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7527 0ebf8283 2019-07-24 stsp {
7528 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7529 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7530 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7531 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7532 0ebf8283 2019-07-24 stsp
7533 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7534 0ebf8283 2019-07-24 stsp if (err)
7535 0ebf8283 2019-07-24 stsp return err;
7536 0ebf8283 2019-07-24 stsp
7537 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7538 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7539 e600f124 2021-03-21 stsp if (err)
7540 e600f124 2021-03-21 stsp goto done;
7541 e600f124 2021-03-21 stsp
7542 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7543 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7544 0ebf8283 2019-07-24 stsp if (err)
7545 0ebf8283 2019-07-24 stsp goto done;
7546 0ebf8283 2019-07-24 stsp
7547 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7548 0ebf8283 2019-07-24 stsp if (err)
7549 0ebf8283 2019-07-24 stsp goto done;
7550 0ebf8283 2019-07-24 stsp
7551 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7552 0ebf8283 2019-07-24 stsp if (err)
7553 0ebf8283 2019-07-24 stsp goto done;
7554 0ebf8283 2019-07-24 stsp
7555 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7556 0ebf8283 2019-07-24 stsp if (err)
7557 0ebf8283 2019-07-24 stsp goto done;
7558 0ebf8283 2019-07-24 stsp
7559 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7560 a615e0e7 2020-12-16 stsp if (err)
7561 a615e0e7 2020-12-16 stsp goto done;
7562 a615e0e7 2020-12-16 stsp
7563 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7564 a615e0e7 2020-12-16 stsp if (err)
7565 a615e0e7 2020-12-16 stsp goto done;
7566 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7567 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7568 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7569 a615e0e7 2020-12-16 stsp err = sync_err;
7570 0ebf8283 2019-07-24 stsp done:
7571 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7572 a615e0e7 2020-12-16 stsp free(fileindex_path);
7573 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7574 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7575 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7576 0ebf8283 2019-07-24 stsp err = unlockerr;
7577 0ebf8283 2019-07-24 stsp return err;
7578 0ebf8283 2019-07-24 stsp }
7579 0ebf8283 2019-07-24 stsp
7580 0ebf8283 2019-07-24 stsp const struct got_error *
7581 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7582 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7583 0ebf8283 2019-07-24 stsp {
7584 0ebf8283 2019-07-24 stsp const struct got_error *err;
7585 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7586 0ebf8283 2019-07-24 stsp
7587 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7588 0ebf8283 2019-07-24 stsp if (err)
7589 0ebf8283 2019-07-24 stsp return err;
7590 0ebf8283 2019-07-24 stsp
7591 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7592 0ebf8283 2019-07-24 stsp if (err)
7593 0ebf8283 2019-07-24 stsp goto done;
7594 0ebf8283 2019-07-24 stsp
7595 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7596 0ebf8283 2019-07-24 stsp done:
7597 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7598 2822a352 2019-10-15 stsp return err;
7599 2822a352 2019-10-15 stsp }
7600 2822a352 2019-10-15 stsp
7601 2822a352 2019-10-15 stsp const struct got_error *
7602 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7603 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7604 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7605 2822a352 2019-10-15 stsp struct got_repository *repo)
7606 2822a352 2019-10-15 stsp {
7607 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7608 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7609 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7610 2822a352 2019-10-15 stsp
7611 2822a352 2019-10-15 stsp *fileindex = NULL;
7612 2822a352 2019-10-15 stsp *branch_ref = NULL;
7613 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7614 2822a352 2019-10-15 stsp
7615 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7616 2822a352 2019-10-15 stsp if (err)
7617 2822a352 2019-10-15 stsp return err;
7618 2822a352 2019-10-15 stsp
7619 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7620 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7621 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7622 2822a352 2019-10-15 stsp "update -b or different branch name required");
7623 2822a352 2019-10-15 stsp goto done;
7624 2822a352 2019-10-15 stsp }
7625 2822a352 2019-10-15 stsp
7626 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7627 2822a352 2019-10-15 stsp if (err)
7628 2822a352 2019-10-15 stsp goto done;
7629 2822a352 2019-10-15 stsp
7630 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7631 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7632 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7633 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7634 2822a352 2019-10-15 stsp &ok_arg);
7635 2822a352 2019-10-15 stsp if (err)
7636 2822a352 2019-10-15 stsp goto done;
7637 2822a352 2019-10-15 stsp
7638 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7639 2822a352 2019-10-15 stsp if (err)
7640 2822a352 2019-10-15 stsp goto done;
7641 2822a352 2019-10-15 stsp
7642 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7643 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7644 2822a352 2019-10-15 stsp done:
7645 2822a352 2019-10-15 stsp if (err) {
7646 2822a352 2019-10-15 stsp if (*branch_ref) {
7647 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7648 2822a352 2019-10-15 stsp *branch_ref = NULL;
7649 2822a352 2019-10-15 stsp }
7650 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7651 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7652 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7653 2822a352 2019-10-15 stsp }
7654 2822a352 2019-10-15 stsp if (*fileindex) {
7655 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7656 2822a352 2019-10-15 stsp *fileindex = NULL;
7657 2822a352 2019-10-15 stsp }
7658 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7659 2822a352 2019-10-15 stsp }
7660 0cb83759 2019-08-03 stsp return err;
7661 0cb83759 2019-08-03 stsp }
7662 0cb83759 2019-08-03 stsp
7663 2822a352 2019-10-15 stsp const struct got_error *
7664 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7665 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7666 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7667 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7668 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7669 2822a352 2019-10-15 stsp {
7670 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7671 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7672 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7673 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7674 2822a352 2019-10-15 stsp
7675 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7676 2822a352 2019-10-15 stsp if (err)
7677 2822a352 2019-10-15 stsp goto done;
7678 2822a352 2019-10-15 stsp
7679 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7680 2822a352 2019-10-15 stsp if (err)
7681 2822a352 2019-10-15 stsp goto done;
7682 2822a352 2019-10-15 stsp
7683 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7684 a44927cc 2022-04-07 stsp if (err)
7685 a44927cc 2022-04-07 stsp goto done;
7686 a44927cc 2022-04-07 stsp
7687 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7688 2822a352 2019-10-15 stsp worktree->path_prefix);
7689 2822a352 2019-10-15 stsp if (err)
7690 2822a352 2019-10-15 stsp goto done;
7691 2822a352 2019-10-15 stsp
7692 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7693 2822a352 2019-10-15 stsp if (err)
7694 2822a352 2019-10-15 stsp goto done;
7695 2822a352 2019-10-15 stsp
7696 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7697 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7698 2822a352 2019-10-15 stsp if (err)
7699 2822a352 2019-10-15 stsp goto sync;
7700 2822a352 2019-10-15 stsp
7701 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7702 2822a352 2019-10-15 stsp if (err)
7703 2822a352 2019-10-15 stsp goto sync;
7704 2822a352 2019-10-15 stsp
7705 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7706 6e210706 2021-01-22 stsp if (err)
7707 6e210706 2021-01-22 stsp goto sync;
7708 6e210706 2021-01-22 stsp
7709 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7710 2822a352 2019-10-15 stsp sync:
7711 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7712 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7713 2822a352 2019-10-15 stsp err = sync_err;
7714 2822a352 2019-10-15 stsp
7715 2822a352 2019-10-15 stsp done:
7716 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7717 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7718 2822a352 2019-10-15 stsp err = unlockerr;
7719 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7720 2822a352 2019-10-15 stsp
7721 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7722 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7723 2822a352 2019-10-15 stsp err = unlockerr;
7724 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7725 2822a352 2019-10-15 stsp
7726 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7727 2822a352 2019-10-15 stsp free(fileindex_path);
7728 2822a352 2019-10-15 stsp free(tree_id);
7729 a44927cc 2022-04-07 stsp if (commit)
7730 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7731 2822a352 2019-10-15 stsp
7732 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7733 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7734 2822a352 2019-10-15 stsp err = unlockerr;
7735 2822a352 2019-10-15 stsp return err;
7736 2822a352 2019-10-15 stsp }
7737 2822a352 2019-10-15 stsp
7738 2822a352 2019-10-15 stsp const struct got_error *
7739 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7740 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7741 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7742 2822a352 2019-10-15 stsp {
7743 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7744 8b692cd0 2019-10-21 stsp
7745 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7746 8b692cd0 2019-10-21 stsp
7747 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7748 8b692cd0 2019-10-21 stsp
7749 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7750 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7751 8b692cd0 2019-10-21 stsp err = unlockerr;
7752 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7753 8b692cd0 2019-10-21 stsp
7754 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7755 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7756 8b692cd0 2019-10-21 stsp err = unlockerr;
7757 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7758 f259c4c1 2021-09-24 stsp
7759 f259c4c1 2021-09-24 stsp return err;
7760 f259c4c1 2021-09-24 stsp }
7761 f259c4c1 2021-09-24 stsp
7762 f259c4c1 2021-09-24 stsp const struct got_error *
7763 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7764 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7765 f259c4c1 2021-09-24 stsp {
7766 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7767 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7768 f259c4c1 2021-09-24 stsp
7769 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7770 f259c4c1 2021-09-24 stsp if (err)
7771 f259c4c1 2021-09-24 stsp goto done;
7772 8b692cd0 2019-10-21 stsp
7773 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7774 f259c4c1 2021-09-24 stsp
7775 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7776 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7777 f259c4c1 2021-09-24 stsp err = sync_err;
7778 f259c4c1 2021-09-24 stsp done:
7779 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7780 f259c4c1 2021-09-24 stsp free(fileindex_path);
7781 8b692cd0 2019-10-21 stsp return err;
7782 2822a352 2019-10-15 stsp }
7783 2822a352 2019-10-15 stsp
7784 f259c4c1 2021-09-24 stsp static const struct got_error *
7785 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7786 f259c4c1 2021-09-24 stsp {
7787 f259c4c1 2021-09-24 stsp const struct got_error *err;
7788 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7789 f259c4c1 2021-09-24 stsp
7790 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7791 f259c4c1 2021-09-24 stsp if (err)
7792 f259c4c1 2021-09-24 stsp goto done;
7793 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7794 f259c4c1 2021-09-24 stsp if (err)
7795 f259c4c1 2021-09-24 stsp goto done;
7796 f259c4c1 2021-09-24 stsp
7797 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7798 f259c4c1 2021-09-24 stsp if (err)
7799 f259c4c1 2021-09-24 stsp goto done;
7800 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7801 f259c4c1 2021-09-24 stsp if (err)
7802 f259c4c1 2021-09-24 stsp goto done;
7803 f259c4c1 2021-09-24 stsp
7804 f259c4c1 2021-09-24 stsp done:
7805 f259c4c1 2021-09-24 stsp free(branch_refname);
7806 f259c4c1 2021-09-24 stsp free(commit_refname);
7807 f259c4c1 2021-09-24 stsp return err;
7808 f259c4c1 2021-09-24 stsp }
7809 f259c4c1 2021-09-24 stsp
7810 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7811 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7812 f259c4c1 2021-09-24 stsp const char *branch_name;
7813 f259c4c1 2021-09-24 stsp };
7814 f259c4c1 2021-09-24 stsp
7815 f259c4c1 2021-09-24 stsp static const struct got_error *
7816 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7817 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7818 f259c4c1 2021-09-24 stsp {
7819 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7820 f259c4c1 2021-09-24 stsp
7821 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7822 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7823 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7824 f259c4c1 2021-09-24 stsp
7825 f259c4c1 2021-09-24 stsp return NULL;
7826 f259c4c1 2021-09-24 stsp }
7827 f259c4c1 2021-09-24 stsp
7828 f259c4c1 2021-09-24 stsp
7829 f259c4c1 2021-09-24 stsp const struct got_error *
7830 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7831 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7832 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7833 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7834 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7835 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7836 f259c4c1 2021-09-24 stsp {
7837 f259c4c1 2021-09-24 stsp const struct got_error *err;
7838 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7839 f259c4c1 2021-09-24 stsp
7840 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7841 f259c4c1 2021-09-24 stsp if (err)
7842 f259c4c1 2021-09-24 stsp goto done;
7843 f259c4c1 2021-09-24 stsp
7844 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7845 f259c4c1 2021-09-24 stsp worktree);
7846 f259c4c1 2021-09-24 stsp if (err)
7847 f259c4c1 2021-09-24 stsp goto done;
7848 f259c4c1 2021-09-24 stsp
7849 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7850 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7851 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7852 f259c4c1 2021-09-24 stsp done:
7853 f259c4c1 2021-09-24 stsp free(fileindex_path);
7854 f259c4c1 2021-09-24 stsp return err;
7855 f259c4c1 2021-09-24 stsp }
7856 f259c4c1 2021-09-24 stsp
7857 f259c4c1 2021-09-24 stsp const struct got_error *
7858 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7859 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7860 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7861 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7862 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7863 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7864 0ff8d236 2021-09-28 stsp
7865 f259c4c1 2021-09-24 stsp {
7866 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7867 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7868 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7869 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7870 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7871 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7872 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7873 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7874 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7875 f259c4c1 2021-09-24 stsp
7876 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7877 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7878 f259c4c1 2021-09-24 stsp
7879 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7880 f259c4c1 2021-09-24 stsp
7881 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7882 f259c4c1 2021-09-24 stsp if (err)
7883 f259c4c1 2021-09-24 stsp goto done;
7884 f259c4c1 2021-09-24 stsp
7885 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7886 f259c4c1 2021-09-24 stsp if (err)
7887 f259c4c1 2021-09-24 stsp goto done;
7888 f259c4c1 2021-09-24 stsp
7889 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7890 f259c4c1 2021-09-24 stsp if (err)
7891 f259c4c1 2021-09-24 stsp goto done;
7892 f259c4c1 2021-09-24 stsp
7893 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7894 f259c4c1 2021-09-24 stsp &have_staged_files);
7895 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7896 f259c4c1 2021-09-24 stsp goto done;
7897 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7898 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7899 f259c4c1 2021-09-24 stsp goto done;
7900 f259c4c1 2021-09-24 stsp }
7901 f259c4c1 2021-09-24 stsp
7902 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7903 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7904 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7905 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7906 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7907 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7908 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7909 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7910 f259c4c1 2021-09-24 stsp if (err)
7911 f259c4c1 2021-09-24 stsp goto done;
7912 f259c4c1 2021-09-24 stsp
7913 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7914 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7915 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7916 f259c4c1 2021-09-24 stsp goto done;
7917 f259c4c1 2021-09-24 stsp }
7918 f259c4c1 2021-09-24 stsp
7919 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7920 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7921 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7922 f259c4c1 2021-09-24 stsp
7923 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7924 f259c4c1 2021-09-24 stsp ct_path++;
7925 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7926 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7927 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7928 f259c4c1 2021-09-24 stsp if (err)
7929 f259c4c1 2021-09-24 stsp goto done;
7930 f259c4c1 2021-09-24 stsp
7931 f259c4c1 2021-09-24 stsp }
7932 f259c4c1 2021-09-24 stsp
7933 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7934 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7935 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7936 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
7937 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7938 f259c4c1 2021-09-24 stsp if (err)
7939 f259c4c1 2021-09-24 stsp goto done;
7940 f259c4c1 2021-09-24 stsp
7941 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7942 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7943 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7944 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7945 f259c4c1 2021-09-24 stsp err = sync_err;
7946 f259c4c1 2021-09-24 stsp done:
7947 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7948 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7949 d8bacb93 2023-01-10 mark
7950 f259c4c1 2021-09-24 stsp free_commitable(ct);
7951 f259c4c1 2021-09-24 stsp }
7952 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7953 f259c4c1 2021-09-24 stsp free(fileindex_path);
7954 f259c4c1 2021-09-24 stsp return err;
7955 f259c4c1 2021-09-24 stsp }
7956 f259c4c1 2021-09-24 stsp
7957 f259c4c1 2021-09-24 stsp const struct got_error *
7958 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7959 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7960 f259c4c1 2021-09-24 stsp {
7961 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7962 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7963 f259c4c1 2021-09-24 stsp
7964 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7965 f259c4c1 2021-09-24 stsp if (err)
7966 f259c4c1 2021-09-24 stsp goto done;
7967 f259c4c1 2021-09-24 stsp
7968 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7969 f259c4c1 2021-09-24 stsp if (err)
7970 f259c4c1 2021-09-24 stsp goto done;
7971 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7972 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7973 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7974 f259c4c1 2021-09-24 stsp err = sync_err;
7975 f259c4c1 2021-09-24 stsp done:
7976 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7977 f259c4c1 2021-09-24 stsp free(fileindex_path);
7978 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7979 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7980 f259c4c1 2021-09-24 stsp err = unlockerr;
7981 f259c4c1 2021-09-24 stsp return err;
7982 f259c4c1 2021-09-24 stsp }
7983 f259c4c1 2021-09-24 stsp
7984 f259c4c1 2021-09-24 stsp const struct got_error *
7985 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7986 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7987 f259c4c1 2021-09-24 stsp {
7988 f259c4c1 2021-09-24 stsp const struct got_error *err;
7989 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7990 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7991 f259c4c1 2021-09-24 stsp
7992 f259c4c1 2021-09-24 stsp *in_progress = 0;
7993 f259c4c1 2021-09-24 stsp
7994 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7995 f259c4c1 2021-09-24 stsp if (err)
7996 f259c4c1 2021-09-24 stsp return err;
7997 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7998 793fcac3 2021-09-24 stsp free(branch_refname);
7999 f259c4c1 2021-09-24 stsp if (err) {
8000 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8001 f259c4c1 2021-09-24 stsp return err;
8002 f259c4c1 2021-09-24 stsp } else
8003 f259c4c1 2021-09-24 stsp *in_progress = 1;
8004 f259c4c1 2021-09-24 stsp
8005 f259c4c1 2021-09-24 stsp return NULL;
8006 f259c4c1 2021-09-24 stsp }
8007 f259c4c1 2021-09-24 stsp
8008 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8009 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8010 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8011 f259c4c1 2021-09-24 stsp {
8012 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8013 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8014 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8015 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8016 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8017 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8018 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8019 f259c4c1 2021-09-24 stsp
8020 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8021 f259c4c1 2021-09-24 stsp
8022 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8023 f259c4c1 2021-09-24 stsp if (err)
8024 f259c4c1 2021-09-24 stsp return err;
8025 f259c4c1 2021-09-24 stsp
8026 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8027 f259c4c1 2021-09-24 stsp if (err)
8028 f259c4c1 2021-09-24 stsp goto done;
8029 f259c4c1 2021-09-24 stsp
8030 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8031 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8032 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8033 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8034 f259c4c1 2021-09-24 stsp &ok_arg);
8035 f259c4c1 2021-09-24 stsp if (err)
8036 f259c4c1 2021-09-24 stsp goto done;
8037 f259c4c1 2021-09-24 stsp
8038 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8039 f259c4c1 2021-09-24 stsp if (err)
8040 f259c4c1 2021-09-24 stsp return err;
8041 f259c4c1 2021-09-24 stsp
8042 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8043 f259c4c1 2021-09-24 stsp if (err)
8044 f259c4c1 2021-09-24 stsp return err;
8045 f259c4c1 2021-09-24 stsp
8046 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8047 f259c4c1 2021-09-24 stsp 0);
8048 f259c4c1 2021-09-24 stsp if (err)
8049 f259c4c1 2021-09-24 stsp goto done;
8050 f259c4c1 2021-09-24 stsp
8051 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8052 f259c4c1 2021-09-24 stsp if (err)
8053 f259c4c1 2021-09-24 stsp goto done;
8054 f259c4c1 2021-09-24 stsp
8055 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8056 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8057 f259c4c1 2021-09-24 stsp goto done;
8058 f259c4c1 2021-09-24 stsp }
8059 f259c4c1 2021-09-24 stsp
8060 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8061 f259c4c1 2021-09-24 stsp if (err)
8062 f259c4c1 2021-09-24 stsp goto done;
8063 f259c4c1 2021-09-24 stsp
8064 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8065 f259c4c1 2021-09-24 stsp if (err)
8066 f259c4c1 2021-09-24 stsp goto done;
8067 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8068 f259c4c1 2021-09-24 stsp if (err)
8069 f259c4c1 2021-09-24 stsp goto done;
8070 f259c4c1 2021-09-24 stsp
8071 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8072 f259c4c1 2021-09-24 stsp if (err)
8073 f259c4c1 2021-09-24 stsp goto done;
8074 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8075 f259c4c1 2021-09-24 stsp if (err)
8076 f259c4c1 2021-09-24 stsp goto done;
8077 f259c4c1 2021-09-24 stsp
8078 f259c4c1 2021-09-24 stsp done:
8079 f259c4c1 2021-09-24 stsp free(branch_refname);
8080 f259c4c1 2021-09-24 stsp free(commit_refname);
8081 f259c4c1 2021-09-24 stsp free(fileindex_path);
8082 f259c4c1 2021-09-24 stsp if (branch_ref)
8083 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8084 f259c4c1 2021-09-24 stsp if (commit_ref)
8085 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8086 f259c4c1 2021-09-24 stsp if (wt_branch)
8087 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8088 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8089 f259c4c1 2021-09-24 stsp if (err) {
8090 f259c4c1 2021-09-24 stsp if (*fileindex) {
8091 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8092 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8093 f259c4c1 2021-09-24 stsp }
8094 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8095 f259c4c1 2021-09-24 stsp }
8096 f259c4c1 2021-09-24 stsp return err;
8097 f259c4c1 2021-09-24 stsp }
8098 f259c4c1 2021-09-24 stsp
8099 f259c4c1 2021-09-24 stsp const struct got_error *
8100 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8101 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8102 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8103 f259c4c1 2021-09-24 stsp {
8104 f259c4c1 2021-09-24 stsp const struct got_error *err;
8105 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8106 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8107 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8108 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8109 f259c4c1 2021-09-24 stsp
8110 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8111 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8112 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8113 f259c4c1 2021-09-24 stsp
8114 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8115 f259c4c1 2021-09-24 stsp if (err)
8116 f259c4c1 2021-09-24 stsp return err;
8117 f259c4c1 2021-09-24 stsp
8118 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8119 f259c4c1 2021-09-24 stsp if (err)
8120 f259c4c1 2021-09-24 stsp goto done;
8121 f259c4c1 2021-09-24 stsp
8122 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8123 f259c4c1 2021-09-24 stsp &have_staged_files);
8124 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8125 f259c4c1 2021-09-24 stsp goto done;
8126 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8127 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8128 f259c4c1 2021-09-24 stsp goto done;
8129 f259c4c1 2021-09-24 stsp }
8130 f259c4c1 2021-09-24 stsp
8131 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8132 f259c4c1 2021-09-24 stsp if (err)
8133 f259c4c1 2021-09-24 stsp goto done;
8134 f259c4c1 2021-09-24 stsp
8135 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8136 f259c4c1 2021-09-24 stsp if (err)
8137 f259c4c1 2021-09-24 stsp goto done;
8138 f259c4c1 2021-09-24 stsp
8139 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8140 f259c4c1 2021-09-24 stsp if (err)
8141 f259c4c1 2021-09-24 stsp goto done;
8142 f259c4c1 2021-09-24 stsp
8143 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8144 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8145 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8146 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8147 f259c4c1 2021-09-24 stsp goto done;
8148 f259c4c1 2021-09-24 stsp }
8149 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8150 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8151 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8152 f259c4c1 2021-09-24 stsp goto done;
8153 f259c4c1 2021-09-24 stsp }
8154 f259c4c1 2021-09-24 stsp
8155 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8156 f259c4c1 2021-09-24 stsp if (err)
8157 f259c4c1 2021-09-24 stsp goto done;
8158 f259c4c1 2021-09-24 stsp
8159 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8160 f259c4c1 2021-09-24 stsp if (err)
8161 f259c4c1 2021-09-24 stsp goto done;
8162 f259c4c1 2021-09-24 stsp done:
8163 f259c4c1 2021-09-24 stsp free(commit_refname);
8164 f259c4c1 2021-09-24 stsp free(branch_refname);
8165 f259c4c1 2021-09-24 stsp free(fileindex_path);
8166 f259c4c1 2021-09-24 stsp if (commit_ref)
8167 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8168 f259c4c1 2021-09-24 stsp if (branch_ref)
8169 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8170 f259c4c1 2021-09-24 stsp if (err) {
8171 f259c4c1 2021-09-24 stsp if (*branch_name) {
8172 f259c4c1 2021-09-24 stsp free(*branch_name);
8173 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8174 f259c4c1 2021-09-24 stsp }
8175 f259c4c1 2021-09-24 stsp free(*branch_tip);
8176 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8177 f259c4c1 2021-09-24 stsp if (*fileindex) {
8178 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8179 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8180 f259c4c1 2021-09-24 stsp }
8181 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8182 f259c4c1 2021-09-24 stsp }
8183 f259c4c1 2021-09-24 stsp return err;
8184 f259c4c1 2021-09-24 stsp }
8185 f259c4c1 2021-09-24 stsp
8186 f259c4c1 2021-09-24 stsp const struct got_error *
8187 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8188 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8189 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8190 f259c4c1 2021-09-24 stsp {
8191 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8192 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8193 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8194 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8195 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8196 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8197 f259c4c1 2021-09-24 stsp
8198 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8199 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8200 a44927cc 2022-04-07 stsp if (err)
8201 a44927cc 2022-04-07 stsp goto done;
8202 a44927cc 2022-04-07 stsp
8203 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8204 a44927cc 2022-04-07 stsp worktree->path_prefix);
8205 f259c4c1 2021-09-24 stsp if (err)
8206 f259c4c1 2021-09-24 stsp goto done;
8207 f259c4c1 2021-09-24 stsp
8208 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8209 f259c4c1 2021-09-24 stsp if (err)
8210 f259c4c1 2021-09-24 stsp goto done;
8211 f259c4c1 2021-09-24 stsp
8212 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8213 f259c4c1 2021-09-24 stsp if (err)
8214 f259c4c1 2021-09-24 stsp goto done;
8215 f259c4c1 2021-09-24 stsp
8216 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8217 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8218 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8219 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8220 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8221 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8222 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8223 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8224 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8225 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8226 f259c4c1 2021-09-24 stsp if (err)
8227 f259c4c1 2021-09-24 stsp goto sync;
8228 f259c4c1 2021-09-24 stsp
8229 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8230 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8231 f259c4c1 2021-09-24 stsp sync:
8232 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8233 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8234 f259c4c1 2021-09-24 stsp err = sync_err;
8235 f259c4c1 2021-09-24 stsp done:
8236 f259c4c1 2021-09-24 stsp free(tree_id);
8237 f259c4c1 2021-09-24 stsp free(commit_id);
8238 a44927cc 2022-04-07 stsp if (commit)
8239 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8240 f259c4c1 2021-09-24 stsp if (fileindex)
8241 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8242 f259c4c1 2021-09-24 stsp free(fileindex_path);
8243 f259c4c1 2021-09-24 stsp
8244 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8245 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8246 f259c4c1 2021-09-24 stsp err = unlockerr;
8247 f259c4c1 2021-09-24 stsp return err;
8248 f259c4c1 2021-09-24 stsp }
8249 f259c4c1 2021-09-24 stsp
8250 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8251 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8252 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8253 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8254 2db2652d 2019-08-07 stsp struct got_repository *repo;
8255 2db2652d 2019-08-07 stsp int have_changes;
8256 2db2652d 2019-08-07 stsp };
8257 2db2652d 2019-08-07 stsp
8258 336075a4 2022-06-25 op static const struct got_error *
8259 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8260 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8261 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8262 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8263 735ef5ac 2019-08-03 stsp {
8264 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8265 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8266 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8267 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8268 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8269 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8270 8b13ce36 2019-08-08 stsp
8271 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8272 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8273 8b13ce36 2019-08-08 stsp return NULL;
8274 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8275 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8276 735ef5ac 2019-08-03 stsp
8277 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8278 735ef5ac 2019-08-03 stsp if (ie == NULL)
8279 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8280 735ef5ac 2019-08-03 stsp
8281 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8282 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8283 735ef5ac 2019-08-03 stsp relpath) == -1)
8284 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8285 735ef5ac 2019-08-03 stsp
8286 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8287 3093e2d7 2023-02-04 op memcpy(base_commit_id.hash, ie->commit_hash,
8288 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8289 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8290 735ef5ac 2019-08-03 stsp }
8291 735ef5ac 2019-08-03 stsp
8292 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8293 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8294 3aa5969e 2019-08-06 stsp goto done;
8295 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8296 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8297 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8298 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8299 735ef5ac 2019-08-03 stsp goto done;
8300 3aa5969e 2019-08-06 stsp }
8301 735ef5ac 2019-08-03 stsp
8302 2db2652d 2019-08-07 stsp a->have_changes = 1;
8303 2db2652d 2019-08-07 stsp
8304 735ef5ac 2019-08-03 stsp p = in_repo_path;
8305 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8306 735ef5ac 2019-08-03 stsp p++;
8307 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8308 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8309 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8310 735ef5ac 2019-08-03 stsp done:
8311 735ef5ac 2019-08-03 stsp free(in_repo_path);
8312 dc424a06 2019-08-07 stsp return err;
8313 dc424a06 2019-08-07 stsp }
8314 dc424a06 2019-08-07 stsp
8315 2db2652d 2019-08-07 stsp struct stage_path_arg {
8316 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8317 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8318 2db2652d 2019-08-07 stsp struct got_repository *repo;
8319 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8320 2db2652d 2019-08-07 stsp void *status_arg;
8321 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8322 2db2652d 2019-08-07 stsp void *patch_arg;
8323 7b5dc508 2019-10-28 stsp int staged_something;
8324 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8325 2db2652d 2019-08-07 stsp };
8326 2db2652d 2019-08-07 stsp
8327 2db2652d 2019-08-07 stsp static const struct got_error *
8328 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8329 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8330 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8331 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8332 0cb83759 2019-08-03 stsp {
8333 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8334 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8335 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8336 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8337 0cb83759 2019-08-03 stsp uint32_t stage;
8338 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8339 0aeb8099 2020-07-23 stsp struct stat sb;
8340 8b13ce36 2019-08-08 stsp
8341 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8342 8b13ce36 2019-08-08 stsp return NULL;
8343 0cb83759 2019-08-03 stsp
8344 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8345 d3e7c587 2019-08-03 stsp if (ie == NULL)
8346 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8347 0cb83759 2019-08-03 stsp
8348 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8349 2db2652d 2019-08-07 stsp relpath)== -1)
8350 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8351 0cb83759 2019-08-03 stsp
8352 0cb83759 2019-08-03 stsp switch (status) {
8353 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8354 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8355 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8356 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8357 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8358 0aeb8099 2020-07-23 stsp break;
8359 0aeb8099 2020-07-23 stsp }
8360 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8361 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8362 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8363 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8364 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8365 dc424a06 2019-08-07 stsp if (err)
8366 dc424a06 2019-08-07 stsp break;
8367 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8368 dc424a06 2019-08-07 stsp break;
8369 dc424a06 2019-08-07 stsp } else {
8370 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8371 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8372 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8373 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8374 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8375 dc424a06 2019-08-07 stsp break;
8376 dc424a06 2019-08-07 stsp }
8377 dc424a06 2019-08-07 stsp }
8378 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8379 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8380 0cb83759 2019-08-03 stsp if (err)
8381 dc424a06 2019-08-07 stsp break;
8382 3093e2d7 2023-02-04 op memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8383 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8384 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8385 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8386 0cb83759 2019-08-03 stsp else
8387 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8388 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8389 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8390 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8391 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8392 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8393 35213c7c 2020-07-23 stsp ssize_t target_len;
8394 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8395 35213c7c 2020-07-23 stsp sizeof(target_path));
8396 35213c7c 2020-07-23 stsp if (target_len == -1) {
8397 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8398 35213c7c 2020-07-23 stsp ondisk_path);
8399 35213c7c 2020-07-23 stsp break;
8400 35213c7c 2020-07-23 stsp }
8401 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8402 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8403 35213c7c 2020-07-23 stsp a->worktree->root_path);
8404 35213c7c 2020-07-23 stsp if (err)
8405 35213c7c 2020-07-23 stsp break;
8406 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8407 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8408 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8409 35213c7c 2020-07-23 stsp break;
8410 35213c7c 2020-07-23 stsp }
8411 35213c7c 2020-07-23 stsp }
8412 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8413 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8414 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8415 35213c7c 2020-07-23 stsp else
8416 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8417 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8418 0aeb8099 2020-07-23 stsp } else {
8419 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8420 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8421 0aeb8099 2020-07-23 stsp }
8422 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8423 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8424 dc424a06 2019-08-07 stsp break;
8425 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8426 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8427 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8428 9fdde394 2022-06-04 op if (err)
8429 9fdde394 2022-06-04 op break;
8430 9fdde394 2022-06-04 op /*
8431 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8432 9fdde394 2022-06-04 op * implicitly unstage the file.
8433 9fdde394 2022-06-04 op */
8434 3093e2d7 2023-02-04 op if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8435 3093e2d7 2023-02-04 op sizeof(ie->blob_hash)) == 0) {
8436 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8437 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8438 9fdde394 2022-06-04 op }
8439 0cb83759 2019-08-03 stsp break;
8440 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8441 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8442 d3e7c587 2019-08-03 stsp break;
8443 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8444 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8445 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8446 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8447 dc424a06 2019-08-07 stsp if (err)
8448 dc424a06 2019-08-07 stsp break;
8449 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8450 88f33a19 2019-08-08 stsp break;
8451 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8452 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8453 dc424a06 2019-08-07 stsp break;
8454 88f33a19 2019-08-08 stsp }
8455 dc424a06 2019-08-07 stsp }
8456 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8457 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8458 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8459 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8460 dc424a06 2019-08-07 stsp break;
8461 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8462 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8463 12463d8b 2019-12-13 stsp de_name);
8464 0cb83759 2019-08-03 stsp break;
8465 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8466 d3e7c587 2019-08-03 stsp break;
8467 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8468 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8469 ebf48fd5 2019-08-03 stsp break;
8470 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8471 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8472 2a06fe5f 2019-08-24 stsp break;
8473 0cb83759 2019-08-03 stsp default:
8474 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8475 537ac44b 2019-08-03 stsp break;
8476 0cb83759 2019-08-03 stsp }
8477 dc424a06 2019-08-07 stsp
8478 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8479 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8480 dc424a06 2019-08-07 stsp free(path_content);
8481 2db2652d 2019-08-07 stsp free(ondisk_path);
8482 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8483 0ebf8283 2019-07-24 stsp return err;
8484 0ebf8283 2019-07-24 stsp }
8485 0cb83759 2019-08-03 stsp
8486 0cb83759 2019-08-03 stsp const struct got_error *
8487 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8488 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8489 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8490 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8491 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8492 0cb83759 2019-08-03 stsp {
8493 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8494 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8495 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8496 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8497 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8498 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8499 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8500 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8501 0cb83759 2019-08-03 stsp
8502 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8503 0cb83759 2019-08-03 stsp if (err)
8504 0cb83759 2019-08-03 stsp return err;
8505 0cb83759 2019-08-03 stsp
8506 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8507 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8508 735ef5ac 2019-08-03 stsp if (err)
8509 735ef5ac 2019-08-03 stsp goto done;
8510 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8511 735ef5ac 2019-08-03 stsp if (err)
8512 735ef5ac 2019-08-03 stsp goto done;
8513 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8514 0cb83759 2019-08-03 stsp if (err)
8515 0cb83759 2019-08-03 stsp goto done;
8516 0cb83759 2019-08-03 stsp
8517 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8518 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8519 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8520 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8521 2db2652d 2019-08-07 stsp oka.repo = repo;
8522 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8523 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8524 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8525 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8526 735ef5ac 2019-08-03 stsp if (err)
8527 735ef5ac 2019-08-03 stsp goto done;
8528 735ef5ac 2019-08-03 stsp }
8529 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8530 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8531 2db2652d 2019-08-07 stsp goto done;
8532 2db2652d 2019-08-07 stsp }
8533 735ef5ac 2019-08-03 stsp
8534 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8535 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8536 2db2652d 2019-08-07 stsp spa.repo = repo;
8537 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8538 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8539 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8540 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8541 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8542 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8543 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8544 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8545 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8546 42005733 2019-08-03 stsp if (err)
8547 2db2652d 2019-08-07 stsp goto done;
8548 7b5dc508 2019-10-28 stsp }
8549 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8550 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8551 7b5dc508 2019-10-28 stsp goto done;
8552 0cb83759 2019-08-03 stsp }
8553 0cb83759 2019-08-03 stsp
8554 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8555 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8556 0cb83759 2019-08-03 stsp err = sync_err;
8557 0cb83759 2019-08-03 stsp done:
8558 735ef5ac 2019-08-03 stsp if (head_ref)
8559 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8560 735ef5ac 2019-08-03 stsp free(head_commit_id);
8561 0cb83759 2019-08-03 stsp free(fileindex_path);
8562 0cb83759 2019-08-03 stsp if (fileindex)
8563 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8564 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8565 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8566 0cb83759 2019-08-03 stsp err = unlockerr;
8567 0cb83759 2019-08-03 stsp return err;
8568 0cb83759 2019-08-03 stsp }
8569 ad493afc 2019-08-03 stsp
8570 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8571 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8572 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8573 ad493afc 2019-08-03 stsp struct got_repository *repo;
8574 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8575 ad493afc 2019-08-03 stsp void *progress_arg;
8576 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8577 2e1f37b0 2019-08-08 stsp void *patch_arg;
8578 ad493afc 2019-08-03 stsp };
8579 2e1f37b0 2019-08-08 stsp
8580 2e1f37b0 2019-08-08 stsp static const struct got_error *
8581 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8582 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8583 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8584 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8585 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8586 2e1f37b0 2019-08-08 stsp {
8587 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8588 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8589 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8590 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8591 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8592 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8593 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8594 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8595 2e1f37b0 2019-08-08 stsp
8596 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8597 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8598 2e1f37b0 2019-08-08 stsp
8599 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8600 2e1f37b0 2019-08-08 stsp if (err)
8601 2e1f37b0 2019-08-08 stsp return err;
8602 eb81bc23 2022-06-28 tracey
8603 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8604 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8605 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8606 eb81bc23 2022-06-28 tracey goto done;
8607 eb81bc23 2022-06-28 tracey }
8608 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8609 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8610 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8611 eb81bc23 2022-06-28 tracey goto done;
8612 eb81bc23 2022-06-28 tracey }
8613 eb81bc23 2022-06-28 tracey
8614 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8615 2e1f37b0 2019-08-08 stsp if (err)
8616 2e1f37b0 2019-08-08 stsp goto done;
8617 2e1f37b0 2019-08-08 stsp
8618 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8619 2e1f37b0 2019-08-08 stsp if (err)
8620 2e1f37b0 2019-08-08 stsp goto done;
8621 2e1f37b0 2019-08-08 stsp
8622 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8623 2e1f37b0 2019-08-08 stsp if (err)
8624 2e1f37b0 2019-08-08 stsp goto done;
8625 2e1f37b0 2019-08-08 stsp
8626 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8627 eb81bc23 2022-06-28 tracey fd2);
8628 2e1f37b0 2019-08-08 stsp if (err)
8629 2e1f37b0 2019-08-08 stsp goto done;
8630 2e1f37b0 2019-08-08 stsp
8631 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8632 2e1f37b0 2019-08-08 stsp if (err)
8633 2e1f37b0 2019-08-08 stsp goto done;
8634 ad493afc 2019-08-03 stsp
8635 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8636 2e1f37b0 2019-08-08 stsp if (err)
8637 2e1f37b0 2019-08-08 stsp goto done;
8638 2e1f37b0 2019-08-08 stsp
8639 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8640 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8641 2e1f37b0 2019-08-08 stsp if (err)
8642 2e1f37b0 2019-08-08 stsp goto done;
8643 2e1f37b0 2019-08-08 stsp
8644 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8645 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8646 2e1f37b0 2019-08-08 stsp if (err)
8647 2e1f37b0 2019-08-08 stsp goto done;
8648 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8649 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8650 2e1f37b0 2019-08-08 stsp if (err)
8651 2e1f37b0 2019-08-08 stsp goto done;
8652 2e1f37b0 2019-08-08 stsp
8653 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8654 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8655 2e1f37b0 2019-08-08 stsp goto done;
8656 2e1f37b0 2019-08-08 stsp }
8657 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8658 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8659 2e1f37b0 2019-08-08 stsp goto done;
8660 2e1f37b0 2019-08-08 stsp }
8661 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8662 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8663 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8664 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8665 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8666 fe621944 2020-11-10 stsp nchanges++;
8667 fe621944 2020-11-10 stsp }
8668 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8669 2e1f37b0 2019-08-08 stsp int choice;
8670 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8671 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8672 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8673 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8674 2e1f37b0 2019-08-08 stsp if (err)
8675 2e1f37b0 2019-08-08 stsp goto done;
8676 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8677 2e1f37b0 2019-08-08 stsp have_content = 1;
8678 19e4b907 2019-08-08 stsp else
8679 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8680 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8681 2e1f37b0 2019-08-08 stsp break;
8682 2e1f37b0 2019-08-08 stsp }
8683 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8684 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8685 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8686 2e1f37b0 2019-08-08 stsp done:
8687 2e1f37b0 2019-08-08 stsp free(label1);
8688 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8689 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8690 2e1f37b0 2019-08-08 stsp if (blob)
8691 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8692 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8693 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8694 2e1f37b0 2019-08-08 stsp if (staged_blob)
8695 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8696 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8697 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8698 fe621944 2020-11-10 stsp err = free_err;
8699 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8700 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8701 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8702 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8703 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8704 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8705 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8706 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8707 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8708 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8709 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8710 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8711 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8712 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8713 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8714 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8715 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8716 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8717 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8718 2e1f37b0 2019-08-08 stsp }
8719 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8720 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8721 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8722 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8723 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8724 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8725 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8726 2e1f37b0 2019-08-08 stsp }
8727 2e1f37b0 2019-08-08 stsp free(path1);
8728 2e1f37b0 2019-08-08 stsp free(path2);
8729 fda8017d 2020-07-23 stsp return err;
8730 fda8017d 2020-07-23 stsp }
8731 fda8017d 2020-07-23 stsp
8732 fda8017d 2020-07-23 stsp static const struct got_error *
8733 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8734 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8735 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8736 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8737 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8738 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8739 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8740 fda8017d 2020-07-23 stsp {
8741 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8742 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8743 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8744 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8745 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8746 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8747 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8748 fda8017d 2020-07-23 stsp struct stat sb;
8749 fda8017d 2020-07-23 stsp
8750 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8751 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8752 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8753 fda8017d 2020-07-23 stsp if (err)
8754 fda8017d 2020-07-23 stsp return err;
8755 fda8017d 2020-07-23 stsp
8756 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8757 fda8017d 2020-07-23 stsp return NULL;
8758 fda8017d 2020-07-23 stsp
8759 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8760 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8761 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8762 fda8017d 2020-07-23 stsp if (err)
8763 fda8017d 2020-07-23 stsp goto done;
8764 fda8017d 2020-07-23 stsp }
8765 fda8017d 2020-07-23 stsp
8766 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8767 fda8017d 2020-07-23 stsp if (f == NULL) {
8768 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8769 fda8017d 2020-07-23 stsp path_unstaged_content);
8770 fda8017d 2020-07-23 stsp goto done;
8771 fda8017d 2020-07-23 stsp }
8772 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8773 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8774 fda8017d 2020-07-23 stsp goto done;
8775 fda8017d 2020-07-23 stsp }
8776 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8777 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8778 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8779 fda8017d 2020-07-23 stsp size_t r;
8780 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8781 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8782 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8783 fda8017d 2020-07-23 stsp goto done;
8784 fda8017d 2020-07-23 stsp }
8785 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8786 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8787 fda8017d 2020-07-23 stsp goto done;
8788 fda8017d 2020-07-23 stsp }
8789 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8790 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8791 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8792 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8793 fda8017d 2020-07-23 stsp progress_arg);
8794 fda8017d 2020-07-23 stsp } else {
8795 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8796 67a66647 2021-05-31 stsp
8797 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8798 67a66647 2021-05-31 stsp if (err)
8799 67a66647 2021-05-31 stsp return err;
8800 67a66647 2021-05-31 stsp
8801 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8802 67a66647 2021-05-31 stsp parent) == -1) {
8803 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8804 67a66647 2021-05-31 stsp base_path = NULL;
8805 67a66647 2021-05-31 stsp goto done;
8806 67a66647 2021-05-31 stsp }
8807 67a66647 2021-05-31 stsp
8808 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8809 b90054ed 2022-11-01 stsp base_path, "");
8810 67a66647 2021-05-31 stsp if (err)
8811 67a66647 2021-05-31 stsp goto done;
8812 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8813 67a66647 2021-05-31 stsp blob_base);
8814 67a66647 2021-05-31 stsp if (err)
8815 67a66647 2021-05-31 stsp goto done;
8816 67a66647 2021-05-31 stsp
8817 5e91dae4 2022-08-30 stsp /*
8818 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8819 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8820 eec2f5a9 2021-06-03 stsp */
8821 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8822 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8823 eec2f5a9 2021-06-03 stsp ondisk_path);
8824 eec2f5a9 2021-06-03 stsp if (err)
8825 eec2f5a9 2021-06-03 stsp goto done;
8826 eec2f5a9 2021-06-03 stsp } else {
8827 eec2f5a9 2021-06-03 stsp int fd;
8828 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8829 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8830 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8831 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8832 eec2f5a9 2021-06-03 stsp goto done;
8833 eec2f5a9 2021-06-03 stsp }
8834 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8835 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8836 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8837 eec2f5a9 2021-06-03 stsp close(fd);
8838 eec2f5a9 2021-06-03 stsp goto done;
8839 eec2f5a9 2021-06-03 stsp }
8840 eec2f5a9 2021-06-03 stsp }
8841 eec2f5a9 2021-06-03 stsp
8842 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8843 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8844 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8845 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8846 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8847 fda8017d 2020-07-23 stsp }
8848 fda8017d 2020-07-23 stsp if (err)
8849 fda8017d 2020-07-23 stsp goto done;
8850 fda8017d 2020-07-23 stsp
8851 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8852 3093e2d7 2023-02-04 op memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8853 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8854 2ac8aa02 2020-11-09 stsp } else {
8855 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8856 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8857 2ac8aa02 2020-11-09 stsp }
8858 fda8017d 2020-07-23 stsp done:
8859 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8860 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8861 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8862 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8863 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8864 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8865 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8866 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8867 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8868 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8869 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8870 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8871 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8872 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8873 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8874 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8875 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8876 67a66647 2021-05-31 stsp free(blob_base_path);
8877 67a66647 2021-05-31 stsp free(parent);
8878 67a66647 2021-05-31 stsp free(base_path);
8879 2e1f37b0 2019-08-08 stsp return err;
8880 2e1f37b0 2019-08-08 stsp }
8881 2e1f37b0 2019-08-08 stsp
8882 ad493afc 2019-08-03 stsp static const struct got_error *
8883 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8884 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8885 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8886 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8887 ad493afc 2019-08-03 stsp {
8888 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8889 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8890 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8891 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8892 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8893 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8894 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8895 9bc94a15 2019-08-03 stsp struct stat sb;
8896 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8897 ad493afc 2019-08-03 stsp
8898 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8899 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8900 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8901 2e1f37b0 2019-08-08 stsp return NULL;
8902 2e1f37b0 2019-08-08 stsp
8903 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8904 ad493afc 2019-08-03 stsp if (ie == NULL)
8905 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8906 9bc94a15 2019-08-03 stsp
8907 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8908 9bc94a15 2019-08-03 stsp == -1)
8909 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8910 ad493afc 2019-08-03 stsp
8911 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8912 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8913 f69721c3 2019-10-21 stsp if (err)
8914 f69721c3 2019-10-21 stsp goto done;
8915 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8916 f69721c3 2019-10-21 stsp id_str) == -1) {
8917 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8918 eb81bc23 2022-06-28 tracey goto done;
8919 eb81bc23 2022-06-28 tracey }
8920 eb81bc23 2022-06-28 tracey
8921 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8922 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8923 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8924 eb81bc23 2022-06-28 tracey goto done;
8925 eb81bc23 2022-06-28 tracey }
8926 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8927 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8928 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8929 f69721c3 2019-10-21 stsp goto done;
8930 f69721c3 2019-10-21 stsp }
8931 f69721c3 2019-10-21 stsp
8932 ad493afc 2019-08-03 stsp switch (staged_status) {
8933 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8934 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8935 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8936 ad493afc 2019-08-03 stsp if (err)
8937 ad493afc 2019-08-03 stsp break;
8938 ad493afc 2019-08-03 stsp /* fall through */
8939 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8940 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8941 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8942 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8943 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8944 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8945 2e1f37b0 2019-08-08 stsp if (err)
8946 2e1f37b0 2019-08-08 stsp break;
8947 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8948 2e1f37b0 2019-08-08 stsp break;
8949 2e1f37b0 2019-08-08 stsp } else {
8950 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8951 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8952 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8953 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8954 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8955 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8956 2e1f37b0 2019-08-08 stsp }
8957 2e1f37b0 2019-08-08 stsp }
8958 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8959 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8960 ad493afc 2019-08-03 stsp if (err)
8961 ad493afc 2019-08-03 stsp break;
8962 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8963 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8964 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8965 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8966 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8967 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8968 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8969 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8970 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8971 ea7786be 2020-07-23 stsp break;
8972 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8973 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8974 36bf999c 2020-07-23 stsp char *staged_target;
8975 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8976 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8977 36bf999c 2020-07-23 stsp if (err)
8978 36bf999c 2020-07-23 stsp goto done;
8979 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8980 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8981 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8982 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8983 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8984 36bf999c 2020-07-23 stsp free(staged_target);
8985 dfe9fba0 2020-07-23 stsp } else {
8986 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8987 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8988 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8989 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8990 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8991 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8992 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8993 dfe9fba0 2020-07-23 stsp }
8994 ea7786be 2020-07-23 stsp break;
8995 ea7786be 2020-07-23 stsp default:
8996 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8997 ea7786be 2020-07-23 stsp break;
8998 ea7786be 2020-07-23 stsp }
8999 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9000 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9001 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9002 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9003 2ac8aa02 2020-11-09 stsp }
9004 ad493afc 2019-08-03 stsp break;
9005 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9006 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9007 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9008 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9009 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9010 2e1f37b0 2019-08-08 stsp if (err)
9011 2e1f37b0 2019-08-08 stsp break;
9012 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9013 2e1f37b0 2019-08-08 stsp break;
9014 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9015 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9016 2e1f37b0 2019-08-08 stsp break;
9017 2e1f37b0 2019-08-08 stsp }
9018 2e1f37b0 2019-08-08 stsp }
9019 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9020 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9021 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9022 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9023 9bc94a15 2019-08-03 stsp if (err)
9024 9bc94a15 2019-08-03 stsp break;
9025 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9026 ad493afc 2019-08-03 stsp break;
9027 ad493afc 2019-08-03 stsp }
9028 f69721c3 2019-10-21 stsp done:
9029 ad493afc 2019-08-03 stsp free(ondisk_path);
9030 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9031 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9032 ad493afc 2019-08-03 stsp if (blob_base)
9033 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9034 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9035 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9036 ad493afc 2019-08-03 stsp if (blob_staged)
9037 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9038 f69721c3 2019-10-21 stsp free(id_str);
9039 f69721c3 2019-10-21 stsp free(label_orig);
9040 ad493afc 2019-08-03 stsp return err;
9041 ad493afc 2019-08-03 stsp }
9042 ad493afc 2019-08-03 stsp
9043 ad493afc 2019-08-03 stsp const struct got_error *
9044 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9045 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9046 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9047 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9048 ad493afc 2019-08-03 stsp struct got_repository *repo)
9049 ad493afc 2019-08-03 stsp {
9050 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9051 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9052 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9053 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9054 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9055 ad493afc 2019-08-03 stsp
9056 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9057 ad493afc 2019-08-03 stsp if (err)
9058 ad493afc 2019-08-03 stsp return err;
9059 ad493afc 2019-08-03 stsp
9060 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9061 ad493afc 2019-08-03 stsp if (err)
9062 ad493afc 2019-08-03 stsp goto done;
9063 ad493afc 2019-08-03 stsp
9064 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9065 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9066 ad493afc 2019-08-03 stsp upa.repo = repo;
9067 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9068 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9069 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9070 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9071 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9072 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9073 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9074 ad493afc 2019-08-03 stsp if (err)
9075 ad493afc 2019-08-03 stsp goto done;
9076 ad493afc 2019-08-03 stsp }
9077 ad493afc 2019-08-03 stsp
9078 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9079 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9080 ad493afc 2019-08-03 stsp err = sync_err;
9081 ad493afc 2019-08-03 stsp done:
9082 ad493afc 2019-08-03 stsp free(fileindex_path);
9083 ad493afc 2019-08-03 stsp if (fileindex)
9084 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9085 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9086 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9087 ad493afc 2019-08-03 stsp err = unlockerr;
9088 ad493afc 2019-08-03 stsp return err;
9089 ad493afc 2019-08-03 stsp }
9090 b2118c49 2020-07-28 stsp
9091 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9092 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9093 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9094 b2118c49 2020-07-28 stsp void *info_arg;
9095 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9096 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9097 b2118c49 2020-07-28 stsp void *cancel_arg;
9098 b2118c49 2020-07-28 stsp };
9099 b2118c49 2020-07-28 stsp
9100 b2118c49 2020-07-28 stsp static const struct got_error *
9101 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9102 b2118c49 2020-07-28 stsp {
9103 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9104 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9105 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9106 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9107 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9108 b2118c49 2020-07-28 stsp int stage;
9109 b2118c49 2020-07-28 stsp
9110 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9111 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9112 b2118c49 2020-07-28 stsp
9113 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9114 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9115 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9116 b2118c49 2020-07-28 stsp break;
9117 b2118c49 2020-07-28 stsp }
9118 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9119 b2118c49 2020-07-28 stsp return NULL;
9120 b2118c49 2020-07-28 stsp
9121 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
9122 3093e2d7 2023-02-04 op memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9123 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
9124 b2118c49 2020-07-28 stsp }
9125 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9126 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9127 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9128 3093e2d7 2023-02-04 op memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9129 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
9130 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
9131 b2118c49 2020-07-28 stsp }
9132 b2118c49 2020-07-28 stsp
9133 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
9134 3093e2d7 2023-02-04 op memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9135 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
9136 b2118c49 2020-07-28 stsp }
9137 b2118c49 2020-07-28 stsp
9138 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9139 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9140 b2118c49 2020-07-28 stsp }
9141 b2118c49 2020-07-28 stsp
9142 b2118c49 2020-07-28 stsp const struct got_error *
9143 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9144 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9145 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9146 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9147 b2118c49 2020-07-28 stsp
9148 b2118c49 2020-07-28 stsp {
9149 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9150 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9151 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9152 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9153 b2118c49 2020-07-28 stsp
9154 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9155 b2118c49 2020-07-28 stsp if (err)
9156 b2118c49 2020-07-28 stsp return err;
9157 b2118c49 2020-07-28 stsp
9158 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9159 b2118c49 2020-07-28 stsp if (err)
9160 b2118c49 2020-07-28 stsp goto done;
9161 b2118c49 2020-07-28 stsp
9162 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9163 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9164 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9165 b2118c49 2020-07-28 stsp arg.paths = paths;
9166 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9167 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9168 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9169 b2118c49 2020-07-28 stsp &arg);
9170 b2118c49 2020-07-28 stsp done:
9171 b2118c49 2020-07-28 stsp free(fileindex_path);
9172 b2118c49 2020-07-28 stsp if (fileindex)
9173 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9174 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9175 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9176 b2118c49 2020-07-28 stsp err = unlockerr;
9177 b2118c49 2020-07-28 stsp return err;
9178 b2118c49 2020-07-28 stsp }
9179 78f5ac24 2022-03-19 op
9180 78f5ac24 2022-03-19 op static const struct got_error *
9181 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9182 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9183 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9184 78f5ac24 2022-03-19 op {
9185 78f5ac24 2022-03-19 op const struct got_error *err;
9186 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9187 78f5ac24 2022-03-19 op struct stat sb;
9188 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9189 78f5ac24 2022-03-19 op
9190 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9191 78f5ac24 2022-03-19 op if (err)
9192 78f5ac24 2022-03-19 op return err;
9193 78f5ac24 2022-03-19 op
9194 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9195 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9196 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9197 78f5ac24 2022-03-19 op
9198 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9199 78f5ac24 2022-03-19 op if (ie) {
9200 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9201 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9202 a05fb460 2022-04-23 op repo);
9203 78f5ac24 2022-03-19 op if (err)
9204 78f5ac24 2022-03-19 op goto done;
9205 78f5ac24 2022-03-19 op } else {
9206 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9207 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9208 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9209 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9210 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9211 78f5ac24 2022-03-19 op ondisk_path);
9212 78f5ac24 2022-03-19 op goto done;
9213 78f5ac24 2022-03-19 op }
9214 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9215 78f5ac24 2022-03-19 op }
9216 78f5ac24 2022-03-19 op }
9217 78f5ac24 2022-03-19 op
9218 78f5ac24 2022-03-19 op done:
9219 78f5ac24 2022-03-19 op free(ondisk_path);
9220 78f5ac24 2022-03-19 op return err;
9221 78f5ac24 2022-03-19 op }
9222 78f5ac24 2022-03-19 op
9223 78f5ac24 2022-03-19 op static const struct got_error *
9224 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9225 78f5ac24 2022-03-19 op unsigned char staged_status)
9226 78f5ac24 2022-03-19 op {
9227 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9228 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9229 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9230 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9231 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9232 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9233 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9235 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9236 78f5ac24 2022-03-19 op return NULL;
9237 78f5ac24 2022-03-19 op }
9238 78f5ac24 2022-03-19 op
9239 78f5ac24 2022-03-19 op static const struct got_error *
9240 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9241 78f5ac24 2022-03-19 op {
9242 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9243 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 78f5ac24 2022-03-19 op return NULL;
9245 78f5ac24 2022-03-19 op }
9246 78f5ac24 2022-03-19 op
9247 78f5ac24 2022-03-19 op static const struct got_error *
9248 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9249 78f5ac24 2022-03-19 op unsigned char staged_status)
9250 78f5ac24 2022-03-19 op {
9251 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9252 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9253 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9254 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9255 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9256 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9257 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9258 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9259 78f5ac24 2022-03-19 op return NULL;
9260 78f5ac24 2022-03-19 op }
9261 78f5ac24 2022-03-19 op
9262 78f5ac24 2022-03-19 op const struct got_error *
9263 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9264 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9265 78f5ac24 2022-03-19 op {
9266 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9267 78f5ac24 2022-03-19 op }
9268 78f5ac24 2022-03-19 op
9269 78f5ac24 2022-03-19 op const struct got_error *
9270 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9271 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9272 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9273 78f5ac24 2022-03-19 op {
9274 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9275 78f5ac24 2022-03-19 op int file_renamed = 0;
9276 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9277 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9278 78f5ac24 2022-03-19 op
9279 78f5ac24 2022-03-19 op *oldpath = NULL;
9280 78f5ac24 2022-03-19 op *newpath = NULL;
9281 78f5ac24 2022-03-19 op
9282 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9283 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9284 78f5ac24 2022-03-19 op if (err)
9285 78f5ac24 2022-03-19 op goto done;
9286 78f5ac24 2022-03-19 op
9287 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9288 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9289 78f5ac24 2022-03-19 op if (err)
9290 78f5ac24 2022-03-19 op goto done;
9291 78f5ac24 2022-03-19 op
9292 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9293 78f5ac24 2022-03-19 op file_renamed = 1;
9294 78f5ac24 2022-03-19 op
9295 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9296 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9297 78f5ac24 2022-03-19 op else if (file_renamed) {
9298 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9299 78f5ac24 2022-03-19 op if (err == NULL)
9300 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9301 78f5ac24 2022-03-19 op } else if (old == NULL)
9302 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9303 78f5ac24 2022-03-19 op else
9304 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9305 78f5ac24 2022-03-19 op
9306 78f5ac24 2022-03-19 op done:
9307 78f5ac24 2022-03-19 op if (err) {
9308 78f5ac24 2022-03-19 op free(*oldpath);
9309 78f5ac24 2022-03-19 op *oldpath = NULL;
9310 78f5ac24 2022-03-19 op free(*newpath);
9311 78f5ac24 2022-03-19 op *newpath = NULL;
9312 78f5ac24 2022-03-19 op }
9313 78f5ac24 2022-03-19 op return err;
9314 78f5ac24 2022-03-19 op }
9315 78f5ac24 2022-03-19 op
9316 78f5ac24 2022-03-19 op const struct got_error *
9317 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9318 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9319 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9320 78f5ac24 2022-03-19 op {
9321 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9322 f2dd7807 2022-05-17 op
9323 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9324 f2dd7807 2022-05-17 op saa.worktree = worktree;
9325 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9326 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9327 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9328 f2dd7807 2022-05-17 op saa.repo = repo;
9329 f2dd7807 2022-05-17 op
9330 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9331 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9332 78f5ac24 2022-03-19 op }
9333 f2dd7807 2022-05-17 op
9334 f2dd7807 2022-05-17 op const struct got_error *
9335 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9336 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9337 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9338 f2dd7807 2022-05-17 op {
9339 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9340 f2dd7807 2022-05-17 op
9341 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9342 f2dd7807 2022-05-17 op sda.worktree = worktree;
9343 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9344 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9345 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9346 f2dd7807 2022-05-17 op sda.repo = repo;
9347 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9348 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9349 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9350 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9351 f2dd7807 2022-05-17 op
9352 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9353 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9354 f2dd7807 2022-05-17 op }
9355 f2dd7807 2022-05-17 op
9356 f2dd7807 2022-05-17 op const struct got_error *
9357 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9358 4bcdc895 2022-05-17 op const char *fileindex_path)
9359 f2dd7807 2022-05-17 op {
9360 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9361 f2dd7807 2022-05-17 op
9362 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9363 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9364 f2dd7807 2022-05-17 op
9365 f2dd7807 2022-05-17 op return err;
9366 f2dd7807 2022-05-17 op }