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 dfe0a3d8 2023-02-04 op
68 dfe0a3d8 2023-02-04 op #define WORKTREE_ALGO(wt) ((wt)->base_commit_id->algo)
69 b2b3fce1 2022-10-29 op
70 b2b3fce1 2022-10-29 op static mode_t apply_umask(mode_t);
71 86c3caaf 2018-03-09 stsp
72 99724ed4 2018-03-10 stsp static const struct got_error *
73 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
74 99724ed4 2018-03-10 stsp {
75 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
76 99724ed4 2018-03-10 stsp char *path;
77 99724ed4 2018-03-10 stsp
78 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
79 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
80 99724ed4 2018-03-10 stsp
81 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
82 99724ed4 2018-03-10 stsp free(path);
83 507dc3bb 2018-12-29 stsp return err;
84 507dc3bb 2018-12-29 stsp }
85 507dc3bb 2018-12-29 stsp
86 507dc3bb 2018-12-29 stsp static const struct got_error *
87 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
88 507dc3bb 2018-12-29 stsp {
89 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
90 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
91 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
92 507dc3bb 2018-12-29 stsp char *path = NULL;
93 507dc3bb 2018-12-29 stsp
94 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
95 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
96 507dc3bb 2018-12-29 stsp path = NULL;
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp }
99 507dc3bb 2018-12-29 stsp
100 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, path, "");
101 507dc3bb 2018-12-29 stsp if (err)
102 507dc3bb 2018-12-29 stsp goto done;
103 507dc3bb 2018-12-29 stsp
104 507dc3bb 2018-12-29 stsp if (content) {
105 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
106 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp }
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp
112 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
114 2a57020b 2019-02-20 stsp unlink(tmppath);
115 507dc3bb 2018-12-29 stsp goto done;
116 507dc3bb 2018-12-29 stsp }
117 507dc3bb 2018-12-29 stsp
118 507dc3bb 2018-12-29 stsp done:
119 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
120 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
121 230a42bd 2019-05-11 jcs free(tmppath);
122 09fe317a 2018-03-11 stsp return err;
123 09fe317a 2018-03-11 stsp }
124 09fe317a 2018-03-11 stsp
125 024e9686 2019-05-14 stsp static const struct got_error *
126 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
127 024e9686 2019-05-14 stsp {
128 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
129 024e9686 2019-05-14 stsp char *refstr = NULL;
130 024e9686 2019-05-14 stsp
131 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
132 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
133 024e9686 2019-05-14 stsp if (refstr == NULL)
134 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
135 024e9686 2019-05-14 stsp } else {
136 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
137 024e9686 2019-05-14 stsp if (refstr == NULL)
138 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
139 024e9686 2019-05-14 stsp }
140 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
141 024e9686 2019-05-14 stsp free(refstr);
142 024e9686 2019-05-14 stsp return err;
143 024e9686 2019-05-14 stsp }
144 024e9686 2019-05-14 stsp
145 86c3caaf 2018-03-09 stsp const struct got_error *
146 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
147 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
148 86c3caaf 2018-03-09 stsp {
149 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
150 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
151 ec22038e 2019-03-10 stsp uuid_t uuid;
152 ec22038e 2019-03-10 stsp uint32_t uuid_status;
153 65596e15 2018-12-24 stsp int obj_type;
154 7ac97322 2018-03-11 stsp char *path_got = NULL;
155 1451e70d 2018-03-10 stsp char *formatstr = NULL;
156 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
157 65596e15 2018-12-24 stsp char *basestr = NULL;
158 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
159 65596e15 2018-12-24 stsp
160 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
161 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
162 0c48fee2 2019-03-11 stsp goto done;
163 0c48fee2 2019-03-11 stsp }
164 0c48fee2 2019-03-11 stsp
165 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
166 65596e15 2018-12-24 stsp if (err)
167 65596e15 2018-12-24 stsp return err;
168 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
169 65596e15 2018-12-24 stsp if (err)
170 65596e15 2018-12-24 stsp return err;
171 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
172 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
173 86c3caaf 2018-03-09 stsp
174 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
175 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
176 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
177 0bb8a95e 2018-03-12 stsp }
178 577ec78f 2018-03-11 stsp
179 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
180 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
181 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
182 86c3caaf 2018-03-09 stsp goto done;
183 86c3caaf 2018-03-09 stsp }
184 86c3caaf 2018-03-09 stsp
185 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
186 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
187 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
188 86c3caaf 2018-03-09 stsp goto done;
189 86c3caaf 2018-03-09 stsp }
190 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
191 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
192 86c3caaf 2018-03-09 stsp goto done;
193 86c3caaf 2018-03-09 stsp }
194 86c3caaf 2018-03-09 stsp
195 056e7441 2018-03-11 stsp /* Create an empty lock file. */
196 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
197 056e7441 2018-03-11 stsp if (err)
198 056e7441 2018-03-11 stsp goto done;
199 056e7441 2018-03-11 stsp
200 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
201 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
202 99724ed4 2018-03-10 stsp if (err)
203 86c3caaf 2018-03-09 stsp goto done;
204 86c3caaf 2018-03-09 stsp
205 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
206 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp goto done;
209 65596e15 2018-12-24 stsp
210 65596e15 2018-12-24 stsp /* Record our base commit. */
211 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
212 65596e15 2018-12-24 stsp if (err)
213 65596e15 2018-12-24 stsp goto done;
214 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
215 99724ed4 2018-03-10 stsp if (err)
216 86c3caaf 2018-03-09 stsp goto done;
217 86c3caaf 2018-03-09 stsp
218 1451e70d 2018-03-10 stsp /* Store path to repository. */
219 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
220 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
221 99724ed4 2018-03-10 stsp if (err)
222 86c3caaf 2018-03-09 stsp goto done;
223 86c3caaf 2018-03-09 stsp
224 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
225 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
226 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
227 ec22038e 2019-03-10 stsp if (err)
228 ec22038e 2019-03-10 stsp goto done;
229 ec22038e 2019-03-10 stsp
230 ec22038e 2019-03-10 stsp /* Generate UUID. */
231 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
232 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
233 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
234 ec22038e 2019-03-10 stsp goto done;
235 ec22038e 2019-03-10 stsp }
236 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
237 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
238 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
239 ec22038e 2019-03-10 stsp goto done;
240 ec22038e 2019-03-10 stsp }
241 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
242 577ec78f 2018-03-11 stsp if (err)
243 577ec78f 2018-03-11 stsp goto done;
244 577ec78f 2018-03-11 stsp
245 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
246 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
247 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
248 1451e70d 2018-03-10 stsp goto done;
249 1451e70d 2018-03-10 stsp }
250 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
251 99724ed4 2018-03-10 stsp if (err)
252 1451e70d 2018-03-10 stsp goto done;
253 1451e70d 2018-03-10 stsp
254 86c3caaf 2018-03-09 stsp done:
255 65596e15 2018-12-24 stsp free(commit_id);
256 7ac97322 2018-03-11 stsp free(path_got);
257 1451e70d 2018-03-10 stsp free(formatstr);
258 0bb8a95e 2018-03-12 stsp free(absprefix);
259 65596e15 2018-12-24 stsp free(basestr);
260 ec22038e 2019-03-10 stsp free(uuidstr);
261 86c3caaf 2018-03-09 stsp return err;
262 86c3caaf 2018-03-09 stsp }
263 86c3caaf 2018-03-09 stsp
264 247140b2 2019-02-05 stsp const struct got_error *
265 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
266 e5dc7198 2018-12-29 stsp const char *path_prefix)
267 e5dc7198 2018-12-29 stsp {
268 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
269 e5dc7198 2018-12-29 stsp
270 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
271 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
272 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
273 e5dc7198 2018-12-29 stsp }
274 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
275 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
276 e5dc7198 2018-12-29 stsp free(absprefix);
277 e5dc7198 2018-12-29 stsp return NULL;
278 86c3caaf 2018-03-09 stsp }
279 86c3caaf 2018-03-09 stsp
280 bc70eb79 2019-05-09 stsp const char *
281 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
282 86c3caaf 2018-03-09 stsp {
283 36a38700 2019-05-10 stsp return worktree->head_ref_name;
284 024e9686 2019-05-14 stsp }
285 024e9686 2019-05-14 stsp
286 024e9686 2019-05-14 stsp const struct got_error *
287 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
288 024e9686 2019-05-14 stsp struct got_reference *head_ref)
289 024e9686 2019-05-14 stsp {
290 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
291 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
292 024e9686 2019-05-14 stsp
293 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
294 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
295 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
296 024e9686 2019-05-14 stsp path_got = NULL;
297 024e9686 2019-05-14 stsp goto done;
298 024e9686 2019-05-14 stsp }
299 024e9686 2019-05-14 stsp
300 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
301 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
302 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
303 024e9686 2019-05-14 stsp goto done;
304 024e9686 2019-05-14 stsp }
305 024e9686 2019-05-14 stsp
306 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
307 024e9686 2019-05-14 stsp if (err)
308 024e9686 2019-05-14 stsp goto done;
309 024e9686 2019-05-14 stsp
310 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
311 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
312 024e9686 2019-05-14 stsp done:
313 024e9686 2019-05-14 stsp free(path_got);
314 024e9686 2019-05-14 stsp if (err)
315 024e9686 2019-05-14 stsp free(head_ref_name);
316 024e9686 2019-05-14 stsp return err;
317 507dc3bb 2018-12-29 stsp }
318 507dc3bb 2018-12-29 stsp
319 b72f483a 2019-02-05 stsp struct got_object_id *
320 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
321 507dc3bb 2018-12-29 stsp {
322 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
323 86c3caaf 2018-03-09 stsp }
324 86c3caaf 2018-03-09 stsp
325 507dc3bb 2018-12-29 stsp const struct got_error *
326 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
327 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
328 507dc3bb 2018-12-29 stsp {
329 507dc3bb 2018-12-29 stsp const struct got_error *err;
330 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
331 507dc3bb 2018-12-29 stsp char *id_str = NULL;
332 507dc3bb 2018-12-29 stsp char *path_got = NULL;
333 507dc3bb 2018-12-29 stsp
334 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
335 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
336 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
337 507dc3bb 2018-12-29 stsp path_got = NULL;
338 507dc3bb 2018-12-29 stsp goto done;
339 507dc3bb 2018-12-29 stsp }
340 507dc3bb 2018-12-29 stsp
341 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
342 507dc3bb 2018-12-29 stsp if (err)
343 507dc3bb 2018-12-29 stsp return err;
344 507dc3bb 2018-12-29 stsp
345 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
346 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
347 507dc3bb 2018-12-29 stsp goto done;
348 507dc3bb 2018-12-29 stsp }
349 507dc3bb 2018-12-29 stsp
350 507dc3bb 2018-12-29 stsp /* Record our base commit. */
351 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
352 507dc3bb 2018-12-29 stsp if (err)
353 507dc3bb 2018-12-29 stsp goto done;
354 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
355 507dc3bb 2018-12-29 stsp if (err)
356 507dc3bb 2018-12-29 stsp goto done;
357 507dc3bb 2018-12-29 stsp
358 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
359 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
360 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
361 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
362 507dc3bb 2018-12-29 stsp goto done;
363 507dc3bb 2018-12-29 stsp }
364 507dc3bb 2018-12-29 stsp done:
365 507dc3bb 2018-12-29 stsp if (obj)
366 507dc3bb 2018-12-29 stsp got_object_close(obj);
367 507dc3bb 2018-12-29 stsp free(id_str);
368 507dc3bb 2018-12-29 stsp free(path_got);
369 507dc3bb 2018-12-29 stsp return err;
370 50b0790e 2020-09-11 stsp }
371 50b0790e 2020-09-11 stsp
372 50b0790e 2020-09-11 stsp const struct got_gotconfig *
373 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
374 50b0790e 2020-09-11 stsp {
375 50b0790e 2020-09-11 stsp return worktree->gotconfig;
376 507dc3bb 2018-12-29 stsp }
377 507dc3bb 2018-12-29 stsp
378 9d31a1d8 2018-03-11 stsp static const struct got_error *
379 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
380 86c3caaf 2018-03-09 stsp {
381 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
382 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
383 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
384 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
385 86c3caaf 2018-03-09 stsp return NULL;
386 21908da4 2019-01-13 stsp }
387 21908da4 2019-01-13 stsp
388 21908da4 2019-01-13 stsp static const struct got_error *
389 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
390 4a1ddfc2 2019-01-12 stsp {
391 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
392 4a1ddfc2 2019-01-12 stsp char *abspath;
393 4a1ddfc2 2019-01-12 stsp
394 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
395 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
396 4a1ddfc2 2019-01-12 stsp
397 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
398 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
399 ddcd8544 2019-03-11 stsp struct stat sb;
400 ddcd8544 2019-03-11 stsp err = NULL;
401 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
402 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
403 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
404 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
405 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
406 ddcd8544 2019-03-11 stsp }
407 ddcd8544 2019-03-11 stsp }
408 4a1ddfc2 2019-01-12 stsp free(abspath);
409 68c76935 2019-02-19 stsp return err;
410 68c76935 2019-02-19 stsp }
411 68c76935 2019-02-19 stsp
412 68c76935 2019-02-19 stsp static const struct got_error *
413 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
414 68c76935 2019-02-19 stsp {
415 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
416 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
417 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
418 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
419 68c76935 2019-02-19 stsp
420 68c76935 2019-02-19 stsp *same = 1;
421 68c76935 2019-02-19 stsp
422 230a42bd 2019-05-11 jcs for (;;) {
423 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
424 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
425 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
426 68c76935 2019-02-19 stsp break;
427 68c76935 2019-02-19 stsp }
428 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
429 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
430 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
431 68c76935 2019-02-19 stsp break;
432 68c76935 2019-02-19 stsp }
433 68c76935 2019-02-19 stsp if (flen1 == 0) {
434 68c76935 2019-02-19 stsp if (flen2 != 0)
435 68c76935 2019-02-19 stsp *same = 0;
436 68c76935 2019-02-19 stsp break;
437 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
438 68c76935 2019-02-19 stsp if (flen1 != 0)
439 68c76935 2019-02-19 stsp *same = 0;
440 68c76935 2019-02-19 stsp break;
441 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
442 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
443 68c76935 2019-02-19 stsp *same = 0;
444 68c76935 2019-02-19 stsp break;
445 68c76935 2019-02-19 stsp }
446 68c76935 2019-02-19 stsp } else {
447 68c76935 2019-02-19 stsp *same = 0;
448 68c76935 2019-02-19 stsp break;
449 68c76935 2019-02-19 stsp }
450 68c76935 2019-02-19 stsp }
451 68c76935 2019-02-19 stsp
452 68c76935 2019-02-19 stsp return err;
453 68c76935 2019-02-19 stsp }
454 68c76935 2019-02-19 stsp
455 68c76935 2019-02-19 stsp static const struct got_error *
456 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
457 68c76935 2019-02-19 stsp {
458 68c76935 2019-02-19 stsp struct stat sb;
459 68c76935 2019-02-19 stsp size_t size1, size2;
460 68c76935 2019-02-19 stsp
461 68c76935 2019-02-19 stsp *same = 1;
462 68c76935 2019-02-19 stsp
463 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
464 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
465 68c76935 2019-02-19 stsp size1 = sb.st_size;
466 68c76935 2019-02-19 stsp
467 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
468 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
469 68c76935 2019-02-19 stsp size2 = sb.st_size;
470 68c76935 2019-02-19 stsp
471 68c76935 2019-02-19 stsp if (size1 != size2) {
472 68c76935 2019-02-19 stsp *same = 0;
473 68c76935 2019-02-19 stsp return NULL;
474 68c76935 2019-02-19 stsp }
475 68c76935 2019-02-19 stsp
476 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
477 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
478 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
479 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
480 68c76935 2019-02-19 stsp
481 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
482 0e039681 2021-11-15 stsp }
483 0e039681 2021-11-15 stsp
484 0e039681 2021-11-15 stsp static const struct got_error *
485 0e039681 2021-11-15 stsp copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
486 0e039681 2021-11-15 stsp {
487 0e039681 2021-11-15 stsp uint8_t fbuf[65536];
488 0e039681 2021-11-15 stsp size_t flen;
489 0e039681 2021-11-15 stsp ssize_t outlen;
490 0e039681 2021-11-15 stsp
491 0e039681 2021-11-15 stsp *outsize = 0;
492 0e039681 2021-11-15 stsp
493 0e039681 2021-11-15 stsp if (fseek(f, 0L, SEEK_SET) == -1)
494 0e039681 2021-11-15 stsp return got_ferror(f, GOT_ERR_IO);
495 0e039681 2021-11-15 stsp
496 0e039681 2021-11-15 stsp for (;;) {
497 0e039681 2021-11-15 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
498 0e039681 2021-11-15 stsp if (flen == 0) {
499 0e039681 2021-11-15 stsp if (ferror(f))
500 0e039681 2021-11-15 stsp return got_error_from_errno("fread");
501 0e039681 2021-11-15 stsp if (feof(f))
502 0e039681 2021-11-15 stsp break;
503 0e039681 2021-11-15 stsp }
504 0e039681 2021-11-15 stsp outlen = write(outfd, fbuf, flen);
505 0e039681 2021-11-15 stsp if (outlen == -1)
506 0e039681 2021-11-15 stsp return got_error_from_errno("write");
507 0e039681 2021-11-15 stsp if (outlen != flen)
508 0e039681 2021-11-15 stsp return got_error(GOT_ERR_IO);
509 0e039681 2021-11-15 stsp *outsize += outlen;
510 0e039681 2021-11-15 stsp }
511 0e039681 2021-11-15 stsp
512 0e039681 2021-11-15 stsp return NULL;
513 0e039681 2021-11-15 stsp }
514 0e039681 2021-11-15 stsp
515 0e039681 2021-11-15 stsp static const struct got_error *
516 0e039681 2021-11-15 stsp merge_binary_file(int *overlapcnt, int merged_fd,
517 0e039681 2021-11-15 stsp FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
518 0e039681 2021-11-15 stsp const char *label_deriv, const char *label_orig, const char *label_deriv2,
519 0e039681 2021-11-15 stsp const char *ondisk_path)
520 0e039681 2021-11-15 stsp {
521 0e039681 2021-11-15 stsp const struct got_error *err = NULL;
522 0e039681 2021-11-15 stsp int same_content, changed_deriv, changed_deriv2;
523 0e039681 2021-11-15 stsp int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
524 0e039681 2021-11-15 stsp off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
525 0e039681 2021-11-15 stsp char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
526 0e039681 2021-11-15 stsp char *base_path_orig = NULL, *base_path_deriv = NULL;
527 0e039681 2021-11-15 stsp char *base_path_deriv2 = NULL;
528 0e039681 2021-11-15 stsp
529 0e039681 2021-11-15 stsp *overlapcnt = 0;
530 0e039681 2021-11-15 stsp
531 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_deriv2);
532 0e039681 2021-11-15 stsp if (err)
533 0e039681 2021-11-15 stsp return err;
534 0e039681 2021-11-15 stsp
535 0e039681 2021-11-15 stsp if (same_content)
536 0e039681 2021-11-15 stsp return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
537 0e039681 2021-11-15 stsp
538 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv, f_orig);
539 0e039681 2021-11-15 stsp if (err)
540 0e039681 2021-11-15 stsp return err;
541 0e039681 2021-11-15 stsp changed_deriv = !same_content;
542 0e039681 2021-11-15 stsp err = check_files_equal(&same_content, f_deriv2, f_orig);
543 0e039681 2021-11-15 stsp if (err)
544 0e039681 2021-11-15 stsp return err;
545 0e039681 2021-11-15 stsp changed_deriv2 = !same_content;
546 0e039681 2021-11-15 stsp
547 0e039681 2021-11-15 stsp if (changed_deriv && changed_deriv2) {
548 0e039681 2021-11-15 stsp *overlapcnt = 1;
549 0e039681 2021-11-15 stsp if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
550 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
551 0e039681 2021-11-15 stsp goto done;
552 0e039681 2021-11-15 stsp }
553 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
554 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
555 0e039681 2021-11-15 stsp goto done;
556 0e039681 2021-11-15 stsp }
557 0e039681 2021-11-15 stsp if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
558 0e039681 2021-11-15 stsp err = got_error_from_errno("asprintf");
559 0e039681 2021-11-15 stsp goto done;
560 0e039681 2021-11-15 stsp }
561 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_orig, &fd_orig,
562 b90054ed 2022-11-01 stsp base_path_orig, "");
563 0e039681 2021-11-15 stsp if (err)
564 0e039681 2021-11-15 stsp goto done;
565 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
566 b90054ed 2022-11-01 stsp base_path_deriv, "");
567 0e039681 2021-11-15 stsp if (err)
568 0e039681 2021-11-15 stsp goto done;
569 0e039681 2021-11-15 stsp err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
570 b90054ed 2022-11-01 stsp base_path_deriv2, "");
571 0e039681 2021-11-15 stsp if (err)
572 0e039681 2021-11-15 stsp goto done;
573 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
574 0e039681 2021-11-15 stsp if (err)
575 0e039681 2021-11-15 stsp goto done;
576 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
577 0e039681 2021-11-15 stsp if (err)
578 0e039681 2021-11-15 stsp goto done;
579 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
580 0e039681 2021-11-15 stsp if (err)
581 0e039681 2021-11-15 stsp goto done;
582 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "Binary files differ and cannot be "
583 0e039681 2021-11-15 stsp "merged automatically:\n") < 0) {
584 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
585 0e039681 2021-11-15 stsp goto done;
586 0e039681 2021-11-15 stsp }
587 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
588 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
589 0e039681 2021-11-15 stsp label_deriv ? " " : "",
590 0e039681 2021-11-15 stsp label_deriv ? label_deriv : "",
591 0e039681 2021-11-15 stsp path_deriv) < 0) {
592 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
593 0e039681 2021-11-15 stsp goto done;
594 0e039681 2021-11-15 stsp }
595 0e039681 2021-11-15 stsp if (size_orig > 0) {
596 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
597 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_ORIG,
598 0e039681 2021-11-15 stsp label_orig ? " " : "",
599 0e039681 2021-11-15 stsp label_orig ? label_orig : "",
600 0e039681 2021-11-15 stsp path_orig) < 0) {
601 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
602 0e039681 2021-11-15 stsp goto done;
603 0e039681 2021-11-15 stsp }
604 0e039681 2021-11-15 stsp }
605 0e039681 2021-11-15 stsp if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
606 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
607 0e039681 2021-11-15 stsp path_deriv2,
608 0e039681 2021-11-15 stsp GOT_DIFF_CONFLICT_MARKER_END,
609 0e039681 2021-11-15 stsp label_deriv2 ? " " : "",
610 0e039681 2021-11-15 stsp label_deriv2 ? label_deriv2 : "") < 0) {
611 0e039681 2021-11-15 stsp err = got_error_from_errno("dprintf");
612 0e039681 2021-11-15 stsp goto done;
613 0e039681 2021-11-15 stsp }
614 0e039681 2021-11-15 stsp } else if (changed_deriv)
615 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
616 0e039681 2021-11-15 stsp else if (changed_deriv2)
617 0e039681 2021-11-15 stsp err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
618 0e039681 2021-11-15 stsp done:
619 0e039681 2021-11-15 stsp if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
620 0e039681 2021-11-15 stsp err == NULL)
621 0e039681 2021-11-15 stsp err = got_error_from_errno2("unlink", path_orig);
622 0e039681 2021-11-15 stsp if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
623 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_orig);
624 0e039681 2021-11-15 stsp if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
625 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv);
626 0e039681 2021-11-15 stsp if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
627 0e039681 2021-11-15 stsp err = got_error_from_errno2("close", path_deriv2);
628 0e039681 2021-11-15 stsp free(path_orig);
629 0e039681 2021-11-15 stsp free(path_deriv);
630 0e039681 2021-11-15 stsp free(path_deriv2);
631 0e039681 2021-11-15 stsp free(base_path_orig);
632 0e039681 2021-11-15 stsp free(base_path_deriv);
633 0e039681 2021-11-15 stsp free(base_path_deriv2);
634 0e039681 2021-11-15 stsp return err;
635 6353ad76 2019-02-08 stsp }
636 6353ad76 2019-02-08 stsp
637 6353ad76 2019-02-08 stsp /*
638 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
639 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
640 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
641 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
642 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
643 6353ad76 2019-02-08 stsp */
644 6353ad76 2019-02-08 stsp static const struct got_error *
645 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
646 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
647 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
648 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
649 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
650 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
651 6353ad76 2019-02-08 stsp {
652 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
653 6353ad76 2019-02-08 stsp int merged_fd = -1;
654 db590691 2021-06-02 stsp FILE *f_merged = NULL;
655 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
656 234035bc 2019-06-01 stsp int overlapcnt = 0;
657 3524bbf9 2020-10-20 stsp char *parent = NULL;
658 6353ad76 2019-02-08 stsp
659 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
660 234035bc 2019-06-01 stsp
661 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
662 3524bbf9 2020-10-20 stsp if (err)
663 3524bbf9 2020-10-20 stsp return err;
664 af54ae4a 2019-02-19 stsp
665 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
666 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
667 3524bbf9 2020-10-20 stsp goto done;
668 3524bbf9 2020-10-20 stsp }
669 af54ae4a 2019-02-19 stsp
670 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
671 6353ad76 2019-02-08 stsp if (err)
672 6353ad76 2019-02-08 stsp goto done;
673 3b9f0f87 2020-07-23 stsp
674 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
675 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
676 0e039681 2021-11-15 stsp if (err) {
677 0e039681 2021-11-15 stsp if (err->code != GOT_ERR_FILE_BINARY)
678 0e039681 2021-11-15 stsp goto done;
679 0e039681 2021-11-15 stsp err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
680 0e039681 2021-11-15 stsp f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
681 0e039681 2021-11-15 stsp ondisk_path);
682 0e039681 2021-11-15 stsp if (err)
683 0e039681 2021-11-15 stsp goto done;
684 0e039681 2021-11-15 stsp }
685 6353ad76 2019-02-08 stsp
686 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
687 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
688 1ee397ad 2019-07-12 stsp if (err)
689 1ee397ad 2019-07-12 stsp goto done;
690 6353ad76 2019-02-08 stsp
691 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
692 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
693 816dc654 2019-02-16 stsp goto done;
694 68c76935 2019-02-19 stsp }
695 68c76935 2019-02-19 stsp
696 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
697 db590691 2021-06-02 stsp if (f_merged == NULL) {
698 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
699 db590691 2021-06-02 stsp goto done;
700 db590691 2021-06-02 stsp }
701 db590691 2021-06-02 stsp merged_fd = -1;
702 db590691 2021-06-02 stsp
703 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
704 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
705 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
706 db590691 2021-06-02 stsp f_merged);
707 68c76935 2019-02-19 stsp if (err)
708 68c76935 2019-02-19 stsp goto done;
709 816dc654 2019-02-16 stsp }
710 6353ad76 2019-02-08 stsp
711 b2b3fce1 2022-10-29 op if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
712 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
713 70a0c8ec 2019-02-20 stsp goto done;
714 70a0c8ec 2019-02-20 stsp }
715 70a0c8ec 2019-02-20 stsp
716 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
717 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
718 230a42bd 2019-05-11 jcs ondisk_path);
719 6353ad76 2019-02-08 stsp goto done;
720 6353ad76 2019-02-08 stsp }
721 6353ad76 2019-02-08 stsp done:
722 fdcb7daf 2019-12-15 stsp if (err) {
723 fdcb7daf 2019-12-15 stsp if (merged_path)
724 fdcb7daf 2019-12-15 stsp unlink(merged_path);
725 3b9f0f87 2020-07-23 stsp }
726 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
727 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
728 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
729 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
730 6353ad76 2019-02-08 stsp free(merged_path);
731 af54ae4a 2019-02-19 stsp free(base_path);
732 3524bbf9 2020-10-20 stsp free(parent);
733 af57b12a 2020-07-23 stsp return err;
734 af57b12a 2020-07-23 stsp }
735 af57b12a 2020-07-23 stsp
736 af57b12a 2020-07-23 stsp static const struct got_error *
737 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
738 af57b12a 2020-07-23 stsp size_t target_len)
739 af57b12a 2020-07-23 stsp {
740 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
741 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
742 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
743 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
744 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
745 af57b12a 2020-07-23 stsp ondisk_path);
746 af57b12a 2020-07-23 stsp return NULL;
747 af57b12a 2020-07-23 stsp }
748 af57b12a 2020-07-23 stsp
749 af57b12a 2020-07-23 stsp /*
750 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
751 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
752 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
753 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
754 993e2a1b 2020-07-23 stsp *
755 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
756 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
757 993e2a1b 2020-07-23 stsp *
758 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
759 993e2a1b 2020-07-23 stsp * has deleted this symlink.
760 11cc08c1 2020-07-23 stsp */
761 11cc08c1 2020-07-23 stsp static const struct got_error *
762 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
763 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
764 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
765 11cc08c1 2020-07-23 stsp {
766 11cc08c1 2020-07-23 stsp const struct got_error *err;
767 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
768 11cc08c1 2020-07-23 stsp FILE *f = NULL;
769 11cc08c1 2020-07-23 stsp
770 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
771 11cc08c1 2020-07-23 stsp if (err)
772 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
773 11cc08c1 2020-07-23 stsp
774 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
775 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
776 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
777 11cc08c1 2020-07-23 stsp goto done;
778 11cc08c1 2020-07-23 stsp }
779 11cc08c1 2020-07-23 stsp
780 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
781 11cc08c1 2020-07-23 stsp if (err)
782 3818e3c4 2020-11-01 naddy goto done;
783 3818e3c4 2020-11-01 naddy
784 b2b3fce1 2022-10-29 op if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
785 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
786 11cc08c1 2020-07-23 stsp goto done;
787 3818e3c4 2020-11-01 naddy }
788 11cc08c1 2020-07-23 stsp
789 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
790 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
791 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
792 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
793 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
794 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
795 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
796 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
797 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
798 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
799 11cc08c1 2020-07-23 stsp goto done;
800 11cc08c1 2020-07-23 stsp }
801 11cc08c1 2020-07-23 stsp
802 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
803 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
804 11cc08c1 2020-07-23 stsp goto done;
805 11cc08c1 2020-07-23 stsp }
806 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
807 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
808 11cc08c1 2020-07-23 stsp goto done;
809 11cc08c1 2020-07-23 stsp }
810 11cc08c1 2020-07-23 stsp done:
811 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
812 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
813 11cc08c1 2020-07-23 stsp free(path);
814 11cc08c1 2020-07-23 stsp free(id_str);
815 11cc08c1 2020-07-23 stsp free(label_deriv);
816 11cc08c1 2020-07-23 stsp return err;
817 11cc08c1 2020-07-23 stsp }
818 d219f183 2020-07-23 stsp
819 d219f183 2020-07-23 stsp /* forward declaration */
820 d219f183 2020-07-23 stsp static const struct got_error *
821 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
822 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
823 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
824 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
825 11cc08c1 2020-07-23 stsp
826 11cc08c1 2020-07-23 stsp /*
827 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
828 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
829 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
830 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
831 af57b12a 2020-07-23 stsp */
832 af57b12a 2020-07-23 stsp static const struct got_error *
833 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
834 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
835 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
836 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
837 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
838 af57b12a 2020-07-23 stsp {
839 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
840 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
841 af57b12a 2020-07-23 stsp struct stat sb;
842 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
843 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
844 11cc08c1 2020-07-23 stsp int have_local_change = 0;
845 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
846 af57b12a 2020-07-23 stsp
847 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
848 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
849 af57b12a 2020-07-23 stsp
850 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
851 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
852 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
853 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
854 af57b12a 2020-07-23 stsp ondisk_path);
855 af57b12a 2020-07-23 stsp goto done;
856 af57b12a 2020-07-23 stsp }
857 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
858 af57b12a 2020-07-23 stsp
859 526a746f 2020-07-23 stsp if (blob_orig) {
860 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
861 526a746f 2020-07-23 stsp if (err)
862 526a746f 2020-07-23 stsp goto done;
863 526a746f 2020-07-23 stsp }
864 af57b12a 2020-07-23 stsp
865 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
866 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
867 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
868 11cc08c1 2020-07-23 stsp have_local_change = 1;
869 11cc08c1 2020-07-23 stsp
870 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
871 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
872 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
873 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
874 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
875 11cc08c1 2020-07-23 stsp
876 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
877 11cc08c1 2020-07-23 stsp if (ancestor_target) {
878 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
879 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 11cc08c1 2020-07-23 stsp path);
881 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
882 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
883 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
884 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
885 11cc08c1 2020-07-23 stsp path);
886 11cc08c1 2020-07-23 stsp } else {
887 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
888 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
889 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
890 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
891 11cc08c1 2020-07-23 stsp if (err)
892 11cc08c1 2020-07-23 stsp goto done;
893 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
894 11cc08c1 2020-07-23 stsp path);
895 11cc08c1 2020-07-23 stsp }
896 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
897 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
898 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
899 af57b12a 2020-07-23 stsp strlen(deriv_target));
900 af57b12a 2020-07-23 stsp if (err)
901 af57b12a 2020-07-23 stsp goto done;
902 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
903 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
904 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
905 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
906 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
907 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
908 ea7786be 2020-07-23 stsp path);
909 ea7786be 2020-07-23 stsp } else {
910 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
911 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
912 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
913 ea7786be 2020-07-23 stsp if (err)
914 ea7786be 2020-07-23 stsp goto done;
915 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
916 ea7786be 2020-07-23 stsp path);
917 ea7786be 2020-07-23 stsp }
918 6353ad76 2019-02-08 stsp }
919 11cc08c1 2020-07-23 stsp
920 af57b12a 2020-07-23 stsp done:
921 af57b12a 2020-07-23 stsp free(ancestor_target);
922 eec2f5a9 2021-06-03 stsp return err;
923 eec2f5a9 2021-06-03 stsp }
924 eec2f5a9 2021-06-03 stsp
925 eec2f5a9 2021-06-03 stsp static const struct got_error *
926 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
927 eec2f5a9 2021-06-03 stsp {
928 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
929 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
930 eec2f5a9 2021-06-03 stsp ssize_t target_len;
931 eec2f5a9 2021-06-03 stsp size_t n;
932 eec2f5a9 2021-06-03 stsp FILE *f;
933 eec2f5a9 2021-06-03 stsp
934 eec2f5a9 2021-06-03 stsp *outfile = NULL;
935 eec2f5a9 2021-06-03 stsp
936 eec2f5a9 2021-06-03 stsp f = got_opentemp();
937 eec2f5a9 2021-06-03 stsp if (f == NULL)
938 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
939 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
940 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
941 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
942 eec2f5a9 2021-06-03 stsp goto done;
943 eec2f5a9 2021-06-03 stsp }
944 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
945 eec2f5a9 2021-06-03 stsp if (n != target_len) {
946 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
947 eec2f5a9 2021-06-03 stsp goto done;
948 eec2f5a9 2021-06-03 stsp }
949 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
950 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
951 eec2f5a9 2021-06-03 stsp goto done;
952 eec2f5a9 2021-06-03 stsp }
953 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
954 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
955 eec2f5a9 2021-06-03 stsp goto done;
956 eec2f5a9 2021-06-03 stsp }
957 eec2f5a9 2021-06-03 stsp done:
958 eec2f5a9 2021-06-03 stsp if (err)
959 eec2f5a9 2021-06-03 stsp fclose(f);
960 eec2f5a9 2021-06-03 stsp else
961 eec2f5a9 2021-06-03 stsp *outfile = f;
962 14c901f1 2019-08-08 stsp return err;
963 14c901f1 2019-08-08 stsp }
964 14c901f1 2019-08-08 stsp
965 14c901f1 2019-08-08 stsp /*
966 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
967 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
968 14c901f1 2019-08-08 stsp * acts as the second derived version.
969 14c901f1 2019-08-08 stsp */
970 14c901f1 2019-08-08 stsp static const struct got_error *
971 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
972 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
973 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
974 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
975 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
976 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
977 14c901f1 2019-08-08 stsp {
978 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
979 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
980 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
981 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
982 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
983 14c901f1 2019-08-08 stsp
984 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
985 14c901f1 2019-08-08 stsp
986 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
987 ed6b5030 2020-10-20 stsp if (err)
988 ed6b5030 2020-10-20 stsp return err;
989 14c901f1 2019-08-08 stsp
990 67a66647 2021-05-31 stsp if (blob_orig) {
991 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
992 67a66647 2021-05-31 stsp parent) == -1) {
993 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
994 67a66647 2021-05-31 stsp base_path = NULL;
995 67a66647 2021-05-31 stsp goto done;
996 67a66647 2021-05-31 stsp }
997 67a66647 2021-05-31 stsp
998 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_orig_path, &f_orig,
999 b90054ed 2022-11-01 stsp base_path, "");
1000 67a66647 2021-05-31 stsp if (err)
1001 67a66647 2021-05-31 stsp goto done;
1002 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1003 67a66647 2021-05-31 stsp blob_orig);
1004 67a66647 2021-05-31 stsp if (err)
1005 67a66647 2021-05-31 stsp goto done;
1006 67a66647 2021-05-31 stsp free(base_path);
1007 db590691 2021-06-02 stsp } else {
1008 db590691 2021-06-02 stsp /*
1009 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1010 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1011 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1012 db590691 2021-06-02 stsp */
1013 db590691 2021-06-02 stsp f_orig = got_opentemp();
1014 db590691 2021-06-02 stsp if (f_orig == NULL) {
1015 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1016 db590691 2021-06-02 stsp goto done;
1017 db590691 2021-06-02 stsp }
1018 67a66647 2021-05-31 stsp }
1019 67a66647 2021-05-31 stsp
1020 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1021 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1022 14c901f1 2019-08-08 stsp base_path = NULL;
1023 14c901f1 2019-08-08 stsp goto done;
1024 14c901f1 2019-08-08 stsp }
1025 14c901f1 2019-08-08 stsp
1026 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1027 14c901f1 2019-08-08 stsp if (err)
1028 14c901f1 2019-08-08 stsp goto done;
1029 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1030 14c901f1 2019-08-08 stsp blob_deriv);
1031 14c901f1 2019-08-08 stsp if (err)
1032 14c901f1 2019-08-08 stsp goto done;
1033 14c901f1 2019-08-08 stsp
1034 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1035 14c901f1 2019-08-08 stsp if (err)
1036 14c901f1 2019-08-08 stsp goto done;
1037 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1038 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1039 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1040 14c901f1 2019-08-08 stsp goto done;
1041 eec2f5a9 2021-06-03 stsp }
1042 eec2f5a9 2021-06-03 stsp
1043 5e91dae4 2022-08-30 stsp /*
1044 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1045 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1046 eec2f5a9 2021-06-03 stsp */
1047 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1048 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1049 eec2f5a9 2021-06-03 stsp if (err)
1050 eec2f5a9 2021-06-03 stsp goto done;
1051 eec2f5a9 2021-06-03 stsp } else {
1052 eec2f5a9 2021-06-03 stsp int fd;
1053 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1054 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1055 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1056 eec2f5a9 2021-06-03 stsp goto done;
1057 eec2f5a9 2021-06-03 stsp }
1058 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1059 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1060 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1061 eec2f5a9 2021-06-03 stsp close(fd);
1062 eec2f5a9 2021-06-03 stsp goto done;
1063 eec2f5a9 2021-06-03 stsp }
1064 14c901f1 2019-08-08 stsp }
1065 14c901f1 2019-08-08 stsp
1066 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1067 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1068 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1069 14c901f1 2019-08-08 stsp done:
1070 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1071 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1072 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1073 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1074 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1075 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1076 14c901f1 2019-08-08 stsp free(base_path);
1077 67a66647 2021-05-31 stsp if (blob_orig_path) {
1078 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1079 67a66647 2021-05-31 stsp free(blob_orig_path);
1080 67a66647 2021-05-31 stsp }
1081 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1082 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1083 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1084 14c901f1 2019-08-08 stsp }
1085 6353ad76 2019-02-08 stsp free(id_str);
1086 818c7501 2019-07-11 stsp free(label_deriv);
1087 ed6b5030 2020-10-20 stsp free(parent);
1088 4a1ddfc2 2019-01-12 stsp return err;
1089 4a1ddfc2 2019-01-12 stsp }
1090 4a1ddfc2 2019-01-12 stsp
1091 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1092 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1093 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1094 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1095 13d9040b 2019-03-27 stsp {
1096 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1097 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1098 13d9040b 2019-03-27 stsp
1099 65b05cec 2020-07-23 stsp *new_iep = NULL;
1100 65b05cec 2020-07-23 stsp
1101 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1102 054041d0 2020-06-24 stsp if (err)
1103 054041d0 2020-06-24 stsp return err;
1104 054041d0 2020-06-24 stsp
1105 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1106 3093e2d7 2023-02-04 op blob_id->hash, base_commit_id->hash, 1);
1107 054041d0 2020-06-24 stsp if (err)
1108 054041d0 2020-06-24 stsp goto done;
1109 054041d0 2020-06-24 stsp
1110 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1111 054041d0 2020-06-24 stsp done:
1112 054041d0 2020-06-24 stsp if (err)
1113 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1114 65b05cec 2020-07-23 stsp else
1115 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1116 13d9040b 2019-03-27 stsp return err;
1117 1ebedb77 2019-10-19 stsp }
1118 1ebedb77 2019-10-19 stsp
1119 1ebedb77 2019-10-19 stsp static mode_t
1120 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1121 1ebedb77 2019-10-19 stsp {
1122 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1123 1ebedb77 2019-10-19 stsp
1124 1ebedb77 2019-10-19 stsp if (executable) {
1125 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1126 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1127 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1128 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1129 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1130 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1131 1ebedb77 2019-10-19 stsp }
1132 1ebedb77 2019-10-19 stsp
1133 b2b3fce1 2022-10-29 op return st_mode;
1134 b2b3fce1 2022-10-29 op }
1135 b2b3fce1 2022-10-29 op
1136 b2b3fce1 2022-10-29 op static mode_t
1137 b2b3fce1 2022-10-29 op apply_umask(mode_t mode)
1138 b2b3fce1 2022-10-29 op {
1139 b2b3fce1 2022-10-29 op mode_t um;
1140 b2b3fce1 2022-10-29 op
1141 b2b3fce1 2022-10-29 op um = umask(000);
1142 b2b3fce1 2022-10-29 op umask(um);
1143 b2b3fce1 2022-10-29 op return mode & ~um;
1144 13d9040b 2019-03-27 stsp }
1145 13d9040b 2019-03-27 stsp
1146 8ba819a3 2020-07-23 stsp /* forward declaration */
1147 13d9040b 2019-03-27 stsp static const struct got_error *
1148 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1149 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1150 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1151 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1152 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1153 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 8ba819a3 2020-07-23 stsp
1155 5a1fbc73 2020-07-23 stsp /*
1156 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1157 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1158 5a1fbc73 2020-07-23 stsp */
1159 8ba819a3 2020-07-23 stsp static const struct got_error *
1160 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1161 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1162 5a1fbc73 2020-07-23 stsp {
1163 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1164 5a1fbc73 2020-07-23 stsp ssize_t elen;
1165 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1166 5a1fbc73 2020-07-23 stsp int fd;
1167 5a1fbc73 2020-07-23 stsp
1168 c6e8a826 2021-04-05 stsp *did_something = 0;
1169 c6e8a826 2021-04-05 stsp
1170 5a1fbc73 2020-07-23 stsp /*
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1177 5a1fbc73 2020-07-23 stsp */
1178 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1182 5a1fbc73 2020-07-23 stsp
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1187 5a1fbc73 2020-07-23 stsp
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1191 5a1fbc73 2020-07-23 stsp }
1192 5a1fbc73 2020-07-23 stsp
1193 c6e8a826 2021-04-05 stsp *did_something = 1;
1194 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1195 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1196 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1197 5a1fbc73 2020-07-23 stsp return err;
1198 3c1ec782 2020-07-23 stsp }
1199 3c1ec782 2020-07-23 stsp
1200 3c1ec782 2020-07-23 stsp static const struct got_error *
1201 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1202 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1203 3c1ec782 2020-07-23 stsp {
1204 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1205 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1206 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1207 3c1ec782 2020-07-23 stsp
1208 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1209 3c1ec782 2020-07-23 stsp
1210 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1211 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1212 3c1ec782 2020-07-23 stsp return NULL;
1213 3c1ec782 2020-07-23 stsp }
1214 3c1ec782 2020-07-23 stsp
1215 3c1ec782 2020-07-23 stsp /*
1216 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1217 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1218 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1219 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1220 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1221 3c1ec782 2020-07-23 stsp */
1222 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1223 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1224 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1225 ce031e9e 2020-10-20 stsp if (err)
1226 ce031e9e 2020-10-20 stsp return err;
1227 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1228 ce031e9e 2020-10-20 stsp free(parent);
1229 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1230 ce031e9e 2020-10-20 stsp }
1231 ce031e9e 2020-10-20 stsp free(parent);
1232 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1233 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1234 3c1ec782 2020-07-23 stsp free(abspath);
1235 3c1ec782 2020-07-23 stsp return err;
1236 3c1ec782 2020-07-23 stsp }
1237 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1238 3c1ec782 2020-07-23 stsp free(abspath);
1239 3c1ec782 2020-07-23 stsp if (err)
1240 3c1ec782 2020-07-23 stsp return err;
1241 3c1ec782 2020-07-23 stsp } else {
1242 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1243 3c1ec782 2020-07-23 stsp if (err)
1244 3c1ec782 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp }
1246 3c1ec782 2020-07-23 stsp
1247 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1248 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1249 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1250 3c1ec782 2020-07-23 stsp return NULL;
1251 3c1ec782 2020-07-23 stsp }
1252 3c1ec782 2020-07-23 stsp
1253 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1254 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1255 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1256 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1257 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1259 3c1ec782 2020-07-23 stsp
1260 3c1ec782 2020-07-23 stsp free(path_got);
1261 3c1ec782 2020-07-23 stsp return NULL;
1262 5a1fbc73 2020-07-23 stsp }
1263 5a1fbc73 2020-07-23 stsp
1264 5a1fbc73 2020-07-23 stsp static const struct got_error *
1265 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1266 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1267 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1268 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1269 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1270 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1271 9d31a1d8 2018-03-11 stsp {
1272 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1273 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1274 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1275 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1276 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1277 8ba819a3 2020-07-23 stsp
1278 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1279 2e1fa222 2020-07-23 stsp
1280 3c29341b 2022-07-21 florian /*
1281 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1282 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1283 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1284 8ba819a3 2020-07-23 stsp * in the blob object.
1285 8ba819a3 2020-07-23 stsp */
1286 8ba819a3 2020-07-23 stsp do {
1287 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1288 538b6881 2022-07-21 florian if (err)
1289 538b6881 2022-07-21 florian return err;
1290 538b6881 2022-07-21 florian
1291 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1292 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1293 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1294 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1295 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1296 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1297 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1298 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1299 3b9f0f87 2020-07-23 stsp progress_arg);
1300 8ba819a3 2020-07-23 stsp }
1301 8ba819a3 2020-07-23 stsp if (len > 0) {
1302 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1303 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1304 8ba819a3 2020-07-23 stsp len - hdrlen);
1305 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1306 8ba819a3 2020-07-23 stsp hdrlen = 0;
1307 8ba819a3 2020-07-23 stsp }
1308 8ba819a3 2020-07-23 stsp } while (len != 0);
1309 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1310 8ba819a3 2020-07-23 stsp
1311 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1312 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1313 3c1ec782 2020-07-23 stsp if (err)
1314 3c1ec782 2020-07-23 stsp return err;
1315 8ba819a3 2020-07-23 stsp
1316 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1317 906c123b 2020-07-23 stsp /* install as a regular file */
1318 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1319 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1320 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1321 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1322 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1323 3c29341b 2022-07-21 florian return err;
1324 906c123b 2020-07-23 stsp }
1325 906c123b 2020-07-23 stsp
1326 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1327 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1328 c6e8a826 2021-04-05 stsp int symlink_replaced;
1329 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1330 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1331 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1332 3c29341b 2022-07-21 florian return err;
1333 c90c8ce3 2020-07-23 stsp }
1334 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1335 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1336 5a1fbc73 2020-07-23 stsp if (err)
1337 3c29341b 2022-07-21 florian return err;
1338 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1339 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1340 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1341 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1342 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1343 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1344 c6e8a826 2021-04-05 stsp } else {
1345 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1346 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1347 c6e8a826 2021-04-05 stsp }
1348 f35fa46a 2020-07-23 stsp }
1349 3c29341b 2022-07-21 florian return err; /* Nothing else to do. */
1350 f35fa46a 2020-07-23 stsp }
1351 f35fa46a 2020-07-23 stsp
1352 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1353 f4994adc 2020-10-20 stsp char *parent;
1354 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1355 f4994adc 2020-10-20 stsp if (err)
1356 3c29341b 2022-07-21 florian return err;
1357 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1358 f4994adc 2020-10-20 stsp free(parent);
1359 f35fa46a 2020-07-23 stsp if (err)
1360 3c29341b 2022-07-21 florian return err;
1361 f35fa46a 2020-07-23 stsp /*
1362 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1363 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1364 f35fa46a 2020-07-23 stsp */
1365 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1366 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1367 3c29341b 2022-07-21 florian return err;
1368 f35fa46a 2020-07-23 stsp }
1369 f35fa46a 2020-07-23 stsp }
1370 f35fa46a 2020-07-23 stsp
1371 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1372 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1373 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1374 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1375 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1376 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1377 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1379 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1380 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1381 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1382 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1383 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1384 8ba819a3 2020-07-23 stsp } else {
1385 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1386 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1387 8ba819a3 2020-07-23 stsp }
1388 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1389 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1390 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1391 8ba819a3 2020-07-23 stsp return err;
1392 8ba819a3 2020-07-23 stsp }
1393 8ba819a3 2020-07-23 stsp
1394 8ba819a3 2020-07-23 stsp static const struct got_error *
1395 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1396 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1397 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1398 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1399 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1400 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1401 8ba819a3 2020-07-23 stsp {
1402 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1403 507dc3bb 2018-12-29 stsp int fd = -1;
1404 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1405 507dc3bb 2018-12-29 stsp int update = 0;
1406 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1407 b2b3fce1 2022-10-29 op mode_t mode;
1408 8ba819a3 2020-07-23 stsp
1409 b2b3fce1 2022-10-29 op mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1410 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1411 b2b3fce1 2022-10-29 op O_CLOEXEC, mode);
1412 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1413 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1414 f5375317 2020-10-20 stsp char *parent;
1415 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1416 f5375317 2020-10-20 stsp if (err)
1417 f5375317 2020-10-20 stsp return err;
1418 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1419 f5375317 2020-10-20 stsp free(parent);
1420 21908da4 2019-01-13 stsp if (err)
1421 21908da4 2019-01-13 stsp return err;
1422 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1423 8bd0cdad 2021-12-31 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 b2b3fce1 2022-10-29 op mode);
1425 21908da4 2019-01-13 stsp if (fd == -1)
1426 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1427 230a42bd 2019-05-11 jcs ondisk_path);
1428 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1429 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1430 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1431 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1432 3b9f0f87 2020-07-23 stsp goto done;
1433 3b9f0f87 2020-07-23 stsp }
1434 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1437 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1438 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1439 507dc3bb 2018-12-29 stsp goto done;
1440 d70b8e30 2018-12-27 stsp } else {
1441 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1442 b90054ed 2022-11-01 stsp ondisk_path, "");
1443 507dc3bb 2018-12-29 stsp if (err)
1444 507dc3bb 2018-12-29 stsp goto done;
1445 507dc3bb 2018-12-29 stsp update = 1;
1446 b2b3fce1 2022-10-29 op
1447 b2b3fce1 2022-10-29 op if (fchmod(fd, apply_umask(mode)) == -1) {
1448 b2b3fce1 2022-10-29 op err = got_error_from_errno2("fchmod",
1449 b2b3fce1 2022-10-29 op tmppath);
1450 b2b3fce1 2022-10-29 op goto done;
1451 b2b3fce1 2022-10-29 op }
1452 9d31a1d8 2018-03-11 stsp }
1453 507dc3bb 2018-12-29 stsp } else
1454 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1455 3818e3c4 2020-11-01 naddy }
1456 3818e3c4 2020-11-01 naddy
1457 bd6aa359 2020-07-23 stsp if (progress_cb) {
1458 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1459 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1460 bd6aa359 2020-07-23 stsp path);
1461 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1462 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 bd6aa359 2020-07-23 stsp path);
1464 bd6aa359 2020-07-23 stsp else
1465 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1466 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 bd6aa359 2020-07-23 stsp if (err)
1468 bd6aa359 2020-07-23 stsp goto done;
1469 bd6aa359 2020-07-23 stsp }
1470 d7b62c98 2018-12-27 stsp
1471 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1472 9d31a1d8 2018-03-11 stsp do {
1473 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1475 9d31a1d8 2018-03-11 stsp if (err)
1476 9d31a1d8 2018-03-11 stsp break;
1477 9d31a1d8 2018-03-11 stsp if (len > 0) {
1478 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1479 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1480 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1481 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1482 b87c6f83 2018-12-24 stsp goto done;
1483 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1484 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1485 b87c6f83 2018-12-24 stsp goto done;
1486 9d31a1d8 2018-03-11 stsp }
1487 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1488 9d31a1d8 2018-03-11 stsp }
1489 9d31a1d8 2018-03-11 stsp } while (len != 0);
1490 9d31a1d8 2018-03-11 stsp
1491 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1492 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1493 816dc654 2019-02-16 stsp goto done;
1494 816dc654 2019-02-16 stsp }
1495 9d31a1d8 2018-03-11 stsp
1496 507dc3bb 2018-12-29 stsp if (update) {
1497 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1499 f6d8c0ac 2020-11-09 stsp goto done;
1500 f6d8c0ac 2020-11-09 stsp }
1501 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1502 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1503 230a42bd 2019-05-11 jcs ondisk_path);
1504 68ed9ba5 2019-02-10 stsp goto done;
1505 68ed9ba5 2019-02-10 stsp }
1506 63df146d 2020-11-09 stsp free(tmppath);
1507 63df146d 2020-11-09 stsp tmppath = NULL;
1508 507dc3bb 2018-12-29 stsp }
1509 507dc3bb 2018-12-29 stsp
1510 9d31a1d8 2018-03-11 stsp done:
1511 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1513 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1515 507dc3bb 2018-12-29 stsp free(tmppath);
1516 6353ad76 2019-02-08 stsp return err;
1517 6353ad76 2019-02-08 stsp }
1518 6353ad76 2019-02-08 stsp
1519 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1520 6353ad76 2019-02-08 stsp static const struct got_error *
1521 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1522 7154f6ce 2019-03-27 stsp {
1523 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1524 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1525 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1527 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1528 7154f6ce 2019-03-27 stsp };
1529 7154f6ce 2019-03-27 stsp int i = 0;
1530 9bdd68dd 2021-01-02 naddy char *line = NULL;
1531 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1532 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1533 7154f6ce 2019-03-27 stsp
1534 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1535 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1536 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1537 7154f6ce 2019-03-27 stsp if (feof(f))
1538 7154f6ce 2019-03-27 stsp break;
1539 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1540 7154f6ce 2019-03-27 stsp break;
1541 7154f6ce 2019-03-27 stsp }
1542 7154f6ce 2019-03-27 stsp
1543 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1544 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1545 19332e6d 2019-05-13 stsp == 0)
1546 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1547 7154f6ce 2019-03-27 stsp else
1548 7154f6ce 2019-03-27 stsp i++;
1549 7154f6ce 2019-03-27 stsp }
1550 7154f6ce 2019-03-27 stsp }
1551 9bdd68dd 2021-01-02 naddy free(line);
1552 7154f6ce 2019-03-27 stsp
1553 7154f6ce 2019-03-27 stsp return err;
1554 e2b1e152 2019-07-13 stsp }
1555 e2b1e152 2019-07-13 stsp
1556 e2b1e152 2019-07-13 stsp static int
1557 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1558 1ebedb77 2019-10-19 stsp {
1559 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1560 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1561 1ebedb77 2019-10-19 stsp }
1562 1ebedb77 2019-10-19 stsp
1563 1ebedb77 2019-10-19 stsp static int
1564 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1565 e2b1e152 2019-07-13 stsp {
1566 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1567 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1568 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1569 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1570 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1571 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1572 c363b2c1 2019-08-03 stsp }
1573 c363b2c1 2019-08-03 stsp
1574 c363b2c1 2019-08-03 stsp static unsigned char
1575 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1576 c363b2c1 2019-08-03 stsp {
1577 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1578 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1579 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1580 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1581 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1582 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1583 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1584 c363b2c1 2019-08-03 stsp default:
1585 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1586 a919d5c4 2020-07-23 stsp }
1587 a919d5c4 2020-07-23 stsp }
1588 a919d5c4 2020-07-23 stsp
1589 a919d5c4 2020-07-23 stsp static const struct got_error *
1590 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1591 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1592 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1593 a919d5c4 2020-07-23 stsp {
1594 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1595 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1596 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1597 a919d5c4 2020-07-23 stsp ssize_t elen;
1598 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1599 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1600 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1601 a919d5c4 2020-07-23 stsp
1602 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1603 a919d5c4 2020-07-23 stsp
1604 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1605 a919d5c4 2020-07-23 stsp do {
1606 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1607 a919d5c4 2020-07-23 stsp if (err)
1608 a919d5c4 2020-07-23 stsp return err;
1609 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1610 a919d5c4 2020-07-23 stsp /*
1611 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1612 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1613 a919d5c4 2020-07-23 stsp */
1614 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1615 a919d5c4 2020-07-23 stsp }
1616 a919d5c4 2020-07-23 stsp if (len > 0) {
1617 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1618 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1619 a919d5c4 2020-07-23 stsp len - hdrlen);
1620 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1621 a919d5c4 2020-07-23 stsp hdrlen = 0;
1622 a919d5c4 2020-07-23 stsp }
1623 a919d5c4 2020-07-23 stsp } while (len != 0);
1624 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1625 a919d5c4 2020-07-23 stsp
1626 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1627 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1628 a919d5c4 2020-07-23 stsp if (elen == -1)
1629 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1630 a919d5c4 2020-07-23 stsp } else {
1631 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1632 a919d5c4 2020-07-23 stsp if (elen == -1)
1633 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1634 c363b2c1 2019-08-03 stsp }
1635 a919d5c4 2020-07-23 stsp
1636 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1637 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1638 a919d5c4 2020-07-23 stsp
1639 a919d5c4 2020-07-23 stsp return NULL;
1640 7154f6ce 2019-03-27 stsp }
1641 7154f6ce 2019-03-27 stsp
1642 7154f6ce 2019-03-27 stsp static const struct got_error *
1643 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1644 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1645 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1646 6353ad76 2019-02-08 stsp {
1647 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1648 6353ad76 2019-02-08 stsp struct got_object_id id;
1649 6353ad76 2019-02-08 stsp size_t hdrlen;
1650 eb81bc23 2022-06-28 tracey int fd = -1, fd1 = -1;
1651 6353ad76 2019-02-08 stsp FILE *f = NULL;
1652 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1653 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1654 6353ad76 2019-02-08 stsp size_t flen, blen;
1655 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1656 6353ad76 2019-02-08 stsp
1657 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1658 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1659 6353ad76 2019-02-08 stsp
1660 7f91a133 2019-12-13 stsp /*
1661 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1662 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1663 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1664 7f91a133 2019-12-13 stsp */
1665 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1666 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1667 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1668 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1669 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1670 882ef1b9 2019-12-13 stsp else
1671 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1672 882ef1b9 2019-12-13 stsp goto done;
1673 882ef1b9 2019-12-13 stsp }
1674 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1675 3d35a492 2019-12-13 stsp goto done;
1676 3d35a492 2019-12-13 stsp }
1677 7f91a133 2019-12-13 stsp } else {
1678 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1679 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1680 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1681 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1682 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1683 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1684 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1685 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1686 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1687 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1688 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1689 3d35a492 2019-12-13 stsp else
1690 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1691 3d35a492 2019-12-13 stsp goto done;
1692 3d35a492 2019-12-13 stsp }
1693 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1694 1338848f 2019-12-13 stsp goto done;
1695 a378724f 2019-02-10 stsp }
1696 a378724f 2019-02-10 stsp }
1697 6353ad76 2019-02-08 stsp
1698 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1699 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1700 1338848f 2019-12-13 stsp goto done;
1701 3f148bc6 2019-07-27 stsp }
1702 efdd40df 2019-07-27 stsp
1703 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1704 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1705 1338848f 2019-12-13 stsp goto done;
1706 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1707 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1708 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1709 1338848f 2019-12-13 stsp goto done;
1710 d00136be 2019-03-26 stsp }
1711 b8f41171 2019-02-10 stsp
1712 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1713 f179e66d 2020-07-23 stsp goto done;
1714 f179e66d 2020-07-23 stsp
1715 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1716 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1717 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1718 1338848f 2019-12-13 stsp goto done;
1719 f179e66d 2020-07-23 stsp }
1720 6353ad76 2019-02-08 stsp
1721 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1722 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1723 3093e2d7 2023-02-04 op memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1724 c363b2c1 2019-08-03 stsp else
1725 3093e2d7 2023-02-04 op memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1726 90843939 2023-02-04 op id.algo = got_repo_get_hash_algorithm(repo);
1727 c363b2c1 2019-08-03 stsp
1728 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1729 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1730 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1731 eb81bc23 2022-06-28 tracey goto done;
1732 eb81bc23 2022-06-28 tracey }
1733 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1734 6353ad76 2019-02-08 stsp if (err)
1735 1338848f 2019-12-13 stsp goto done;
1736 6353ad76 2019-02-08 stsp
1737 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1738 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1739 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1740 a919d5c4 2020-07-23 stsp goto done;
1741 a919d5c4 2020-07-23 stsp }
1742 a919d5c4 2020-07-23 stsp
1743 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1744 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 ab0d4361 2019-12-13 stsp if (fd == -1) {
1746 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1747 ab0d4361 2019-12-13 stsp goto done;
1748 ab0d4361 2019-12-13 stsp }
1749 3d35a492 2019-12-13 stsp }
1750 3d35a492 2019-12-13 stsp
1751 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1752 6353ad76 2019-02-08 stsp if (f == NULL) {
1753 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1754 6353ad76 2019-02-08 stsp goto done;
1755 6353ad76 2019-02-08 stsp }
1756 1338848f 2019-12-13 stsp fd = -1;
1757 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1758 656b1f76 2019-05-11 jcs for (;;) {
1759 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1760 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1761 6353ad76 2019-02-08 stsp if (err)
1762 7154f6ce 2019-03-27 stsp goto done;
1763 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1764 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1765 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1766 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1767 7154f6ce 2019-03-27 stsp goto done;
1768 80c5c120 2019-02-19 stsp }
1769 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1770 6353ad76 2019-02-08 stsp if (flen != 0)
1771 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1772 6353ad76 2019-02-08 stsp break;
1773 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1774 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1775 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1776 6353ad76 2019-02-08 stsp break;
1777 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1778 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1779 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1780 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1781 6353ad76 2019-02-08 stsp break;
1782 6353ad76 2019-02-08 stsp }
1783 6353ad76 2019-02-08 stsp } else {
1784 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1785 6353ad76 2019-02-08 stsp break;
1786 6353ad76 2019-02-08 stsp }
1787 6353ad76 2019-02-08 stsp hdrlen = 0;
1788 6353ad76 2019-02-08 stsp }
1789 7154f6ce 2019-03-27 stsp
1790 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1791 7154f6ce 2019-03-27 stsp rewind(f);
1792 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1793 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1794 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1795 6353ad76 2019-02-08 stsp done:
1796 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1797 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1798 6353ad76 2019-02-08 stsp if (blob)
1799 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1800 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1801 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1802 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1803 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1804 9d31a1d8 2018-03-11 stsp return err;
1805 e2b1e152 2019-07-13 stsp }
1806 e2b1e152 2019-07-13 stsp
1807 e2b1e152 2019-07-13 stsp /*
1808 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1809 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1810 e2b1e152 2019-07-13 stsp */
1811 e2b1e152 2019-07-13 stsp static const struct got_error *
1812 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1813 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1814 e2b1e152 2019-07-13 stsp {
1815 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1816 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1817 3093e2d7 2023-02-04 op ie->blob_hash, ie->commit_hash, 1);
1818 e2b1e152 2019-07-13 stsp
1819 e2b1e152 2019-07-13 stsp return NULL;
1820 9d31a1d8 2018-03-11 stsp }
1821 9d31a1d8 2018-03-11 stsp
1822 9d31a1d8 2018-03-11 stsp static const struct got_error *
1823 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1824 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1825 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1826 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1827 0584f854 2019-04-06 stsp void *progress_arg)
1828 9d31a1d8 2018-03-11 stsp {
1829 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1830 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1831 eb81bc23 2022-06-28 tracey char *ondisk_path = NULL;
1832 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1833 b8f41171 2019-02-10 stsp struct stat sb;
1834 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
1835 9d31a1d8 2018-03-11 stsp
1836 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1837 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1838 6353ad76 2019-02-08 stsp
1839 abb4604f 2019-07-27 stsp if (ie) {
1840 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1841 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1842 a76c42e6 2019-08-03 stsp goto done;
1843 a76c42e6 2019-08-03 stsp }
1844 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1845 7f91a133 2019-12-13 stsp repo);
1846 abb4604f 2019-07-27 stsp if (err)
1847 abb4604f 2019-07-27 stsp goto done;
1848 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1849 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1850 c90c8ce3 2020-07-23 stsp } else {
1851 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1852 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1853 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1854 f6764181 2021-09-24 stsp ondisk_path);
1855 f6764181 2021-09-24 stsp goto done;
1856 f6764181 2021-09-24 stsp }
1857 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1858 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1859 f6764181 2021-09-24 stsp } else {
1860 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1861 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1862 f6764181 2021-09-24 stsp else
1863 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1864 f6764181 2021-09-24 stsp }
1865 c90c8ce3 2020-07-23 stsp }
1866 6353ad76 2019-02-08 stsp
1867 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1868 2c41dce7 2021-06-27 stsp if (ie)
1869 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1870 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1871 b8f41171 2019-02-10 stsp goto done;
1872 b8f41171 2019-02-10 stsp }
1873 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1874 a769b60b 2021-06-27 stsp if (ie)
1875 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1876 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1877 5036ab18 2020-04-18 stsp path);
1878 5036ab18 2020-04-18 stsp goto done;
1879 5036ab18 2020-04-18 stsp }
1880 b8f41171 2019-02-10 stsp
1881 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1882 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1883 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1884 c6e8a826 2021-04-05 stsp /*
1885 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1886 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1887 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1888 c6e8a826 2021-04-05 stsp * updating contents of this file.
1889 c6e8a826 2021-04-05 stsp */
1890 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1891 3093e2d7 2023-02-04 op memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1892 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1893 c6e8a826 2021-04-05 stsp /* Same commit. */
1894 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1895 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1896 e2b1e152 2019-07-13 stsp if (err)
1897 e2b1e152 2019-07-13 stsp goto done;
1898 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1899 b8f41171 2019-02-10 stsp path);
1900 6353ad76 2019-02-08 stsp goto done;
1901 a378724f 2019-02-10 stsp }
1902 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1903 3093e2d7 2023-02-04 op memcmp(ie->blob_hash, te->id.hash,
1904 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1905 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1906 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1907 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1908 0f58026f 2021-04-05 stsp if (err)
1909 0f58026f 2021-04-05 stsp goto done;
1910 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1911 0f58026f 2021-04-05 stsp path);
1912 b8f41171 2019-02-10 stsp goto done;
1913 e2b1e152 2019-07-13 stsp }
1914 9d31a1d8 2018-03-11 stsp }
1915 9d31a1d8 2018-03-11 stsp
1916 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
1917 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
1918 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1919 eb81bc23 2022-06-28 tracey goto done;
1920 eb81bc23 2022-06-28 tracey }
1921 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1922 8da9e5f4 2019-01-12 stsp if (err)
1923 6353ad76 2019-02-08 stsp goto done;
1924 512f0d0e 2019-01-02 stsp
1925 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1926 234035bc 2019-06-01 stsp int update_timestamps;
1927 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1928 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1929 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1930 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
1931 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
1932 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
1933 eb81bc23 2022-06-28 tracey goto done;
1934 eb81bc23 2022-06-28 tracey }
1935 234035bc 2019-06-01 stsp struct got_object_id id2;
1936 3093e2d7 2023-02-04 op memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1937 dfe0a3d8 2023-02-04 op id2.algo = got_repo_get_hash_algorithm(repo);
1938 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1939 eb81bc23 2022-06-28 tracey fd2);
1940 234035bc 2019-06-01 stsp if (err)
1941 f69721c3 2019-10-21 stsp goto done;
1942 f69721c3 2019-10-21 stsp }
1943 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1944 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1945 3093e2d7 2023-02-04 op if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1946 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1947 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1948 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1949 f69721c3 2019-10-21 stsp goto done;
1950 f69721c3 2019-10-21 stsp }
1951 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1952 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1953 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1954 234035bc 2019-06-01 stsp goto done;
1955 f69721c3 2019-10-21 stsp }
1956 234035bc 2019-06-01 stsp }
1957 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1958 36bf999c 2020-07-23 stsp char *link_target;
1959 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1960 36bf999c 2020-07-23 stsp if (err)
1961 36bf999c 2020-07-23 stsp goto done;
1962 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1963 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1964 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1965 36bf999c 2020-07-23 stsp free(link_target);
1966 993e2a1b 2020-07-23 stsp } else {
1967 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1968 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1969 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1970 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1971 993e2a1b 2020-07-23 stsp }
1972 f69721c3 2019-10-21 stsp free(label_orig);
1973 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1974 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
1975 eb81bc23 2022-06-28 tracey goto done;
1976 eb81bc23 2022-06-28 tracey }
1977 234035bc 2019-06-01 stsp if (blob2)
1978 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1979 909d120e 2019-09-22 stsp if (err)
1980 909d120e 2019-09-22 stsp goto done;
1981 234035bc 2019-06-01 stsp /*
1982 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1983 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1984 234035bc 2019-06-01 stsp * unmodified files again.
1985 234035bc 2019-06-01 stsp */
1986 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 3093e2d7 2023-02-04 op blob->id.hash, worktree->base_commit_id->hash,
1988 234035bc 2019-06-01 stsp update_timestamps);
1989 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1990 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1991 3093e2d7 2023-02-04 op blob->id.hash, worktree->base_commit_id->hash, 0);
1992 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1993 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1994 1ee397ad 2019-07-12 stsp if (err)
1995 1ee397ad 2019-07-12 stsp goto done;
1996 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1997 3093e2d7 2023-02-04 op blob->id.hash, worktree->base_commit_id->hash, 0);
1998 13d9040b 2019-03-27 stsp if (err)
1999 13d9040b 2019-03-27 stsp goto done;
2000 13d9040b 2019-03-27 stsp } else {
2001 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2002 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2003 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2004 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2005 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2006 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2007 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2008 2e1fa222 2020-07-23 stsp } else {
2009 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2010 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2011 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2012 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2013 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2014 2e1fa222 2020-07-23 stsp }
2015 13d9040b 2019-03-27 stsp if (err)
2016 13d9040b 2019-03-27 stsp goto done;
2017 65b05cec 2020-07-23 stsp
2018 054041d0 2020-06-24 stsp if (ie) {
2019 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2020 3093e2d7 2023-02-04 op worktree->root_fd, path, blob->id.hash,
2021 3093e2d7 2023-02-04 op worktree->base_commit_id->hash, 1);
2022 054041d0 2020-06-24 stsp } else {
2023 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2024 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2025 054041d0 2020-06-24 stsp &blob->id);
2026 054041d0 2020-06-24 stsp }
2027 13d9040b 2019-03-27 stsp if (err)
2028 13d9040b 2019-03-27 stsp goto done;
2029 65b05cec 2020-07-23 stsp
2030 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2031 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2032 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2033 2e1fa222 2020-07-23 stsp }
2034 13d9040b 2019-03-27 stsp }
2035 eb81bc23 2022-06-28 tracey
2036 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2037 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
2038 eb81bc23 2022-06-28 tracey goto done;
2039 eb81bc23 2022-06-28 tracey }
2040 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2041 6353ad76 2019-02-08 stsp done:
2042 6353ad76 2019-02-08 stsp free(ondisk_path);
2043 8da9e5f4 2019-01-12 stsp return err;
2044 90285c3b 2019-01-08 stsp }
2045 90285c3b 2019-01-08 stsp
2046 90285c3b 2019-01-08 stsp static const struct got_error *
2047 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2048 90285c3b 2019-01-08 stsp {
2049 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2050 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2051 90285c3b 2019-01-08 stsp
2052 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2053 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2054 90285c3b 2019-01-08 stsp
2055 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2056 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2057 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2058 c97665ae 2019-01-13 stsp } else {
2059 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2060 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2061 1c4cdd89 2021-06-20 stsp if (err)
2062 1c4cdd89 2021-06-20 stsp goto done;
2063 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2064 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2065 fddefe3b 2020-10-20 stsp free(ondisk_path);
2066 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2067 1c4cdd89 2021-06-20 stsp parent = NULL;
2068 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2069 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2070 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2071 fddefe3b 2020-10-20 stsp ondisk_path);
2072 90285c3b 2019-01-08 stsp break;
2073 90285c3b 2019-01-08 stsp }
2074 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2075 1c4cdd89 2021-06-20 stsp if (err)
2076 1c4cdd89 2021-06-20 stsp break;
2077 1c4cdd89 2021-06-20 stsp }
2078 90285c3b 2019-01-08 stsp }
2079 1c4cdd89 2021-06-20 stsp done:
2080 90285c3b 2019-01-08 stsp free(ondisk_path);
2081 1c4cdd89 2021-06-20 stsp free(parent);
2082 90285c3b 2019-01-08 stsp return err;
2083 512f0d0e 2019-01-02 stsp }
2084 512f0d0e 2019-01-02 stsp
2085 708d8e67 2019-03-27 stsp static const struct got_error *
2086 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2087 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2088 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2089 708d8e67 2019-03-27 stsp {
2090 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2091 708d8e67 2019-03-27 stsp unsigned char status;
2092 708d8e67 2019-03-27 stsp struct stat sb;
2093 708d8e67 2019-03-27 stsp char *ondisk_path;
2094 708d8e67 2019-03-27 stsp
2095 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2096 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2097 a76c42e6 2019-08-03 stsp
2098 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2099 708d8e67 2019-03-27 stsp == -1)
2100 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2101 708d8e67 2019-03-27 stsp
2102 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2103 708d8e67 2019-03-27 stsp if (err)
2104 5a58a424 2020-06-23 stsp goto done;
2105 708d8e67 2019-03-27 stsp
2106 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2107 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2108 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2109 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2110 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2111 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2112 993e2a1b 2020-07-23 stsp goto done;
2113 993e2a1b 2020-07-23 stsp }
2114 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2115 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2116 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2117 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2118 993e2a1b 2020-07-23 stsp if (err)
2119 993e2a1b 2020-07-23 stsp goto done;
2120 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2121 993e2a1b 2020-07-23 stsp ie->path);
2122 993e2a1b 2020-07-23 stsp goto done;
2123 993e2a1b 2020-07-23 stsp }
2124 993e2a1b 2020-07-23 stsp
2125 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2126 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2127 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2128 1ee397ad 2019-07-12 stsp if (err)
2129 5a58a424 2020-06-23 stsp goto done;
2130 fc6346c4 2019-03-27 stsp /*
2131 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2132 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2133 fc6346c4 2019-03-27 stsp */
2134 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2135 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2136 fc6346c4 2019-03-27 stsp } else {
2137 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2138 1ee397ad 2019-07-12 stsp if (err)
2139 5a58a424 2020-06-23 stsp goto done;
2140 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2141 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2142 fc6346c4 2019-03-27 stsp if (err)
2143 5a58a424 2020-06-23 stsp goto done;
2144 fc6346c4 2019-03-27 stsp }
2145 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2146 708d8e67 2019-03-27 stsp }
2147 5a58a424 2020-06-23 stsp done:
2148 5a58a424 2020-06-23 stsp free(ondisk_path);
2149 fc6346c4 2019-03-27 stsp return err;
2150 708d8e67 2019-03-27 stsp }
2151 708d8e67 2019-03-27 stsp
2152 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2153 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2154 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2155 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2156 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2157 8da9e5f4 2019-01-12 stsp void *progress_arg;
2158 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2159 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2160 8da9e5f4 2019-01-12 stsp };
2161 8da9e5f4 2019-01-12 stsp
2162 512f0d0e 2019-01-02 stsp static const struct got_error *
2163 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2164 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2165 512f0d0e 2019-01-02 stsp {
2166 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2167 0584f854 2019-04-06 stsp
2168 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2169 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2170 512f0d0e 2019-01-02 stsp
2171 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2172 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2173 8da9e5f4 2019-01-12 stsp }
2174 512f0d0e 2019-01-02 stsp
2175 8da9e5f4 2019-01-12 stsp static const struct got_error *
2176 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2177 8da9e5f4 2019-01-12 stsp {
2178 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2179 7a9df742 2019-01-08 stsp
2180 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2181 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2182 0584f854 2019-04-06 stsp
2183 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2184 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2185 9d31a1d8 2018-03-11 stsp }
2186 9d31a1d8 2018-03-11 stsp
2187 9d31a1d8 2018-03-11 stsp static const struct got_error *
2188 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2189 9d31a1d8 2018-03-11 stsp {
2190 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2191 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2192 8da9e5f4 2019-01-12 stsp char *path;
2193 9d31a1d8 2018-03-11 stsp
2194 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2195 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2196 0584f854 2019-04-06 stsp
2197 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2198 63c5ca5d 2019-08-24 stsp return NULL;
2199 63c5ca5d 2019-08-24 stsp
2200 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2201 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2202 8da9e5f4 2019-01-12 stsp == -1)
2203 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2204 9d31a1d8 2018-03-11 stsp
2205 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2206 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2207 8da9e5f4 2019-01-12 stsp else
2208 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2209 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2210 9d31a1d8 2018-03-11 stsp
2211 8da9e5f4 2019-01-12 stsp free(path);
2212 0cd1c46a 2019-03-11 stsp return err;
2213 0cd1c46a 2019-03-11 stsp }
2214 0cd1c46a 2019-03-11 stsp
2215 b2118c49 2020-07-28 stsp const struct got_error *
2216 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2217 b2118c49 2020-07-28 stsp {
2218 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2219 b2118c49 2020-07-28 stsp
2220 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2221 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2222 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2223 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2224 b2118c49 2020-07-28 stsp }
2225 b2118c49 2020-07-28 stsp
2226 b2118c49 2020-07-28 stsp return NULL;
2227 b2118c49 2020-07-28 stsp }
2228 b2118c49 2020-07-28 stsp
2229 818c7501 2019-07-11 stsp static const struct got_error *
2230 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2231 0cd1c46a 2019-03-11 stsp {
2232 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2233 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2234 0cd1c46a 2019-03-11 stsp
2235 517bab73 2019-03-11 stsp *refname = NULL;
2236 517bab73 2019-03-11 stsp
2237 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2238 b2118c49 2020-07-28 stsp if (err)
2239 b2118c49 2020-07-28 stsp return err;
2240 0cd1c46a 2019-03-11 stsp
2241 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2243 0647c563 2019-03-11 stsp *refname = NULL;
2244 0cd1c46a 2019-03-11 stsp }
2245 517bab73 2019-03-11 stsp free(uuidstr);
2246 517bab73 2019-03-11 stsp return err;
2247 517bab73 2019-03-11 stsp }
2248 517bab73 2019-03-11 stsp
2249 818c7501 2019-07-11 stsp const struct got_error *
2250 9587e6cc 2023-01-28 mark got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2251 9587e6cc 2023-01-28 mark const char *prefix)
2252 9587e6cc 2023-01-28 mark {
2253 9587e6cc 2023-01-28 mark return get_ref_name(refname, worktree, prefix);
2254 9587e6cc 2023-01-28 mark }
2255 9587e6cc 2023-01-28 mark
2256 9587e6cc 2023-01-28 mark const struct got_error *
2257 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2258 818c7501 2019-07-11 stsp {
2259 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2260 818c7501 2019-07-11 stsp }
2261 818c7501 2019-07-11 stsp
2262 818c7501 2019-07-11 stsp static const struct got_error *
2263 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2264 818c7501 2019-07-11 stsp {
2265 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2266 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2267 818c7501 2019-07-11 stsp }
2268 818c7501 2019-07-11 stsp
2269 818c7501 2019-07-11 stsp static const struct got_error *
2270 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2271 818c7501 2019-07-11 stsp {
2272 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2273 818c7501 2019-07-11 stsp }
2274 818c7501 2019-07-11 stsp
2275 818c7501 2019-07-11 stsp static const struct got_error *
2276 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2277 818c7501 2019-07-11 stsp {
2278 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2279 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2280 818c7501 2019-07-11 stsp }
2281 818c7501 2019-07-11 stsp
2282 818c7501 2019-07-11 stsp static const struct got_error *
2283 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 818c7501 2019-07-11 stsp {
2285 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2286 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2287 0ebf8283 2019-07-24 stsp }
2288 0ebf8283 2019-07-24 stsp
2289 0ebf8283 2019-07-24 stsp static const struct got_error *
2290 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2291 0ebf8283 2019-07-24 stsp {
2292 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2293 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2294 0ebf8283 2019-07-24 stsp }
2295 0ebf8283 2019-07-24 stsp
2296 0ebf8283 2019-07-24 stsp static const struct got_error *
2297 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2298 0ebf8283 2019-07-24 stsp {
2299 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2300 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2301 0ebf8283 2019-07-24 stsp }
2302 0ebf8283 2019-07-24 stsp
2303 0ebf8283 2019-07-24 stsp static const struct got_error *
2304 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2305 0ebf8283 2019-07-24 stsp {
2306 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2307 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2308 818c7501 2019-07-11 stsp }
2309 818c7501 2019-07-11 stsp
2310 0ebf8283 2019-07-24 stsp static const struct got_error *
2311 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 0ebf8283 2019-07-24 stsp {
2313 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2314 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2315 0ebf8283 2019-07-24 stsp }
2316 818c7501 2019-07-11 stsp
2317 0ebf8283 2019-07-24 stsp const struct got_error *
2318 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2319 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2320 0ebf8283 2019-07-24 stsp {
2321 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2322 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2323 0ebf8283 2019-07-24 stsp *path = NULL;
2324 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2325 0ebf8283 2019-07-24 stsp }
2326 0ebf8283 2019-07-24 stsp return NULL;
2327 f259c4c1 2021-09-24 stsp }
2328 f259c4c1 2021-09-24 stsp
2329 f259c4c1 2021-09-24 stsp static const struct got_error *
2330 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2331 f259c4c1 2021-09-24 stsp {
2332 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2333 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2334 0ebf8283 2019-07-24 stsp }
2335 0ebf8283 2019-07-24 stsp
2336 f259c4c1 2021-09-24 stsp static const struct got_error *
2337 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2338 f259c4c1 2021-09-24 stsp {
2339 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2340 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2341 f259c4c1 2021-09-24 stsp }
2342 f259c4c1 2021-09-24 stsp
2343 517bab73 2019-03-11 stsp /*
2344 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2345 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2346 517bab73 2019-03-11 stsp */
2347 517bab73 2019-03-11 stsp static const struct got_error *
2348 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2349 517bab73 2019-03-11 stsp {
2350 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2351 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2352 517bab73 2019-03-11 stsp char *refname;
2353 517bab73 2019-03-11 stsp
2354 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2355 517bab73 2019-03-11 stsp if (err)
2356 517bab73 2019-03-11 stsp return err;
2357 0cd1c46a 2019-03-11 stsp
2358 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2359 0cd1c46a 2019-03-11 stsp if (err)
2360 0cd1c46a 2019-03-11 stsp goto done;
2361 0cd1c46a 2019-03-11 stsp
2362 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2363 0cd1c46a 2019-03-11 stsp done:
2364 0cd1c46a 2019-03-11 stsp free(refname);
2365 0cd1c46a 2019-03-11 stsp if (ref)
2366 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2367 3e3a69f1 2019-07-25 stsp return err;
2368 3e3a69f1 2019-07-25 stsp }
2369 3e3a69f1 2019-07-25 stsp
2370 3e3a69f1 2019-07-25 stsp static const struct got_error *
2371 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2372 3e3a69f1 2019-07-25 stsp {
2373 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2374 3e3a69f1 2019-07-25 stsp
2375 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2376 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2377 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2378 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2379 3e3a69f1 2019-07-25 stsp }
2380 9d31a1d8 2018-03-11 stsp return err;
2381 9d31a1d8 2018-03-11 stsp }
2382 9d31a1d8 2018-03-11 stsp
2383 3e3a69f1 2019-07-25 stsp
2384 ebf99748 2019-05-09 stsp static const struct got_error *
2385 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2386 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2387 ebf99748 2019-05-09 stsp {
2388 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2389 ebf99748 2019-05-09 stsp FILE *index = NULL;
2390 0cd1c46a 2019-03-11 stsp
2391 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2392 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2393 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2394 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2395 ebf99748 2019-05-09 stsp
2396 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2397 3e3a69f1 2019-07-25 stsp if (err)
2398 ebf99748 2019-05-09 stsp goto done;
2399 ebf99748 2019-05-09 stsp
2400 00fe21f2 2021-12-31 stsp index = fopen(*fileindex_path, "rbe");
2401 ebf99748 2019-05-09 stsp if (index == NULL) {
2402 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2403 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2404 ebf99748 2019-05-09 stsp } else {
2405 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2406 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2407 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2408 ebf99748 2019-05-09 stsp }
2409 ebf99748 2019-05-09 stsp done:
2410 ebf99748 2019-05-09 stsp if (err) {
2411 ebf99748 2019-05-09 stsp free(*fileindex_path);
2412 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2413 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2414 ebf99748 2019-05-09 stsp *fileindex = NULL;
2415 ebf99748 2019-05-09 stsp }
2416 ebf99748 2019-05-09 stsp return err;
2417 ebf99748 2019-05-09 stsp }
2418 c932eeeb 2019-05-22 stsp
2419 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2420 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2421 c932eeeb 2019-05-22 stsp const char *path;
2422 c932eeeb 2019-05-22 stsp size_t path_len;
2423 c932eeeb 2019-05-22 stsp const char *entry_name;
2424 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2425 a484d721 2019-06-10 stsp void *progress_arg;
2426 c932eeeb 2019-05-22 stsp };
2427 ebf99748 2019-05-09 stsp
2428 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2429 c932eeeb 2019-05-22 stsp static const struct got_error *
2430 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2431 c932eeeb 2019-05-22 stsp {
2432 1ee397ad 2019-07-12 stsp const struct got_error *err;
2433 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2434 c932eeeb 2019-05-22 stsp
2435 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2436 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2437 c932eeeb 2019-05-22 stsp return NULL;
2438 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2439 102ff934 2019-06-11 stsp return NULL;
2440 102ff934 2019-06-11 stsp
2441 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2442 a769b60b 2021-06-27 stsp return NULL;
2443 a769b60b 2021-06-27 stsp
2444 3093e2d7 2023-02-04 op if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2445 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2446 c932eeeb 2019-05-22 stsp return NULL;
2447 c932eeeb 2019-05-22 stsp
2448 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2449 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2450 edd02c5e 2019-07-11 stsp ie->path);
2451 1ee397ad 2019-07-12 stsp if (err)
2452 1ee397ad 2019-07-12 stsp return err;
2453 1ee397ad 2019-07-12 stsp }
2454 3093e2d7 2023-02-04 op memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2455 c932eeeb 2019-05-22 stsp return NULL;
2456 a615e0e7 2020-12-16 stsp }
2457 a615e0e7 2020-12-16 stsp
2458 a615e0e7 2020-12-16 stsp static const struct got_error *
2459 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2460 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2461 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2462 a615e0e7 2020-12-16 stsp {
2463 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2464 a615e0e7 2020-12-16 stsp
2465 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2466 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2467 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2468 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2469 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2470 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2471 a615e0e7 2020-12-16 stsp
2472 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2473 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2474 9c6338c4 2019-06-02 stsp }
2475 9c6338c4 2019-06-02 stsp
2476 9c6338c4 2019-06-02 stsp static const struct got_error *
2477 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2478 9c6338c4 2019-06-02 stsp {
2479 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2480 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2481 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2482 867630bb 2020-01-17 stsp struct timespec timeout;
2483 9c6338c4 2019-06-02 stsp
2484 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2485 b90054ed 2022-11-01 stsp fileindex_path, "");
2486 9c6338c4 2019-06-02 stsp if (err)
2487 9c6338c4 2019-06-02 stsp goto done;
2488 9c6338c4 2019-06-02 stsp
2489 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2490 9c6338c4 2019-06-02 stsp if (err)
2491 9c6338c4 2019-06-02 stsp goto done;
2492 9c6338c4 2019-06-02 stsp
2493 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2494 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2495 9c6338c4 2019-06-02 stsp fileindex_path);
2496 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2497 9c6338c4 2019-06-02 stsp }
2498 867630bb 2020-01-17 stsp
2499 867630bb 2020-01-17 stsp /*
2500 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2501 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2502 867630bb 2020-01-17 stsp * was recorded in the file index.
2503 867630bb 2020-01-17 stsp */
2504 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2505 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2506 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2507 9c6338c4 2019-06-02 stsp done:
2508 9c6338c4 2019-06-02 stsp if (new_index)
2509 9c6338c4 2019-06-02 stsp fclose(new_index);
2510 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2511 9c6338c4 2019-06-02 stsp return err;
2512 c932eeeb 2019-05-22 stsp }
2513 6ced4176 2019-07-12 stsp
2514 6ced4176 2019-07-12 stsp static const struct got_error *
2515 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2516 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2517 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit, struct got_worktree *worktree,
2518 a44927cc 2022-04-07 stsp struct got_repository *repo)
2519 6ced4176 2019-07-12 stsp {
2520 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2521 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2522 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2523 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2524 6ced4176 2019-07-12 stsp
2525 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2526 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2527 6ced4176 2019-07-12 stsp *tree_id = NULL;
2528 6ced4176 2019-07-12 stsp
2529 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2530 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2531 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2532 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2533 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2534 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2535 6ced4176 2019-07-12 stsp goto done;
2536 6ced4176 2019-07-12 stsp }
2537 a44927cc 2022-04-07 stsp err = got_object_id_by_path(tree_id, repo, base_commit,
2538 a44927cc 2022-04-07 stsp worktree->path_prefix);
2539 6ced4176 2019-07-12 stsp if (err)
2540 6ced4176 2019-07-12 stsp goto done;
2541 6ced4176 2019-07-12 stsp return NULL;
2542 6ced4176 2019-07-12 stsp }
2543 6ced4176 2019-07-12 stsp
2544 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2545 6ced4176 2019-07-12 stsp
2546 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2547 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2548 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2549 6ced4176 2019-07-12 stsp goto done;
2550 6ced4176 2019-07-12 stsp }
2551 6ced4176 2019-07-12 stsp
2552 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2553 6ced4176 2019-07-12 stsp if (err)
2554 6ced4176 2019-07-12 stsp goto done;
2555 6ced4176 2019-07-12 stsp
2556 6ced4176 2019-07-12 stsp free(in_repo_path);
2557 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2558 6ced4176 2019-07-12 stsp
2559 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2560 6ced4176 2019-07-12 stsp if (err)
2561 6ced4176 2019-07-12 stsp goto done;
2562 c932eeeb 2019-05-22 stsp
2563 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2564 6ced4176 2019-07-12 stsp /* Check out a single file. */
2565 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2566 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2567 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2568 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2569 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2570 6ced4176 2019-07-12 stsp goto done;
2571 6ced4176 2019-07-12 stsp }
2572 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2573 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2574 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2575 6ced4176 2019-07-12 stsp goto done;
2576 6ced4176 2019-07-12 stsp }
2577 6ced4176 2019-07-12 stsp } else {
2578 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2579 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2580 6ced4176 2019-07-12 stsp if (err)
2581 6ced4176 2019-07-12 stsp return err;
2582 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2583 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2584 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2585 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2586 6ced4176 2019-07-12 stsp goto done;
2587 6ced4176 2019-07-12 stsp }
2588 6ced4176 2019-07-12 stsp }
2589 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2590 a44927cc 2022-04-07 stsp base_commit, in_repo_path);
2591 6ced4176 2019-07-12 stsp } else {
2592 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2593 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2594 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2595 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2596 6ced4176 2019-07-12 stsp goto done;
2597 6ced4176 2019-07-12 stsp }
2598 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2599 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2600 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2601 6ced4176 2019-07-12 stsp goto done;
2602 6ced4176 2019-07-12 stsp }
2603 6ced4176 2019-07-12 stsp }
2604 6ced4176 2019-07-12 stsp done:
2605 6ced4176 2019-07-12 stsp free(id);
2606 6ced4176 2019-07-12 stsp free(in_repo_path);
2607 6ced4176 2019-07-12 stsp if (err) {
2608 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2609 6ced4176 2019-07-12 stsp free(*tree_relpath);
2610 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2611 6ced4176 2019-07-12 stsp free(*tree_id);
2612 6ced4176 2019-07-12 stsp *tree_id = NULL;
2613 6ced4176 2019-07-12 stsp }
2614 07ed472d 2019-07-12 stsp return err;
2615 07ed472d 2019-07-12 stsp }
2616 07ed472d 2019-07-12 stsp
2617 07ed472d 2019-07-12 stsp static const struct got_error *
2618 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2619 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2620 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2621 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2622 07ed472d 2019-07-12 stsp {
2623 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2624 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2625 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2626 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2627 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2628 07ed472d 2019-07-12 stsp
2629 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2630 7f47418f 2019-12-20 stsp if (err) {
2631 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2632 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2633 7f47418f 2019-12-20 stsp goto done;
2634 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2635 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2636 7f47418f 2019-12-20 stsp if (err)
2637 7f47418f 2019-12-20 stsp return err;
2638 7f47418f 2019-12-20 stsp }
2639 07ed472d 2019-07-12 stsp
2640 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2641 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2642 07ed472d 2019-07-12 stsp if (err)
2643 07ed472d 2019-07-12 stsp goto done;
2644 07ed472d 2019-07-12 stsp
2645 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2646 07ed472d 2019-07-12 stsp if (err)
2647 07ed472d 2019-07-12 stsp goto done;
2648 07ed472d 2019-07-12 stsp
2649 07ed472d 2019-07-12 stsp if (entry_name &&
2650 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2651 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2652 07ed472d 2019-07-12 stsp goto done;
2653 07ed472d 2019-07-12 stsp }
2654 07ed472d 2019-07-12 stsp
2655 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2656 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2657 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2658 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2659 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2660 07ed472d 2019-07-12 stsp arg.repo = repo;
2661 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2662 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2663 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2664 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2665 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2666 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2667 07ed472d 2019-07-12 stsp done:
2668 07ed472d 2019-07-12 stsp if (tree)
2669 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2670 07ed472d 2019-07-12 stsp if (commit)
2671 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2672 6ced4176 2019-07-12 stsp return err;
2673 6ced4176 2019-07-12 stsp }
2674 6ced4176 2019-07-12 stsp
2675 9d31a1d8 2018-03-11 stsp const struct got_error *
2676 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2677 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2678 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2679 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2680 9d31a1d8 2018-03-11 stsp {
2681 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2682 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2683 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2684 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2685 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2686 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2687 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2688 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2689 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2690 f2ea84fa 2019-07-27 stsp int entry_type;
2691 f2ea84fa 2019-07-27 stsp char *relpath;
2692 f2ea84fa 2019-07-27 stsp char *entry_name;
2693 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2694 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2695 9d31a1d8 2018-03-11 stsp
2696 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2697 f2ea84fa 2019-07-27 stsp
2698 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2699 9d31a1d8 2018-03-11 stsp if (err)
2700 9d31a1d8 2018-03-11 stsp return err;
2701 a44927cc 2022-04-07 stsp
2702 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
2703 a44927cc 2022-04-07 stsp worktree->base_commit_id);
2704 a44927cc 2022-04-07 stsp if (err)
2705 a44927cc 2022-04-07 stsp goto done;
2706 93a30277 2018-12-24 stsp
2707 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2708 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2709 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2710 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2711 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2712 f2ea84fa 2019-07-27 stsp goto done;
2713 f2ea84fa 2019-07-27 stsp }
2714 6ced4176 2019-07-12 stsp
2715 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2716 a44927cc 2022-04-07 stsp &tpd->relpath, &tpd->tree_id, pe->path, commit,
2717 a44927cc 2022-04-07 stsp worktree, repo);
2718 f2ea84fa 2019-07-27 stsp if (err) {
2719 f2ea84fa 2019-07-27 stsp free(tpd);
2720 6ced4176 2019-07-12 stsp goto done;
2721 6ced4176 2019-07-12 stsp }
2722 f2ea84fa 2019-07-27 stsp
2723 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2724 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2725 f2ea84fa 2019-07-27 stsp if (err) {
2726 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2727 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2728 f2ea84fa 2019-07-27 stsp free(tpd);
2729 f2ea84fa 2019-07-27 stsp goto done;
2730 f2ea84fa 2019-07-27 stsp }
2731 f2ea84fa 2019-07-27 stsp } else
2732 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2733 f2ea84fa 2019-07-27 stsp
2734 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2735 6ced4176 2019-07-12 stsp }
2736 6ced4176 2019-07-12 stsp
2737 51514078 2018-12-25 stsp /*
2738 51514078 2018-12-25 stsp * Read the file index.
2739 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2740 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2741 51514078 2018-12-25 stsp */
2742 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2743 8da9e5f4 2019-01-12 stsp if (err)
2744 c4cdcb68 2019-04-03 stsp goto done;
2745 c4cdcb68 2019-04-03 stsp
2746 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2747 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2748 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2749 f2ea84fa 2019-07-27 stsp
2750 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2751 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2752 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2753 f2ea84fa 2019-07-27 stsp if (err)
2754 f2ea84fa 2019-07-27 stsp break;
2755 f2ea84fa 2019-07-27 stsp
2756 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2757 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2758 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2759 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2760 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2761 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2762 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2763 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2764 f2ea84fa 2019-07-27 stsp if (err)
2765 f2ea84fa 2019-07-27 stsp break;
2766 f2ea84fa 2019-07-27 stsp
2767 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2768 711d9cd9 2019-07-12 stsp }
2769 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2770 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2771 af12c6b9 2019-06-04 stsp err = sync_err;
2772 9d31a1d8 2018-03-11 stsp done:
2773 9c6338c4 2019-06-02 stsp free(fileindex_path);
2774 edfa7d7f 2018-09-11 stsp if (tree)
2775 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2776 9d31a1d8 2018-03-11 stsp if (commit)
2777 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2778 5ade8233 2019-07-12 stsp if (fileindex)
2779 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2780 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2781 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2782 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2783 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2784 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2785 f2ea84fa 2019-07-27 stsp free(tpd);
2786 f2ea84fa 2019-07-27 stsp }
2787 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2788 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2789 9d31a1d8 2018-03-11 stsp err = unlockerr;
2790 f8d1f275 2019-02-04 stsp return err;
2791 f8d1f275 2019-02-04 stsp }
2792 f8d1f275 2019-02-04 stsp
2793 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2794 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2795 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2796 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2797 234035bc 2019-06-01 stsp void *progress_arg;
2798 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2799 234035bc 2019-06-01 stsp void *cancel_arg;
2800 f69721c3 2019-10-21 stsp const char *label_orig;
2801 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2802 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2803 234035bc 2019-06-01 stsp };
2804 234035bc 2019-06-01 stsp
2805 234035bc 2019-06-01 stsp static const struct got_error *
2806 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2807 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
2808 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
2809 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
2810 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2811 234035bc 2019-06-01 stsp {
2812 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2813 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2814 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2815 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2816 234035bc 2019-06-01 stsp struct stat sb;
2817 234035bc 2019-06-01 stsp unsigned char status;
2818 234035bc 2019-06-01 stsp int local_changes_subsumed;
2819 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2820 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2821 234035bc 2019-06-01 stsp
2822 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2823 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2824 d6c87207 2019-08-02 stsp strlen(path2));
2825 1ee397ad 2019-07-12 stsp if (ie == NULL)
2826 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2827 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2828 234035bc 2019-06-01 stsp
2829 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2830 234035bc 2019-06-01 stsp path2) == -1)
2831 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2832 234035bc 2019-06-01 stsp
2833 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2834 7f91a133 2019-12-13 stsp repo);
2835 234035bc 2019-06-01 stsp if (err)
2836 234035bc 2019-06-01 stsp goto done;
2837 234035bc 2019-06-01 stsp
2838 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2839 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2840 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2841 234035bc 2019-06-01 stsp goto done;
2842 234035bc 2019-06-01 stsp }
2843 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2844 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2845 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2846 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2847 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2848 234035bc 2019-06-01 stsp goto done;
2849 234035bc 2019-06-01 stsp }
2850 234035bc 2019-06-01 stsp
2851 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2852 36bf999c 2020-07-23 stsp char *link_target2;
2853 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2854 36bf999c 2020-07-23 stsp if (err)
2855 36bf999c 2020-07-23 stsp goto done;
2856 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2857 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2858 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2859 36bf999c 2020-07-23 stsp free(link_target2);
2860 af57b12a 2020-07-23 stsp } else {
2861 1af628f4 2021-06-03 stsp int fd;
2862 1af628f4 2021-06-03 stsp
2863 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2864 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2865 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2866 1af628f4 2021-06-03 stsp goto done;
2867 1af628f4 2021-06-03 stsp }
2868 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2869 1af628f4 2021-06-03 stsp f_orig, blob1);
2870 1af628f4 2021-06-03 stsp if (err)
2871 1af628f4 2021-06-03 stsp goto done;
2872 1af628f4 2021-06-03 stsp
2873 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2874 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2875 1af628f4 2021-06-03 stsp goto done;
2876 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2877 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2878 1af628f4 2021-06-03 stsp if (err)
2879 1af628f4 2021-06-03 stsp goto done;
2880 1af628f4 2021-06-03 stsp
2881 c0df5966 2021-12-31 stsp fd = open(ondisk_path,
2882 c0df5966 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2883 1af628f4 2021-06-03 stsp if (fd == -1) {
2884 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2885 1af628f4 2021-06-03 stsp ondisk_path);
2886 1af628f4 2021-06-03 stsp goto done;
2887 1af628f4 2021-06-03 stsp }
2888 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2889 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2890 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2891 1af628f4 2021-06-03 stsp ondisk_path);
2892 1af628f4 2021-06-03 stsp close(fd);
2893 1af628f4 2021-06-03 stsp goto done;
2894 1af628f4 2021-06-03 stsp }
2895 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2896 1af628f4 2021-06-03 stsp if (err)
2897 1af628f4 2021-06-03 stsp goto done;
2898 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2899 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2900 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2901 1af628f4 2021-06-03 stsp goto done;
2902 1af628f4 2021-06-03 stsp }
2903 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2904 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2905 c48f94a4 2023-01-29 stsp mode2, a->label_orig, NULL, label_deriv2,
2906 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2907 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2908 af57b12a 2020-07-23 stsp }
2909 234035bc 2019-06-01 stsp } else if (blob1) {
2910 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2911 d6c87207 2019-08-02 stsp strlen(path1));
2912 1ee397ad 2019-07-12 stsp if (ie == NULL)
2913 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2914 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2915 2b92fad7 2019-06-02 stsp
2916 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2917 2b92fad7 2019-06-02 stsp path1) == -1)
2918 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2919 2b92fad7 2019-06-02 stsp
2920 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2921 7f91a133 2019-12-13 stsp repo);
2922 2b92fad7 2019-06-02 stsp if (err)
2923 2b92fad7 2019-06-02 stsp goto done;
2924 2b92fad7 2019-06-02 stsp
2925 2b92fad7 2019-06-02 stsp switch (status) {
2926 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2927 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2928 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2929 1ee397ad 2019-07-12 stsp if (err)
2930 1ee397ad 2019-07-12 stsp goto done;
2931 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2932 2b92fad7 2019-06-02 stsp if (err)
2933 2b92fad7 2019-06-02 stsp goto done;
2934 2b92fad7 2019-06-02 stsp if (ie)
2935 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2936 2b92fad7 2019-06-02 stsp break;
2937 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2938 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2939 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2940 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2941 1ee397ad 2019-07-12 stsp if (err)
2942 1ee397ad 2019-07-12 stsp goto done;
2943 2b92fad7 2019-06-02 stsp if (ie)
2944 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2945 2b92fad7 2019-06-02 stsp break;
2946 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2947 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2948 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2949 72840534 2022-01-19 stsp off_t blob1_size;
2950 0a22ca1a 2020-09-23 stsp /*
2951 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2952 0a22ca1a 2020-09-23 stsp * exists in the repository.
2953 0a22ca1a 2020-09-23 stsp */
2954 72840534 2022-01-19 stsp err = got_object_blob_file_create(&id, &blob1_f,
2955 72840534 2022-01-19 stsp &blob1_size, path1);
2956 0a22ca1a 2020-09-23 stsp if (err)
2957 0a22ca1a 2020-09-23 stsp goto done;
2958 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2959 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2960 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2961 0a22ca1a 2020-09-23 stsp if (err)
2962 0a22ca1a 2020-09-23 stsp goto done;
2963 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2964 0a22ca1a 2020-09-23 stsp path1);
2965 0a22ca1a 2020-09-23 stsp if (err)
2966 0a22ca1a 2020-09-23 stsp goto done;
2967 0a22ca1a 2020-09-23 stsp if (ie)
2968 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2969 0a22ca1a 2020-09-23 stsp ie);
2970 0a22ca1a 2020-09-23 stsp } else {
2971 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2972 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2973 0a22ca1a 2020-09-23 stsp }
2974 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2975 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2976 0a22ca1a 2020-09-23 stsp free(id);
2977 0a22ca1a 2020-09-23 stsp if (err)
2978 0a22ca1a 2020-09-23 stsp goto done;
2979 0a22ca1a 2020-09-23 stsp break;
2980 0a22ca1a 2020-09-23 stsp }
2981 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2982 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2983 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2984 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2985 1ee397ad 2019-07-12 stsp if (err)
2986 1ee397ad 2019-07-12 stsp goto done;
2987 2b92fad7 2019-06-02 stsp break;
2988 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2989 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2990 1ee397ad 2019-07-12 stsp if (err)
2991 1ee397ad 2019-07-12 stsp goto done;
2992 2b92fad7 2019-06-02 stsp break;
2993 2b92fad7 2019-06-02 stsp default:
2994 2b92fad7 2019-06-02 stsp break;
2995 2b92fad7 2019-06-02 stsp }
2996 234035bc 2019-06-01 stsp } else if (blob2) {
2997 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2998 234035bc 2019-06-01 stsp path2) == -1)
2999 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3000 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3001 d6c87207 2019-08-02 stsp strlen(path2));
3002 234035bc 2019-06-01 stsp if (ie) {
3003 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3004 7f91a133 2019-12-13 stsp -1, NULL, repo);
3005 234035bc 2019-06-01 stsp if (err)
3006 234035bc 2019-06-01 stsp goto done;
3007 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3008 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3009 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3010 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3011 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3012 1ee397ad 2019-07-12 stsp status, path2);
3013 234035bc 2019-06-01 stsp goto done;
3014 234035bc 2019-06-01 stsp }
3015 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3016 36bf999c 2020-07-23 stsp char *link_target2;
3017 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3018 36bf999c 2020-07-23 stsp blob2);
3019 36bf999c 2020-07-23 stsp if (err)
3020 36bf999c 2020-07-23 stsp goto done;
3021 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3022 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3023 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3024 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3025 36bf999c 2020-07-23 stsp free(link_target2);
3026 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3027 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3028 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3029 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3030 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3031 526a746f 2020-07-23 stsp a->progress_arg);
3032 dfe9fba0 2020-07-23 stsp } else {
3033 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3034 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3035 526a746f 2020-07-23 stsp }
3036 dfe9fba0 2020-07-23 stsp if (err)
3037 dfe9fba0 2020-07-23 stsp goto done;
3038 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3039 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3040 3093e2d7 2023-02-04 op a->worktree->root_fd, path2, blob2->id.hash,
3041 3093e2d7 2023-02-04 op a->worktree->base_commit_id->hash, 0);
3042 234035bc 2019-06-01 stsp if (err)
3043 234035bc 2019-06-01 stsp goto done;
3044 234035bc 2019-06-01 stsp }
3045 234035bc 2019-06-01 stsp } else {
3046 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3047 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3048 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3049 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3050 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3051 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3052 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3053 2e1fa222 2020-07-23 stsp } else {
3054 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3055 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3056 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3057 2e1fa222 2020-07-23 stsp }
3058 234035bc 2019-06-01 stsp if (err)
3059 234035bc 2019-06-01 stsp goto done;
3060 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3061 234035bc 2019-06-01 stsp if (err)
3062 234035bc 2019-06-01 stsp goto done;
3063 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3064 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3065 3969253a 2020-03-07 stsp if (err) {
3066 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3067 3969253a 2020-03-07 stsp goto done;
3068 3969253a 2020-03-07 stsp }
3069 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3070 2b92fad7 2019-06-02 stsp if (err) {
3071 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3072 2b92fad7 2019-06-02 stsp goto done;
3073 2b92fad7 2019-06-02 stsp }
3074 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3075 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3076 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3077 2e1fa222 2020-07-23 stsp }
3078 234035bc 2019-06-01 stsp }
3079 234035bc 2019-06-01 stsp }
3080 234035bc 2019-06-01 stsp done:
3081 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3082 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3083 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3084 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3085 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3086 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3087 1af628f4 2021-06-03 stsp free(id_str);
3088 54d5be07 2021-06-03 stsp free(label_deriv2);
3089 234035bc 2019-06-01 stsp free(ondisk_path);
3090 234035bc 2019-06-01 stsp return err;
3091 234035bc 2019-06-01 stsp }
3092 234035bc 2019-06-01 stsp
3093 69de9dd4 2021-09-03 stsp static const struct got_error *
3094 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3095 69de9dd4 2021-09-03 stsp {
3096 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3097 69de9dd4 2021-09-03 stsp
3098 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3099 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3100 3093e2d7 2023-02-04 op memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3101 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3102 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3103 69de9dd4 2021-09-03 stsp
3104 69de9dd4 2021-09-03 stsp return NULL;
3105 69de9dd4 2021-09-03 stsp }
3106 69de9dd4 2021-09-03 stsp
3107 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3108 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3109 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3110 234035bc 2019-06-01 stsp struct got_repository *repo;
3111 234035bc 2019-06-01 stsp };
3112 234035bc 2019-06-01 stsp
3113 234035bc 2019-06-01 stsp static const struct got_error *
3114 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3115 b72706c3 2022-06-01 stsp struct got_blob_object *blob2, FILE *f1, FILE *f2,
3116 b72706c3 2022-06-01 stsp struct got_object_id *id1, struct got_object_id *id2,
3117 b72706c3 2022-06-01 stsp const char *path1, const char *path2,
3118 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3119 234035bc 2019-06-01 stsp {
3120 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3121 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3122 234035bc 2019-06-01 stsp unsigned char status;
3123 234035bc 2019-06-01 stsp struct stat sb;
3124 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3125 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3126 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3127 234035bc 2019-06-01 stsp char *ondisk_path;
3128 234035bc 2019-06-01 stsp
3129 69de9dd4 2021-09-03 stsp if (id == NULL)
3130 69de9dd4 2021-09-03 stsp return NULL;
3131 234035bc 2019-06-01 stsp
3132 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3133 69de9dd4 2021-09-03 stsp if (ie == NULL)
3134 69de9dd4 2021-09-03 stsp return NULL;
3135 69de9dd4 2021-09-03 stsp
3136 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3137 234035bc 2019-06-01 stsp == -1)
3138 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3139 234035bc 2019-06-01 stsp
3140 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3141 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3142 5546d466 2021-09-02 stsp free(ondisk_path);
3143 234035bc 2019-06-01 stsp if (err)
3144 234035bc 2019-06-01 stsp return err;
3145 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3146 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3147 234035bc 2019-06-01 stsp
3148 234035bc 2019-06-01 stsp return NULL;
3149 234035bc 2019-06-01 stsp }
3150 234035bc 2019-06-01 stsp
3151 818c7501 2019-07-11 stsp static const struct got_error *
3152 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3153 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3154 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3155 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3156 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3157 234035bc 2019-06-01 stsp {
3158 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3159 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3160 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3161 a44927cc 2022-04-07 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3162 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3163 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3164 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3165 b72706c3 2022-06-01 stsp FILE *f1 = NULL, *f2 = NULL;
3166 f9d37699 2022-06-28 stsp int fd1 = -1, fd2 = -1;
3167 234035bc 2019-06-01 stsp
3168 03415a1a 2019-06-02 stsp if (commit_id1) {
3169 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit1, repo, commit_id1);
3170 a44927cc 2022-04-07 stsp if (err)
3171 a44927cc 2022-04-07 stsp goto done;
3172 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id1, repo, commit1,
3173 03415a1a 2019-06-02 stsp worktree->path_prefix);
3174 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3175 03415a1a 2019-06-02 stsp goto done;
3176 69d57f3d 2020-07-31 stsp }
3177 69d57f3d 2020-07-31 stsp if (tree_id1) {
3178 69d57f3d 2020-07-31 stsp char *id_str;
3179 03415a1a 2019-06-02 stsp
3180 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3181 03415a1a 2019-06-02 stsp if (err)
3182 03415a1a 2019-06-02 stsp goto done;
3183 f69721c3 2019-10-21 stsp
3184 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3185 f69721c3 2019-10-21 stsp if (err)
3186 f69721c3 2019-10-21 stsp goto done;
3187 f69721c3 2019-10-21 stsp
3188 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3189 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3190 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3191 f69721c3 2019-10-21 stsp free(id_str);
3192 f69721c3 2019-10-21 stsp goto done;
3193 f69721c3 2019-10-21 stsp }
3194 f69721c3 2019-10-21 stsp free(id_str);
3195 b72706c3 2022-06-01 stsp
3196 b72706c3 2022-06-01 stsp f1 = got_opentemp();
3197 b72706c3 2022-06-01 stsp if (f1 == NULL) {
3198 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3199 b72706c3 2022-06-01 stsp goto done;
3200 b72706c3 2022-06-01 stsp }
3201 03415a1a 2019-06-02 stsp }
3202 234035bc 2019-06-01 stsp
3203 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit2, repo, commit_id2);
3204 a44927cc 2022-04-07 stsp if (err)
3205 a44927cc 2022-04-07 stsp goto done;
3206 a44927cc 2022-04-07 stsp
3207 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id2, repo, commit2,
3208 234035bc 2019-06-01 stsp worktree->path_prefix);
3209 234035bc 2019-06-01 stsp if (err)
3210 234035bc 2019-06-01 stsp goto done;
3211 234035bc 2019-06-01 stsp
3212 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3213 234035bc 2019-06-01 stsp if (err)
3214 234035bc 2019-06-01 stsp goto done;
3215 234035bc 2019-06-01 stsp
3216 b72706c3 2022-06-01 stsp f2 = got_opentemp();
3217 b72706c3 2022-06-01 stsp if (f2 == NULL) {
3218 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3219 f9d37699 2022-06-28 stsp goto done;
3220 f9d37699 2022-06-28 stsp }
3221 f9d37699 2022-06-28 stsp
3222 f9d37699 2022-06-28 stsp fd1 = got_opentempfd();
3223 f9d37699 2022-06-28 stsp if (fd1 == -1) {
3224 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3225 b72706c3 2022-06-01 stsp goto done;
3226 b72706c3 2022-06-01 stsp }
3227 b72706c3 2022-06-01 stsp
3228 f9d37699 2022-06-28 stsp fd2 = got_opentempfd();
3229 f9d37699 2022-06-28 stsp if (fd2 == -1) {
3230 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
3231 f9d37699 2022-06-28 stsp goto done;
3232 f9d37699 2022-06-28 stsp }
3233 f9d37699 2022-06-28 stsp
3234 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3235 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3236 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3237 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3238 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3239 69de9dd4 2021-09-03 stsp if (err)
3240 69de9dd4 2021-09-03 stsp goto done;
3241 69de9dd4 2021-09-03 stsp
3242 234035bc 2019-06-01 stsp arg.worktree = worktree;
3243 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3244 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3245 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3246 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3247 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3248 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3249 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3250 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3251 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3252 b72706c3 2022-06-01 stsp merge_file_cb, &arg, 1);
3253 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3254 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3255 af12c6b9 2019-06-04 stsp err = sync_err;
3256 234035bc 2019-06-01 stsp done:
3257 a44927cc 2022-04-07 stsp if (commit1)
3258 a44927cc 2022-04-07 stsp got_object_commit_close(commit1);
3259 a44927cc 2022-04-07 stsp if (commit2)
3260 a44927cc 2022-04-07 stsp got_object_commit_close(commit2);
3261 234035bc 2019-06-01 stsp if (tree1)
3262 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3263 234035bc 2019-06-01 stsp if (tree2)
3264 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3265 b72706c3 2022-06-01 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3266 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3267 b72706c3 2022-06-01 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3268 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3269 f9d37699 2022-06-28 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3270 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3271 f9d37699 2022-06-28 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3272 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
3273 f69721c3 2019-10-21 stsp free(label_orig);
3274 818c7501 2019-07-11 stsp return err;
3275 818c7501 2019-07-11 stsp }
3276 234035bc 2019-06-01 stsp
3277 818c7501 2019-07-11 stsp const struct got_error *
3278 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3279 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3280 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3281 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3282 818c7501 2019-07-11 stsp {
3283 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3284 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3285 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3286 818c7501 2019-07-11 stsp
3287 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3288 818c7501 2019-07-11 stsp if (err)
3289 818c7501 2019-07-11 stsp return err;
3290 818c7501 2019-07-11 stsp
3291 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3292 818c7501 2019-07-11 stsp if (err)
3293 818c7501 2019-07-11 stsp goto done;
3294 818c7501 2019-07-11 stsp
3295 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3296 69de9dd4 2021-09-03 stsp worktree);
3297 818c7501 2019-07-11 stsp if (err)
3298 818c7501 2019-07-11 stsp goto done;
3299 818c7501 2019-07-11 stsp
3300 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3301 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3302 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3303 818c7501 2019-07-11 stsp done:
3304 818c7501 2019-07-11 stsp if (fileindex)
3305 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3306 818c7501 2019-07-11 stsp free(fileindex_path);
3307 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3308 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3309 234035bc 2019-06-01 stsp err = unlockerr;
3310 234035bc 2019-06-01 stsp return err;
3311 234035bc 2019-06-01 stsp }
3312 234035bc 2019-06-01 stsp
3313 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3314 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3315 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3316 927df6b7 2019-02-10 stsp const char *status_path;
3317 927df6b7 2019-02-10 stsp size_t status_path_len;
3318 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3319 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3320 f8d1f275 2019-02-04 stsp void *status_arg;
3321 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3322 f8d1f275 2019-02-04 stsp void *cancel_arg;
3323 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3324 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3325 f2a9dc41 2019-12-13 tracey int report_unchanged;
3326 3143d852 2020-06-25 stsp int no_ignores;
3327 f8d1f275 2019-02-04 stsp };
3328 88d0e355 2019-08-03 stsp
3329 f8d1f275 2019-02-04 stsp static const struct got_error *
3330 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3331 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3332 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3333 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3334 927df6b7 2019-02-10 stsp {
3335 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3336 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3337 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3338 927df6b7 2019-02-10 stsp struct stat sb;
3339 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3340 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3341 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3342 927df6b7 2019-02-10 stsp
3343 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3344 98eaaa12 2019-08-03 stsp if (err)
3345 98eaaa12 2019-08-03 stsp return err;
3346 98eaaa12 2019-08-03 stsp
3347 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3348 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3349 98eaaa12 2019-08-03 stsp return NULL;
3350 98eaaa12 2019-08-03 stsp
3351 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3352 3093e2d7 2023-02-04 op memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3353 dfe0a3d8 2023-02-04 op blob_id.algo = got_repo_get_hash_algorithm(repo);
3354 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3355 98eaaa12 2019-08-03 stsp }
3356 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3357 3093e2d7 2023-02-04 op memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3358 dfe0a3d8 2023-02-04 op commit_id.algo = got_repo_get_hash_algorithm(repo);
3359 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3360 927df6b7 2019-02-10 stsp }
3361 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3362 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3363 3093e2d7 2023-02-04 op memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3364 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3365 dfe0a3d8 2023-02-04 op staged_blob_id.algo = got_repo_get_hash_algorithm(repo);
3366 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3367 98eaaa12 2019-08-03 stsp }
3368 98eaaa12 2019-08-03 stsp
3369 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3370 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3371 927df6b7 2019-02-10 stsp }
3372 927df6b7 2019-02-10 stsp
3373 927df6b7 2019-02-10 stsp static const struct got_error *
3374 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3375 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3376 f8d1f275 2019-02-04 stsp {
3377 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3378 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3379 f8d1f275 2019-02-04 stsp char *abspath;
3380 f8d1f275 2019-02-04 stsp
3381 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3382 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3383 0584f854 2019-04-06 stsp
3384 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3385 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3386 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3387 927df6b7 2019-02-10 stsp return NULL;
3388 927df6b7 2019-02-10 stsp
3389 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3390 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3391 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3392 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3393 f8d1f275 2019-02-04 stsp } else {
3394 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3395 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3396 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3397 f8d1f275 2019-02-04 stsp }
3398 f8d1f275 2019-02-04 stsp
3399 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3400 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3401 f8d1f275 2019-02-04 stsp free(abspath);
3402 9d31a1d8 2018-03-11 stsp return err;
3403 9d31a1d8 2018-03-11 stsp }
3404 f8d1f275 2019-02-04 stsp
3405 f8d1f275 2019-02-04 stsp static const struct got_error *
3406 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3407 f8d1f275 2019-02-04 stsp {
3408 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3409 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3410 2ec1f75b 2019-03-26 stsp unsigned char status;
3411 927df6b7 2019-02-10 stsp
3412 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3413 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3414 0584f854 2019-04-06 stsp
3415 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3416 927df6b7 2019-02-10 stsp return NULL;
3417 927df6b7 2019-02-10 stsp
3418 3093e2d7 2023-02-04 op memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3419 dfe0a3d8 2023-02-04 op blob_id.algo = got_repo_get_hash_algorithm(a->repo);
3420 3093e2d7 2023-02-04 op memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3421 dfe0a3d8 2023-02-04 op commit_id.algo = got_repo_get_hash_algorithm(a->repo);
3422 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3423 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3424 2ec1f75b 2019-03-26 stsp else
3425 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3426 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3427 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3428 6841da00 2019-08-08 stsp }
3429 6841da00 2019-08-08 stsp
3430 336075a4 2022-06-25 op static void
3431 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3432 6841da00 2019-08-08 stsp {
3433 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3434 6841da00 2019-08-08 stsp
3435 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3436 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3437 d8bacb93 2023-01-10 mark
3438 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3439 6841da00 2019-08-08 stsp }
3440 d8bacb93 2023-01-10 mark got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3441 6841da00 2019-08-08 stsp }
3442 6841da00 2019-08-08 stsp
3443 6841da00 2019-08-08 stsp static const struct got_error *
3444 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3445 6841da00 2019-08-08 stsp {
3446 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3447 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3448 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3449 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3450 6841da00 2019-08-08 stsp size_t linesize = 0;
3451 6841da00 2019-08-08 stsp ssize_t linelen;
3452 6841da00 2019-08-08 stsp
3453 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3454 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3455 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3456 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3457 6841da00 2019-08-08 stsp
3458 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3459 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3460 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3461 bd8de430 2019-10-04 stsp
3462 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3463 bd8de430 2019-10-04 stsp if (line[0] == '#')
3464 bd8de430 2019-10-04 stsp continue;
3465 bd8de430 2019-10-04 stsp
3466 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3467 bd8de430 2019-10-04 stsp if (line[0] == '!')
3468 bd8de430 2019-10-04 stsp continue;
3469 bd8de430 2019-10-04 stsp
3470 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3471 6841da00 2019-08-08 stsp line) == -1) {
3472 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3473 6841da00 2019-08-08 stsp goto done;
3474 6841da00 2019-08-08 stsp }
3475 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3476 6841da00 2019-08-08 stsp if (err)
3477 6841da00 2019-08-08 stsp goto done;
3478 6841da00 2019-08-08 stsp }
3479 6841da00 2019-08-08 stsp if (ferror(f)) {
3480 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3481 6841da00 2019-08-08 stsp goto done;
3482 6841da00 2019-08-08 stsp }
3483 6841da00 2019-08-08 stsp
3484 6841da00 2019-08-08 stsp dirpath = strdup(path);
3485 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3486 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3487 6841da00 2019-08-08 stsp goto done;
3488 6841da00 2019-08-08 stsp }
3489 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3490 6841da00 2019-08-08 stsp done:
3491 6841da00 2019-08-08 stsp free(line);
3492 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3493 6841da00 2019-08-08 stsp free(dirpath);
3494 d8bacb93 2023-01-10 mark got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3495 6841da00 2019-08-08 stsp }
3496 6841da00 2019-08-08 stsp return err;
3497 6841da00 2019-08-08 stsp }
3498 6841da00 2019-08-08 stsp
3499 336075a4 2022-06-25 op static int
3500 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3501 6841da00 2019-08-08 stsp {
3502 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3503 bd8de430 2019-10-04 stsp
3504 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3505 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3506 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3507 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3508 bd8de430 2019-10-04 stsp
3509 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3510 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3511 6841da00 2019-08-08 stsp
3512 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3513 bd8de430 2019-10-04 stsp continue;
3514 bd8de430 2019-10-04 stsp pattern += 3;
3515 bd8de430 2019-10-04 stsp p = path;
3516 bd8de430 2019-10-04 stsp while (*p) {
3517 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3518 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3519 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3520 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3521 bd8de430 2019-10-04 stsp p++;
3522 bd8de430 2019-10-04 stsp while (*p == '/')
3523 bd8de430 2019-10-04 stsp p++;
3524 bd8de430 2019-10-04 stsp continue;
3525 bd8de430 2019-10-04 stsp }
3526 bd8de430 2019-10-04 stsp return 1;
3527 bd8de430 2019-10-04 stsp }
3528 bd8de430 2019-10-04 stsp }
3529 bd8de430 2019-10-04 stsp }
3530 bd8de430 2019-10-04 stsp
3531 6841da00 2019-08-08 stsp /*
3532 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3533 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3534 6841da00 2019-08-08 stsp * ignores backwards.
3535 6841da00 2019-08-08 stsp */
3536 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3537 6841da00 2019-08-08 stsp while (pe) {
3538 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3539 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3540 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3541 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3542 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3543 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3544 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3545 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3546 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3547 6841da00 2019-08-08 stsp continue;
3548 6841da00 2019-08-08 stsp return 1;
3549 6841da00 2019-08-08 stsp }
3550 6841da00 2019-08-08 stsp }
3551 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3552 6841da00 2019-08-08 stsp }
3553 6841da00 2019-08-08 stsp
3554 6841da00 2019-08-08 stsp return 0;
3555 6841da00 2019-08-08 stsp }
3556 6841da00 2019-08-08 stsp
3557 6841da00 2019-08-08 stsp static const struct got_error *
3558 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3559 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3560 6841da00 2019-08-08 stsp {
3561 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3562 6841da00 2019-08-08 stsp char *ignorespath;
3563 886cec17 2019-12-15 stsp int fd = -1;
3564 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3565 6841da00 2019-08-08 stsp
3566 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3567 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3568 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3569 6841da00 2019-08-08 stsp
3570 886cec17 2019-12-15 stsp if (dirfd != -1) {
3571 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, ignores_filename,
3572 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3573 886cec17 2019-12-15 stsp if (fd == -1) {
3574 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3575 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3576 886cec17 2019-12-15 stsp ignorespath);
3577 886cec17 2019-12-15 stsp } else {
3578 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3579 5e91dae4 2022-08-30 stsp if (ignoresfile == NULL)
3580 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3581 886cec17 2019-12-15 stsp ignorespath);
3582 886cec17 2019-12-15 stsp else {
3583 886cec17 2019-12-15 stsp fd = -1;
3584 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3585 886cec17 2019-12-15 stsp }
3586 886cec17 2019-12-15 stsp }
3587 886cec17 2019-12-15 stsp } else {
3588 00fe21f2 2021-12-31 stsp ignoresfile = fopen(ignorespath, "re");
3589 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3590 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3591 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3592 886cec17 2019-12-15 stsp ignorespath);
3593 886cec17 2019-12-15 stsp } else
3594 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3595 886cec17 2019-12-15 stsp }
3596 6841da00 2019-08-08 stsp
3597 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3598 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3599 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3600 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3601 6841da00 2019-08-08 stsp free(ignorespath);
3602 6841da00 2019-08-08 stsp return err;
3603 f8d1f275 2019-02-04 stsp }
3604 f8d1f275 2019-02-04 stsp
3605 f8d1f275 2019-02-04 stsp static const struct got_error *
3606 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3607 62da3196 2021-10-01 stsp int dirfd)
3608 f8d1f275 2019-02-04 stsp {
3609 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3610 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3611 f8d1f275 2019-02-04 stsp char *path = NULL;
3612 62da3196 2021-10-01 stsp
3613 62da3196 2021-10-01 stsp if (ignore != NULL)
3614 62da3196 2021-10-01 stsp *ignore = 0;
3615 f8d1f275 2019-02-04 stsp
3616 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3617 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3618 0584f854 2019-04-06 stsp
3619 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3620 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3621 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3622 f8d1f275 2019-02-04 stsp } else {
3623 f8d1f275 2019-02-04 stsp path = de->d_name;
3624 f8d1f275 2019-02-04 stsp }
3625 f8d1f275 2019-02-04 stsp
3626 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3627 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3628 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3629 62da3196 2021-10-01 stsp *ignore = 1;
3630 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3631 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3632 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3633 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3634 f8d1f275 2019-02-04 stsp if (parent_path[0])
3635 f8d1f275 2019-02-04 stsp free(path);
3636 b72f483a 2019-02-05 stsp return err;
3637 f8d1f275 2019-02-04 stsp }
3638 f8d1f275 2019-02-04 stsp
3639 347d1d3e 2019-07-12 stsp static const struct got_error *
3640 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3641 3143d852 2020-06-25 stsp {
3642 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3643 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3644 3143d852 2020-06-25 stsp
3645 3143d852 2020-06-25 stsp if (a->no_ignores)
3646 3143d852 2020-06-25 stsp return NULL;
3647 3143d852 2020-06-25 stsp
3648 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3649 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3650 3143d852 2020-06-25 stsp if (err)
3651 3143d852 2020-06-25 stsp return err;
3652 3143d852 2020-06-25 stsp
3653 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3654 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3655 3143d852 2020-06-25 stsp
3656 3143d852 2020-06-25 stsp return err;
3657 3143d852 2020-06-25 stsp }
3658 3143d852 2020-06-25 stsp
3659 3143d852 2020-06-25 stsp static const struct got_error *
3660 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3661 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3662 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3663 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3664 abb4604f 2019-07-27 stsp {
3665 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3666 abb4604f 2019-07-27 stsp struct stat sb;
3667 ff56836b 2021-07-08 stsp
3668 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3669 abb4604f 2019-07-27 stsp if (ie)
3670 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3671 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3672 abb4604f 2019-07-27 stsp
3673 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3674 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3675 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3676 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
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
3680 916237f3 2022-02-11 stsp if (!no_ignores && match_ignores(ignores, path))
3681 916237f3 2022-02-11 stsp return NULL;
3682 916237f3 2022-02-11 stsp
3683 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3684 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3685 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3686 abb4604f 2019-07-27 stsp
3687 abb4604f 2019-07-27 stsp return NULL;
3688 3143d852 2020-06-25 stsp }
3689 3143d852 2020-06-25 stsp
3690 3143d852 2020-06-25 stsp static const struct got_error *
3691 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3692 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3693 3143d852 2020-06-25 stsp {
3694 3143d852 2020-06-25 stsp const struct got_error *err;
3695 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3696 3143d852 2020-06-25 stsp
3697 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3698 3143d852 2020-06-25 stsp ".cvsignore");
3699 3143d852 2020-06-25 stsp if (err)
3700 3143d852 2020-06-25 stsp return err;
3701 3143d852 2020-06-25 stsp
3702 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3703 3143d852 2020-06-25 stsp ".gitignore");
3704 3143d852 2020-06-25 stsp if (err)
3705 3143d852 2020-06-25 stsp return err;
3706 3143d852 2020-06-25 stsp
3707 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3708 3143d852 2020-06-25 stsp if (err) {
3709 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3710 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3711 3143d852 2020-06-25 stsp return err;
3712 3143d852 2020-06-25 stsp }
3713 3143d852 2020-06-25 stsp for (;;) {
3714 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3715 3143d852 2020-06-25 stsp ".cvsignore");
3716 3143d852 2020-06-25 stsp if (err)
3717 3143d852 2020-06-25 stsp break;
3718 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3719 3143d852 2020-06-25 stsp ".gitignore");
3720 3143d852 2020-06-25 stsp if (err)
3721 3143d852 2020-06-25 stsp break;
3722 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3723 3143d852 2020-06-25 stsp if (err) {
3724 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3725 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3726 3143d852 2020-06-25 stsp break;
3727 3143d852 2020-06-25 stsp }
3728 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3729 ff56836b 2021-07-08 stsp break;
3730 b737c85a 2020-06-26 stsp free(parent_path);
3731 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3732 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3733 3143d852 2020-06-25 stsp }
3734 3143d852 2020-06-25 stsp
3735 b737c85a 2020-06-26 stsp free(parent_path);
3736 b737c85a 2020-06-26 stsp free(next_parent_path);
3737 3143d852 2020-06-25 stsp return err;
3738 abb4604f 2019-07-27 stsp }
3739 abb4604f 2019-07-27 stsp
3740 abb4604f 2019-07-27 stsp static const struct got_error *
3741 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3742 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3743 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3744 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3745 f2a9dc41 2019-12-13 tracey int report_unchanged)
3746 f8d1f275 2019-02-04 stsp {
3747 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3748 6fc93f37 2019-12-13 stsp int fd = -1;
3749 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3750 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3751 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3752 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3753 a84c0d30 2022-03-12 stsp struct got_fileindex_entry *ie;
3754 3143d852 2020-06-25 stsp
3755 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3756 f8d1f275 2019-02-04 stsp
3757 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3758 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3759 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3760 8dc303cc 2019-07-27 stsp
3761 a84c0d30 2022-03-12 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3762 a84c0d30 2022-03-12 stsp if (ie) {
3763 a84c0d30 2022-03-12 stsp err = report_single_file_status(path, ondisk_path,
3764 a84c0d30 2022-03-12 stsp fileindex, status_cb, status_arg, repo,
3765 a84c0d30 2022-03-12 stsp report_unchanged, &ignores, no_ignores);
3766 a84c0d30 2022-03-12 stsp goto done;
3767 a84c0d30 2022-03-12 stsp }
3768 a84c0d30 2022-03-12 stsp
3769 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3770 6fc93f37 2019-12-13 stsp if (fd == -1) {
3771 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3772 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3773 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3774 ff56836b 2021-07-08 stsp else {
3775 ff56836b 2021-07-08 stsp if (!no_ignores) {
3776 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3777 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3778 ff56836b 2021-07-08 stsp if (err)
3779 ff56836b 2021-07-08 stsp goto done;
3780 ff56836b 2021-07-08 stsp }
3781 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3782 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3783 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3784 ff56836b 2021-07-08 stsp }
3785 abb4604f 2019-07-27 stsp } else {
3786 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3787 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3788 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3789 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3790 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3791 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3792 abb4604f 2019-07-27 stsp arg.status_path = path;
3793 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3794 abb4604f 2019-07-27 stsp arg.repo = repo;
3795 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3796 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3797 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3798 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3799 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3800 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3801 022fae89 2019-12-06 tracey if (!no_ignores) {
3802 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3803 3143d852 2020-06-25 stsp worktree->root_path, path);
3804 3143d852 2020-06-25 stsp if (err)
3805 3143d852 2020-06-25 stsp goto done;
3806 022fae89 2019-12-06 tracey }
3807 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3808 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3809 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3810 927df6b7 2019-02-10 stsp }
3811 3143d852 2020-06-25 stsp done:
3812 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3813 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3814 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3815 927df6b7 2019-02-10 stsp free(ondisk_path);
3816 347d1d3e 2019-07-12 stsp return err;
3817 347d1d3e 2019-07-12 stsp }
3818 347d1d3e 2019-07-12 stsp
3819 347d1d3e 2019-07-12 stsp const struct got_error *
3820 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3821 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3822 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3823 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3824 347d1d3e 2019-07-12 stsp {
3825 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3826 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3827 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3828 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3829 347d1d3e 2019-07-12 stsp
3830 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3831 347d1d3e 2019-07-12 stsp if (err)
3832 347d1d3e 2019-07-12 stsp return err;
3833 347d1d3e 2019-07-12 stsp
3834 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3835 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3836 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3837 f6343036 2021-06-22 stsp no_ignores, 0);
3838 72ea6654 2019-07-27 stsp if (err)
3839 72ea6654 2019-07-27 stsp break;
3840 72ea6654 2019-07-27 stsp }
3841 f8d1f275 2019-02-04 stsp free(fileindex_path);
3842 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3843 f8d1f275 2019-02-04 stsp return err;
3844 f8d1f275 2019-02-04 stsp }
3845 6c7ab921 2019-03-18 stsp
3846 6c7ab921 2019-03-18 stsp const struct got_error *
3847 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3848 6c7ab921 2019-03-18 stsp const char *arg)
3849 6c7ab921 2019-03-18 stsp {
3850 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3851 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3852 6c7ab921 2019-03-18 stsp size_t len;
3853 00bb5ea0 2020-07-23 stsp struct stat sb;
3854 01740607 2020-11-04 stsp char *abspath = NULL;
3855 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3856 6c7ab921 2019-03-18 stsp
3857 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3858 6c7ab921 2019-03-18 stsp
3859 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3860 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3861 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3862 00bb5ea0 2020-07-23 stsp
3863 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3864 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3865 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3866 00bb5ea0 2020-07-23 stsp goto done;
3867 00bb5ea0 2020-07-23 stsp }
3868 727173c3 2020-11-06 stsp sb.st_mode = 0;
3869 00bb5ea0 2020-07-23 stsp }
3870 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3871 00bb5ea0 2020-07-23 stsp /*
3872 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3873 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3874 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3875 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3876 00bb5ea0 2020-07-23 stsp */
3877 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3878 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3880 00bb5ea0 2020-07-23 stsp goto done;
3881 00bb5ea0 2020-07-23 stsp }
3882 00bb5ea0 2020-07-23 stsp
3883 00bb5ea0 2020-07-23 stsp }
3884 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3885 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3886 00bb5ea0 2020-07-23 stsp if (err)
3887 d0710d08 2019-07-22 stsp goto done;
3888 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3889 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3890 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3891 00bb5ea0 2020-07-23 stsp goto done;
3892 00bb5ea0 2020-07-23 stsp }
3893 00bb5ea0 2020-07-23 stsp } else {
3894 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3895 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3896 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3897 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3898 00bb5ea0 2020-07-23 stsp goto done;
3899 00bb5ea0 2020-07-23 stsp }
3900 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3901 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3902 01740607 2020-11-04 stsp goto done;
3903 01740607 2020-11-04 stsp }
3904 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3905 01740607 2020-11-04 stsp sizeof(canonpath));
3906 01740607 2020-11-04 stsp if (err)
3907 00bb5ea0 2020-07-23 stsp goto done;
3908 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3909 01740607 2020-11-04 stsp if (resolved == NULL) {
3910 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3911 01740607 2020-11-04 stsp goto done;
3912 00bb5ea0 2020-07-23 stsp }
3913 d0710d08 2019-07-22 stsp }
3914 d0710d08 2019-07-22 stsp }
3915 6c7ab921 2019-03-18 stsp
3916 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3917 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3918 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3919 6c7ab921 2019-03-18 stsp goto done;
3920 6c7ab921 2019-03-18 stsp }
3921 6c7ab921 2019-03-18 stsp
3922 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3923 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3924 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3925 f6d88e1a 2019-05-29 stsp if (err)
3926 f6d88e1a 2019-05-29 stsp goto done;
3927 f6d88e1a 2019-05-29 stsp } else {
3928 f6d88e1a 2019-05-29 stsp path = strdup("");
3929 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3930 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3931 f6d88e1a 2019-05-29 stsp goto done;
3932 f6d88e1a 2019-05-29 stsp }
3933 6c7ab921 2019-03-18 stsp }
3934 6c7ab921 2019-03-18 stsp
3935 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3936 6c7ab921 2019-03-18 stsp len = strlen(path);
3937 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3938 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3939 6c7ab921 2019-03-18 stsp len--;
3940 6c7ab921 2019-03-18 stsp }
3941 6c7ab921 2019-03-18 stsp done:
3942 01740607 2020-11-04 stsp free(abspath);
3943 6c7ab921 2019-03-18 stsp free(resolved);
3944 d0710d08 2019-07-22 stsp free(cwd);
3945 6c7ab921 2019-03-18 stsp if (err == NULL)
3946 6c7ab921 2019-03-18 stsp *wt_path = path;
3947 6c7ab921 2019-03-18 stsp else
3948 6c7ab921 2019-03-18 stsp free(path);
3949 d00136be 2019-03-26 stsp return err;
3950 d00136be 2019-03-26 stsp }
3951 d00136be 2019-03-26 stsp
3952 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3953 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3954 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3955 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3956 4e68cba3 2019-11-23 stsp void *progress_arg;
3957 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3958 4e68cba3 2019-11-23 stsp };
3959 4e68cba3 2019-11-23 stsp
3960 4e68cba3 2019-11-23 stsp static const struct got_error *
3961 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3962 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3963 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3964 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3965 07f5b47a 2019-06-02 stsp {
3966 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3967 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3968 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3969 6d022e97 2019-08-04 stsp struct stat sb;
3970 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3971 07f5b47a 2019-06-02 stsp
3972 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3973 dbb83fbd 2019-12-12 stsp relpath) == -1)
3974 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3975 dbb83fbd 2019-12-12 stsp
3976 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3977 6d022e97 2019-08-04 stsp if (ie) {
3978 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3979 12463d8b 2019-12-13 stsp de_name, a->repo);
3980 6d022e97 2019-08-04 stsp if (err)
3981 dbb83fbd 2019-12-12 stsp goto done;
3982 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3983 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3984 dbb83fbd 2019-12-12 stsp goto done;
3985 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3986 dbb83fbd 2019-12-12 stsp if (err)
3987 dbb83fbd 2019-12-12 stsp goto done;
3988 6d022e97 2019-08-04 stsp }
3989 07f5b47a 2019-06-02 stsp
3990 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3991 9b4603c0 2022-01-31 stsp if (status == GOT_STATUS_NONEXISTENT)
3992 9b4603c0 2022-01-31 stsp err = got_error_set_errno(ENOENT, ondisk_path);
3993 9b4603c0 2022-01-31 stsp else
3994 9b4603c0 2022-01-31 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3995 dbb83fbd 2019-12-12 stsp goto done;
3996 4e68cba3 2019-11-23 stsp }
3997 07f5b47a 2019-06-02 stsp
3998 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3999 4e68cba3 2019-11-23 stsp if (err)
4000 4e68cba3 2019-11-23 stsp goto done;
4001 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4002 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4003 3969253a 2020-03-07 stsp if (err) {
4004 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4005 3969253a 2020-03-07 stsp goto done;
4006 3969253a 2020-03-07 stsp }
4007 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4008 07f5b47a 2019-06-02 stsp if (err) {
4009 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4010 4e68cba3 2019-11-23 stsp goto done;
4011 07f5b47a 2019-06-02 stsp }
4012 4e68cba3 2019-11-23 stsp done:
4013 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4014 dbb83fbd 2019-12-12 stsp if (err)
4015 dbb83fbd 2019-12-12 stsp return err;
4016 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4017 dbb83fbd 2019-12-12 stsp return NULL;
4018 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4019 07f5b47a 2019-06-02 stsp }
4020 07f5b47a 2019-06-02 stsp
4021 d00136be 2019-03-26 stsp const struct got_error *
4022 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4023 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4024 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4025 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4026 d00136be 2019-03-26 stsp {
4027 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4028 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4029 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4030 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4031 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4032 d00136be 2019-03-26 stsp
4033 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4034 d00136be 2019-03-26 stsp if (err)
4035 d00136be 2019-03-26 stsp return err;
4036 d00136be 2019-03-26 stsp
4037 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4038 d00136be 2019-03-26 stsp if (err)
4039 d00136be 2019-03-26 stsp goto done;
4040 d00136be 2019-03-26 stsp
4041 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4042 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4043 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4044 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4045 4e68cba3 2019-11-23 stsp saa.repo = repo;
4046 4e68cba3 2019-11-23 stsp
4047 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4048 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4049 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4050 1dd54920 2019-05-11 stsp if (err)
4051 af12c6b9 2019-06-04 stsp break;
4052 1dd54920 2019-05-11 stsp }
4053 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4054 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4055 af12c6b9 2019-06-04 stsp err = sync_err;
4056 d00136be 2019-03-26 stsp done:
4057 fb399478 2019-07-12 stsp free(fileindex_path);
4058 d00136be 2019-03-26 stsp if (fileindex)
4059 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4060 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4061 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4062 d00136be 2019-03-26 stsp err = unlockerr;
4063 6c7ab921 2019-03-18 stsp return err;
4064 6c7ab921 2019-03-18 stsp }
4065 17ed4618 2019-06-02 stsp
4066 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4067 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4068 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4069 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4070 f2a9dc41 2019-12-13 tracey void *progress_arg;
4071 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4072 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4073 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4074 4e12cd97 2022-01-25 stsp int ignore_missing_paths;
4075 766841c2 2020-08-13 stsp const char *status_codes;
4076 f2a9dc41 2019-12-13 tracey };
4077 f2a9dc41 2019-12-13 tracey
4078 f2a9dc41 2019-12-13 tracey static const struct got_error *
4079 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4080 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4081 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4082 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4083 17ed4618 2019-06-02 stsp {
4084 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4085 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4086 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4087 17ed4618 2019-06-02 stsp struct stat sb;
4088 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4089 4e12cd97 2022-01-25 stsp
4090 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_NONEXISTENT) {
4091 4e12cd97 2022-01-25 stsp if (a->ignore_missing_paths)
4092 4e12cd97 2022-01-25 stsp return NULL;
4093 4e12cd97 2022-01-25 stsp return got_error_set_errno(ENOENT, relpath);
4094 4e12cd97 2022-01-25 stsp }
4095 17ed4618 2019-06-02 stsp
4096 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4097 17ed4618 2019-06-02 stsp if (ie == NULL)
4098 692bdcc4 2022-01-25 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4099 2ec1f75b 2019-03-26 stsp
4100 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4101 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4102 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4103 9acbc4fa 2019-08-03 stsp return NULL;
4104 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4105 9acbc4fa 2019-08-03 stsp }
4106 9acbc4fa 2019-08-03 stsp
4107 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4108 f2a9dc41 2019-12-13 tracey relpath) == -1)
4109 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4110 f2a9dc41 2019-12-13 tracey
4111 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4112 12463d8b 2019-12-13 stsp a->repo);
4113 17ed4618 2019-06-02 stsp if (err)
4114 f2a9dc41 2019-12-13 tracey goto done;
4115 17ed4618 2019-06-02 stsp
4116 766841c2 2020-08-13 stsp if (a->status_codes) {
4117 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4118 766841c2 2020-08-13 stsp int i;
4119 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4120 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4121 766841c2 2020-08-13 stsp break;
4122 766841c2 2020-08-13 stsp }
4123 5e91dae4 2022-08-30 stsp if (i == ncodes) {
4124 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4125 766841c2 2020-08-13 stsp free(ondisk_path);
4126 766841c2 2020-08-13 stsp return NULL;
4127 766841c2 2020-08-13 stsp }
4128 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4129 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4130 766841c2 2020-08-13 stsp static char msg[64];
4131 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4132 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4133 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4134 766841c2 2020-08-13 stsp goto done;
4135 766841c2 2020-08-13 stsp }
4136 766841c2 2020-08-13 stsp }
4137 766841c2 2020-08-13 stsp
4138 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4139 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4140 f2a9dc41 2019-12-13 tracey goto done;
4141 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4142 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4143 f2a9dc41 2019-12-13 tracey goto done;
4144 f2a9dc41 2019-12-13 tracey }
4145 4e12cd97 2022-01-25 stsp if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4146 4e12cd97 2022-01-25 stsp err = got_error_set_errno(ENOENT, relpath);
4147 4e12cd97 2022-01-25 stsp goto done;
4148 4e12cd97 2022-01-25 stsp }
4149 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4150 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4151 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4152 f2a9dc41 2019-12-13 tracey goto done;
4153 f2a9dc41 2019-12-13 tracey }
4154 17ed4618 2019-06-02 stsp }
4155 17ed4618 2019-06-02 stsp
4156 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4157 20a2ad1c 2020-10-20 stsp size_t root_len;
4158 20a2ad1c 2020-10-20 stsp
4159 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4160 a06ca3f7 2022-10-15 stsp if (unlinkat(dirfd, de_name, 0) == -1) {
4161 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4162 12463d8b 2019-12-13 stsp ondisk_path);
4163 12463d8b 2019-12-13 stsp goto done;
4164 12463d8b 2019-12-13 stsp }
4165 a06ca3f7 2022-10-15 stsp } else if (unlink(ondisk_path) == -1) {
4166 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4167 12463d8b 2019-12-13 stsp goto done;
4168 15341bfd 2020-03-05 tracey }
4169 15341bfd 2020-03-05 tracey
4170 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4171 20a2ad1c 2020-10-20 stsp do {
4172 20a2ad1c 2020-10-20 stsp char *parent;
4173 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4174 20a2ad1c 2020-10-20 stsp if (err)
4175 2513f20a 2020-10-20 stsp goto done;
4176 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4177 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4178 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4179 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4180 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4181 20a2ad1c 2020-10-20 stsp ondisk_path);
4182 15341bfd 2020-03-05 tracey break;
4183 15341bfd 2020-03-05 tracey }
4184 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4185 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4186 f2a9dc41 2019-12-13 tracey }
4187 17ed4618 2019-06-02 stsp
4188 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4189 f2a9dc41 2019-12-13 tracey done:
4190 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4191 f2a9dc41 2019-12-13 tracey if (err)
4192 f2a9dc41 2019-12-13 tracey return err;
4193 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4194 f2a9dc41 2019-12-13 tracey return NULL;
4195 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4196 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4197 17ed4618 2019-06-02 stsp }
4198 17ed4618 2019-06-02 stsp
4199 2ec1f75b 2019-03-26 stsp const struct got_error *
4200 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4201 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4202 766841c2 2020-08-13 stsp const char *status_codes,
4203 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4204 4e12cd97 2022-01-25 stsp struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4205 2ec1f75b 2019-03-26 stsp {
4206 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4207 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4208 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4209 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4210 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4211 2ec1f75b 2019-03-26 stsp
4212 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4213 2ec1f75b 2019-03-26 stsp if (err)
4214 2ec1f75b 2019-03-26 stsp return err;
4215 2ec1f75b 2019-03-26 stsp
4216 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4217 2ec1f75b 2019-03-26 stsp if (err)
4218 2ec1f75b 2019-03-26 stsp goto done;
4219 f2a9dc41 2019-12-13 tracey
4220 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4221 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4222 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4223 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4224 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4225 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4226 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4227 4e12cd97 2022-01-25 stsp sda.ignore_missing_paths = ignore_missing_paths;
4228 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4229 2ec1f75b 2019-03-26 stsp
4230 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4231 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4232 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4233 17ed4618 2019-06-02 stsp if (err)
4234 af12c6b9 2019-06-04 stsp break;
4235 2ec1f75b 2019-03-26 stsp }
4236 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4237 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4238 af12c6b9 2019-06-04 stsp err = sync_err;
4239 2ec1f75b 2019-03-26 stsp done:
4240 fb399478 2019-07-12 stsp free(fileindex_path);
4241 2ec1f75b 2019-03-26 stsp if (fileindex)
4242 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4243 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4244 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4245 2ec1f75b 2019-03-26 stsp err = unlockerr;
4246 33aa809d 2019-08-08 stsp return err;
4247 33aa809d 2019-08-08 stsp }
4248 33aa809d 2019-08-08 stsp
4249 33aa809d 2019-08-08 stsp static const struct got_error *
4250 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4251 33aa809d 2019-08-08 stsp {
4252 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4253 33aa809d 2019-08-08 stsp char *line = NULL;
4254 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4255 33aa809d 2019-08-08 stsp ssize_t linelen;
4256 33aa809d 2019-08-08 stsp
4257 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4258 33aa809d 2019-08-08 stsp if (linelen == -1) {
4259 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4260 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4261 33aa809d 2019-08-08 stsp goto done;
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp return NULL;
4264 33aa809d 2019-08-08 stsp }
4265 33aa809d 2019-08-08 stsp if (outfile) {
4266 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4267 33aa809d 2019-08-08 stsp if (n != linelen) {
4268 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4269 33aa809d 2019-08-08 stsp goto done;
4270 33aa809d 2019-08-08 stsp }
4271 33aa809d 2019-08-08 stsp }
4272 33aa809d 2019-08-08 stsp if (rejectfile) {
4273 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4274 33aa809d 2019-08-08 stsp if (n != linelen)
4275 910d235d 2023-01-10 mark err = got_ferror(rejectfile, GOT_ERR_IO);
4276 33aa809d 2019-08-08 stsp }
4277 33aa809d 2019-08-08 stsp done:
4278 33aa809d 2019-08-08 stsp free(line);
4279 2ec1f75b 2019-03-26 stsp return err;
4280 2ec1f75b 2019-03-26 stsp }
4281 1f1abb7e 2019-08-08 stsp
4282 33aa809d 2019-08-08 stsp static const struct got_error *
4283 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4284 33aa809d 2019-08-08 stsp {
4285 33aa809d 2019-08-08 stsp char *line = NULL;
4286 33aa809d 2019-08-08 stsp size_t linesize = 0;
4287 33aa809d 2019-08-08 stsp ssize_t linelen;
4288 33aa809d 2019-08-08 stsp
4289 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4290 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4291 500467ff 2019-09-25 hiltjo if (ferror(f))
4292 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4293 500467ff 2019-09-25 hiltjo return NULL;
4294 500467ff 2019-09-25 hiltjo }
4295 33aa809d 2019-08-08 stsp free(line);
4296 33aa809d 2019-08-08 stsp return NULL;
4297 33aa809d 2019-08-08 stsp }
4298 33aa809d 2019-08-08 stsp
4299 33aa809d 2019-08-08 stsp static const struct got_error *
4300 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4301 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4302 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4303 abc59930 2021-09-05 naddy {
4304 33aa809d 2019-08-08 stsp const struct got_error *err;
4305 33aa809d 2019-08-08 stsp
4306 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4307 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4308 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4309 33aa809d 2019-08-08 stsp if (err)
4310 33aa809d 2019-08-08 stsp return err;
4311 33aa809d 2019-08-08 stsp (*line_cur1)++;
4312 33aa809d 2019-08-08 stsp }
4313 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4314 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4315 33aa809d 2019-08-08 stsp if (rejectfile)
4316 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4317 33aa809d 2019-08-08 stsp else
4318 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4319 33aa809d 2019-08-08 stsp if (err)
4320 33aa809d 2019-08-08 stsp return err;
4321 33aa809d 2019-08-08 stsp (*line_cur2)++;
4322 33aa809d 2019-08-08 stsp }
4323 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4324 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4325 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4326 33aa809d 2019-08-08 stsp if (err)
4327 33aa809d 2019-08-08 stsp return err;
4328 33aa809d 2019-08-08 stsp (*line_cur2)++;
4329 33aa809d 2019-08-08 stsp }
4330 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4331 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4332 33aa809d 2019-08-08 stsp if (rejectfile)
4333 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4334 33aa809d 2019-08-08 stsp else
4335 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4336 33aa809d 2019-08-08 stsp if (err)
4337 33aa809d 2019-08-08 stsp return err;
4338 33aa809d 2019-08-08 stsp (*line_cur1)++;
4339 33aa809d 2019-08-08 stsp }
4340 f1e81a05 2019-08-10 stsp
4341 f1e81a05 2019-08-10 stsp return NULL;
4342 f1e81a05 2019-08-10 stsp }
4343 f1e81a05 2019-08-10 stsp
4344 f1e81a05 2019-08-10 stsp static const struct got_error *
4345 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4346 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4347 f1e81a05 2019-08-10 stsp {
4348 f1e81a05 2019-08-10 stsp const struct got_error *err;
4349 f1e81a05 2019-08-10 stsp
4350 f1e81a05 2019-08-10 stsp if (outfile) {
4351 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4352 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4353 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4354 f1e81a05 2019-08-10 stsp if (err)
4355 f1e81a05 2019-08-10 stsp return err;
4356 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4357 f1e81a05 2019-08-10 stsp }
4358 f1e81a05 2019-08-10 stsp }
4359 f1e81a05 2019-08-10 stsp if (rejectfile) {
4360 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4361 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4362 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4363 f1e81a05 2019-08-10 stsp if (err)
4364 f1e81a05 2019-08-10 stsp return err;
4365 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4366 f1e81a05 2019-08-10 stsp }
4367 33aa809d 2019-08-08 stsp }
4368 33aa809d 2019-08-08 stsp
4369 33aa809d 2019-08-08 stsp return NULL;
4370 33aa809d 2019-08-08 stsp }
4371 33aa809d 2019-08-08 stsp
4372 33aa809d 2019-08-08 stsp static const struct got_error *
4373 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4374 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4375 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4376 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4377 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4378 33aa809d 2019-08-08 stsp {
4379 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4380 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4381 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4382 33aa809d 2019-08-08 stsp FILE *hunkfile;
4383 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4384 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4385 fe621944 2020-11-10 stsp int rc;
4386 33aa809d 2019-08-08 stsp
4387 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4388 33aa809d 2019-08-08 stsp
4389 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4390 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4391 fe621944 2020-11-10 stsp start_old = cc.left.start;
4392 fe621944 2020-11-10 stsp end_old = cc.left.end;
4393 fe621944 2020-11-10 stsp start_new = cc.right.start;
4394 fe621944 2020-11-10 stsp end_new = cc.right.end;
4395 33aa809d 2019-08-08 stsp
4396 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4397 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4398 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4399 33aa809d 2019-08-08 stsp
4400 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4401 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4402 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4403 33aa809d 2019-08-08 stsp
4404 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4405 fe621944 2020-11-10 stsp if (diff_state == NULL)
4406 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4407 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4408 fe621944 2020-11-10 stsp
4409 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4410 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4411 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4412 33aa809d 2019-08-08 stsp goto done;
4413 33aa809d 2019-08-08 stsp }
4414 fe621944 2020-11-10 stsp
4415 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4416 fe621944 2020-11-10 stsp diff_result, &cc);
4417 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4418 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4419 33aa809d 2019-08-08 stsp goto done;
4420 33aa809d 2019-08-08 stsp }
4421 fe621944 2020-11-10 stsp
4422 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4423 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4424 33aa809d 2019-08-08 stsp goto done;
4425 33aa809d 2019-08-08 stsp }
4426 33aa809d 2019-08-08 stsp
4427 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4428 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4429 33aa809d 2019-08-08 stsp if (err)
4430 33aa809d 2019-08-08 stsp goto done;
4431 33aa809d 2019-08-08 stsp
4432 33aa809d 2019-08-08 stsp switch (*choice) {
4433 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4434 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4435 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4436 33aa809d 2019-08-08 stsp break;
4437 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4438 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4439 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4440 33aa809d 2019-08-08 stsp break;
4441 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4442 33aa809d 2019-08-08 stsp break;
4443 33aa809d 2019-08-08 stsp default:
4444 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4445 33aa809d 2019-08-08 stsp break;
4446 33aa809d 2019-08-08 stsp }
4447 33aa809d 2019-08-08 stsp done:
4448 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4449 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4450 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4451 33aa809d 2019-08-08 stsp return err;
4452 33aa809d 2019-08-08 stsp }
4453 33aa809d 2019-08-08 stsp
4454 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4455 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4456 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4457 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4458 1f1abb7e 2019-08-08 stsp void *progress_arg;
4459 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4460 33aa809d 2019-08-08 stsp void *patch_arg;
4461 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4462 f259c4c1 2021-09-24 stsp int unlink_added_files;
4463 1f1abb7e 2019-08-08 stsp };
4464 a129376b 2019-03-28 stsp
4465 e20a8b6f 2019-06-04 stsp static const struct got_error *
4466 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4467 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4468 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4469 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4470 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4471 33aa809d 2019-08-08 stsp {
4472 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4473 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4474 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4475 eb81bc23 2022-06-28 tracey int fd = -1, fd2 = -1;
4476 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4477 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4478 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4479 fe621944 2020-11-10 stsp struct stat sb2;
4480 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4481 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4482 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4483 33aa809d 2019-08-08 stsp
4484 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4485 33aa809d 2019-08-08 stsp
4486 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4487 33aa809d 2019-08-08 stsp if (err)
4488 33aa809d 2019-08-08 stsp return err;
4489 33aa809d 2019-08-08 stsp
4490 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4491 e7ae0baf 2021-12-31 stsp fd2 = openat(dirfd2, de_name2,
4492 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4493 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4494 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4495 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4496 fa3cef63 2020-07-23 stsp goto done;
4497 fa3cef63 2020-07-23 stsp }
4498 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4499 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4500 c0df5966 2021-12-31 stsp if (link_len == -1) {
4501 c0df5966 2021-12-31 stsp return got_error_from_errno2("readlinkat",
4502 c0df5966 2021-12-31 stsp path2);
4503 c0df5966 2021-12-31 stsp }
4504 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4505 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4506 12463d8b 2019-12-13 stsp }
4507 12463d8b 2019-12-13 stsp } else {
4508 8bd0cdad 2021-12-31 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4509 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4510 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4511 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4512 fa3cef63 2020-07-23 stsp goto done;
4513 fa3cef63 2020-07-23 stsp }
4514 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4515 fa3cef63 2020-07-23 stsp sizeof(link_target));
4516 fa3cef63 2020-07-23 stsp if (link_len == -1)
4517 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4518 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4519 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4520 12463d8b 2019-12-13 stsp }
4521 1ebedb77 2019-10-19 stsp }
4522 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4523 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4524 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4525 fa3cef63 2020-07-23 stsp goto done;
4526 fa3cef63 2020-07-23 stsp }
4527 1ebedb77 2019-10-19 stsp
4528 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4529 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4530 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4531 fa3cef63 2020-07-23 stsp goto done;
4532 fa3cef63 2020-07-23 stsp }
4533 fa3cef63 2020-07-23 stsp fd2 = -1;
4534 fa3cef63 2020-07-23 stsp } else {
4535 fa3cef63 2020-07-23 stsp size_t n;
4536 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4537 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4538 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4539 fa3cef63 2020-07-23 stsp goto done;
4540 fa3cef63 2020-07-23 stsp }
4541 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4542 fa3cef63 2020-07-23 stsp if (n != link_len) {
4543 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4544 fa3cef63 2020-07-23 stsp goto done;
4545 fa3cef63 2020-07-23 stsp }
4546 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4547 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4548 fa3cef63 2020-07-23 stsp goto done;
4549 fa3cef63 2020-07-23 stsp }
4550 fa3cef63 2020-07-23 stsp rewind(f2);
4551 33aa809d 2019-08-08 stsp }
4552 33aa809d 2019-08-08 stsp
4553 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4554 eb81bc23 2022-06-28 tracey if (fd == -1) {
4555 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4556 eb81bc23 2022-06-28 tracey goto done;
4557 eb81bc23 2022-06-28 tracey }
4558 eb81bc23 2022-06-28 tracey
4559 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4560 33aa809d 2019-08-08 stsp if (err)
4561 33aa809d 2019-08-08 stsp goto done;
4562 33aa809d 2019-08-08 stsp
4563 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4564 33aa809d 2019-08-08 stsp if (err)
4565 33aa809d 2019-08-08 stsp goto done;
4566 33aa809d 2019-08-08 stsp
4567 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4568 33aa809d 2019-08-08 stsp if (err)
4569 33aa809d 2019-08-08 stsp goto done;
4570 33aa809d 2019-08-08 stsp
4571 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4572 4b752015 2022-06-30 stsp 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4573 33aa809d 2019-08-08 stsp if (err)
4574 33aa809d 2019-08-08 stsp goto done;
4575 33aa809d 2019-08-08 stsp
4576 b90054ed 2022-11-01 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4577 b90054ed 2022-11-01 stsp "");
4578 33aa809d 2019-08-08 stsp if (err)
4579 33aa809d 2019-08-08 stsp goto done;
4580 33aa809d 2019-08-08 stsp
4581 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4582 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4583 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4584 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4585 fe621944 2020-11-10 stsp
4586 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4587 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4588 5e91dae4 2022-08-30 stsp struct diff_chunk_context cc = {};
4589 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4590 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4591 fe621944 2020-11-10 stsp nchanges++;
4592 fe621944 2020-11-10 stsp }
4593 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4594 33aa809d 2019-08-08 stsp int choice;
4595 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4596 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4597 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4598 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4599 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4600 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4601 33aa809d 2019-08-08 stsp if (err)
4602 33aa809d 2019-08-08 stsp goto done;
4603 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4604 33aa809d 2019-08-08 stsp have_content = 1;
4605 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4606 33aa809d 2019-08-08 stsp break;
4607 33aa809d 2019-08-08 stsp }
4608 1ebedb77 2019-10-19 stsp if (have_content) {
4609 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4610 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4611 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4612 1ebedb77 2019-10-19 stsp if (err)
4613 1ebedb77 2019-10-19 stsp goto done;
4614 1ebedb77 2019-10-19 stsp
4615 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4616 b2b3fce1 2022-10-29 op mode_t mode;
4617 b2b3fce1 2022-10-29 op
4618 b2b3fce1 2022-10-29 op mode = apply_umask(sb2.st_mode);
4619 b2b3fce1 2022-10-29 op if (fchmod(fileno(outfile), mode) == -1) {
4620 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4621 fa3cef63 2020-07-23 stsp goto done;
4622 fa3cef63 2020-07-23 stsp }
4623 1ebedb77 2019-10-19 stsp }
4624 1ebedb77 2019-10-19 stsp }
4625 33aa809d 2019-08-08 stsp done:
4626 33aa809d 2019-08-08 stsp free(id_str);
4627 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4628 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4629 33aa809d 2019-08-08 stsp if (blob)
4630 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4631 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4632 fe621944 2020-11-10 stsp if (err == NULL)
4633 fe621944 2020-11-10 stsp err = free_err;
4634 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4635 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4636 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4637 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4638 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4639 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4640 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4641 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4642 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4643 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4644 33aa809d 2019-08-08 stsp if (err || !have_content) {
4645 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4646 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4647 33aa809d 2019-08-08 stsp free(*path_outfile);
4648 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4649 33aa809d 2019-08-08 stsp }
4650 33aa809d 2019-08-08 stsp free(path1);
4651 33aa809d 2019-08-08 stsp return err;
4652 33aa809d 2019-08-08 stsp }
4653 33aa809d 2019-08-08 stsp
4654 33aa809d 2019-08-08 stsp static const struct got_error *
4655 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4656 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4657 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4658 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4659 a129376b 2019-03-28 stsp {
4660 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4661 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4662 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4663 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4664 a44927cc 2022-04-07 stsp struct got_commit_object *base_commit = NULL;
4665 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4666 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4667 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4668 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4669 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4670 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4671 eb81bc23 2022-06-28 tracey int fd = -1;
4672 a129376b 2019-03-28 stsp
4673 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4674 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4675 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4676 d3bcc3d1 2019-08-08 stsp return NULL;
4677 3d69ad8d 2019-08-17 semarie
4678 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4679 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4680 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4681 d3bcc3d1 2019-08-08 stsp
4682 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4683 65084dad 2019-08-08 stsp if (ie == NULL)
4684 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4685 a129376b 2019-03-28 stsp
4686 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4687 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4688 a129376b 2019-03-28 stsp if (err) {
4689 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4690 a129376b 2019-03-28 stsp goto done;
4691 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4692 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4693 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4694 e20a8b6f 2019-06-04 stsp goto done;
4695 e20a8b6f 2019-06-04 stsp }
4696 a129376b 2019-03-28 stsp }
4697 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4698 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4699 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4700 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4701 a129376b 2019-03-28 stsp goto done;
4702 a129376b 2019-03-28 stsp }
4703 a129376b 2019-03-28 stsp } else {
4704 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4705 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4706 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4707 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4708 a129376b 2019-03-28 stsp goto done;
4709 a129376b 2019-03-28 stsp }
4710 a129376b 2019-03-28 stsp } else {
4711 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4712 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4713 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4714 a129376b 2019-03-28 stsp goto done;
4715 a129376b 2019-03-28 stsp }
4716 a129376b 2019-03-28 stsp }
4717 a129376b 2019-03-28 stsp }
4718 a129376b 2019-03-28 stsp
4719 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&base_commit, a->repo,
4720 a44927cc 2022-04-07 stsp a->worktree->base_commit_id);
4721 a44927cc 2022-04-07 stsp if (err)
4722 a44927cc 2022-04-07 stsp goto done;
4723 a44927cc 2022-04-07 stsp
4724 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4725 a9fa2909 2019-07-27 stsp if (err) {
4726 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4727 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4728 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4729 a9fa2909 2019-07-27 stsp goto done;
4730 a9fa2909 2019-07-27 stsp } else {
4731 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4732 a9fa2909 2019-07-27 stsp if (err)
4733 a9fa2909 2019-07-27 stsp goto done;
4734 a9fa2909 2019-07-27 stsp
4735 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4736 1233e6b6 2020-10-19 stsp if (err)
4737 a9fa2909 2019-07-27 stsp goto done;
4738 a9fa2909 2019-07-27 stsp
4739 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4740 1233e6b6 2020-10-19 stsp free(te_name);
4741 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4742 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4743 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4744 a9fa2909 2019-07-27 stsp goto done;
4745 a9fa2909 2019-07-27 stsp }
4746 a129376b 2019-03-28 stsp }
4747 a129376b 2019-03-28 stsp
4748 a129376b 2019-03-28 stsp switch (status) {
4749 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4750 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4751 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4752 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4753 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4754 33aa809d 2019-08-08 stsp if (err)
4755 33aa809d 2019-08-08 stsp goto done;
4756 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4757 33aa809d 2019-08-08 stsp break;
4758 33aa809d 2019-08-08 stsp }
4759 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4760 1f1abb7e 2019-08-08 stsp ie->path);
4761 1ee397ad 2019-07-12 stsp if (err)
4762 1ee397ad 2019-07-12 stsp goto done;
4763 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4764 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4765 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4766 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4767 f259c4c1 2021-09-24 stsp relpath) == -1) {
4768 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4769 f259c4c1 2021-09-24 stsp goto done;
4770 f259c4c1 2021-09-24 stsp }
4771 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4772 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4773 f259c4c1 2021-09-24 stsp ondisk_path);
4774 f259c4c1 2021-09-24 stsp break;
4775 f259c4c1 2021-09-24 stsp }
4776 f259c4c1 2021-09-24 stsp }
4777 a129376b 2019-03-28 stsp break;
4778 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4779 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4780 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4781 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4782 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4783 33aa809d 2019-08-08 stsp if (err)
4784 33aa809d 2019-08-08 stsp goto done;
4785 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4786 33aa809d 2019-08-08 stsp break;
4787 33aa809d 2019-08-08 stsp }
4788 33aa809d 2019-08-08 stsp /* fall through */
4789 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4790 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4791 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4792 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4793 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4794 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4795 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4796 3093e2d7 2023-02-04 op memcpy(id.hash, ie->staged_blob_hash,
4797 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4798 24278f30 2019-08-03 stsp } else
4799 3093e2d7 2023-02-04 op memcpy(id.hash, ie->blob_hash,
4800 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4801 dfe0a3d8 2023-02-04 op id.algo = got_repo_get_hash_algorithm(a->repo);
4802 dfe0a3d8 2023-02-04 op
4803 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
4804 eb81bc23 2022-06-28 tracey if (fd == -1) {
4805 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
4806 eb81bc23 2022-06-28 tracey goto done;
4807 eb81bc23 2022-06-28 tracey }
4808 eb81bc23 2022-06-28 tracey
4809 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4810 a129376b 2019-03-28 stsp if (err)
4811 65084dad 2019-08-08 stsp goto done;
4812 65084dad 2019-08-08 stsp
4813 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4814 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4815 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4816 a129376b 2019-03-28 stsp goto done;
4817 65084dad 2019-08-08 stsp }
4818 65084dad 2019-08-08 stsp
4819 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4820 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4821 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4822 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4823 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4824 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4825 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4826 33aa809d 2019-08-08 stsp break;
4827 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4828 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4829 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4830 369fd7e5 2020-07-23 stsp path_content);
4831 369fd7e5 2020-07-23 stsp break;
4832 369fd7e5 2020-07-23 stsp }
4833 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4834 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4835 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4836 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4837 369fd7e5 2020-07-23 stsp } else {
4838 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4839 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4840 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4841 369fd7e5 2020-07-23 stsp goto done;
4842 369fd7e5 2020-07-23 stsp }
4843 33aa809d 2019-08-08 stsp }
4844 33aa809d 2019-08-08 stsp } else {
4845 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4846 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4847 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4848 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4849 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4850 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4851 2e1fa222 2020-07-23 stsp } else {
4852 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4853 2e1fa222 2020-07-23 stsp ie->path,
4854 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4855 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4856 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4857 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4858 2e1fa222 2020-07-23 stsp }
4859 a129376b 2019-03-28 stsp if (err)
4860 a129376b 2019-03-28 stsp goto done;
4861 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4862 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4863 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4864 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4865 3093e2d7 2023-02-04 op blob->id.hash,
4866 3093e2d7 2023-02-04 op a->worktree->base_commit_id->hash, 1);
4867 2e1fa222 2020-07-23 stsp if (err)
4868 2e1fa222 2020-07-23 stsp goto done;
4869 2e1fa222 2020-07-23 stsp }
4870 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4871 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4872 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4873 33aa809d 2019-08-08 stsp }
4874 a129376b 2019-03-28 stsp }
4875 a129376b 2019-03-28 stsp break;
4876 e20a8b6f 2019-06-04 stsp }
4877 a129376b 2019-03-28 stsp default:
4878 1f1abb7e 2019-08-08 stsp break;
4879 a129376b 2019-03-28 stsp }
4880 a129376b 2019-03-28 stsp done:
4881 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4882 33aa809d 2019-08-08 stsp free(path_content);
4883 e20a8b6f 2019-06-04 stsp free(parent_path);
4884 a129376b 2019-03-28 stsp free(tree_path);
4885 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4886 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
4887 a129376b 2019-03-28 stsp if (blob)
4888 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4889 a129376b 2019-03-28 stsp if (tree)
4890 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4891 a129376b 2019-03-28 stsp free(tree_id);
4892 a44927cc 2022-04-07 stsp if (base_commit)
4893 a44927cc 2022-04-07 stsp got_object_commit_close(base_commit);
4894 e20a8b6f 2019-06-04 stsp return err;
4895 e20a8b6f 2019-06-04 stsp }
4896 e20a8b6f 2019-06-04 stsp
4897 e20a8b6f 2019-06-04 stsp const struct got_error *
4898 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4899 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4900 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4901 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4902 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4903 e20a8b6f 2019-06-04 stsp {
4904 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4905 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4906 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4907 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4908 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4909 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4910 e20a8b6f 2019-06-04 stsp
4911 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4912 e20a8b6f 2019-06-04 stsp if (err)
4913 e20a8b6f 2019-06-04 stsp return err;
4914 e20a8b6f 2019-06-04 stsp
4915 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4916 e20a8b6f 2019-06-04 stsp if (err)
4917 e20a8b6f 2019-06-04 stsp goto done;
4918 e20a8b6f 2019-06-04 stsp
4919 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4920 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4921 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4922 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4923 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4924 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4925 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4926 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4927 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4928 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4929 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4930 e20a8b6f 2019-06-04 stsp if (err)
4931 af12c6b9 2019-06-04 stsp break;
4932 e20a8b6f 2019-06-04 stsp }
4933 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4934 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4935 e20a8b6f 2019-06-04 stsp err = sync_err;
4936 af12c6b9 2019-06-04 stsp done:
4937 fb399478 2019-07-12 stsp free(fileindex_path);
4938 a129376b 2019-03-28 stsp if (fileindex)
4939 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4940 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4941 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4942 a129376b 2019-03-28 stsp err = unlockerr;
4943 c4296144 2019-05-09 stsp return err;
4944 c4296144 2019-05-09 stsp }
4945 c4296144 2019-05-09 stsp
4946 cf066bf8 2019-05-09 stsp static void
4947 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4948 cf066bf8 2019-05-09 stsp {
4949 24519714 2019-05-09 stsp free(ct->path);
4950 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4951 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4952 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4953 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4954 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4955 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4956 cf066bf8 2019-05-09 stsp free(ct);
4957 cf066bf8 2019-05-09 stsp }
4958 24519714 2019-05-09 stsp
4959 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4960 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4961 24519714 2019-05-09 stsp struct got_repository *repo;
4962 24519714 2019-05-09 stsp struct got_worktree *worktree;
4963 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4964 5f8a88c6 2019-08-03 stsp int have_staged_files;
4965 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4966 2a47b1e5 2022-11-01 stsp int diff_header_shown;
4967 2a47b1e5 2022-11-01 stsp FILE *diff_outfile;
4968 2a47b1e5 2022-11-01 stsp FILE *f1;
4969 2a47b1e5 2022-11-01 stsp FILE *f2;
4970 24519714 2019-05-09 stsp };
4971 2a47b1e5 2022-11-01 stsp
4972 2a47b1e5 2022-11-01 stsp /*
4973 2a47b1e5 2022-11-01 stsp * Create a file which contains the target path of a symlink so we can feed
4974 2a47b1e5 2022-11-01 stsp * it as content to the diff engine.
4975 2a47b1e5 2022-11-01 stsp */
4976 2a47b1e5 2022-11-01 stsp static const struct got_error *
4977 2a47b1e5 2022-11-01 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4978 2a47b1e5 2022-11-01 stsp const char *abspath)
4979 2a47b1e5 2022-11-01 stsp {
4980 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
4981 2a47b1e5 2022-11-01 stsp char target_path[PATH_MAX];
4982 2a47b1e5 2022-11-01 stsp ssize_t target_len, outlen;
4983 2a47b1e5 2022-11-01 stsp
4984 2a47b1e5 2022-11-01 stsp *fd = -1;
4985 2a47b1e5 2022-11-01 stsp
4986 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
4987 2a47b1e5 2022-11-01 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4988 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4989 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlinkat", abspath);
4990 2a47b1e5 2022-11-01 stsp } else {
4991 2a47b1e5 2022-11-01 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4992 2a47b1e5 2022-11-01 stsp if (target_len == -1)
4993 2a47b1e5 2022-11-01 stsp return got_error_from_errno2("readlink", abspath);
4994 2a47b1e5 2022-11-01 stsp }
4995 2a47b1e5 2022-11-01 stsp
4996 2a47b1e5 2022-11-01 stsp *fd = got_opentempfd();
4997 2a47b1e5 2022-11-01 stsp if (*fd == -1)
4998 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentempfd");
4999 2a47b1e5 2022-11-01 stsp
5000 2a47b1e5 2022-11-01 stsp outlen = write(*fd, target_path, target_len);
5001 2a47b1e5 2022-11-01 stsp if (outlen == -1) {
5002 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5003 2a47b1e5 2022-11-01 stsp goto done;
5004 2a47b1e5 2022-11-01 stsp }
5005 2a47b1e5 2022-11-01 stsp
5006 2a47b1e5 2022-11-01 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
5007 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("lseek", abspath);
5008 2a47b1e5 2022-11-01 stsp goto done;
5009 2a47b1e5 2022-11-01 stsp }
5010 2a47b1e5 2022-11-01 stsp done:
5011 2a47b1e5 2022-11-01 stsp if (err) {
5012 2a47b1e5 2022-11-01 stsp close(*fd);
5013 2a47b1e5 2022-11-01 stsp *fd = -1;
5014 2a47b1e5 2022-11-01 stsp }
5015 2a47b1e5 2022-11-01 stsp return err;
5016 2a47b1e5 2022-11-01 stsp }
5017 2a47b1e5 2022-11-01 stsp
5018 2a47b1e5 2022-11-01 stsp static const struct got_error *
5019 2a47b1e5 2022-11-01 stsp append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5020 2a47b1e5 2022-11-01 stsp FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5021 6d15dc69 2022-11-01 stsp int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5022 2a47b1e5 2022-11-01 stsp {
5023 2a47b1e5 2022-11-01 stsp const struct got_error *err = NULL;
5024 2a47b1e5 2022-11-01 stsp struct got_blob_object *blob1 = NULL;
5025 2a47b1e5 2022-11-01 stsp int fd = -1, fd1 = -1, fd2 = -1;
5026 2a47b1e5 2022-11-01 stsp FILE *ondisk_file = NULL;
5027 2a47b1e5 2022-11-01 stsp char *label1 = NULL;
5028 2a47b1e5 2022-11-01 stsp struct stat sb;
5029 2a47b1e5 2022-11-01 stsp off_t size1 = 0;
5030 2a47b1e5 2022-11-01 stsp int f2_exists = 0;
5031 2a47b1e5 2022-11-01 stsp char *id_str = NULL;
5032 2a47b1e5 2022-11-01 stsp
5033 2a47b1e5 2022-11-01 stsp memset(&sb, 0, sizeof(sb));
5034 4ba5cca9 2022-11-01 stsp
5035 4ba5cca9 2022-11-01 stsp if (diff_staged) {
5036 4ba5cca9 2022-11-01 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5037 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_ADD &&
5038 4ba5cca9 2022-11-01 stsp ct->staged_status != GOT_STATUS_DELETE)
5039 4ba5cca9 2022-11-01 stsp return NULL;
5040 4ba5cca9 2022-11-01 stsp } else {
5041 4ba5cca9 2022-11-01 stsp if (ct->status != GOT_STATUS_MODIFY &&
5042 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_ADD &&
5043 4ba5cca9 2022-11-01 stsp ct->status != GOT_STATUS_DELETE)
5044 4ba5cca9 2022-11-01 stsp return NULL;
5045 4ba5cca9 2022-11-01 stsp }
5046 2a47b1e5 2022-11-01 stsp
5047 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f1);
5048 2a47b1e5 2022-11-01 stsp if (err)
5049 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5050 2a47b1e5 2022-11-01 stsp err = got_opentemp_truncate(f2);
5051 2a47b1e5 2022-11-01 stsp if (err)
5052 2a47b1e5 2022-11-01 stsp return got_error_from_errno("got_opentemp_truncate");
5053 2a47b1e5 2022-11-01 stsp
5054 2a47b1e5 2022-11-01 stsp if (!*diff_header_shown) {
5055 2a47b1e5 2022-11-01 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
5056 2a47b1e5 2022-11-01 stsp if (err)
5057 2a47b1e5 2022-11-01 stsp return err;
5058 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5059 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree));
5060 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "commit - %s\n", id_str);
5061 2a47b1e5 2022-11-01 stsp fprintf(diff_outfile, "path + %s%s\n",
5062 2a47b1e5 2022-11-01 stsp got_worktree_get_root_path(worktree),
5063 2a47b1e5 2022-11-01 stsp diff_staged ? " (staged changes)" : "");
5064 2a47b1e5 2022-11-01 stsp *diff_header_shown = 1;
5065 2a47b1e5 2022-11-01 stsp }
5066 2a47b1e5 2022-11-01 stsp
5067 2a47b1e5 2022-11-01 stsp if (diff_staged) {
5068 2a47b1e5 2022-11-01 stsp const char *label1 = NULL, *label2 = NULL;
5069 2a47b1e5 2022-11-01 stsp switch (ct->staged_status) {
5070 2a47b1e5 2022-11-01 stsp case GOT_STATUS_MODIFY:
5071 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5072 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5073 2a47b1e5 2022-11-01 stsp break;
5074 2a47b1e5 2022-11-01 stsp case GOT_STATUS_ADD:
5075 2a47b1e5 2022-11-01 stsp label2 = ct->path;
5076 2a47b1e5 2022-11-01 stsp break;
5077 2a47b1e5 2022-11-01 stsp case GOT_STATUS_DELETE:
5078 2a47b1e5 2022-11-01 stsp label1 = ct->path;
5079 2a47b1e5 2022-11-01 stsp break;
5080 2a47b1e5 2022-11-01 stsp default:
5081 2a47b1e5 2022-11-01 stsp return got_error(GOT_ERR_FILE_STATUS);
5082 2a47b1e5 2022-11-01 stsp }
5083 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5084 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5085 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5086 2a47b1e5 2022-11-01 stsp goto done;
5087 2a47b1e5 2022-11-01 stsp }
5088 2a47b1e5 2022-11-01 stsp fd2 = got_opentempfd();
5089 2a47b1e5 2022-11-01 stsp if (fd2 == -1) {
5090 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5091 2a47b1e5 2022-11-01 stsp goto done;
5092 2a47b1e5 2022-11-01 stsp }
5093 2a47b1e5 2022-11-01 stsp err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5094 2a47b1e5 2022-11-01 stsp fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5095 1f3405c9 2023-01-17 mark label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5096 a76e88e5 2023-01-10 mark NULL, repo, diff_outfile);
5097 2a47b1e5 2022-11-01 stsp goto done;
5098 2a47b1e5 2022-11-01 stsp }
5099 2a47b1e5 2022-11-01 stsp
5100 2a47b1e5 2022-11-01 stsp fd1 = got_opentempfd();
5101 2a47b1e5 2022-11-01 stsp if (fd1 == -1) {
5102 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentempfd");
5103 2a47b1e5 2022-11-01 stsp goto done;
5104 2a47b1e5 2022-11-01 stsp }
5105 2a47b1e5 2022-11-01 stsp
5106 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_ADD) {
5107 2a47b1e5 2022-11-01 stsp err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5108 2a47b1e5 2022-11-01 stsp 8192, fd1);
5109 2a47b1e5 2022-11-01 stsp if (err)
5110 2a47b1e5 2022-11-01 stsp goto done;
5111 2a47b1e5 2022-11-01 stsp }
5112 2a47b1e5 2022-11-01 stsp
5113 2a47b1e5 2022-11-01 stsp if (ct->status != GOT_STATUS_DELETE) {
5114 2a47b1e5 2022-11-01 stsp if (dirfd != -1) {
5115 2a47b1e5 2022-11-01 stsp fd = openat(dirfd, de_name,
5116 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5117 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5118 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5119 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("openat",
5120 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5121 2a47b1e5 2022-11-01 stsp goto done;
5122 2a47b1e5 2022-11-01 stsp }
5123 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5124 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5125 2a47b1e5 2022-11-01 stsp if (err)
5126 2a47b1e5 2022-11-01 stsp goto done;
5127 2a47b1e5 2022-11-01 stsp }
5128 2a47b1e5 2022-11-01 stsp } else {
5129 2a47b1e5 2022-11-01 stsp fd = open(ct->ondisk_path,
5130 2a47b1e5 2022-11-01 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5131 2a47b1e5 2022-11-01 stsp if (fd == -1) {
5132 2a47b1e5 2022-11-01 stsp if (!got_err_open_nofollow_on_symlink()) {
5133 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("open",
5134 2a47b1e5 2022-11-01 stsp ct->ondisk_path);
5135 2a47b1e5 2022-11-01 stsp goto done;
5136 2a47b1e5 2022-11-01 stsp }
5137 2a47b1e5 2022-11-01 stsp err = get_symlink_target_file(&fd, dirfd,
5138 2a47b1e5 2022-11-01 stsp de_name, ct->ondisk_path);
5139 2a47b1e5 2022-11-01 stsp if (err)
5140 2a47b1e5 2022-11-01 stsp goto done;
5141 2a47b1e5 2022-11-01 stsp }
5142 2a47b1e5 2022-11-01 stsp }
5143 2a47b1e5 2022-11-01 stsp if (fstatat(fd, ct->ondisk_path, &sb,
5144 2a47b1e5 2022-11-01 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5145 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fstatat", ct->ondisk_path);
5146 2a47b1e5 2022-11-01 stsp goto done;
5147 2a47b1e5 2022-11-01 stsp }
5148 2a47b1e5 2022-11-01 stsp ondisk_file = fdopen(fd, "r");
5149 2a47b1e5 2022-11-01 stsp if (ondisk_file == NULL) {
5150 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("fdopen", ct->ondisk_path);
5151 2a47b1e5 2022-11-01 stsp goto done;
5152 2a47b1e5 2022-11-01 stsp }
5153 2a47b1e5 2022-11-01 stsp fd = -1;
5154 2a47b1e5 2022-11-01 stsp f2_exists = 1;
5155 2a47b1e5 2022-11-01 stsp }
5156 2a47b1e5 2022-11-01 stsp
5157 2a47b1e5 2022-11-01 stsp if (blob1) {
5158 2a47b1e5 2022-11-01 stsp err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5159 2a47b1e5 2022-11-01 stsp f1, blob1);
5160 2a47b1e5 2022-11-01 stsp if (err)
5161 2a47b1e5 2022-11-01 stsp goto done;
5162 2a47b1e5 2022-11-01 stsp }
5163 2a47b1e5 2022-11-01 stsp
5164 2a47b1e5 2022-11-01 stsp err = got_diff_blob_file(blob1, f1, size1, label1,
5165 2a47b1e5 2022-11-01 stsp ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5166 1f3405c9 2023-01-17 mark GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5167 2a47b1e5 2022-11-01 stsp done:
5168 2a47b1e5 2022-11-01 stsp if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5169 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5170 2a47b1e5 2022-11-01 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5171 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5172 2a47b1e5 2022-11-01 stsp if (blob1)
5173 2a47b1e5 2022-11-01 stsp got_object_blob_close(blob1);
5174 2a47b1e5 2022-11-01 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5175 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("close");
5176 2a47b1e5 2022-11-01 stsp if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5177 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
5178 2a47b1e5 2022-11-01 stsp return err;
5179 2a47b1e5 2022-11-01 stsp }
5180 cf066bf8 2019-05-09 stsp
5181 c4296144 2019-05-09 stsp static const struct got_error *
5182 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
5183 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
5184 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5185 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
5186 c4296144 2019-05-09 stsp {
5187 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
5188 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
5189 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5190 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
5191 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
5192 768aea60 2019-05-09 stsp struct stat sb;
5193 c4296144 2019-05-09 stsp
5194 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
5195 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
5196 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
5197 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5198 5f8a88c6 2019-08-03 stsp return NULL;
5199 5f8a88c6 2019-08-03 stsp } else {
5200 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
5201 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
5202 c4296144 2019-05-09 stsp
5203 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
5204 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
5205 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
5206 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
5207 5f8a88c6 2019-08-03 stsp return NULL;
5208 5f8a88c6 2019-08-03 stsp }
5209 0b5cc0d6 2019-05-09 stsp
5210 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
5211 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5212 036813ee 2019-05-09 stsp goto done;
5213 036813ee 2019-05-09 stsp }
5214 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
5215 036813ee 2019-05-09 stsp parent_path = strdup("");
5216 69960a46 2019-05-09 stsp if (parent_path == NULL)
5217 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
5218 69960a46 2019-05-09 stsp } else {
5219 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
5220 69960a46 2019-05-09 stsp if (err)
5221 69960a46 2019-05-09 stsp return err;
5222 69960a46 2019-05-09 stsp }
5223 c4296144 2019-05-09 stsp
5224 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
5225 cf066bf8 2019-05-09 stsp if (ct == NULL) {
5226 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5227 c4296144 2019-05-09 stsp goto done;
5228 768aea60 2019-05-09 stsp }
5229 768aea60 2019-05-09 stsp
5230 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5231 768aea60 2019-05-09 stsp relpath) == -1) {
5232 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5233 768aea60 2019-05-09 stsp goto done;
5234 768aea60 2019-05-09 stsp }
5235 0aeb8099 2020-07-23 stsp
5236 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
5237 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
5238 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
5239 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5240 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
5241 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
5242 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
5243 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
5244 0aeb8099 2020-07-23 stsp break;
5245 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
5246 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
5247 0aeb8099 2020-07-23 stsp break;
5248 0aeb8099 2020-07-23 stsp default:
5249 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5250 0aeb8099 2020-07-23 stsp goto done;
5251 0aeb8099 2020-07-23 stsp }
5252 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
5253 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
5254 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5255 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5256 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5257 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5258 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5259 12463d8b 2019-12-13 stsp ct->ondisk_path);
5260 12463d8b 2019-12-13 stsp goto done;
5261 12463d8b 2019-12-13 stsp }
5262 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5263 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5264 768aea60 2019-05-09 stsp goto done;
5265 768aea60 2019-05-09 stsp }
5266 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5267 c4296144 2019-05-09 stsp }
5268 c4296144 2019-05-09 stsp
5269 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5270 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5271 44d03001 2019-05-09 stsp relpath) == -1) {
5272 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5273 44d03001 2019-05-09 stsp goto done;
5274 44d03001 2019-05-09 stsp }
5275 44d03001 2019-05-09 stsp
5276 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5277 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5278 35213c7c 2020-07-23 stsp int is_bad_symlink;
5279 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5280 35213c7c 2020-07-23 stsp ssize_t target_len;
5281 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5282 35213c7c 2020-07-23 stsp sizeof(target_path));
5283 35213c7c 2020-07-23 stsp if (target_len == -1) {
5284 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5285 35213c7c 2020-07-23 stsp ct->ondisk_path);
5286 35213c7c 2020-07-23 stsp goto done;
5287 35213c7c 2020-07-23 stsp }
5288 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5289 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5290 35213c7c 2020-07-23 stsp if (err)
5291 35213c7c 2020-07-23 stsp goto done;
5292 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5293 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5294 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5295 35213c7c 2020-07-23 stsp goto done;
5296 35213c7c 2020-07-23 stsp }
5297 35213c7c 2020-07-23 stsp }
5298 35213c7c 2020-07-23 stsp
5299 35213c7c 2020-07-23 stsp
5300 cf066bf8 2019-05-09 stsp ct->status = status;
5301 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5302 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5303 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5304 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5305 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5306 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5307 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5308 b416585c 2019-05-13 stsp goto done;
5309 b416585c 2019-05-13 stsp }
5310 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5311 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5312 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5313 f0b75401 2019-08-03 stsp goto done;
5314 f0b75401 2019-08-03 stsp }
5315 f0b75401 2019-08-03 stsp }
5316 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5317 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5318 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5319 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5320 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5321 036813ee 2019-05-09 stsp goto done;
5322 036813ee 2019-05-09 stsp }
5323 ca2503ea 2019-05-09 stsp }
5324 24519714 2019-05-09 stsp ct->path = strdup(path);
5325 24519714 2019-05-09 stsp if (ct->path == NULL) {
5326 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5327 24519714 2019-05-09 stsp goto done;
5328 24519714 2019-05-09 stsp }
5329 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5330 2a47b1e5 2022-11-01 stsp if (err)
5331 2a47b1e5 2022-11-01 stsp goto done;
5332 2a47b1e5 2022-11-01 stsp
5333 2a47b1e5 2022-11-01 stsp if (a->diff_outfile && ct && new != NULL) {
5334 2a47b1e5 2022-11-01 stsp err = append_ct_diff(ct, &a->diff_header_shown,
5335 2a47b1e5 2022-11-01 stsp a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5336 6d15dc69 2022-11-01 stsp a->have_staged_files, a->repo, a->worktree);
5337 2a47b1e5 2022-11-01 stsp if (err)
5338 2a47b1e5 2022-11-01 stsp goto done;
5339 2a47b1e5 2022-11-01 stsp }
5340 c4296144 2019-05-09 stsp done:
5341 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5342 ed175427 2019-05-09 stsp free_commitable(ct);
5343 24519714 2019-05-09 stsp free(parent_path);
5344 036813ee 2019-05-09 stsp free(path);
5345 a129376b 2019-03-28 stsp return err;
5346 a129376b 2019-03-28 stsp }
5347 c4296144 2019-05-09 stsp
5348 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5349 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5350 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5351 036813ee 2019-05-09 stsp struct got_repository *);
5352 ed175427 2019-05-09 stsp
5353 ed175427 2019-05-09 stsp static const struct got_error *
5354 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5355 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5356 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5357 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5358 afa376bf 2019-05-09 stsp struct got_repository *repo)
5359 ed175427 2019-05-09 stsp {
5360 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5361 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5362 036813ee 2019-05-09 stsp char *subpath;
5363 ed175427 2019-05-09 stsp
5364 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5365 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5366 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5367 ed175427 2019-05-09 stsp
5368 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5369 ed175427 2019-05-09 stsp if (err)
5370 ed175427 2019-05-09 stsp return err;
5371 ed175427 2019-05-09 stsp
5372 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5373 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5374 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5375 036813ee 2019-05-09 stsp free(subpath);
5376 036813ee 2019-05-09 stsp return err;
5377 036813ee 2019-05-09 stsp }
5378 ed175427 2019-05-09 stsp
5379 036813ee 2019-05-09 stsp static const struct got_error *
5380 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5381 036813ee 2019-05-09 stsp {
5382 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5383 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5384 ed175427 2019-05-09 stsp
5385 036813ee 2019-05-09 stsp *match = 0;
5386 ed175427 2019-05-09 stsp
5387 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5388 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5389 0f63689d 2019-05-10 stsp return NULL;
5390 036813ee 2019-05-09 stsp }
5391 0b5cc0d6 2019-05-09 stsp
5392 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5393 0f63689d 2019-05-10 stsp if (err)
5394 0f63689d 2019-05-10 stsp return err;
5395 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5396 036813ee 2019-05-09 stsp free(ct_parent_path);
5397 036813ee 2019-05-09 stsp return err;
5398 036813ee 2019-05-09 stsp }
5399 0b5cc0d6 2019-05-09 stsp
5400 768aea60 2019-05-09 stsp static mode_t
5401 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5402 768aea60 2019-05-09 stsp {
5403 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5404 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5405 3d9a4ec4 2020-07-23 stsp
5406 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5407 768aea60 2019-05-09 stsp }
5408 768aea60 2019-05-09 stsp
5409 036813ee 2019-05-09 stsp static const struct got_error *
5410 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5411 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5412 036813ee 2019-05-09 stsp {
5413 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5414 ca2503ea 2019-05-09 stsp
5415 036813ee 2019-05-09 stsp *new_te = NULL;
5416 0b5cc0d6 2019-05-09 stsp
5417 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5418 036813ee 2019-05-09 stsp if (err)
5419 036813ee 2019-05-09 stsp goto done;
5420 0b5cc0d6 2019-05-09 stsp
5421 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5422 036813ee 2019-05-09 stsp
5423 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5424 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5425 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5426 5f8a88c6 2019-08-03 stsp else
5427 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5428 036813ee 2019-05-09 stsp done:
5429 036813ee 2019-05-09 stsp if (err && *new_te) {
5430 56e0773d 2019-11-28 stsp free(*new_te);
5431 036813ee 2019-05-09 stsp *new_te = NULL;
5432 036813ee 2019-05-09 stsp }
5433 036813ee 2019-05-09 stsp return err;
5434 036813ee 2019-05-09 stsp }
5435 036813ee 2019-05-09 stsp
5436 036813ee 2019-05-09 stsp static const struct got_error *
5437 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5438 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5439 036813ee 2019-05-09 stsp {
5440 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5441 102b254e 2020-10-19 stsp char *ct_name = NULL;
5442 036813ee 2019-05-09 stsp
5443 036813ee 2019-05-09 stsp *new_te = NULL;
5444 036813ee 2019-05-09 stsp
5445 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5446 036813ee 2019-05-09 stsp if (*new_te == NULL)
5447 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5448 036813ee 2019-05-09 stsp
5449 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5450 102b254e 2020-10-19 stsp if (err)
5451 036813ee 2019-05-09 stsp goto done;
5452 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5453 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5454 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5455 036813ee 2019-05-09 stsp goto done;
5456 036813ee 2019-05-09 stsp }
5457 036813ee 2019-05-09 stsp
5458 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5459 036813ee 2019-05-09 stsp
5460 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5461 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5462 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5463 5f8a88c6 2019-08-03 stsp else
5464 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5465 036813ee 2019-05-09 stsp done:
5466 102b254e 2020-10-19 stsp free(ct_name);
5467 036813ee 2019-05-09 stsp if (err && *new_te) {
5468 56e0773d 2019-11-28 stsp free(*new_te);
5469 036813ee 2019-05-09 stsp *new_te = NULL;
5470 036813ee 2019-05-09 stsp }
5471 036813ee 2019-05-09 stsp return err;
5472 036813ee 2019-05-09 stsp }
5473 036813ee 2019-05-09 stsp
5474 036813ee 2019-05-09 stsp static const struct got_error *
5475 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5476 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5477 036813ee 2019-05-09 stsp {
5478 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5479 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5480 036813ee 2019-05-09 stsp
5481 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5482 036813ee 2019-05-09 stsp if (err)
5483 036813ee 2019-05-09 stsp return err;
5484 036813ee 2019-05-09 stsp if (new_pe == NULL)
5485 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5486 036813ee 2019-05-09 stsp return NULL;
5487 afa376bf 2019-05-09 stsp }
5488 afa376bf 2019-05-09 stsp
5489 afa376bf 2019-05-09 stsp static const struct got_error *
5490 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5491 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5492 afa376bf 2019-05-09 stsp {
5493 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5494 5f8a88c6 2019-08-03 stsp unsigned char status;
5495 0ff8d236 2021-09-28 stsp
5496 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5497 0ff8d236 2021-09-28 stsp return NULL;
5498 5f8a88c6 2019-08-03 stsp
5499 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5500 afa376bf 2019-05-09 stsp ct_path++;
5501 5f8a88c6 2019-08-03 stsp
5502 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5503 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5504 5f8a88c6 2019-08-03 stsp else
5505 5f8a88c6 2019-08-03 stsp status = ct->status;
5506 5f8a88c6 2019-08-03 stsp
5507 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5508 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5509 036813ee 2019-05-09 stsp }
5510 036813ee 2019-05-09 stsp
5511 036813ee 2019-05-09 stsp static const struct got_error *
5512 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5513 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5514 44d03001 2019-05-09 stsp {
5515 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5516 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5517 44d03001 2019-05-09 stsp char *te_path;
5518 44d03001 2019-05-09 stsp
5519 44d03001 2019-05-09 stsp *modified = 0;
5520 44d03001 2019-05-09 stsp
5521 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5522 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5523 44d03001 2019-05-09 stsp te->name) == -1)
5524 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5525 44d03001 2019-05-09 stsp
5526 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5527 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5528 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5529 44d03001 2019-05-09 stsp strlen(te_path));
5530 62d463ca 2020-10-20 naddy if (*modified)
5531 44d03001 2019-05-09 stsp break;
5532 44d03001 2019-05-09 stsp }
5533 44d03001 2019-05-09 stsp
5534 44d03001 2019-05-09 stsp free(te_path);
5535 44d03001 2019-05-09 stsp return err;
5536 44d03001 2019-05-09 stsp }
5537 44d03001 2019-05-09 stsp
5538 44d03001 2019-05-09 stsp static const struct got_error *
5539 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5540 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5541 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5542 036813ee 2019-05-09 stsp {
5543 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5544 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5545 036813ee 2019-05-09 stsp
5546 036813ee 2019-05-09 stsp *ctp = NULL;
5547 036813ee 2019-05-09 stsp
5548 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5549 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5550 036813ee 2019-05-09 stsp char *ct_name = NULL;
5551 036813ee 2019-05-09 stsp int path_matches;
5552 036813ee 2019-05-09 stsp
5553 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5554 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5555 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5556 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5557 5f8a88c6 2019-08-03 stsp continue;
5558 5f8a88c6 2019-08-03 stsp } else {
5559 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5560 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5561 5f8a88c6 2019-08-03 stsp continue;
5562 5f8a88c6 2019-08-03 stsp }
5563 036813ee 2019-05-09 stsp
5564 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5565 036813ee 2019-05-09 stsp continue;
5566 036813ee 2019-05-09 stsp
5567 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5568 62d463ca 2020-10-20 naddy if (err)
5569 036813ee 2019-05-09 stsp return err;
5570 036813ee 2019-05-09 stsp if (!path_matches)
5571 036813ee 2019-05-09 stsp continue;
5572 036813ee 2019-05-09 stsp
5573 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5574 d34b633e 2020-10-19 stsp if (err)
5575 d34b633e 2020-10-19 stsp return err;
5576 d34b633e 2020-10-19 stsp
5577 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5578 d34b633e 2020-10-19 stsp free(ct_name);
5579 036813ee 2019-05-09 stsp continue;
5580 d34b633e 2020-10-19 stsp }
5581 d34b633e 2020-10-19 stsp free(ct_name);
5582 036813ee 2019-05-09 stsp
5583 036813ee 2019-05-09 stsp *ctp = ct;
5584 036813ee 2019-05-09 stsp break;
5585 036813ee 2019-05-09 stsp }
5586 2b496619 2019-07-10 stsp
5587 2b496619 2019-07-10 stsp return err;
5588 2b496619 2019-07-10 stsp }
5589 2b496619 2019-07-10 stsp
5590 2b496619 2019-07-10 stsp static const struct got_error *
5591 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5592 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5593 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5594 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5595 2b496619 2019-07-10 stsp struct got_repository *repo)
5596 2b496619 2019-07-10 stsp {
5597 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5598 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5599 2b496619 2019-07-10 stsp char *subtree_path;
5600 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5601 ba580f68 2020-03-22 stsp int nentries;
5602 2b496619 2019-07-10 stsp
5603 2b496619 2019-07-10 stsp *new_tep = NULL;
5604 2b496619 2019-07-10 stsp
5605 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5606 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5607 2b496619 2019-07-10 stsp child_path) == -1)
5608 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5609 036813ee 2019-05-09 stsp
5610 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5611 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5612 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5613 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5614 56e0773d 2019-11-28 stsp
5615 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5616 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5617 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5618 2b496619 2019-07-10 stsp goto done;
5619 2b496619 2019-07-10 stsp }
5620 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5621 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5622 2b496619 2019-07-10 stsp if (err) {
5623 56e0773d 2019-11-28 stsp free(new_te);
5624 2b496619 2019-07-10 stsp goto done;
5625 2b496619 2019-07-10 stsp }
5626 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5627 2b496619 2019-07-10 stsp done:
5628 56e0773d 2019-11-28 stsp free(id);
5629 2b496619 2019-07-10 stsp free(subtree_path);
5630 2b496619 2019-07-10 stsp if (err == NULL)
5631 2b496619 2019-07-10 stsp *new_tep = new_te;
5632 036813ee 2019-05-09 stsp return err;
5633 036813ee 2019-05-09 stsp }
5634 036813ee 2019-05-09 stsp
5635 036813ee 2019-05-09 stsp static const struct got_error *
5636 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5637 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5638 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5639 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5640 036813ee 2019-05-09 stsp struct got_repository *repo)
5641 036813ee 2019-05-09 stsp {
5642 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5643 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5644 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5645 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5646 036813ee 2019-05-09 stsp
5647 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5648 ba580f68 2020-03-22 stsp *nentries = 0;
5649 036813ee 2019-05-09 stsp
5650 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5651 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5652 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5653 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5654 036813ee 2019-05-09 stsp
5655 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5656 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5657 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5658 036813ee 2019-05-09 stsp continue;
5659 036813ee 2019-05-09 stsp
5660 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5661 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5662 036813ee 2019-05-09 stsp continue;
5663 036813ee 2019-05-09 stsp
5664 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5665 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5666 036813ee 2019-05-09 stsp if (err)
5667 036813ee 2019-05-09 stsp goto done;
5668 036813ee 2019-05-09 stsp
5669 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5670 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5671 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5672 036813ee 2019-05-09 stsp if (err)
5673 036813ee 2019-05-09 stsp goto done;
5674 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5675 afa376bf 2019-05-09 stsp if (err)
5676 afa376bf 2019-05-09 stsp goto done;
5677 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5678 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5679 2b496619 2019-07-10 stsp if (err)
5680 2f51b5b3 2019-05-09 stsp goto done;
5681 ba580f68 2020-03-22 stsp (*nentries)++;
5682 2b496619 2019-07-10 stsp } else {
5683 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5684 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5685 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5686 2b496619 2019-07-10 stsp == NULL) {
5687 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5688 2b496619 2019-07-10 stsp child_path, path_base_tree,
5689 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5690 2b496619 2019-07-10 stsp repo);
5691 2b496619 2019-07-10 stsp if (err)
5692 2b496619 2019-07-10 stsp goto done;
5693 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5694 2b496619 2019-07-10 stsp if (err)
5695 2b496619 2019-07-10 stsp goto done;
5696 ba580f68 2020-03-22 stsp (*nentries)++;
5697 9ba0479c 2019-05-10 stsp }
5698 ed175427 2019-05-09 stsp }
5699 2f51b5b3 2019-05-09 stsp }
5700 2f51b5b3 2019-05-09 stsp
5701 2f51b5b3 2019-05-09 stsp if (base_tree) {
5702 56e0773d 2019-11-28 stsp int i, nbase_entries;
5703 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5704 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5705 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5706 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5707 63c5ca5d 2019-08-24 stsp
5708 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5709 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5710 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5711 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5712 63c5ca5d 2019-08-24 stsp if (err)
5713 63c5ca5d 2019-08-24 stsp goto done;
5714 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5715 63c5ca5d 2019-08-24 stsp if (err)
5716 63c5ca5d 2019-08-24 stsp goto done;
5717 ba580f68 2020-03-22 stsp (*nentries)++;
5718 63c5ca5d 2019-08-24 stsp continue;
5719 63c5ca5d 2019-08-24 stsp }
5720 2f51b5b3 2019-05-09 stsp
5721 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5722 44d03001 2019-05-09 stsp int modified;
5723 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5724 036813ee 2019-05-09 stsp if (err)
5725 036813ee 2019-05-09 stsp goto done;
5726 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5727 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5728 2f51b5b3 2019-05-09 stsp if (err)
5729 2f51b5b3 2019-05-09 stsp goto done;
5730 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5731 44d03001 2019-05-09 stsp if (modified) {
5732 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5733 ba580f68 2020-03-22 stsp int nsubentries;
5734 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5735 ba580f68 2020-03-22 stsp &nsubentries, te,
5736 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5737 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5738 44d03001 2019-05-09 stsp if (err)
5739 44d03001 2019-05-09 stsp goto done;
5740 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5741 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5742 ba580f68 2020-03-22 stsp free(new_id);
5743 ba580f68 2020-03-22 stsp continue;
5744 ba580f68 2020-03-22 stsp }
5745 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5746 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5747 56e0773d 2019-11-28 stsp free(new_id);
5748 44d03001 2019-05-09 stsp }
5749 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5750 036813ee 2019-05-09 stsp if (err)
5751 036813ee 2019-05-09 stsp goto done;
5752 ba580f68 2020-03-22 stsp (*nentries)++;
5753 2f51b5b3 2019-05-09 stsp continue;
5754 036813ee 2019-05-09 stsp }
5755 2f51b5b3 2019-05-09 stsp
5756 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5757 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5758 f66c734c 2019-09-22 stsp if (err)
5759 f66c734c 2019-09-22 stsp goto done;
5760 2f51b5b3 2019-05-09 stsp if (ct) {
5761 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5762 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5763 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5764 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5765 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5766 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5767 2f51b5b3 2019-05-09 stsp if (err)
5768 2f51b5b3 2019-05-09 stsp goto done;
5769 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5770 2f51b5b3 2019-05-09 stsp if (err)
5771 2f51b5b3 2019-05-09 stsp goto done;
5772 ba580f68 2020-03-22 stsp (*nentries)++;
5773 2f51b5b3 2019-05-09 stsp }
5774 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5775 b416585c 2019-05-13 stsp status_arg);
5776 afa376bf 2019-05-09 stsp if (err)
5777 afa376bf 2019-05-09 stsp goto done;
5778 2f51b5b3 2019-05-09 stsp } else {
5779 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5780 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5781 2f51b5b3 2019-05-09 stsp if (err)
5782 2f51b5b3 2019-05-09 stsp goto done;
5783 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5784 2f51b5b3 2019-05-09 stsp if (err)
5785 2f51b5b3 2019-05-09 stsp goto done;
5786 ba580f68 2020-03-22 stsp (*nentries)++;
5787 2f51b5b3 2019-05-09 stsp }
5788 ca2503ea 2019-05-09 stsp }
5789 ed175427 2019-05-09 stsp }
5790 0b5cc0d6 2019-05-09 stsp
5791 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5792 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5793 ed175427 2019-05-09 stsp done:
5794 d8bacb93 2023-01-10 mark got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5795 2e1fa222 2020-07-23 stsp return err;
5796 2e1fa222 2020-07-23 stsp }
5797 2e1fa222 2020-07-23 stsp
5798 2e1fa222 2020-07-23 stsp static const struct got_error *
5799 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5800 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5801 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5802 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5803 ebf99748 2019-05-09 stsp {
5804 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5805 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5806 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5807 ebf99748 2019-05-09 stsp
5808 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5809 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5810 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5811 ebf99748 2019-05-09 stsp
5812 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5813 437adc9d 2020-12-10 yzhong
5814 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5815 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5816 437adc9d 2020-12-10 yzhong if (err)
5817 437adc9d 2020-12-10 yzhong goto done;
5818 437adc9d 2020-12-10 yzhong
5819 ebf99748 2019-05-09 stsp if (ie) {
5820 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5821 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5822 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5823 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5824 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5825 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5826 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5827 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5828 437adc9d 2020-12-10 yzhong
5829 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5830 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5831 3093e2d7 2023-02-04 op ct->staged_blob_id->hash,
5832 3093e2d7 2023-02-04 op new_base_commit_id->hash,
5833 72fd46fa 2019-09-06 stsp !have_staged_files);
5834 ebf99748 2019-05-09 stsp } else
5835 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5836 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5837 3093e2d7 2023-02-04 op ct->blob_id->hash,
5838 3093e2d7 2023-02-04 op new_base_commit_id->hash,
5839 72fd46fa 2019-09-06 stsp !have_staged_files);
5840 ebf99748 2019-05-09 stsp } else {
5841 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5842 ebf99748 2019-05-09 stsp if (err)
5843 437adc9d 2020-12-10 yzhong goto done;
5844 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5845 3093e2d7 2023-02-04 op worktree->root_fd, relpath, ct->blob_id->hash,
5846 3093e2d7 2023-02-04 op new_base_commit_id->hash, 1);
5847 3969253a 2020-03-07 stsp if (err) {
5848 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5849 437adc9d 2020-12-10 yzhong goto done;
5850 3969253a 2020-03-07 stsp }
5851 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5852 3969253a 2020-03-07 stsp if (err) {
5853 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5854 437adc9d 2020-12-10 yzhong goto done;
5855 3969253a 2020-03-07 stsp }
5856 ebf99748 2019-05-09 stsp }
5857 437adc9d 2020-12-10 yzhong free(relpath);
5858 437adc9d 2020-12-10 yzhong relpath = NULL;
5859 ebf99748 2019-05-09 stsp }
5860 437adc9d 2020-12-10 yzhong done:
5861 437adc9d 2020-12-10 yzhong free(relpath);
5862 d56d26ce 2019-05-10 stsp return err;
5863 d56d26ce 2019-05-10 stsp }
5864 735ef5ac 2019-08-03 stsp
5865 d56d26ce 2019-05-10 stsp
5866 d56d26ce 2019-05-10 stsp static const struct got_error *
5867 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5868 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5869 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5870 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5871 735ef5ac 2019-08-03 stsp int ood_errcode)
5872 d56d26ce 2019-05-10 stsp {
5873 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5874 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5875 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5876 1a36436d 2019-06-10 stsp
5877 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5878 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5879 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5880 1a36436d 2019-06-10 stsp return NULL;
5881 9bead371 2019-07-28 stsp /*
5882 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5883 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5884 9bead371 2019-07-28 stsp */
5885 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5886 a44927cc 2022-04-07 stsp if (err)
5887 a44927cc 2022-04-07 stsp goto done;
5888 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5889 9bead371 2019-07-28 stsp if (err) {
5890 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5891 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5892 1a36436d 2019-06-10 stsp goto done;
5893 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5894 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5895 1a36436d 2019-06-10 stsp } else {
5896 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5897 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, head_commit_id);
5898 a44927cc 2022-04-07 stsp if (err)
5899 a44927cc 2022-04-07 stsp goto done;
5900 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5901 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5902 1a36436d 2019-06-10 stsp goto done;
5903 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5904 1a36436d 2019-06-10 stsp }
5905 1a36436d 2019-06-10 stsp done:
5906 1a36436d 2019-06-10 stsp free(id);
5907 a44927cc 2022-04-07 stsp if (commit)
5908 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5909 1a36436d 2019-06-10 stsp return err;
5910 ebf99748 2019-05-09 stsp }
5911 ebf99748 2019-05-09 stsp
5912 336075a4 2022-06-25 op static const struct got_error *
5913 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5914 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5915 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5916 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5917 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5918 2a47b1e5 2022-11-01 stsp const char *author, const char *committer, char *diff_path,
5919 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5920 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5921 afa376bf 2019-05-09 stsp struct got_repository *repo)
5922 c4296144 2019-05-09 stsp {
5923 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5924 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5925 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5926 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5927 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5928 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5929 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5930 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5931 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5932 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5933 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5934 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5935 f8399b8f 2022-07-22 stsp time_t timestamp;
5936 c4296144 2019-05-09 stsp
5937 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5938 c4296144 2019-05-09 stsp
5939 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5940 675c7539 2019-05-09 stsp
5941 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5942 588edf97 2019-05-10 stsp if (err)
5943 588edf97 2019-05-10 stsp goto done;
5944 de18fc63 2019-05-09 stsp
5945 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5946 588edf97 2019-05-10 stsp if (err)
5947 588edf97 2019-05-10 stsp goto done;
5948 588edf97 2019-05-10 stsp
5949 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5950 2a47b1e5 2022-11-01 stsp err = commit_msg_cb(commitable_paths, diff_path,
5951 2a47b1e5 2022-11-01 stsp &logmsg, commit_arg);
5952 33ad4cbe 2019-05-12 jcs if (err)
5953 33ad4cbe 2019-05-12 jcs goto done;
5954 33ad4cbe 2019-05-12 jcs }
5955 c4296144 2019-05-09 stsp
5956 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5957 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5958 33ad4cbe 2019-05-12 jcs goto done;
5959 33ad4cbe 2019-05-12 jcs }
5960 33ad4cbe 2019-05-12 jcs
5961 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5962 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5963 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5964 cf066bf8 2019-05-09 stsp char *ondisk_path;
5965 cf066bf8 2019-05-09 stsp
5966 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5967 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5968 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5969 5f8a88c6 2019-08-03 stsp continue;
5970 5f8a88c6 2019-08-03 stsp
5971 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5972 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5973 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5974 cf066bf8 2019-05-09 stsp continue;
5975 cf066bf8 2019-05-09 stsp
5976 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5977 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5978 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5979 cf066bf8 2019-05-09 stsp goto done;
5980 cf066bf8 2019-05-09 stsp }
5981 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5982 2e1fa222 2020-07-23 stsp free(ondisk_path);
5983 2e1fa222 2020-07-23 stsp if (err)
5984 cf066bf8 2019-05-09 stsp goto done;
5985 cf066bf8 2019-05-09 stsp }
5986 036813ee 2019-05-09 stsp
5987 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5988 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5989 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5990 036813ee 2019-05-09 stsp if (err)
5991 036813ee 2019-05-09 stsp goto done;
5992 036813ee 2019-05-09 stsp
5993 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5994 de18fc63 2019-05-09 stsp if (err)
5995 de18fc63 2019-05-09 stsp goto done;
5996 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5997 f259c4c1 2021-09-24 stsp nparents++;
5998 f259c4c1 2021-09-24 stsp if (parent_id2) {
5999 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
6000 f259c4c1 2021-09-24 stsp if (err)
6001 f259c4c1 2021-09-24 stsp goto done;
6002 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6003 f259c4c1 2021-09-24 stsp nparents++;
6004 f259c4c1 2021-09-24 stsp }
6005 f8399b8f 2022-07-22 stsp timestamp = time(NULL);
6006 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6007 f8399b8f 2022-07-22 stsp nparents, author, timestamp, committer, timestamp, logmsg, repo);
6008 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
6009 33ad4cbe 2019-05-12 jcs free(logmsg);
6010 9d40349a 2019-05-09 stsp if (err)
6011 9d40349a 2019-05-09 stsp goto done;
6012 9d40349a 2019-05-09 stsp
6013 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
6014 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
6015 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
6016 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
6017 09f5bd90 2019-05-09 stsp goto done;
6018 09f5bd90 2019-05-09 stsp }
6019 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
6020 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6021 09f5bd90 2019-05-09 stsp if (err)
6022 09f5bd90 2019-05-09 stsp goto done;
6023 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6024 ebf99748 2019-05-09 stsp if (err)
6025 ebf99748 2019-05-09 stsp goto done;
6026 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6027 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6028 09f5bd90 2019-05-09 stsp goto done;
6029 09f5bd90 2019-05-09 stsp }
6030 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
6031 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
6032 f2c16586 2019-05-09 stsp if (err)
6033 f2c16586 2019-05-09 stsp goto done;
6034 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
6035 f2c16586 2019-05-09 stsp if (err)
6036 f2c16586 2019-05-09 stsp goto done;
6037 f2c16586 2019-05-09 stsp
6038 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6039 09f5bd90 2019-05-09 stsp if (err)
6040 09f5bd90 2019-05-09 stsp goto done;
6041 09f5bd90 2019-05-09 stsp
6042 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
6043 ebf99748 2019-05-09 stsp if (err)
6044 ebf99748 2019-05-09 stsp goto done;
6045 c4296144 2019-05-09 stsp done:
6046 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
6047 588edf97 2019-05-10 stsp if (head_tree)
6048 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
6049 588edf97 2019-05-10 stsp if (head_commit)
6050 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
6051 09f5bd90 2019-05-09 stsp free(head_commit_id2);
6052 2f17228e 2019-05-12 stsp if (head_ref2) {
6053 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
6054 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
6055 2f17228e 2019-05-12 stsp err = unlockerr;
6056 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
6057 39cd0ff6 2019-07-12 stsp }
6058 39cd0ff6 2019-07-12 stsp return err;
6059 39cd0ff6 2019-07-12 stsp }
6060 5c1e53bc 2019-07-28 stsp
6061 5c1e53bc 2019-07-28 stsp static const struct got_error *
6062 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
6063 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
6064 5c1e53bc 2019-07-28 stsp {
6065 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
6066 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
6067 39cd0ff6 2019-07-12 stsp
6068 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
6069 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
6070 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
6071 5c1e53bc 2019-07-28 stsp
6072 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
6073 5c1e53bc 2019-07-28 stsp ct_path++;
6074 5c1e53bc 2019-07-28 stsp
6075 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
6076 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
6077 5c1e53bc 2019-07-28 stsp break;
6078 5c1e53bc 2019-07-28 stsp }
6079 5c1e53bc 2019-07-28 stsp
6080 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
6081 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
6082 5c1e53bc 2019-07-28 stsp
6083 5c1e53bc 2019-07-28 stsp return NULL;
6084 5c1e53bc 2019-07-28 stsp }
6085 5c1e53bc 2019-07-28 stsp
6086 f0b75401 2019-08-03 stsp static const struct got_error *
6087 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
6088 f0b75401 2019-08-03 stsp {
6089 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
6090 f0b75401 2019-08-03 stsp
6091 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6092 f0b75401 2019-08-03 stsp *have_staged_files = 1;
6093 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
6094 f0b75401 2019-08-03 stsp }
6095 f0b75401 2019-08-03 stsp
6096 f0b75401 2019-08-03 stsp return NULL;
6097 f0b75401 2019-08-03 stsp }
6098 f0b75401 2019-08-03 stsp
6099 5f8a88c6 2019-08-03 stsp static const struct got_error *
6100 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
6101 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
6102 5f8a88c6 2019-08-03 stsp {
6103 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
6104 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
6105 5f8a88c6 2019-08-03 stsp
6106 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6107 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
6108 5f8a88c6 2019-08-03 stsp continue;
6109 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6110 5f8a88c6 2019-08-03 stsp if (ie == NULL)
6111 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6112 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6113 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
6114 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
6115 5f8a88c6 2019-08-03 stsp }
6116 5f8a88c6 2019-08-03 stsp
6117 5f8a88c6 2019-08-03 stsp return NULL;
6118 5f8a88c6 2019-08-03 stsp }
6119 5f8a88c6 2019-08-03 stsp
6120 39cd0ff6 2019-07-12 stsp const struct got_error *
6121 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
6122 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
6123 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
6124 2a47b1e5 2022-11-01 stsp int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6125 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
6126 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
6127 39cd0ff6 2019-07-12 stsp {
6128 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6129 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
6130 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
6131 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6132 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6133 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
6134 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
6135 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6136 2a47b1e5 2022-11-01 stsp char *diff_path = NULL;
6137 f0b75401 2019-08-03 stsp int have_staged_files = 0;
6138 39cd0ff6 2019-07-12 stsp
6139 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
6140 39cd0ff6 2019-07-12 stsp
6141 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6142 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6143 39cd0ff6 2019-07-12 stsp
6144 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
6145 39cd0ff6 2019-07-12 stsp if (err)
6146 39cd0ff6 2019-07-12 stsp goto done;
6147 39cd0ff6 2019-07-12 stsp
6148 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6149 39cd0ff6 2019-07-12 stsp if (err)
6150 39cd0ff6 2019-07-12 stsp goto done;
6151 39cd0ff6 2019-07-12 stsp
6152 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6153 39cd0ff6 2019-07-12 stsp if (err)
6154 39cd0ff6 2019-07-12 stsp goto done;
6155 39cd0ff6 2019-07-12 stsp
6156 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6157 39cd0ff6 2019-07-12 stsp if (err)
6158 39cd0ff6 2019-07-12 stsp goto done;
6159 39cd0ff6 2019-07-12 stsp
6160 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6161 5f8a88c6 2019-08-03 stsp &have_staged_files);
6162 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
6163 5f8a88c6 2019-08-03 stsp goto done;
6164 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
6165 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
6166 5f8a88c6 2019-08-03 stsp if (err)
6167 5f8a88c6 2019-08-03 stsp goto done;
6168 5f8a88c6 2019-08-03 stsp }
6169 5f8a88c6 2019-08-03 stsp
6170 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6171 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
6172 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
6173 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
6174 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
6175 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6176 2a47b1e5 2022-11-01 stsp cc_arg.diff_header_shown = 0;
6177 2a47b1e5 2022-11-01 stsp if (show_diff) {
6178 2a47b1e5 2022-11-01 stsp err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6179 b90054ed 2022-11-01 stsp GOT_TMPDIR_STR "/got", ".diff");
6180 2a47b1e5 2022-11-01 stsp if (err)
6181 2a47b1e5 2022-11-01 stsp goto done;
6182 2a47b1e5 2022-11-01 stsp cc_arg.f1 = got_opentemp();
6183 2a47b1e5 2022-11-01 stsp if (cc_arg.f1 == NULL) {
6184 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6185 2a47b1e5 2022-11-01 stsp goto done;
6186 2a47b1e5 2022-11-01 stsp }
6187 2a47b1e5 2022-11-01 stsp cc_arg.f2 = got_opentemp();
6188 2a47b1e5 2022-11-01 stsp if (cc_arg.f2 == NULL) {
6189 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("got_opentemp");
6190 2a47b1e5 2022-11-01 stsp goto done;
6191 2a47b1e5 2022-11-01 stsp }
6192 2a47b1e5 2022-11-01 stsp }
6193 2a47b1e5 2022-11-01 stsp
6194 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6195 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6196 4ba2e955 2022-09-02 sh+got collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6197 5c1e53bc 2019-07-28 stsp if (err)
6198 5c1e53bc 2019-07-28 stsp goto done;
6199 5c1e53bc 2019-07-28 stsp }
6200 39cd0ff6 2019-07-12 stsp
6201 2a47b1e5 2022-11-01 stsp if (show_diff) {
6202 2a47b1e5 2022-11-01 stsp if (fflush(cc_arg.diff_outfile) == EOF) {
6203 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fflush");
6204 2a47b1e5 2022-11-01 stsp goto done;
6205 2a47b1e5 2022-11-01 stsp }
6206 2a47b1e5 2022-11-01 stsp }
6207 2a47b1e5 2022-11-01 stsp
6208 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6209 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6210 39cd0ff6 2019-07-12 stsp goto done;
6211 5c1e53bc 2019-07-28 stsp }
6212 5c1e53bc 2019-07-28 stsp
6213 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
6214 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
6215 5c1e53bc 2019-07-28 stsp if (err)
6216 5c1e53bc 2019-07-28 stsp goto done;
6217 39cd0ff6 2019-07-12 stsp }
6218 f0b75401 2019-08-03 stsp
6219 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6220 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
6221 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
6222 f0b75401 2019-08-03 stsp
6223 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
6224 f0b75401 2019-08-03 stsp ct_path++;
6225 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
6226 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6227 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6228 b50cabdf 2019-07-12 stsp if (err)
6229 b50cabdf 2019-07-12 stsp goto done;
6230 f0b75401 2019-08-03 stsp
6231 b50cabdf 2019-07-12 stsp }
6232 b50cabdf 2019-07-12 stsp
6233 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
6234 210c2321 2022-11-01 stsp head_commit_id, NULL, worktree, author, committer,
6235 210c2321 2022-11-01 stsp (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6236 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6237 39cd0ff6 2019-07-12 stsp if (err)
6238 39cd0ff6 2019-07-12 stsp goto done;
6239 39cd0ff6 2019-07-12 stsp
6240 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6241 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
6242 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6243 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
6244 39cd0ff6 2019-07-12 stsp err = sync_err;
6245 39cd0ff6 2019-07-12 stsp done:
6246 39cd0ff6 2019-07-12 stsp if (fileindex)
6247 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
6248 39cd0ff6 2019-07-12 stsp free(fileindex_path);
6249 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6250 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
6251 39cd0ff6 2019-07-12 stsp err = unlockerr;
6252 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
6253 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
6254 d8bacb93 2023-01-10 mark
6255 39cd0ff6 2019-07-12 stsp free_commitable(ct);
6256 39cd0ff6 2019-07-12 stsp }
6257 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6258 2a47b1e5 2022-11-01 stsp if (diff_path && unlink(diff_path) == -1 && err == NULL)
6259 2a47b1e5 2022-11-01 stsp err = got_error_from_errno2("unlink", diff_path);
6260 2a47b1e5 2022-11-01 stsp free(diff_path);
6261 2a47b1e5 2022-11-01 stsp if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6262 2a47b1e5 2022-11-01 stsp err == NULL)
6263 2a47b1e5 2022-11-01 stsp err = got_error_from_errno("fclose");
6264 c4296144 2019-05-09 stsp return err;
6265 8656d6c4 2019-05-20 stsp }
6266 8656d6c4 2019-05-20 stsp
6267 8656d6c4 2019-05-20 stsp const char *
6268 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
6269 8656d6c4 2019-05-20 stsp {
6270 8656d6c4 2019-05-20 stsp return ct->path;
6271 8656d6c4 2019-05-20 stsp }
6272 8656d6c4 2019-05-20 stsp
6273 8656d6c4 2019-05-20 stsp unsigned int
6274 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
6275 8656d6c4 2019-05-20 stsp {
6276 8656d6c4 2019-05-20 stsp return ct->status;
6277 818c7501 2019-07-11 stsp }
6278 818c7501 2019-07-11 stsp
6279 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
6280 818c7501 2019-07-11 stsp struct got_worktree *worktree;
6281 818c7501 2019-07-11 stsp struct got_repository *repo;
6282 818c7501 2019-07-11 stsp };
6283 818c7501 2019-07-11 stsp
6284 818c7501 2019-07-11 stsp static const struct got_error *
6285 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6286 818c7501 2019-07-11 stsp {
6287 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6288 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
6289 818c7501 2019-07-11 stsp unsigned char status;
6290 818c7501 2019-07-11 stsp struct stat sb;
6291 818c7501 2019-07-11 stsp char *ondisk_path;
6292 818c7501 2019-07-11 stsp
6293 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
6294 3093e2d7 2023-02-04 op if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6295 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
6296 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
6297 818c7501 2019-07-11 stsp
6298 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6299 818c7501 2019-07-11 stsp == -1)
6300 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
6301 818c7501 2019-07-11 stsp
6302 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
6303 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6304 818c7501 2019-07-11 stsp free(ondisk_path);
6305 818c7501 2019-07-11 stsp if (err)
6306 818c7501 2019-07-11 stsp return err;
6307 818c7501 2019-07-11 stsp
6308 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
6309 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6310 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6311 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6312 818c7501 2019-07-11 stsp
6313 818c7501 2019-07-11 stsp return NULL;
6314 818c7501 2019-07-11 stsp }
6315 818c7501 2019-07-11 stsp
6316 818c7501 2019-07-11 stsp const struct got_error *
6317 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6318 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6319 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6320 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6321 818c7501 2019-07-11 stsp {
6322 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6323 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6324 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6325 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6326 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6327 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6328 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6329 818c7501 2019-07-11 stsp
6330 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6331 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6332 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6333 818c7501 2019-07-11 stsp
6334 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6335 818c7501 2019-07-11 stsp if (err)
6336 818c7501 2019-07-11 stsp return err;
6337 818c7501 2019-07-11 stsp
6338 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6339 818c7501 2019-07-11 stsp if (err)
6340 818c7501 2019-07-11 stsp goto done;
6341 818c7501 2019-07-11 stsp
6342 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6343 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6344 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6345 818c7501 2019-07-11 stsp &ok_arg);
6346 818c7501 2019-07-11 stsp if (err)
6347 818c7501 2019-07-11 stsp goto done;
6348 818c7501 2019-07-11 stsp
6349 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6350 818c7501 2019-07-11 stsp if (err)
6351 818c7501 2019-07-11 stsp goto done;
6352 818c7501 2019-07-11 stsp
6353 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6354 818c7501 2019-07-11 stsp if (err)
6355 818c7501 2019-07-11 stsp goto done;
6356 818c7501 2019-07-11 stsp
6357 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6358 818c7501 2019-07-11 stsp if (err)
6359 818c7501 2019-07-11 stsp goto done;
6360 818c7501 2019-07-11 stsp
6361 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6362 818c7501 2019-07-11 stsp 0);
6363 e51d7b55 2020-01-04 stsp if (err)
6364 e51d7b55 2020-01-04 stsp goto done;
6365 e51d7b55 2020-01-04 stsp
6366 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6367 818c7501 2019-07-11 stsp if (err)
6368 818c7501 2019-07-11 stsp goto done;
6369 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6370 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6371 e51d7b55 2020-01-04 stsp goto done;
6372 e51d7b55 2020-01-04 stsp }
6373 818c7501 2019-07-11 stsp
6374 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6375 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6376 818c7501 2019-07-11 stsp if (err)
6377 818c7501 2019-07-11 stsp goto done;
6378 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_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 /* TODO Lock original branch's ref while rebasing? */
6383 818c7501 2019-07-11 stsp
6384 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6385 818c7501 2019-07-11 stsp if (err)
6386 818c7501 2019-07-11 stsp goto done;
6387 818c7501 2019-07-11 stsp
6388 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6389 818c7501 2019-07-11 stsp if (err)
6390 818c7501 2019-07-11 stsp goto done;
6391 818c7501 2019-07-11 stsp
6392 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6393 818c7501 2019-07-11 stsp worktree->base_commit_id);
6394 818c7501 2019-07-11 stsp if (err)
6395 818c7501 2019-07-11 stsp goto done;
6396 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6397 818c7501 2019-07-11 stsp if (err)
6398 818c7501 2019-07-11 stsp goto done;
6399 818c7501 2019-07-11 stsp
6400 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6401 818c7501 2019-07-11 stsp if (err)
6402 818c7501 2019-07-11 stsp goto done;
6403 818c7501 2019-07-11 stsp done:
6404 818c7501 2019-07-11 stsp free(fileindex_path);
6405 818c7501 2019-07-11 stsp free(tmp_branch_name);
6406 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6407 818c7501 2019-07-11 stsp free(branch_ref_name);
6408 818c7501 2019-07-11 stsp if (branch_ref)
6409 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6410 818c7501 2019-07-11 stsp if (wt_branch)
6411 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6412 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6413 818c7501 2019-07-11 stsp if (err) {
6414 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6415 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6416 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6417 818c7501 2019-07-11 stsp }
6418 818c7501 2019-07-11 stsp if (*tmp_branch) {
6419 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6420 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6421 818c7501 2019-07-11 stsp }
6422 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6423 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6424 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6425 3e3a69f1 2019-07-25 stsp }
6426 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6427 818c7501 2019-07-11 stsp }
6428 818c7501 2019-07-11 stsp return err;
6429 818c7501 2019-07-11 stsp }
6430 818c7501 2019-07-11 stsp
6431 818c7501 2019-07-11 stsp const struct got_error *
6432 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6433 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6434 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6435 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6436 818c7501 2019-07-11 stsp {
6437 818c7501 2019-07-11 stsp const struct got_error *err;
6438 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6439 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6440 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6441 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6442 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6443 818c7501 2019-07-11 stsp
6444 818c7501 2019-07-11 stsp *commit_id = NULL;
6445 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6446 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6447 3e3a69f1 2019-07-25 stsp *branch = NULL;
6448 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6449 3e3a69f1 2019-07-25 stsp
6450 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6451 3e3a69f1 2019-07-25 stsp if (err)
6452 3e3a69f1 2019-07-25 stsp return err;
6453 818c7501 2019-07-11 stsp
6454 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6455 3e3a69f1 2019-07-25 stsp if (err)
6456 f032f1f7 2019-08-04 stsp goto done;
6457 f032f1f7 2019-08-04 stsp
6458 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6459 f032f1f7 2019-08-04 stsp &have_staged_files);
6460 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6461 f032f1f7 2019-08-04 stsp goto done;
6462 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6463 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6464 3e3a69f1 2019-07-25 stsp goto done;
6465 f032f1f7 2019-08-04 stsp }
6466 3e3a69f1 2019-07-25 stsp
6467 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6468 818c7501 2019-07-11 stsp if (err)
6469 f032f1f7 2019-08-04 stsp goto done;
6470 818c7501 2019-07-11 stsp
6471 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6472 818c7501 2019-07-11 stsp if (err)
6473 818c7501 2019-07-11 stsp goto done;
6474 818c7501 2019-07-11 stsp
6475 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6476 818c7501 2019-07-11 stsp if (err)
6477 818c7501 2019-07-11 stsp goto done;
6478 818c7501 2019-07-11 stsp
6479 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6480 818c7501 2019-07-11 stsp if (err)
6481 818c7501 2019-07-11 stsp goto done;
6482 818c7501 2019-07-11 stsp
6483 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6484 818c7501 2019-07-11 stsp if (err)
6485 818c7501 2019-07-11 stsp goto done;
6486 818c7501 2019-07-11 stsp
6487 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6488 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6489 818c7501 2019-07-11 stsp if (err)
6490 818c7501 2019-07-11 stsp goto done;
6491 818c7501 2019-07-11 stsp
6492 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6493 818c7501 2019-07-11 stsp if (err)
6494 818c7501 2019-07-11 stsp goto done;
6495 818c7501 2019-07-11 stsp
6496 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6497 818c7501 2019-07-11 stsp if (err)
6498 818c7501 2019-07-11 stsp goto done;
6499 818c7501 2019-07-11 stsp
6500 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6501 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6502 818c7501 2019-07-11 stsp if (err)
6503 818c7501 2019-07-11 stsp goto done;
6504 818c7501 2019-07-11 stsp
6505 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6506 818c7501 2019-07-11 stsp if (err)
6507 818c7501 2019-07-11 stsp goto done;
6508 818c7501 2019-07-11 stsp done:
6509 818c7501 2019-07-11 stsp free(commit_ref_name);
6510 818c7501 2019-07-11 stsp free(branch_ref_name);
6511 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6512 818c7501 2019-07-11 stsp if (commit_ref)
6513 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6514 818c7501 2019-07-11 stsp if (branch_ref)
6515 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6516 818c7501 2019-07-11 stsp if (err) {
6517 818c7501 2019-07-11 stsp free(*commit_id);
6518 818c7501 2019-07-11 stsp *commit_id = NULL;
6519 818c7501 2019-07-11 stsp if (*tmp_branch) {
6520 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6521 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6522 818c7501 2019-07-11 stsp }
6523 818c7501 2019-07-11 stsp if (*new_base_branch) {
6524 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6525 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6526 818c7501 2019-07-11 stsp }
6527 818c7501 2019-07-11 stsp if (*branch) {
6528 818c7501 2019-07-11 stsp got_ref_close(*branch);
6529 818c7501 2019-07-11 stsp *branch = NULL;
6530 818c7501 2019-07-11 stsp }
6531 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6532 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6533 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6534 3e3a69f1 2019-07-25 stsp }
6535 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6536 818c7501 2019-07-11 stsp }
6537 818c7501 2019-07-11 stsp return err;
6538 c4296144 2019-05-09 stsp }
6539 818c7501 2019-07-11 stsp
6540 818c7501 2019-07-11 stsp const struct got_error *
6541 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6542 818c7501 2019-07-11 stsp {
6543 818c7501 2019-07-11 stsp const struct got_error *err;
6544 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6545 818c7501 2019-07-11 stsp
6546 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6547 818c7501 2019-07-11 stsp if (err)
6548 818c7501 2019-07-11 stsp return err;
6549 818c7501 2019-07-11 stsp
6550 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6551 818c7501 2019-07-11 stsp free(tmp_branch_name);
6552 818c7501 2019-07-11 stsp return NULL;
6553 818c7501 2019-07-11 stsp }
6554 818c7501 2019-07-11 stsp
6555 818c7501 2019-07-11 stsp static const struct got_error *
6556 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6557 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
6558 818c7501 2019-07-11 stsp {
6559 0ebf8283 2019-07-24 stsp *logmsg = arg;
6560 818c7501 2019-07-11 stsp return NULL;
6561 818c7501 2019-07-11 stsp }
6562 818c7501 2019-07-11 stsp
6563 818c7501 2019-07-11 stsp static const struct got_error *
6564 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6565 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6566 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6567 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6568 818c7501 2019-07-11 stsp {
6569 818c7501 2019-07-11 stsp return NULL;
6570 01757395 2019-07-12 stsp }
6571 01757395 2019-07-12 stsp
6572 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6573 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6574 01757395 2019-07-12 stsp void *progress_arg;
6575 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6576 01757395 2019-07-12 stsp };
6577 01757395 2019-07-12 stsp
6578 01757395 2019-07-12 stsp static const struct got_error *
6579 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6580 01757395 2019-07-12 stsp {
6581 01757395 2019-07-12 stsp const struct got_error *err;
6582 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6583 01757395 2019-07-12 stsp char *p;
6584 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6585 01757395 2019-07-12 stsp
6586 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6587 01757395 2019-07-12 stsp if (err)
6588 01757395 2019-07-12 stsp return err;
6589 01757395 2019-07-12 stsp
6590 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6591 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6592 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6593 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6594 01757395 2019-07-12 stsp return NULL;
6595 01757395 2019-07-12 stsp
6596 01757395 2019-07-12 stsp p = strdup(path);
6597 01757395 2019-07-12 stsp if (p == NULL)
6598 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6599 01757395 2019-07-12 stsp
6600 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6601 01757395 2019-07-12 stsp if (err || new == NULL)
6602 01757395 2019-07-12 stsp free(p);
6603 01757395 2019-07-12 stsp return err;
6604 818c7501 2019-07-11 stsp }
6605 818c7501 2019-07-11 stsp
6606 0ebf8283 2019-07-24 stsp static const struct got_error *
6607 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6608 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6609 818c7501 2019-07-11 stsp {
6610 818c7501 2019-07-11 stsp const struct got_error *err;
6611 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6612 818c7501 2019-07-11 stsp
6613 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6614 818c7501 2019-07-11 stsp if (err) {
6615 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6616 818c7501 2019-07-11 stsp goto done;
6617 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6618 818c7501 2019-07-11 stsp if (err)
6619 818c7501 2019-07-11 stsp goto done;
6620 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6621 818c7501 2019-07-11 stsp if (err)
6622 818c7501 2019-07-11 stsp goto done;
6623 de05890f 2020-03-05 stsp } else if (is_rebase) {
6624 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6625 818c7501 2019-07-11 stsp int cmp;
6626 818c7501 2019-07-11 stsp
6627 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6628 818c7501 2019-07-11 stsp if (err)
6629 818c7501 2019-07-11 stsp goto done;
6630 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6631 818c7501 2019-07-11 stsp free(stored_id);
6632 818c7501 2019-07-11 stsp if (cmp != 0) {
6633 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6634 818c7501 2019-07-11 stsp goto done;
6635 818c7501 2019-07-11 stsp }
6636 818c7501 2019-07-11 stsp }
6637 0ebf8283 2019-07-24 stsp done:
6638 0ebf8283 2019-07-24 stsp if (commit_ref)
6639 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6640 0ebf8283 2019-07-24 stsp return err;
6641 0ebf8283 2019-07-24 stsp }
6642 0ebf8283 2019-07-24 stsp
6643 0ebf8283 2019-07-24 stsp static const struct got_error *
6644 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6645 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6646 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6647 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6648 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6649 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6650 0ebf8283 2019-07-24 stsp {
6651 0ebf8283 2019-07-24 stsp const struct got_error *err;
6652 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6653 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6654 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6655 818c7501 2019-07-11 stsp
6656 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6657 0ebf8283 2019-07-24 stsp
6658 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6659 0ebf8283 2019-07-24 stsp if (err)
6660 0ebf8283 2019-07-24 stsp return err;
6661 0ebf8283 2019-07-24 stsp
6662 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6663 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6664 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6665 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6666 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6667 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6668 818c7501 2019-07-11 stsp if (commit_ref)
6669 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6670 818c7501 2019-07-11 stsp return err;
6671 818c7501 2019-07-11 stsp }
6672 818c7501 2019-07-11 stsp
6673 818c7501 2019-07-11 stsp const struct got_error *
6674 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6675 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6676 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6677 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6678 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6679 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6680 818c7501 2019-07-11 stsp {
6681 0ebf8283 2019-07-24 stsp const struct got_error *err;
6682 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6683 0ebf8283 2019-07-24 stsp
6684 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6685 0ebf8283 2019-07-24 stsp if (err)
6686 0ebf8283 2019-07-24 stsp return err;
6687 0ebf8283 2019-07-24 stsp
6688 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6689 0ebf8283 2019-07-24 stsp if (err)
6690 0ebf8283 2019-07-24 stsp goto done;
6691 0ebf8283 2019-07-24 stsp
6692 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6693 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6694 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6695 0ebf8283 2019-07-24 stsp done:
6696 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6697 0ebf8283 2019-07-24 stsp return err;
6698 0ebf8283 2019-07-24 stsp }
6699 0ebf8283 2019-07-24 stsp
6700 0ebf8283 2019-07-24 stsp const struct got_error *
6701 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6702 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6703 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6704 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6705 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6706 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6707 0ebf8283 2019-07-24 stsp {
6708 0ebf8283 2019-07-24 stsp const struct got_error *err;
6709 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6710 0ebf8283 2019-07-24 stsp
6711 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6712 0ebf8283 2019-07-24 stsp if (err)
6713 0ebf8283 2019-07-24 stsp return err;
6714 0ebf8283 2019-07-24 stsp
6715 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6716 0ebf8283 2019-07-24 stsp if (err)
6717 0ebf8283 2019-07-24 stsp goto done;
6718 0ebf8283 2019-07-24 stsp
6719 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6720 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6721 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6722 0ebf8283 2019-07-24 stsp done:
6723 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6724 0ebf8283 2019-07-24 stsp return err;
6725 0ebf8283 2019-07-24 stsp }
6726 0ebf8283 2019-07-24 stsp
6727 0ebf8283 2019-07-24 stsp static const struct got_error *
6728 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6729 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6730 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6731 598eac43 2022-07-22 stsp struct got_reference *tmp_branch, const char *committer,
6732 598eac43 2022-07-22 stsp struct got_commit_object *orig_commit, const char *new_logmsg,
6733 598eac43 2022-07-22 stsp struct got_repository *repo)
6734 0ebf8283 2019-07-24 stsp {
6735 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6736 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6737 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6738 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6739 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6740 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6741 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6742 818c7501 2019-07-11 stsp
6743 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
6744 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6745 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6746 a0e95631 2019-07-12 stsp
6747 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6748 818c7501 2019-07-11 stsp
6749 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6750 a0e95631 2019-07-12 stsp if (err)
6751 3e3a69f1 2019-07-25 stsp return err;
6752 a0e95631 2019-07-12 stsp
6753 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6754 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6755 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6756 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6757 01757395 2019-07-12 stsp /*
6758 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6759 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6760 6c13b005 2021-09-02 stsp *
6761 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6762 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6763 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6764 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6765 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6766 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6767 01757395 2019-07-12 stsp */
6768 01757395 2019-07-12 stsp if (merged_paths) {
6769 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6770 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6771 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6772 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6773 f2a9dc41 2019-12-13 tracey 0);
6774 01757395 2019-07-12 stsp if (err)
6775 01757395 2019-07-12 stsp goto done;
6776 01757395 2019-07-12 stsp }
6777 01757395 2019-07-12 stsp } else {
6778 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6779 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6780 01757395 2019-07-12 stsp if (err)
6781 01757395 2019-07-12 stsp goto done;
6782 01757395 2019-07-12 stsp }
6783 a0e95631 2019-07-12 stsp
6784 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6785 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6786 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6787 a0e95631 2019-07-12 stsp if (err)
6788 a0e95631 2019-07-12 stsp goto done;
6789 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6790 a0e95631 2019-07-12 stsp goto done;
6791 ff0d2220 2019-07-11 stsp }
6792 818c7501 2019-07-11 stsp
6793 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6794 edd02c5e 2019-07-11 stsp if (err)
6795 edd02c5e 2019-07-11 stsp goto done;
6796 edd02c5e 2019-07-11 stsp
6797 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp
6801 5943eee2 2019-08-13 stsp if (new_logmsg) {
6802 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6803 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6804 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6805 5943eee2 2019-08-13 stsp goto done;
6806 5943eee2 2019-08-13 stsp }
6807 5943eee2 2019-08-13 stsp } else {
6808 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6809 5943eee2 2019-08-13 stsp if (err)
6810 5943eee2 2019-08-13 stsp goto done;
6811 5943eee2 2019-08-13 stsp }
6812 0ebf8283 2019-07-24 stsp
6813 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6814 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6815 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6816 50e7a649 2022-07-22 stsp committer ? committer :
6817 2a47b1e5 2022-11-01 stsp got_object_commit_get_committer(orig_commit), NULL,
6818 50e7a649 2022-07-22 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6819 a0e95631 2019-07-12 stsp if (err)
6820 a0e95631 2019-07-12 stsp goto done;
6821 a0e95631 2019-07-12 stsp
6822 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6823 a0e95631 2019-07-12 stsp if (err)
6824 a0e95631 2019-07-12 stsp goto done;
6825 a0e95631 2019-07-12 stsp
6826 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6827 a0e95631 2019-07-12 stsp if (err)
6828 a0e95631 2019-07-12 stsp goto done;
6829 a0e95631 2019-07-12 stsp
6830 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6831 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6832 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6833 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6834 a0e95631 2019-07-12 stsp err = sync_err;
6835 818c7501 2019-07-11 stsp done:
6836 a0e95631 2019-07-12 stsp free(fileindex_path);
6837 a0e95631 2019-07-12 stsp free(head_commit_id);
6838 a0e95631 2019-07-12 stsp if (head_ref)
6839 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6840 818c7501 2019-07-11 stsp if (err) {
6841 818c7501 2019-07-11 stsp free(*new_commit_id);
6842 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6843 818c7501 2019-07-11 stsp }
6844 818c7501 2019-07-11 stsp return err;
6845 818c7501 2019-07-11 stsp }
6846 818c7501 2019-07-11 stsp
6847 818c7501 2019-07-11 stsp const struct got_error *
6848 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6849 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6850 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6851 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6852 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6853 0ebf8283 2019-07-24 stsp {
6854 0ebf8283 2019-07-24 stsp const struct got_error *err;
6855 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6856 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6857 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6858 0ebf8283 2019-07-24 stsp
6859 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6860 0ebf8283 2019-07-24 stsp if (err)
6861 0ebf8283 2019-07-24 stsp return err;
6862 0ebf8283 2019-07-24 stsp
6863 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6864 0ebf8283 2019-07-24 stsp if (err)
6865 0ebf8283 2019-07-24 stsp goto done;
6866 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6867 0ebf8283 2019-07-24 stsp if (err)
6868 0ebf8283 2019-07-24 stsp goto done;
6869 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6870 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6871 0ebf8283 2019-07-24 stsp goto done;
6872 0ebf8283 2019-07-24 stsp }
6873 0ebf8283 2019-07-24 stsp
6874 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6875 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6876 598eac43 2022-07-22 stsp NULL, repo);
6877 0ebf8283 2019-07-24 stsp done:
6878 0ebf8283 2019-07-24 stsp if (commit_ref)
6879 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6880 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6881 0ebf8283 2019-07-24 stsp free(commit_id);
6882 0ebf8283 2019-07-24 stsp return err;
6883 0ebf8283 2019-07-24 stsp }
6884 0ebf8283 2019-07-24 stsp
6885 0ebf8283 2019-07-24 stsp const struct got_error *
6886 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6887 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6888 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6889 598eac43 2022-07-22 stsp const char *committer, struct got_commit_object *orig_commit,
6890 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6891 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6892 0ebf8283 2019-07-24 stsp {
6893 0ebf8283 2019-07-24 stsp const struct got_error *err;
6894 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6895 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6898 0ebf8283 2019-07-24 stsp if (err)
6899 0ebf8283 2019-07-24 stsp return err;
6900 0ebf8283 2019-07-24 stsp
6901 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6902 0ebf8283 2019-07-24 stsp if (err)
6903 0ebf8283 2019-07-24 stsp goto done;
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6906 598eac43 2022-07-22 stsp worktree, fileindex, tmp_branch, committer, orig_commit,
6907 598eac43 2022-07-22 stsp new_logmsg, repo);
6908 0ebf8283 2019-07-24 stsp done:
6909 0ebf8283 2019-07-24 stsp if (commit_ref)
6910 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6911 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6912 0ebf8283 2019-07-24 stsp return err;
6913 0ebf8283 2019-07-24 stsp }
6914 0ebf8283 2019-07-24 stsp
6915 0ebf8283 2019-07-24 stsp const struct got_error *
6916 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6917 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6918 818c7501 2019-07-11 stsp {
6919 3e3a69f1 2019-07-25 stsp if (fileindex)
6920 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6921 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6922 69844fba 2019-07-11 stsp }
6923 69844fba 2019-07-11 stsp
6924 69844fba 2019-07-11 stsp static const struct got_error *
6925 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6926 69844fba 2019-07-11 stsp {
6927 69844fba 2019-07-11 stsp const struct got_error *err;
6928 69844fba 2019-07-11 stsp struct got_reference *ref;
6929 69844fba 2019-07-11 stsp
6930 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6931 69844fba 2019-07-11 stsp if (err) {
6932 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6933 69844fba 2019-07-11 stsp return NULL;
6934 69844fba 2019-07-11 stsp return err;
6935 69844fba 2019-07-11 stsp }
6936 69844fba 2019-07-11 stsp
6937 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6938 69844fba 2019-07-11 stsp got_ref_close(ref);
6939 69844fba 2019-07-11 stsp return err;
6940 818c7501 2019-07-11 stsp }
6941 818c7501 2019-07-11 stsp
6942 69844fba 2019-07-11 stsp static const struct got_error *
6943 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6944 69844fba 2019-07-11 stsp {
6945 69844fba 2019-07-11 stsp const struct got_error *err;
6946 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6947 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6948 69844fba 2019-07-11 stsp
6949 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6950 69844fba 2019-07-11 stsp if (err)
6951 69844fba 2019-07-11 stsp goto done;
6952 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6953 69844fba 2019-07-11 stsp if (err)
6954 69844fba 2019-07-11 stsp goto done;
6955 69844fba 2019-07-11 stsp
6956 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6957 69844fba 2019-07-11 stsp if (err)
6958 69844fba 2019-07-11 stsp goto done;
6959 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6960 69844fba 2019-07-11 stsp if (err)
6961 69844fba 2019-07-11 stsp goto done;
6962 69844fba 2019-07-11 stsp
6963 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6964 69844fba 2019-07-11 stsp if (err)
6965 69844fba 2019-07-11 stsp goto done;
6966 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6967 69844fba 2019-07-11 stsp if (err)
6968 69844fba 2019-07-11 stsp goto done;
6969 69844fba 2019-07-11 stsp
6970 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6971 69844fba 2019-07-11 stsp if (err)
6972 69844fba 2019-07-11 stsp goto done;
6973 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6974 69844fba 2019-07-11 stsp if (err)
6975 69844fba 2019-07-11 stsp goto done;
6976 69844fba 2019-07-11 stsp
6977 69844fba 2019-07-11 stsp done:
6978 69844fba 2019-07-11 stsp free(tmp_branch_name);
6979 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6980 69844fba 2019-07-11 stsp free(branch_ref_name);
6981 69844fba 2019-07-11 stsp free(commit_ref_name);
6982 e600f124 2021-03-21 stsp return err;
6983 e600f124 2021-03-21 stsp }
6984 e600f124 2021-03-21 stsp
6985 336075a4 2022-06-25 op static const struct got_error *
6986 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6987 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6988 e600f124 2021-03-21 stsp {
6989 e600f124 2021-03-21 stsp const struct got_error *err;
6990 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6991 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6992 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6993 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6994 e600f124 2021-03-21 stsp char *refname = NULL;
6995 e600f124 2021-03-21 stsp
6996 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6997 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6998 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6999 e600f124 2021-03-21 stsp branch_name += 11;
7000 e600f124 2021-03-21 stsp
7001 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
7002 e600f124 2021-03-21 stsp if (err)
7003 e600f124 2021-03-21 stsp return err;
7004 e600f124 2021-03-21 stsp
7005 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7006 e600f124 2021-03-21 stsp new_id_str) == -1) {
7007 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
7008 e600f124 2021-03-21 stsp goto done;
7009 e600f124 2021-03-21 stsp }
7010 e600f124 2021-03-21 stsp
7011 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
7012 e600f124 2021-03-21 stsp if (err)
7013 e600f124 2021-03-21 stsp goto done;
7014 e600f124 2021-03-21 stsp
7015 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
7016 e600f124 2021-03-21 stsp if (err)
7017 e600f124 2021-03-21 stsp goto done;
7018 e600f124 2021-03-21 stsp
7019 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
7020 e600f124 2021-03-21 stsp done:
7021 e600f124 2021-03-21 stsp free(new_id_str);
7022 e600f124 2021-03-21 stsp free(refname);
7023 e600f124 2021-03-21 stsp free(old_commit_id);
7024 e600f124 2021-03-21 stsp if (ref)
7025 e600f124 2021-03-21 stsp got_ref_close(ref);
7026 69844fba 2019-07-11 stsp return err;
7027 69844fba 2019-07-11 stsp }
7028 69844fba 2019-07-11 stsp
7029 818c7501 2019-07-11 stsp const struct got_error *
7030 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
7031 ef85a376 2023-02-01 mark struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7032 ef85a376 2023-02-01 mark struct got_reference *rebased_branch, struct got_repository *repo,
7033 ef85a376 2023-02-01 mark int create_backup)
7034 818c7501 2019-07-11 stsp {
7035 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7036 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
7037 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7038 818c7501 2019-07-11 stsp
7039 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7040 818c7501 2019-07-11 stsp if (err)
7041 818c7501 2019-07-11 stsp return err;
7042 e600f124 2021-03-21 stsp
7043 e600f124 2021-03-21 stsp if (create_backup) {
7044 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7045 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
7046 e600f124 2021-03-21 stsp if (err)
7047 e600f124 2021-03-21 stsp goto done;
7048 e600f124 2021-03-21 stsp }
7049 818c7501 2019-07-11 stsp
7050 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7051 818c7501 2019-07-11 stsp if (err)
7052 818c7501 2019-07-11 stsp goto done;
7053 818c7501 2019-07-11 stsp
7054 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
7055 818c7501 2019-07-11 stsp if (err)
7056 818c7501 2019-07-11 stsp goto done;
7057 818c7501 2019-07-11 stsp
7058 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
7059 818c7501 2019-07-11 stsp if (err)
7060 818c7501 2019-07-11 stsp goto done;
7061 818c7501 2019-07-11 stsp
7062 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
7063 a615e0e7 2020-12-16 stsp if (err)
7064 a615e0e7 2020-12-16 stsp goto done;
7065 a615e0e7 2020-12-16 stsp
7066 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7067 a615e0e7 2020-12-16 stsp if (err)
7068 a615e0e7 2020-12-16 stsp goto done;
7069 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7070 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7071 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7072 a615e0e7 2020-12-16 stsp err = sync_err;
7073 818c7501 2019-07-11 stsp done:
7074 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7075 a615e0e7 2020-12-16 stsp free(fileindex_path);
7076 818c7501 2019-07-11 stsp free(new_head_commit_id);
7077 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7078 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7079 818c7501 2019-07-11 stsp err = unlockerr;
7080 818c7501 2019-07-11 stsp return err;
7081 818c7501 2019-07-11 stsp }
7082 818c7501 2019-07-11 stsp
7083 818c7501 2019-07-11 stsp const struct got_error *
7084 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
7085 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7086 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
7087 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
7088 818c7501 2019-07-11 stsp {
7089 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
7090 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
7091 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
7092 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7093 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
7094 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7095 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
7096 818c7501 2019-07-11 stsp
7097 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
7098 818c7501 2019-07-11 stsp if (err)
7099 818c7501 2019-07-11 stsp return err;
7100 818c7501 2019-07-11 stsp
7101 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7102 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7103 a44927cc 2022-04-07 stsp if (err)
7104 a44927cc 2022-04-07 stsp goto done;
7105 a44927cc 2022-04-07 stsp
7106 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
7107 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
7108 818c7501 2019-07-11 stsp if (err)
7109 818c7501 2019-07-11 stsp goto done;
7110 818c7501 2019-07-11 stsp
7111 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
7112 818c7501 2019-07-11 stsp if (err)
7113 818c7501 2019-07-11 stsp goto done;
7114 818c7501 2019-07-11 stsp
7115 818c7501 2019-07-11 stsp /*
7116 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
7117 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
7118 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
7119 818c7501 2019-07-11 stsp */
7120 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
7121 818c7501 2019-07-11 stsp if (err)
7122 818c7501 2019-07-11 stsp goto done;
7123 818c7501 2019-07-11 stsp
7124 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7125 818c7501 2019-07-11 stsp if (err)
7126 818c7501 2019-07-11 stsp goto done;
7127 818c7501 2019-07-11 stsp
7128 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7129 a44927cc 2022-04-07 stsp worktree->path_prefix);
7130 ca355955 2019-07-12 stsp if (err)
7131 ca355955 2019-07-12 stsp goto done;
7132 ca355955 2019-07-12 stsp
7133 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
7134 ca355955 2019-07-12 stsp if (err)
7135 ca355955 2019-07-12 stsp goto done;
7136 ca355955 2019-07-12 stsp
7137 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7138 818c7501 2019-07-11 stsp if (err)
7139 818c7501 2019-07-11 stsp goto done;
7140 818c7501 2019-07-11 stsp
7141 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7142 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7143 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7144 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7145 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7146 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7147 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7148 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7149 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
7150 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7151 55bd499d 2019-07-12 stsp if (err)
7152 1f1abb7e 2019-08-08 stsp goto sync;
7153 55bd499d 2019-07-12 stsp
7154 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7155 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
7156 ca355955 2019-07-12 stsp sync:
7157 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7158 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
7159 ca355955 2019-07-12 stsp err = sync_err;
7160 818c7501 2019-07-11 stsp done:
7161 818c7501 2019-07-11 stsp got_ref_close(resolved);
7162 a3a2faf2 2019-07-12 stsp free(tree_id);
7163 818c7501 2019-07-11 stsp free(commit_id);
7164 a44927cc 2022-04-07 stsp if (commit)
7165 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7166 0ebf8283 2019-07-24 stsp if (fileindex)
7167 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
7168 0ebf8283 2019-07-24 stsp free(fileindex_path);
7169 0ebf8283 2019-07-24 stsp
7170 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7171 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7172 0ebf8283 2019-07-24 stsp err = unlockerr;
7173 0ebf8283 2019-07-24 stsp return err;
7174 0ebf8283 2019-07-24 stsp }
7175 0ebf8283 2019-07-24 stsp
7176 0ebf8283 2019-07-24 stsp const struct got_error *
7177 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7178 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7179 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7180 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7181 0ebf8283 2019-07-24 stsp {
7182 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7183 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7184 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
7185 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
7186 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7187 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
7188 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
7189 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7190 0ebf8283 2019-07-24 stsp
7191 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7192 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7193 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7194 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7195 0ebf8283 2019-07-24 stsp
7196 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7197 0ebf8283 2019-07-24 stsp if (err)
7198 0ebf8283 2019-07-24 stsp return err;
7199 0ebf8283 2019-07-24 stsp
7200 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7201 0ebf8283 2019-07-24 stsp if (err)
7202 0ebf8283 2019-07-24 stsp goto done;
7203 0ebf8283 2019-07-24 stsp
7204 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
7205 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
7206 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7207 0ebf8283 2019-07-24 stsp &ok_arg);
7208 0ebf8283 2019-07-24 stsp if (err)
7209 0ebf8283 2019-07-24 stsp goto done;
7210 0ebf8283 2019-07-24 stsp
7211 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7212 0ebf8283 2019-07-24 stsp if (err)
7213 0ebf8283 2019-07-24 stsp goto done;
7214 0ebf8283 2019-07-24 stsp
7215 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
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 = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7220 0ebf8283 2019-07-24 stsp worktree);
7221 0ebf8283 2019-07-24 stsp if (err)
7222 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp
7224 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7225 0ebf8283 2019-07-24 stsp 0);
7226 0ebf8283 2019-07-24 stsp if (err)
7227 0ebf8283 2019-07-24 stsp goto done;
7228 0ebf8283 2019-07-24 stsp
7229 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7230 0ebf8283 2019-07-24 stsp if (err)
7231 0ebf8283 2019-07-24 stsp goto done;
7232 0ebf8283 2019-07-24 stsp
7233 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
7234 0ebf8283 2019-07-24 stsp if (err)
7235 0ebf8283 2019-07-24 stsp goto done;
7236 0ebf8283 2019-07-24 stsp
7237 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7238 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7239 0ebf8283 2019-07-24 stsp if (err)
7240 0ebf8283 2019-07-24 stsp goto done;
7241 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
7242 0ebf8283 2019-07-24 stsp if (err)
7243 0ebf8283 2019-07-24 stsp goto done;
7244 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7245 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
7246 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
7247 0ebf8283 2019-07-24 stsp goto done;
7248 0ebf8283 2019-07-24 stsp }
7249 0ebf8283 2019-07-24 stsp
7250 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
7251 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
7252 0ebf8283 2019-07-24 stsp if (err)
7253 0ebf8283 2019-07-24 stsp goto done;
7254 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
7255 0ebf8283 2019-07-24 stsp if (err)
7256 0ebf8283 2019-07-24 stsp goto done;
7257 0ebf8283 2019-07-24 stsp
7258 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
7259 0ebf8283 2019-07-24 stsp if (err)
7260 0ebf8283 2019-07-24 stsp goto done;
7261 0ebf8283 2019-07-24 stsp done:
7262 0ebf8283 2019-07-24 stsp free(fileindex_path);
7263 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7264 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7265 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7266 0ebf8283 2019-07-24 stsp if (wt_branch)
7267 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
7268 0ebf8283 2019-07-24 stsp if (err) {
7269 0ebf8283 2019-07-24 stsp if (*branch_ref) {
7270 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
7271 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
7272 0ebf8283 2019-07-24 stsp }
7273 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7274 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7275 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7276 0ebf8283 2019-07-24 stsp }
7277 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7278 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7279 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7280 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7281 3e3a69f1 2019-07-25 stsp }
7282 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
7283 0ebf8283 2019-07-24 stsp }
7284 0ebf8283 2019-07-24 stsp return err;
7285 0ebf8283 2019-07-24 stsp }
7286 0ebf8283 2019-07-24 stsp
7287 0ebf8283 2019-07-24 stsp const struct got_error *
7288 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
7289 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
7290 0ebf8283 2019-07-24 stsp {
7291 3e3a69f1 2019-07-25 stsp if (fileindex)
7292 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
7293 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
7294 0ebf8283 2019-07-24 stsp }
7295 0ebf8283 2019-07-24 stsp
7296 0ebf8283 2019-07-24 stsp const struct got_error *
7297 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
7298 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
7299 0ebf8283 2019-07-24 stsp {
7300 0ebf8283 2019-07-24 stsp const struct got_error *err;
7301 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
7302 0ebf8283 2019-07-24 stsp
7303 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7304 0ebf8283 2019-07-24 stsp if (err)
7305 0ebf8283 2019-07-24 stsp return err;
7306 0ebf8283 2019-07-24 stsp
7307 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7308 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7309 0ebf8283 2019-07-24 stsp return NULL;
7310 0ebf8283 2019-07-24 stsp }
7311 0ebf8283 2019-07-24 stsp
7312 0ebf8283 2019-07-24 stsp const struct got_error *
7313 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7314 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7315 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7316 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7317 0ebf8283 2019-07-24 stsp {
7318 0ebf8283 2019-07-24 stsp const struct got_error *err;
7319 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7320 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7321 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7322 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7323 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7324 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7325 0ebf8283 2019-07-24 stsp
7326 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7327 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7328 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7329 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7330 0ebf8283 2019-07-24 stsp
7331 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7332 3e3a69f1 2019-07-25 stsp if (err)
7333 3e3a69f1 2019-07-25 stsp return err;
7334 3e3a69f1 2019-07-25 stsp
7335 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7336 3e3a69f1 2019-07-25 stsp if (err)
7337 f032f1f7 2019-08-04 stsp goto done;
7338 f032f1f7 2019-08-04 stsp
7339 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7340 f032f1f7 2019-08-04 stsp &have_staged_files);
7341 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7342 f032f1f7 2019-08-04 stsp goto done;
7343 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7344 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7345 3e3a69f1 2019-07-25 stsp goto done;
7346 f032f1f7 2019-08-04 stsp }
7347 3e3a69f1 2019-07-25 stsp
7348 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7349 0ebf8283 2019-07-24 stsp if (err)
7350 f032f1f7 2019-08-04 stsp goto done;
7351 0ebf8283 2019-07-24 stsp
7352 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7353 0ebf8283 2019-07-24 stsp if (err)
7354 0ebf8283 2019-07-24 stsp goto done;
7355 0ebf8283 2019-07-24 stsp
7356 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7357 0ebf8283 2019-07-24 stsp if (err)
7358 0ebf8283 2019-07-24 stsp goto done;
7359 0ebf8283 2019-07-24 stsp
7360 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7361 0ebf8283 2019-07-24 stsp worktree);
7362 0ebf8283 2019-07-24 stsp if (err)
7363 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp
7365 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7366 0ebf8283 2019-07-24 stsp if (err)
7367 0ebf8283 2019-07-24 stsp goto done;
7368 0ebf8283 2019-07-24 stsp
7369 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7370 0ebf8283 2019-07-24 stsp if (err)
7371 0ebf8283 2019-07-24 stsp goto done;
7372 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7373 0ebf8283 2019-07-24 stsp if (err)
7374 0ebf8283 2019-07-24 stsp goto done;
7375 0ebf8283 2019-07-24 stsp
7376 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7377 0ebf8283 2019-07-24 stsp if (err)
7378 0ebf8283 2019-07-24 stsp goto done;
7379 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7380 0ebf8283 2019-07-24 stsp if (err)
7381 0ebf8283 2019-07-24 stsp goto done;
7382 0ebf8283 2019-07-24 stsp
7383 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7384 0ebf8283 2019-07-24 stsp if (err)
7385 0ebf8283 2019-07-24 stsp goto done;
7386 0ebf8283 2019-07-24 stsp done:
7387 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7388 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7389 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7390 0ebf8283 2019-07-24 stsp if (commit_ref)
7391 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7392 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7393 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7394 0ebf8283 2019-07-24 stsp if (err) {
7395 0ebf8283 2019-07-24 stsp free(*commit_id);
7396 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7397 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7398 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7399 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7400 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7401 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7402 0ebf8283 2019-07-24 stsp }
7403 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7404 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7405 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7406 3e3a69f1 2019-07-25 stsp }
7407 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7408 0ebf8283 2019-07-24 stsp }
7409 0ebf8283 2019-07-24 stsp return err;
7410 0ebf8283 2019-07-24 stsp }
7411 0ebf8283 2019-07-24 stsp
7412 0ebf8283 2019-07-24 stsp static const struct got_error *
7413 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7414 0ebf8283 2019-07-24 stsp {
7415 0ebf8283 2019-07-24 stsp const struct got_error *err;
7416 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7417 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7418 0ebf8283 2019-07-24 stsp
7419 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7420 0ebf8283 2019-07-24 stsp if (err)
7421 0ebf8283 2019-07-24 stsp goto done;
7422 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7423 0ebf8283 2019-07-24 stsp if (err)
7424 0ebf8283 2019-07-24 stsp goto done;
7425 0ebf8283 2019-07-24 stsp
7426 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7427 0ebf8283 2019-07-24 stsp worktree);
7428 0ebf8283 2019-07-24 stsp if (err)
7429 0ebf8283 2019-07-24 stsp goto done;
7430 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7431 0ebf8283 2019-07-24 stsp if (err)
7432 0ebf8283 2019-07-24 stsp goto done;
7433 0ebf8283 2019-07-24 stsp
7434 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7435 0ebf8283 2019-07-24 stsp if (err)
7436 0ebf8283 2019-07-24 stsp goto done;
7437 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7438 0ebf8283 2019-07-24 stsp if (err)
7439 0ebf8283 2019-07-24 stsp goto done;
7440 0ebf8283 2019-07-24 stsp
7441 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7442 0ebf8283 2019-07-24 stsp if (err)
7443 0ebf8283 2019-07-24 stsp goto done;
7444 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7445 0ebf8283 2019-07-24 stsp if (err)
7446 0ebf8283 2019-07-24 stsp goto done;
7447 0ebf8283 2019-07-24 stsp done:
7448 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7449 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7450 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7451 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7452 0ebf8283 2019-07-24 stsp return err;
7453 0ebf8283 2019-07-24 stsp }
7454 0ebf8283 2019-07-24 stsp
7455 0ebf8283 2019-07-24 stsp const struct got_error *
7456 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7457 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7458 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7459 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7460 0ebf8283 2019-07-24 stsp {
7461 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7462 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7463 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7464 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7465 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7466 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7467 0ebf8283 2019-07-24 stsp
7468 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7469 0ebf8283 2019-07-24 stsp if (err)
7470 0ebf8283 2019-07-24 stsp return err;
7471 0ebf8283 2019-07-24 stsp
7472 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
7473 a44927cc 2022-04-07 stsp worktree->base_commit_id);
7474 a44927cc 2022-04-07 stsp if (err)
7475 a44927cc 2022-04-07 stsp goto done;
7476 a44927cc 2022-04-07 stsp
7477 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7478 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7479 0ebf8283 2019-07-24 stsp if (err)
7480 0ebf8283 2019-07-24 stsp goto done;
7481 0ebf8283 2019-07-24 stsp
7482 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7483 0ebf8283 2019-07-24 stsp if (err)
7484 0ebf8283 2019-07-24 stsp goto done;
7485 0ebf8283 2019-07-24 stsp
7486 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7487 0ebf8283 2019-07-24 stsp if (err)
7488 0ebf8283 2019-07-24 stsp goto done;
7489 0ebf8283 2019-07-24 stsp
7490 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7491 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7492 0ebf8283 2019-07-24 stsp if (err)
7493 0ebf8283 2019-07-24 stsp goto done;
7494 0ebf8283 2019-07-24 stsp
7495 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7496 0ebf8283 2019-07-24 stsp if (err)
7497 0ebf8283 2019-07-24 stsp goto done;
7498 0ebf8283 2019-07-24 stsp
7499 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7500 0ebf8283 2019-07-24 stsp if (err)
7501 0ebf8283 2019-07-24 stsp goto done;
7502 0ebf8283 2019-07-24 stsp
7503 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7504 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7505 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7506 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7507 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7508 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7509 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7510 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7511 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7512 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7513 0ebf8283 2019-07-24 stsp if (err)
7514 1f1abb7e 2019-08-08 stsp goto sync;
7515 0ebf8283 2019-07-24 stsp
7516 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7517 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7518 0ebf8283 2019-07-24 stsp sync:
7519 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7520 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7521 0ebf8283 2019-07-24 stsp err = sync_err;
7522 0ebf8283 2019-07-24 stsp done:
7523 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7524 0ebf8283 2019-07-24 stsp free(tree_id);
7525 818c7501 2019-07-11 stsp free(fileindex_path);
7526 818c7501 2019-07-11 stsp
7527 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7528 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7529 818c7501 2019-07-11 stsp err = unlockerr;
7530 818c7501 2019-07-11 stsp return err;
7531 818c7501 2019-07-11 stsp }
7532 0ebf8283 2019-07-24 stsp
7533 0ebf8283 2019-07-24 stsp const struct got_error *
7534 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7535 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7536 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7537 0ebf8283 2019-07-24 stsp {
7538 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7539 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7540 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7541 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7542 0ebf8283 2019-07-24 stsp
7543 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7544 0ebf8283 2019-07-24 stsp if (err)
7545 0ebf8283 2019-07-24 stsp return err;
7546 0ebf8283 2019-07-24 stsp
7547 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7548 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7549 e600f124 2021-03-21 stsp if (err)
7550 e600f124 2021-03-21 stsp goto done;
7551 e600f124 2021-03-21 stsp
7552 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7553 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7554 0ebf8283 2019-07-24 stsp if (err)
7555 0ebf8283 2019-07-24 stsp goto done;
7556 0ebf8283 2019-07-24 stsp
7557 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7558 0ebf8283 2019-07-24 stsp if (err)
7559 0ebf8283 2019-07-24 stsp goto done;
7560 0ebf8283 2019-07-24 stsp
7561 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7562 0ebf8283 2019-07-24 stsp if (err)
7563 0ebf8283 2019-07-24 stsp goto done;
7564 0ebf8283 2019-07-24 stsp
7565 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7566 0ebf8283 2019-07-24 stsp if (err)
7567 0ebf8283 2019-07-24 stsp goto done;
7568 0ebf8283 2019-07-24 stsp
7569 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7570 a615e0e7 2020-12-16 stsp if (err)
7571 a615e0e7 2020-12-16 stsp goto done;
7572 a615e0e7 2020-12-16 stsp
7573 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7574 a615e0e7 2020-12-16 stsp if (err)
7575 a615e0e7 2020-12-16 stsp goto done;
7576 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7577 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7578 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7579 a615e0e7 2020-12-16 stsp err = sync_err;
7580 0ebf8283 2019-07-24 stsp done:
7581 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7582 a615e0e7 2020-12-16 stsp free(fileindex_path);
7583 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7584 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7585 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7586 0ebf8283 2019-07-24 stsp err = unlockerr;
7587 0ebf8283 2019-07-24 stsp return err;
7588 0ebf8283 2019-07-24 stsp }
7589 0ebf8283 2019-07-24 stsp
7590 0ebf8283 2019-07-24 stsp const struct got_error *
7591 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7592 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7593 0ebf8283 2019-07-24 stsp {
7594 0ebf8283 2019-07-24 stsp const struct got_error *err;
7595 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7596 0ebf8283 2019-07-24 stsp
7597 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7598 0ebf8283 2019-07-24 stsp if (err)
7599 0ebf8283 2019-07-24 stsp return err;
7600 0ebf8283 2019-07-24 stsp
7601 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7602 0ebf8283 2019-07-24 stsp if (err)
7603 0ebf8283 2019-07-24 stsp goto done;
7604 0ebf8283 2019-07-24 stsp
7605 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7606 0ebf8283 2019-07-24 stsp done:
7607 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7608 2822a352 2019-10-15 stsp return err;
7609 2822a352 2019-10-15 stsp }
7610 2822a352 2019-10-15 stsp
7611 2822a352 2019-10-15 stsp const struct got_error *
7612 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7613 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7614 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7615 2822a352 2019-10-15 stsp struct got_repository *repo)
7616 2822a352 2019-10-15 stsp {
7617 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7618 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7619 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7620 2822a352 2019-10-15 stsp
7621 2822a352 2019-10-15 stsp *fileindex = NULL;
7622 2822a352 2019-10-15 stsp *branch_ref = NULL;
7623 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7624 2822a352 2019-10-15 stsp
7625 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7626 2822a352 2019-10-15 stsp if (err)
7627 2822a352 2019-10-15 stsp return err;
7628 2822a352 2019-10-15 stsp
7629 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7630 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7631 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7632 2822a352 2019-10-15 stsp "update -b or different branch name required");
7633 2822a352 2019-10-15 stsp goto done;
7634 2822a352 2019-10-15 stsp }
7635 2822a352 2019-10-15 stsp
7636 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7637 2822a352 2019-10-15 stsp if (err)
7638 2822a352 2019-10-15 stsp goto done;
7639 2822a352 2019-10-15 stsp
7640 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7641 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7642 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7643 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7644 2822a352 2019-10-15 stsp &ok_arg);
7645 2822a352 2019-10-15 stsp if (err)
7646 2822a352 2019-10-15 stsp goto done;
7647 2822a352 2019-10-15 stsp
7648 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7649 2822a352 2019-10-15 stsp if (err)
7650 2822a352 2019-10-15 stsp goto done;
7651 2822a352 2019-10-15 stsp
7652 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7653 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7654 2822a352 2019-10-15 stsp done:
7655 2822a352 2019-10-15 stsp if (err) {
7656 2822a352 2019-10-15 stsp if (*branch_ref) {
7657 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7658 2822a352 2019-10-15 stsp *branch_ref = NULL;
7659 2822a352 2019-10-15 stsp }
7660 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7661 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7662 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7663 2822a352 2019-10-15 stsp }
7664 2822a352 2019-10-15 stsp if (*fileindex) {
7665 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7666 2822a352 2019-10-15 stsp *fileindex = NULL;
7667 2822a352 2019-10-15 stsp }
7668 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7669 2822a352 2019-10-15 stsp }
7670 0cb83759 2019-08-03 stsp return err;
7671 0cb83759 2019-08-03 stsp }
7672 0cb83759 2019-08-03 stsp
7673 2822a352 2019-10-15 stsp const struct got_error *
7674 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7675 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7676 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7677 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7678 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7679 2822a352 2019-10-15 stsp {
7680 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7681 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7682 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7683 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7684 2822a352 2019-10-15 stsp
7685 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7686 2822a352 2019-10-15 stsp if (err)
7687 2822a352 2019-10-15 stsp goto done;
7688 2822a352 2019-10-15 stsp
7689 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7690 2822a352 2019-10-15 stsp if (err)
7691 2822a352 2019-10-15 stsp goto done;
7692 2822a352 2019-10-15 stsp
7693 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7694 a44927cc 2022-04-07 stsp if (err)
7695 a44927cc 2022-04-07 stsp goto done;
7696 a44927cc 2022-04-07 stsp
7697 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
7698 2822a352 2019-10-15 stsp worktree->path_prefix);
7699 2822a352 2019-10-15 stsp if (err)
7700 2822a352 2019-10-15 stsp goto done;
7701 2822a352 2019-10-15 stsp
7702 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7703 2822a352 2019-10-15 stsp if (err)
7704 2822a352 2019-10-15 stsp goto done;
7705 2822a352 2019-10-15 stsp
7706 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7707 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7708 2822a352 2019-10-15 stsp if (err)
7709 2822a352 2019-10-15 stsp goto sync;
7710 2822a352 2019-10-15 stsp
7711 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7712 2822a352 2019-10-15 stsp if (err)
7713 2822a352 2019-10-15 stsp goto sync;
7714 2822a352 2019-10-15 stsp
7715 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7716 6e210706 2021-01-22 stsp if (err)
7717 6e210706 2021-01-22 stsp goto sync;
7718 6e210706 2021-01-22 stsp
7719 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7720 2822a352 2019-10-15 stsp sync:
7721 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7722 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7723 2822a352 2019-10-15 stsp err = sync_err;
7724 2822a352 2019-10-15 stsp
7725 2822a352 2019-10-15 stsp done:
7726 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7727 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7728 2822a352 2019-10-15 stsp err = unlockerr;
7729 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7730 2822a352 2019-10-15 stsp
7731 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7732 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7733 2822a352 2019-10-15 stsp err = unlockerr;
7734 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7735 2822a352 2019-10-15 stsp
7736 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7737 2822a352 2019-10-15 stsp free(fileindex_path);
7738 2822a352 2019-10-15 stsp free(tree_id);
7739 a44927cc 2022-04-07 stsp if (commit)
7740 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7741 2822a352 2019-10-15 stsp
7742 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7743 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7744 2822a352 2019-10-15 stsp err = unlockerr;
7745 2822a352 2019-10-15 stsp return err;
7746 2822a352 2019-10-15 stsp }
7747 2822a352 2019-10-15 stsp
7748 2822a352 2019-10-15 stsp const struct got_error *
7749 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7750 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7751 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7752 2822a352 2019-10-15 stsp {
7753 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7754 8b692cd0 2019-10-21 stsp
7755 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7756 8b692cd0 2019-10-21 stsp
7757 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7758 8b692cd0 2019-10-21 stsp
7759 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7760 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7761 8b692cd0 2019-10-21 stsp err = unlockerr;
7762 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7763 8b692cd0 2019-10-21 stsp
7764 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7765 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7766 8b692cd0 2019-10-21 stsp err = unlockerr;
7767 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7768 f259c4c1 2021-09-24 stsp
7769 f259c4c1 2021-09-24 stsp return err;
7770 f259c4c1 2021-09-24 stsp }
7771 f259c4c1 2021-09-24 stsp
7772 f259c4c1 2021-09-24 stsp const struct got_error *
7773 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7774 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7775 f259c4c1 2021-09-24 stsp {
7776 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7777 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7778 f259c4c1 2021-09-24 stsp
7779 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7780 f259c4c1 2021-09-24 stsp if (err)
7781 f259c4c1 2021-09-24 stsp goto done;
7782 8b692cd0 2019-10-21 stsp
7783 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7784 f259c4c1 2021-09-24 stsp
7785 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7786 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7787 f259c4c1 2021-09-24 stsp err = sync_err;
7788 f259c4c1 2021-09-24 stsp done:
7789 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7790 f259c4c1 2021-09-24 stsp free(fileindex_path);
7791 8b692cd0 2019-10-21 stsp return err;
7792 2822a352 2019-10-15 stsp }
7793 2822a352 2019-10-15 stsp
7794 f259c4c1 2021-09-24 stsp static const struct got_error *
7795 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7796 f259c4c1 2021-09-24 stsp {
7797 f259c4c1 2021-09-24 stsp const struct got_error *err;
7798 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7799 f259c4c1 2021-09-24 stsp
7800 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7801 f259c4c1 2021-09-24 stsp if (err)
7802 f259c4c1 2021-09-24 stsp goto done;
7803 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7804 f259c4c1 2021-09-24 stsp if (err)
7805 f259c4c1 2021-09-24 stsp goto done;
7806 f259c4c1 2021-09-24 stsp
7807 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7808 f259c4c1 2021-09-24 stsp if (err)
7809 f259c4c1 2021-09-24 stsp goto done;
7810 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7811 f259c4c1 2021-09-24 stsp if (err)
7812 f259c4c1 2021-09-24 stsp goto done;
7813 f259c4c1 2021-09-24 stsp
7814 f259c4c1 2021-09-24 stsp done:
7815 f259c4c1 2021-09-24 stsp free(branch_refname);
7816 f259c4c1 2021-09-24 stsp free(commit_refname);
7817 f259c4c1 2021-09-24 stsp return err;
7818 f259c4c1 2021-09-24 stsp }
7819 f259c4c1 2021-09-24 stsp
7820 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7821 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7822 f259c4c1 2021-09-24 stsp const char *branch_name;
7823 f259c4c1 2021-09-24 stsp };
7824 f259c4c1 2021-09-24 stsp
7825 f259c4c1 2021-09-24 stsp static const struct got_error *
7826 2a47b1e5 2022-11-01 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7827 2a47b1e5 2022-11-01 stsp const char *diff_path, char **logmsg, void *arg)
7828 f259c4c1 2021-09-24 stsp {
7829 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7830 f259c4c1 2021-09-24 stsp
7831 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7832 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7833 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7834 f259c4c1 2021-09-24 stsp
7835 f259c4c1 2021-09-24 stsp return NULL;
7836 f259c4c1 2021-09-24 stsp }
7837 f259c4c1 2021-09-24 stsp
7838 f259c4c1 2021-09-24 stsp
7839 f259c4c1 2021-09-24 stsp const struct got_error *
7840 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7841 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7842 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7843 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7844 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7845 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7846 f259c4c1 2021-09-24 stsp {
7847 f259c4c1 2021-09-24 stsp const struct got_error *err;
7848 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7849 f259c4c1 2021-09-24 stsp
7850 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7851 f259c4c1 2021-09-24 stsp if (err)
7852 f259c4c1 2021-09-24 stsp goto done;
7853 f259c4c1 2021-09-24 stsp
7854 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7855 f259c4c1 2021-09-24 stsp worktree);
7856 f259c4c1 2021-09-24 stsp if (err)
7857 f259c4c1 2021-09-24 stsp goto done;
7858 f259c4c1 2021-09-24 stsp
7859 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7860 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7861 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7862 f259c4c1 2021-09-24 stsp done:
7863 f259c4c1 2021-09-24 stsp free(fileindex_path);
7864 f259c4c1 2021-09-24 stsp return err;
7865 f259c4c1 2021-09-24 stsp }
7866 f259c4c1 2021-09-24 stsp
7867 f259c4c1 2021-09-24 stsp const struct got_error *
7868 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7869 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7870 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7871 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7872 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7873 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7874 0ff8d236 2021-09-28 stsp
7875 f259c4c1 2021-09-24 stsp {
7876 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7877 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7878 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7879 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7880 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7881 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7882 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7883 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7884 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7885 f259c4c1 2021-09-24 stsp
7886 2a47b1e5 2022-11-01 stsp memset(&cc_arg, 0, sizeof(cc_arg));
7887 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7888 f259c4c1 2021-09-24 stsp
7889 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7890 f259c4c1 2021-09-24 stsp
7891 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7892 f259c4c1 2021-09-24 stsp if (err)
7893 f259c4c1 2021-09-24 stsp goto done;
7894 f259c4c1 2021-09-24 stsp
7895 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7896 f259c4c1 2021-09-24 stsp if (err)
7897 f259c4c1 2021-09-24 stsp goto done;
7898 f259c4c1 2021-09-24 stsp
7899 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7900 f259c4c1 2021-09-24 stsp if (err)
7901 f259c4c1 2021-09-24 stsp goto done;
7902 f259c4c1 2021-09-24 stsp
7903 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7904 f259c4c1 2021-09-24 stsp &have_staged_files);
7905 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7906 f259c4c1 2021-09-24 stsp goto done;
7907 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7908 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7909 f259c4c1 2021-09-24 stsp goto done;
7910 f259c4c1 2021-09-24 stsp }
7911 f259c4c1 2021-09-24 stsp
7912 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7913 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7914 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7915 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7916 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7917 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7918 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7919 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7920 f259c4c1 2021-09-24 stsp if (err)
7921 f259c4c1 2021-09-24 stsp goto done;
7922 f259c4c1 2021-09-24 stsp
7923 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7924 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7925 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7926 f259c4c1 2021-09-24 stsp goto done;
7927 f259c4c1 2021-09-24 stsp }
7928 f259c4c1 2021-09-24 stsp
7929 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7930 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7931 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7932 f259c4c1 2021-09-24 stsp
7933 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7934 f259c4c1 2021-09-24 stsp ct_path++;
7935 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7936 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7937 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
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 }
7942 f259c4c1 2021-09-24 stsp
7943 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7944 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7945 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7946 2a47b1e5 2022-11-01 stsp head_commit_id, branch_tip, worktree, author, committer, NULL,
7947 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7948 f259c4c1 2021-09-24 stsp if (err)
7949 f259c4c1 2021-09-24 stsp goto done;
7950 f259c4c1 2021-09-24 stsp
7951 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7952 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7953 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7954 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7955 f259c4c1 2021-09-24 stsp err = sync_err;
7956 f259c4c1 2021-09-24 stsp done:
7957 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7958 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7959 d8bacb93 2023-01-10 mark
7960 f259c4c1 2021-09-24 stsp free_commitable(ct);
7961 f259c4c1 2021-09-24 stsp }
7962 d8bacb93 2023-01-10 mark got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7963 f259c4c1 2021-09-24 stsp free(fileindex_path);
7964 f259c4c1 2021-09-24 stsp return err;
7965 f259c4c1 2021-09-24 stsp }
7966 f259c4c1 2021-09-24 stsp
7967 f259c4c1 2021-09-24 stsp const struct got_error *
7968 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7969 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7970 f259c4c1 2021-09-24 stsp {
7971 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7972 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7973 f259c4c1 2021-09-24 stsp
7974 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7975 f259c4c1 2021-09-24 stsp if (err)
7976 f259c4c1 2021-09-24 stsp goto done;
7977 f259c4c1 2021-09-24 stsp
7978 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7979 f259c4c1 2021-09-24 stsp if (err)
7980 f259c4c1 2021-09-24 stsp goto done;
7981 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7982 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7983 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7984 f259c4c1 2021-09-24 stsp err = sync_err;
7985 f259c4c1 2021-09-24 stsp done:
7986 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7987 f259c4c1 2021-09-24 stsp free(fileindex_path);
7988 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7989 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7990 f259c4c1 2021-09-24 stsp err = unlockerr;
7991 f259c4c1 2021-09-24 stsp return err;
7992 f259c4c1 2021-09-24 stsp }
7993 f259c4c1 2021-09-24 stsp
7994 f259c4c1 2021-09-24 stsp const struct got_error *
7995 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7996 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7997 f259c4c1 2021-09-24 stsp {
7998 f259c4c1 2021-09-24 stsp const struct got_error *err;
7999 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
8000 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
8001 f259c4c1 2021-09-24 stsp
8002 f259c4c1 2021-09-24 stsp *in_progress = 0;
8003 f259c4c1 2021-09-24 stsp
8004 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8005 f259c4c1 2021-09-24 stsp if (err)
8006 f259c4c1 2021-09-24 stsp return err;
8007 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8008 793fcac3 2021-09-24 stsp free(branch_refname);
8009 f259c4c1 2021-09-24 stsp if (err) {
8010 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
8011 f259c4c1 2021-09-24 stsp return err;
8012 f259c4c1 2021-09-24 stsp } else
8013 f259c4c1 2021-09-24 stsp *in_progress = 1;
8014 f259c4c1 2021-09-24 stsp
8015 f259c4c1 2021-09-24 stsp return NULL;
8016 f259c4c1 2021-09-24 stsp }
8017 f259c4c1 2021-09-24 stsp
8018 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
8019 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
8020 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
8021 f259c4c1 2021-09-24 stsp {
8022 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
8023 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8024 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
8025 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8026 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
8027 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8028 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
8029 f259c4c1 2021-09-24 stsp
8030 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8031 f259c4c1 2021-09-24 stsp
8032 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8033 f259c4c1 2021-09-24 stsp if (err)
8034 f259c4c1 2021-09-24 stsp return err;
8035 f259c4c1 2021-09-24 stsp
8036 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8037 f259c4c1 2021-09-24 stsp if (err)
8038 f259c4c1 2021-09-24 stsp goto done;
8039 f259c4c1 2021-09-24 stsp
8040 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
8041 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
8042 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
8043 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8044 f259c4c1 2021-09-24 stsp &ok_arg);
8045 f259c4c1 2021-09-24 stsp if (err)
8046 f259c4c1 2021-09-24 stsp goto done;
8047 f259c4c1 2021-09-24 stsp
8048 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8049 f259c4c1 2021-09-24 stsp if (err)
8050 f259c4c1 2021-09-24 stsp return err;
8051 f259c4c1 2021-09-24 stsp
8052 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8053 f259c4c1 2021-09-24 stsp if (err)
8054 f259c4c1 2021-09-24 stsp return err;
8055 f259c4c1 2021-09-24 stsp
8056 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8057 f259c4c1 2021-09-24 stsp 0);
8058 f259c4c1 2021-09-24 stsp if (err)
8059 f259c4c1 2021-09-24 stsp goto done;
8060 f259c4c1 2021-09-24 stsp
8061 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8062 f259c4c1 2021-09-24 stsp if (err)
8063 f259c4c1 2021-09-24 stsp goto done;
8064 f259c4c1 2021-09-24 stsp
8065 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8066 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8067 f259c4c1 2021-09-24 stsp goto done;
8068 f259c4c1 2021-09-24 stsp }
8069 f259c4c1 2021-09-24 stsp
8070 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
8071 f259c4c1 2021-09-24 stsp if (err)
8072 f259c4c1 2021-09-24 stsp goto done;
8073 f259c4c1 2021-09-24 stsp
8074 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8075 f259c4c1 2021-09-24 stsp if (err)
8076 f259c4c1 2021-09-24 stsp goto done;
8077 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
8078 f259c4c1 2021-09-24 stsp if (err)
8079 f259c4c1 2021-09-24 stsp goto done;
8080 f259c4c1 2021-09-24 stsp
8081 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8082 f259c4c1 2021-09-24 stsp if (err)
8083 f259c4c1 2021-09-24 stsp goto done;
8084 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
8085 f259c4c1 2021-09-24 stsp if (err)
8086 f259c4c1 2021-09-24 stsp goto done;
8087 f259c4c1 2021-09-24 stsp
8088 f259c4c1 2021-09-24 stsp done:
8089 f259c4c1 2021-09-24 stsp free(branch_refname);
8090 f259c4c1 2021-09-24 stsp free(commit_refname);
8091 f259c4c1 2021-09-24 stsp free(fileindex_path);
8092 f259c4c1 2021-09-24 stsp if (branch_ref)
8093 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8094 f259c4c1 2021-09-24 stsp if (commit_ref)
8095 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8096 f259c4c1 2021-09-24 stsp if (wt_branch)
8097 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
8098 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
8099 f259c4c1 2021-09-24 stsp if (err) {
8100 f259c4c1 2021-09-24 stsp if (*fileindex) {
8101 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8102 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8103 f259c4c1 2021-09-24 stsp }
8104 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8105 f259c4c1 2021-09-24 stsp }
8106 f259c4c1 2021-09-24 stsp return err;
8107 f259c4c1 2021-09-24 stsp }
8108 f259c4c1 2021-09-24 stsp
8109 f259c4c1 2021-09-24 stsp const struct got_error *
8110 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
8111 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8112 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8113 f259c4c1 2021-09-24 stsp {
8114 f259c4c1 2021-09-24 stsp const struct got_error *err;
8115 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
8116 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8117 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8118 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
8119 f259c4c1 2021-09-24 stsp
8120 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8121 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8122 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8123 f259c4c1 2021-09-24 stsp
8124 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
8125 f259c4c1 2021-09-24 stsp if (err)
8126 f259c4c1 2021-09-24 stsp return err;
8127 f259c4c1 2021-09-24 stsp
8128 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
8129 f259c4c1 2021-09-24 stsp if (err)
8130 f259c4c1 2021-09-24 stsp goto done;
8131 f259c4c1 2021-09-24 stsp
8132 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8133 f259c4c1 2021-09-24 stsp &have_staged_files);
8134 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
8135 f259c4c1 2021-09-24 stsp goto done;
8136 f259c4c1 2021-09-24 stsp if (have_staged_files) {
8137 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
8138 f259c4c1 2021-09-24 stsp goto done;
8139 f259c4c1 2021-09-24 stsp }
8140 f259c4c1 2021-09-24 stsp
8141 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
8142 f259c4c1 2021-09-24 stsp if (err)
8143 f259c4c1 2021-09-24 stsp goto done;
8144 f259c4c1 2021-09-24 stsp
8145 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
8146 f259c4c1 2021-09-24 stsp if (err)
8147 f259c4c1 2021-09-24 stsp goto done;
8148 f259c4c1 2021-09-24 stsp
8149 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8150 f259c4c1 2021-09-24 stsp if (err)
8151 f259c4c1 2021-09-24 stsp goto done;
8152 f259c4c1 2021-09-24 stsp
8153 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
8154 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8155 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
8156 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
8157 f259c4c1 2021-09-24 stsp goto done;
8158 f259c4c1 2021-09-24 stsp }
8159 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8160 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
8161 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
8162 f259c4c1 2021-09-24 stsp goto done;
8163 f259c4c1 2021-09-24 stsp }
8164 f259c4c1 2021-09-24 stsp
8165 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8166 f259c4c1 2021-09-24 stsp if (err)
8167 f259c4c1 2021-09-24 stsp goto done;
8168 f259c4c1 2021-09-24 stsp
8169 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
8170 f259c4c1 2021-09-24 stsp if (err)
8171 f259c4c1 2021-09-24 stsp goto done;
8172 f259c4c1 2021-09-24 stsp done:
8173 f259c4c1 2021-09-24 stsp free(commit_refname);
8174 f259c4c1 2021-09-24 stsp free(branch_refname);
8175 f259c4c1 2021-09-24 stsp free(fileindex_path);
8176 f259c4c1 2021-09-24 stsp if (commit_ref)
8177 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
8178 f259c4c1 2021-09-24 stsp if (branch_ref)
8179 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
8180 f259c4c1 2021-09-24 stsp if (err) {
8181 f259c4c1 2021-09-24 stsp if (*branch_name) {
8182 f259c4c1 2021-09-24 stsp free(*branch_name);
8183 f259c4c1 2021-09-24 stsp *branch_name = NULL;
8184 f259c4c1 2021-09-24 stsp }
8185 f259c4c1 2021-09-24 stsp free(*branch_tip);
8186 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
8187 f259c4c1 2021-09-24 stsp if (*fileindex) {
8188 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
8189 f259c4c1 2021-09-24 stsp *fileindex = NULL;
8190 f259c4c1 2021-09-24 stsp }
8191 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
8192 f259c4c1 2021-09-24 stsp }
8193 f259c4c1 2021-09-24 stsp return err;
8194 f259c4c1 2021-09-24 stsp }
8195 f259c4c1 2021-09-24 stsp
8196 f259c4c1 2021-09-24 stsp const struct got_error *
8197 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
8198 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
8199 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8200 f259c4c1 2021-09-24 stsp {
8201 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
8202 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
8203 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8204 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
8205 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
8206 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
8207 f259c4c1 2021-09-24 stsp
8208 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, repo,
8209 a44927cc 2022-04-07 stsp worktree->base_commit_id);
8210 a44927cc 2022-04-07 stsp if (err)
8211 a44927cc 2022-04-07 stsp goto done;
8212 a44927cc 2022-04-07 stsp
8213 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, repo, commit,
8214 a44927cc 2022-04-07 stsp worktree->path_prefix);
8215 f259c4c1 2021-09-24 stsp if (err)
8216 f259c4c1 2021-09-24 stsp goto done;
8217 f259c4c1 2021-09-24 stsp
8218 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
8219 f259c4c1 2021-09-24 stsp if (err)
8220 f259c4c1 2021-09-24 stsp goto done;
8221 f259c4c1 2021-09-24 stsp
8222 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
8223 f259c4c1 2021-09-24 stsp if (err)
8224 f259c4c1 2021-09-24 stsp goto done;
8225 f259c4c1 2021-09-24 stsp
8226 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
8227 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
8228 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
8229 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
8230 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
8231 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
8232 f259c4c1 2021-09-24 stsp rfa.repo = repo;
8233 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
8234 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
8235 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
8236 f259c4c1 2021-09-24 stsp if (err)
8237 f259c4c1 2021-09-24 stsp goto sync;
8238 f259c4c1 2021-09-24 stsp
8239 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8240 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
8241 f259c4c1 2021-09-24 stsp sync:
8242 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8243 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
8244 f259c4c1 2021-09-24 stsp err = sync_err;
8245 f259c4c1 2021-09-24 stsp done:
8246 f259c4c1 2021-09-24 stsp free(tree_id);
8247 f259c4c1 2021-09-24 stsp free(commit_id);
8248 a44927cc 2022-04-07 stsp if (commit)
8249 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8250 f259c4c1 2021-09-24 stsp if (fileindex)
8251 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
8252 f259c4c1 2021-09-24 stsp free(fileindex_path);
8253 f259c4c1 2021-09-24 stsp
8254 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8255 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
8256 f259c4c1 2021-09-24 stsp err = unlockerr;
8257 f259c4c1 2021-09-24 stsp return err;
8258 f259c4c1 2021-09-24 stsp }
8259 f259c4c1 2021-09-24 stsp
8260 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
8261 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
8262 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8263 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8264 2db2652d 2019-08-07 stsp struct got_repository *repo;
8265 2db2652d 2019-08-07 stsp int have_changes;
8266 2db2652d 2019-08-07 stsp };
8267 2db2652d 2019-08-07 stsp
8268 336075a4 2022-06-25 op static const struct got_error *
8269 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
8270 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8271 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8272 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8273 735ef5ac 2019-08-03 stsp {
8274 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
8275 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
8276 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
8277 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
8278 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
8279 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
8280 8b13ce36 2019-08-08 stsp
8281 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
8282 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
8283 8b13ce36 2019-08-08 stsp return NULL;
8284 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
8285 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
8286 735ef5ac 2019-08-03 stsp
8287 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8288 735ef5ac 2019-08-03 stsp if (ie == NULL)
8289 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8290 735ef5ac 2019-08-03 stsp
8291 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8292 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8293 735ef5ac 2019-08-03 stsp relpath) == -1)
8294 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
8295 735ef5ac 2019-08-03 stsp
8296 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
8297 3093e2d7 2023-02-04 op memcpy(base_commit_id.hash, ie->commit_hash,
8298 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8299 dfe0a3d8 2023-02-04 op base_commit_id.algo = got_repo_get_hash_algorithm(a->repo);
8300 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
8301 735ef5ac 2019-08-03 stsp }
8302 735ef5ac 2019-08-03 stsp
8303 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
8304 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8305 3aa5969e 2019-08-06 stsp goto done;
8306 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
8307 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
8308 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
8309 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8310 735ef5ac 2019-08-03 stsp goto done;
8311 3aa5969e 2019-08-06 stsp }
8312 735ef5ac 2019-08-03 stsp
8313 2db2652d 2019-08-07 stsp a->have_changes = 1;
8314 2db2652d 2019-08-07 stsp
8315 735ef5ac 2019-08-03 stsp p = in_repo_path;
8316 735ef5ac 2019-08-03 stsp while (p[0] == '/')
8317 735ef5ac 2019-08-03 stsp p++;
8318 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
8319 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
8320 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
8321 735ef5ac 2019-08-03 stsp done:
8322 735ef5ac 2019-08-03 stsp free(in_repo_path);
8323 dc424a06 2019-08-07 stsp return err;
8324 dc424a06 2019-08-07 stsp }
8325 dc424a06 2019-08-07 stsp
8326 2db2652d 2019-08-07 stsp struct stage_path_arg {
8327 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
8328 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
8329 2db2652d 2019-08-07 stsp struct got_repository *repo;
8330 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
8331 2db2652d 2019-08-07 stsp void *status_arg;
8332 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
8333 2db2652d 2019-08-07 stsp void *patch_arg;
8334 7b5dc508 2019-10-28 stsp int staged_something;
8335 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8336 2db2652d 2019-08-07 stsp };
8337 2db2652d 2019-08-07 stsp
8338 2db2652d 2019-08-07 stsp static const struct got_error *
8339 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8340 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8341 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8342 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8343 0cb83759 2019-08-03 stsp {
8344 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8345 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8346 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8347 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8348 0cb83759 2019-08-03 stsp uint32_t stage;
8349 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8350 0aeb8099 2020-07-23 stsp struct stat sb;
8351 8b13ce36 2019-08-08 stsp
8352 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8353 8b13ce36 2019-08-08 stsp return NULL;
8354 0cb83759 2019-08-03 stsp
8355 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8356 d3e7c587 2019-08-03 stsp if (ie == NULL)
8357 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8358 0cb83759 2019-08-03 stsp
8359 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8360 2db2652d 2019-08-07 stsp relpath)== -1)
8361 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8362 0cb83759 2019-08-03 stsp
8363 0cb83759 2019-08-03 stsp switch (status) {
8364 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8365 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8366 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8367 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8368 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8369 0aeb8099 2020-07-23 stsp break;
8370 0aeb8099 2020-07-23 stsp }
8371 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8372 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8373 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8374 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8375 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8376 dc424a06 2019-08-07 stsp if (err)
8377 dc424a06 2019-08-07 stsp break;
8378 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8379 dc424a06 2019-08-07 stsp break;
8380 dc424a06 2019-08-07 stsp } else {
8381 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8382 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8383 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8384 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8385 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8386 dc424a06 2019-08-07 stsp break;
8387 dc424a06 2019-08-07 stsp }
8388 dc424a06 2019-08-07 stsp }
8389 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8390 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8391 0cb83759 2019-08-03 stsp if (err)
8392 dc424a06 2019-08-07 stsp break;
8393 3093e2d7 2023-02-04 op memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8394 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8395 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8396 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8397 0cb83759 2019-08-03 stsp else
8398 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8399 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8400 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8401 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8402 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8403 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8404 35213c7c 2020-07-23 stsp ssize_t target_len;
8405 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8406 35213c7c 2020-07-23 stsp sizeof(target_path));
8407 35213c7c 2020-07-23 stsp if (target_len == -1) {
8408 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8409 35213c7c 2020-07-23 stsp ondisk_path);
8410 35213c7c 2020-07-23 stsp break;
8411 35213c7c 2020-07-23 stsp }
8412 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8413 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8414 35213c7c 2020-07-23 stsp a->worktree->root_path);
8415 35213c7c 2020-07-23 stsp if (err)
8416 35213c7c 2020-07-23 stsp break;
8417 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8418 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8419 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8420 35213c7c 2020-07-23 stsp break;
8421 35213c7c 2020-07-23 stsp }
8422 35213c7c 2020-07-23 stsp }
8423 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8424 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8425 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8426 35213c7c 2020-07-23 stsp else
8427 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8428 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8429 0aeb8099 2020-07-23 stsp } else {
8430 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8431 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8432 0aeb8099 2020-07-23 stsp }
8433 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8434 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8435 dc424a06 2019-08-07 stsp break;
8436 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8437 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8438 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8439 9fdde394 2022-06-04 op if (err)
8440 9fdde394 2022-06-04 op break;
8441 9fdde394 2022-06-04 op /*
8442 9fdde394 2022-06-04 op * When staging the reverse of the staged diff,
8443 9fdde394 2022-06-04 op * implicitly unstage the file.
8444 9fdde394 2022-06-04 op */
8445 3093e2d7 2023-02-04 op if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8446 3093e2d7 2023-02-04 op sizeof(ie->blob_hash)) == 0) {
8447 9fdde394 2022-06-04 op got_fileindex_entry_stage_set(ie,
8448 9fdde394 2022-06-04 op GOT_FILEIDX_STAGE_NONE);
8449 9fdde394 2022-06-04 op }
8450 0cb83759 2019-08-03 stsp break;
8451 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8452 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8453 d3e7c587 2019-08-03 stsp break;
8454 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8455 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8456 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8457 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8458 dc424a06 2019-08-07 stsp if (err)
8459 dc424a06 2019-08-07 stsp break;
8460 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8461 88f33a19 2019-08-08 stsp break;
8462 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8463 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8464 dc424a06 2019-08-07 stsp break;
8465 88f33a19 2019-08-08 stsp }
8466 dc424a06 2019-08-07 stsp }
8467 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8468 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8469 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8470 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8471 dc424a06 2019-08-07 stsp break;
8472 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8473 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8474 12463d8b 2019-12-13 stsp de_name);
8475 0cb83759 2019-08-03 stsp break;
8476 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8477 d3e7c587 2019-08-03 stsp break;
8478 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8479 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8480 ebf48fd5 2019-08-03 stsp break;
8481 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8482 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8483 2a06fe5f 2019-08-24 stsp break;
8484 0cb83759 2019-08-03 stsp default:
8485 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8486 537ac44b 2019-08-03 stsp break;
8487 0cb83759 2019-08-03 stsp }
8488 dc424a06 2019-08-07 stsp
8489 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8490 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8491 dc424a06 2019-08-07 stsp free(path_content);
8492 2db2652d 2019-08-07 stsp free(ondisk_path);
8493 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8494 0ebf8283 2019-07-24 stsp return err;
8495 0ebf8283 2019-07-24 stsp }
8496 0cb83759 2019-08-03 stsp
8497 0cb83759 2019-08-03 stsp const struct got_error *
8498 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8499 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8500 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8501 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8502 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8503 0cb83759 2019-08-03 stsp {
8504 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8505 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8506 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8507 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8508 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8509 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8510 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8511 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8512 0cb83759 2019-08-03 stsp
8513 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8514 0cb83759 2019-08-03 stsp if (err)
8515 0cb83759 2019-08-03 stsp return err;
8516 0cb83759 2019-08-03 stsp
8517 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8518 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8519 735ef5ac 2019-08-03 stsp if (err)
8520 735ef5ac 2019-08-03 stsp goto done;
8521 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8522 735ef5ac 2019-08-03 stsp if (err)
8523 735ef5ac 2019-08-03 stsp goto done;
8524 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8525 0cb83759 2019-08-03 stsp if (err)
8526 0cb83759 2019-08-03 stsp goto done;
8527 0cb83759 2019-08-03 stsp
8528 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8529 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8530 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8531 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8532 2db2652d 2019-08-07 stsp oka.repo = repo;
8533 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8534 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8535 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8536 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8537 735ef5ac 2019-08-03 stsp if (err)
8538 735ef5ac 2019-08-03 stsp goto done;
8539 735ef5ac 2019-08-03 stsp }
8540 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8541 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8542 2db2652d 2019-08-07 stsp goto done;
8543 2db2652d 2019-08-07 stsp }
8544 735ef5ac 2019-08-03 stsp
8545 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8546 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8547 2db2652d 2019-08-07 stsp spa.repo = repo;
8548 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8549 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8550 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8551 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8552 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8553 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8554 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8555 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8556 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8557 42005733 2019-08-03 stsp if (err)
8558 2db2652d 2019-08-07 stsp goto done;
8559 7b5dc508 2019-10-28 stsp }
8560 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8561 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8562 7b5dc508 2019-10-28 stsp goto done;
8563 0cb83759 2019-08-03 stsp }
8564 0cb83759 2019-08-03 stsp
8565 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8566 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8567 0cb83759 2019-08-03 stsp err = sync_err;
8568 0cb83759 2019-08-03 stsp done:
8569 735ef5ac 2019-08-03 stsp if (head_ref)
8570 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8571 735ef5ac 2019-08-03 stsp free(head_commit_id);
8572 0cb83759 2019-08-03 stsp free(fileindex_path);
8573 0cb83759 2019-08-03 stsp if (fileindex)
8574 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8575 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8576 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8577 0cb83759 2019-08-03 stsp err = unlockerr;
8578 0cb83759 2019-08-03 stsp return err;
8579 0cb83759 2019-08-03 stsp }
8580 ad493afc 2019-08-03 stsp
8581 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8582 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8583 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8584 ad493afc 2019-08-03 stsp struct got_repository *repo;
8585 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8586 ad493afc 2019-08-03 stsp void *progress_arg;
8587 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8588 2e1f37b0 2019-08-08 stsp void *patch_arg;
8589 ad493afc 2019-08-03 stsp };
8590 2e1f37b0 2019-08-08 stsp
8591 2e1f37b0 2019-08-08 stsp static const struct got_error *
8592 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8593 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8594 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8595 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8596 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8597 2e1f37b0 2019-08-08 stsp {
8598 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8599 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8600 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8601 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8602 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8603 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8604 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8605 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8606 2e1f37b0 2019-08-08 stsp
8607 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8608 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8609 2e1f37b0 2019-08-08 stsp
8610 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8611 2e1f37b0 2019-08-08 stsp if (err)
8612 2e1f37b0 2019-08-08 stsp return err;
8613 eb81bc23 2022-06-28 tracey
8614 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8615 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8616 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8617 eb81bc23 2022-06-28 tracey goto done;
8618 eb81bc23 2022-06-28 tracey }
8619 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8620 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8621 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8622 eb81bc23 2022-06-28 tracey goto done;
8623 eb81bc23 2022-06-28 tracey }
8624 eb81bc23 2022-06-28 tracey
8625 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8626 2e1f37b0 2019-08-08 stsp if (err)
8627 2e1f37b0 2019-08-08 stsp goto done;
8628 2e1f37b0 2019-08-08 stsp
8629 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8630 2e1f37b0 2019-08-08 stsp if (err)
8631 2e1f37b0 2019-08-08 stsp goto done;
8632 2e1f37b0 2019-08-08 stsp
8633 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8634 2e1f37b0 2019-08-08 stsp if (err)
8635 2e1f37b0 2019-08-08 stsp goto done;
8636 2e1f37b0 2019-08-08 stsp
8637 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8638 eb81bc23 2022-06-28 tracey fd2);
8639 2e1f37b0 2019-08-08 stsp if (err)
8640 2e1f37b0 2019-08-08 stsp goto done;
8641 2e1f37b0 2019-08-08 stsp
8642 b90054ed 2022-11-01 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8643 2e1f37b0 2019-08-08 stsp if (err)
8644 2e1f37b0 2019-08-08 stsp goto done;
8645 ad493afc 2019-08-03 stsp
8646 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8647 2e1f37b0 2019-08-08 stsp if (err)
8648 2e1f37b0 2019-08-08 stsp goto done;
8649 2e1f37b0 2019-08-08 stsp
8650 49d4a017 2022-06-30 stsp err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8651 4b752015 2022-06-30 stsp path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8652 2e1f37b0 2019-08-08 stsp if (err)
8653 2e1f37b0 2019-08-08 stsp goto done;
8654 2e1f37b0 2019-08-08 stsp
8655 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8656 b90054ed 2022-11-01 stsp "got-unstaged-content", "");
8657 2e1f37b0 2019-08-08 stsp if (err)
8658 2e1f37b0 2019-08-08 stsp goto done;
8659 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8660 b90054ed 2022-11-01 stsp "got-new-staged-content", "");
8661 2e1f37b0 2019-08-08 stsp if (err)
8662 2e1f37b0 2019-08-08 stsp goto done;
8663 2e1f37b0 2019-08-08 stsp
8664 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8665 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8666 2e1f37b0 2019-08-08 stsp goto done;
8667 2e1f37b0 2019-08-08 stsp }
8668 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8669 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8670 2e1f37b0 2019-08-08 stsp goto done;
8671 2e1f37b0 2019-08-08 stsp }
8672 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8673 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8674 eb81bc23 2022-06-28 tracey struct diff_chunk_context cc = {};
8675 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8676 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8677 fe621944 2020-11-10 stsp nchanges++;
8678 fe621944 2020-11-10 stsp }
8679 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8680 2e1f37b0 2019-08-08 stsp int choice;
8681 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8682 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8683 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8684 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8685 2e1f37b0 2019-08-08 stsp if (err)
8686 2e1f37b0 2019-08-08 stsp goto done;
8687 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8688 2e1f37b0 2019-08-08 stsp have_content = 1;
8689 19e4b907 2019-08-08 stsp else
8690 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8691 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8692 2e1f37b0 2019-08-08 stsp break;
8693 2e1f37b0 2019-08-08 stsp }
8694 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8695 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8696 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8697 2e1f37b0 2019-08-08 stsp done:
8698 2e1f37b0 2019-08-08 stsp free(label1);
8699 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8700 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8701 2e1f37b0 2019-08-08 stsp if (blob)
8702 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8703 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8704 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
8705 2e1f37b0 2019-08-08 stsp if (staged_blob)
8706 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8707 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8708 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8709 fe621944 2020-11-10 stsp err = free_err;
8710 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8711 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8712 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8713 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8714 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8715 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8716 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8717 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8718 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8719 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8720 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8721 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8722 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8723 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8724 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8725 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8726 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8727 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8728 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8729 2e1f37b0 2019-08-08 stsp }
8730 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8731 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8732 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8733 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8734 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8735 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8736 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8737 2e1f37b0 2019-08-08 stsp }
8738 2e1f37b0 2019-08-08 stsp free(path1);
8739 2e1f37b0 2019-08-08 stsp free(path2);
8740 fda8017d 2020-07-23 stsp return err;
8741 fda8017d 2020-07-23 stsp }
8742 fda8017d 2020-07-23 stsp
8743 fda8017d 2020-07-23 stsp static const struct got_error *
8744 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8745 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8746 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8747 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8748 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8749 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8750 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8751 fda8017d 2020-07-23 stsp {
8752 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8753 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8754 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8755 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8756 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8757 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8758 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8759 fda8017d 2020-07-23 stsp struct stat sb;
8760 fda8017d 2020-07-23 stsp
8761 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8762 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8763 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8764 fda8017d 2020-07-23 stsp if (err)
8765 fda8017d 2020-07-23 stsp return err;
8766 fda8017d 2020-07-23 stsp
8767 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8768 fda8017d 2020-07-23 stsp return NULL;
8769 fda8017d 2020-07-23 stsp
8770 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8771 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8772 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8773 fda8017d 2020-07-23 stsp if (err)
8774 fda8017d 2020-07-23 stsp goto done;
8775 fda8017d 2020-07-23 stsp }
8776 fda8017d 2020-07-23 stsp
8777 00fe21f2 2021-12-31 stsp f = fopen(path_unstaged_content, "re");
8778 fda8017d 2020-07-23 stsp if (f == NULL) {
8779 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8780 fda8017d 2020-07-23 stsp path_unstaged_content);
8781 fda8017d 2020-07-23 stsp goto done;
8782 fda8017d 2020-07-23 stsp }
8783 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8784 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8785 fda8017d 2020-07-23 stsp goto done;
8786 fda8017d 2020-07-23 stsp }
8787 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8788 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8789 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8790 fda8017d 2020-07-23 stsp size_t r;
8791 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8792 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8793 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8794 fda8017d 2020-07-23 stsp goto done;
8795 fda8017d 2020-07-23 stsp }
8796 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8797 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8798 fda8017d 2020-07-23 stsp goto done;
8799 fda8017d 2020-07-23 stsp }
8800 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8801 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8802 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8803 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8804 fda8017d 2020-07-23 stsp progress_arg);
8805 fda8017d 2020-07-23 stsp } else {
8806 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8807 67a66647 2021-05-31 stsp
8808 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8809 67a66647 2021-05-31 stsp if (err)
8810 67a66647 2021-05-31 stsp return err;
8811 67a66647 2021-05-31 stsp
8812 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8813 67a66647 2021-05-31 stsp parent) == -1) {
8814 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8815 67a66647 2021-05-31 stsp base_path = NULL;
8816 67a66647 2021-05-31 stsp goto done;
8817 67a66647 2021-05-31 stsp }
8818 67a66647 2021-05-31 stsp
8819 b90054ed 2022-11-01 stsp err = got_opentemp_named(&blob_base_path, &f_base,
8820 b90054ed 2022-11-01 stsp base_path, "");
8821 67a66647 2021-05-31 stsp if (err)
8822 67a66647 2021-05-31 stsp goto done;
8823 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8824 67a66647 2021-05-31 stsp blob_base);
8825 67a66647 2021-05-31 stsp if (err)
8826 67a66647 2021-05-31 stsp goto done;
8827 67a66647 2021-05-31 stsp
8828 5e91dae4 2022-08-30 stsp /*
8829 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8830 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8831 eec2f5a9 2021-06-03 stsp */
8832 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8833 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8834 eec2f5a9 2021-06-03 stsp ondisk_path);
8835 eec2f5a9 2021-06-03 stsp if (err)
8836 eec2f5a9 2021-06-03 stsp goto done;
8837 eec2f5a9 2021-06-03 stsp } else {
8838 eec2f5a9 2021-06-03 stsp int fd;
8839 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path,
8840 8bd0cdad 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8841 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8842 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8843 eec2f5a9 2021-06-03 stsp goto done;
8844 eec2f5a9 2021-06-03 stsp }
8845 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8846 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8847 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8848 eec2f5a9 2021-06-03 stsp close(fd);
8849 eec2f5a9 2021-06-03 stsp goto done;
8850 eec2f5a9 2021-06-03 stsp }
8851 eec2f5a9 2021-06-03 stsp }
8852 eec2f5a9 2021-06-03 stsp
8853 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8854 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8855 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8856 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8857 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8858 fda8017d 2020-07-23 stsp }
8859 fda8017d 2020-07-23 stsp if (err)
8860 fda8017d 2020-07-23 stsp goto done;
8861 fda8017d 2020-07-23 stsp
8862 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8863 3093e2d7 2023-02-04 op memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8864 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8865 2ac8aa02 2020-11-09 stsp } else {
8866 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8867 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8868 2ac8aa02 2020-11-09 stsp }
8869 fda8017d 2020-07-23 stsp done:
8870 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8871 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8872 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8873 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8874 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8875 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8876 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8877 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8878 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8879 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8880 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8881 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8882 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8883 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8884 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8885 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8886 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8887 67a66647 2021-05-31 stsp free(blob_base_path);
8888 67a66647 2021-05-31 stsp free(parent);
8889 67a66647 2021-05-31 stsp free(base_path);
8890 2e1f37b0 2019-08-08 stsp return err;
8891 2e1f37b0 2019-08-08 stsp }
8892 2e1f37b0 2019-08-08 stsp
8893 ad493afc 2019-08-03 stsp static const struct got_error *
8894 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8895 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8896 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8897 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8898 ad493afc 2019-08-03 stsp {
8899 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8900 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8901 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8902 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8903 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8904 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8905 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8906 9bc94a15 2019-08-03 stsp struct stat sb;
8907 eb81bc23 2022-06-28 tracey int fd1 = -1, fd2 = -1;
8908 ad493afc 2019-08-03 stsp
8909 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8910 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8911 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8912 2e1f37b0 2019-08-08 stsp return NULL;
8913 2e1f37b0 2019-08-08 stsp
8914 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8915 ad493afc 2019-08-03 stsp if (ie == NULL)
8916 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8917 9bc94a15 2019-08-03 stsp
8918 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8919 9bc94a15 2019-08-03 stsp == -1)
8920 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8921 ad493afc 2019-08-03 stsp
8922 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8923 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8924 f69721c3 2019-10-21 stsp if (err)
8925 f69721c3 2019-10-21 stsp goto done;
8926 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8927 f69721c3 2019-10-21 stsp id_str) == -1) {
8928 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8929 eb81bc23 2022-06-28 tracey goto done;
8930 eb81bc23 2022-06-28 tracey }
8931 eb81bc23 2022-06-28 tracey
8932 eb81bc23 2022-06-28 tracey fd1 = got_opentempfd();
8933 eb81bc23 2022-06-28 tracey if (fd1 == -1) {
8934 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8935 eb81bc23 2022-06-28 tracey goto done;
8936 eb81bc23 2022-06-28 tracey }
8937 eb81bc23 2022-06-28 tracey fd2 = got_opentempfd();
8938 eb81bc23 2022-06-28 tracey if (fd2 == -1) {
8939 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
8940 f69721c3 2019-10-21 stsp goto done;
8941 f69721c3 2019-10-21 stsp }
8942 f69721c3 2019-10-21 stsp
8943 ad493afc 2019-08-03 stsp switch (staged_status) {
8944 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8945 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8946 eb81bc23 2022-06-28 tracey blob_id, 8192, fd1);
8947 ad493afc 2019-08-03 stsp if (err)
8948 ad493afc 2019-08-03 stsp break;
8949 ad493afc 2019-08-03 stsp /* fall through */
8950 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8951 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8952 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8953 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8954 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8955 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8956 2e1f37b0 2019-08-08 stsp if (err)
8957 2e1f37b0 2019-08-08 stsp break;
8958 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8959 2e1f37b0 2019-08-08 stsp break;
8960 2e1f37b0 2019-08-08 stsp } else {
8961 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8962 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8963 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8964 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8965 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8966 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8967 2e1f37b0 2019-08-08 stsp }
8968 2e1f37b0 2019-08-08 stsp }
8969 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8970 eb81bc23 2022-06-28 tracey staged_blob_id, 8192, fd2);
8971 ad493afc 2019-08-03 stsp if (err)
8972 ad493afc 2019-08-03 stsp break;
8973 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8974 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8975 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8976 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8977 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8978 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8979 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8980 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8981 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8982 ea7786be 2020-07-23 stsp break;
8983 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8984 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8985 36bf999c 2020-07-23 stsp char *staged_target;
8986 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8987 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8988 36bf999c 2020-07-23 stsp if (err)
8989 36bf999c 2020-07-23 stsp goto done;
8990 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8991 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8992 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8993 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8994 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8995 36bf999c 2020-07-23 stsp free(staged_target);
8996 dfe9fba0 2020-07-23 stsp } else {
8997 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8998 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8999 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
9000 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
9001 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
9002 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
9003 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
9004 dfe9fba0 2020-07-23 stsp }
9005 ea7786be 2020-07-23 stsp break;
9006 ea7786be 2020-07-23 stsp default:
9007 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9008 ea7786be 2020-07-23 stsp break;
9009 ea7786be 2020-07-23 stsp }
9010 2ac8aa02 2020-11-09 stsp if (err == NULL) {
9011 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
9012 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
9013 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9014 2ac8aa02 2020-11-09 stsp }
9015 ad493afc 2019-08-03 stsp break;
9016 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
9017 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
9018 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
9019 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
9020 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
9021 2e1f37b0 2019-08-08 stsp if (err)
9022 2e1f37b0 2019-08-08 stsp break;
9023 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
9024 2e1f37b0 2019-08-08 stsp break;
9025 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
9026 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
9027 2e1f37b0 2019-08-08 stsp break;
9028 2e1f37b0 2019-08-08 stsp }
9029 2e1f37b0 2019-08-08 stsp }
9030 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9031 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
9032 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
9033 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
9034 9bc94a15 2019-08-03 stsp if (err)
9035 9bc94a15 2019-08-03 stsp break;
9036 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
9037 ad493afc 2019-08-03 stsp break;
9038 ad493afc 2019-08-03 stsp }
9039 f69721c3 2019-10-21 stsp done:
9040 ad493afc 2019-08-03 stsp free(ondisk_path);
9041 eb81bc23 2022-06-28 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9042 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9043 ad493afc 2019-08-03 stsp if (blob_base)
9044 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
9045 eb81bc23 2022-06-28 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9046 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
9047 ad493afc 2019-08-03 stsp if (blob_staged)
9048 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
9049 f69721c3 2019-10-21 stsp free(id_str);
9050 f69721c3 2019-10-21 stsp free(label_orig);
9051 ad493afc 2019-08-03 stsp return err;
9052 ad493afc 2019-08-03 stsp }
9053 ad493afc 2019-08-03 stsp
9054 ad493afc 2019-08-03 stsp const struct got_error *
9055 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
9056 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
9057 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
9058 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
9059 ad493afc 2019-08-03 stsp struct got_repository *repo)
9060 ad493afc 2019-08-03 stsp {
9061 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
9062 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9063 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
9064 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
9065 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
9066 ad493afc 2019-08-03 stsp
9067 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
9068 ad493afc 2019-08-03 stsp if (err)
9069 ad493afc 2019-08-03 stsp return err;
9070 ad493afc 2019-08-03 stsp
9071 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9072 ad493afc 2019-08-03 stsp if (err)
9073 ad493afc 2019-08-03 stsp goto done;
9074 ad493afc 2019-08-03 stsp
9075 ad493afc 2019-08-03 stsp upa.worktree = worktree;
9076 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
9077 ad493afc 2019-08-03 stsp upa.repo = repo;
9078 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
9079 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
9080 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
9081 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
9082 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
9083 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
9084 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
9085 ad493afc 2019-08-03 stsp if (err)
9086 ad493afc 2019-08-03 stsp goto done;
9087 ad493afc 2019-08-03 stsp }
9088 ad493afc 2019-08-03 stsp
9089 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
9090 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
9091 ad493afc 2019-08-03 stsp err = sync_err;
9092 ad493afc 2019-08-03 stsp done:
9093 ad493afc 2019-08-03 stsp free(fileindex_path);
9094 ad493afc 2019-08-03 stsp if (fileindex)
9095 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
9096 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
9097 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
9098 ad493afc 2019-08-03 stsp err = unlockerr;
9099 ad493afc 2019-08-03 stsp return err;
9100 ad493afc 2019-08-03 stsp }
9101 b2118c49 2020-07-28 stsp
9102 b2118c49 2020-07-28 stsp struct report_file_info_arg {
9103 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
9104 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
9105 b2118c49 2020-07-28 stsp void *info_arg;
9106 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
9107 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
9108 b2118c49 2020-07-28 stsp void *cancel_arg;
9109 b2118c49 2020-07-28 stsp };
9110 b2118c49 2020-07-28 stsp
9111 b2118c49 2020-07-28 stsp static const struct got_error *
9112 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
9113 b2118c49 2020-07-28 stsp {
9114 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
9115 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9116 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
9117 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9118 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
9119 b2118c49 2020-07-28 stsp int stage;
9120 b2118c49 2020-07-28 stsp
9121 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9122 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
9123 b2118c49 2020-07-28 stsp
9124 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
9125 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9126 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
9127 b2118c49 2020-07-28 stsp break;
9128 b2118c49 2020-07-28 stsp }
9129 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
9130 b2118c49 2020-07-28 stsp return NULL;
9131 b2118c49 2020-07-28 stsp
9132 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
9133 3093e2d7 2023-02-04 op memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9134 dfe0a3d8 2023-02-04 op blob_id.algo = WORKTREE_ALGO(a->worktree);
9135 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
9136 b2118c49 2020-07-28 stsp }
9137 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
9138 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9139 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
9140 3093e2d7 2023-02-04 op memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9141 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
9142 dfe0a3d8 2023-02-04 op staged_blob_id.algo = WORKTREE_ALGO(a->worktree);
9143 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
9144 b2118c49 2020-07-28 stsp }
9145 b2118c49 2020-07-28 stsp
9146 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
9147 3093e2d7 2023-02-04 op memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9148 dfe0a3d8 2023-02-04 op commit_id.algo = WORKTREE_ALGO(a->worktree);
9149 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
9150 b2118c49 2020-07-28 stsp }
9151 b2118c49 2020-07-28 stsp
9152 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9153 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9154 b2118c49 2020-07-28 stsp }
9155 b2118c49 2020-07-28 stsp
9156 b2118c49 2020-07-28 stsp const struct got_error *
9157 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
9158 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
9159 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
9160 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
9161 b2118c49 2020-07-28 stsp
9162 b2118c49 2020-07-28 stsp {
9163 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
9164 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
9165 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
9166 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
9167 b2118c49 2020-07-28 stsp
9168 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
9169 b2118c49 2020-07-28 stsp if (err)
9170 b2118c49 2020-07-28 stsp return err;
9171 b2118c49 2020-07-28 stsp
9172 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
9173 b2118c49 2020-07-28 stsp if (err)
9174 b2118c49 2020-07-28 stsp goto done;
9175 b2118c49 2020-07-28 stsp
9176 b2118c49 2020-07-28 stsp arg.worktree = worktree;
9177 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
9178 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
9179 b2118c49 2020-07-28 stsp arg.paths = paths;
9180 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
9181 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
9182 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9183 b2118c49 2020-07-28 stsp &arg);
9184 b2118c49 2020-07-28 stsp done:
9185 b2118c49 2020-07-28 stsp free(fileindex_path);
9186 b2118c49 2020-07-28 stsp if (fileindex)
9187 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
9188 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
9189 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
9190 b2118c49 2020-07-28 stsp err = unlockerr;
9191 b2118c49 2020-07-28 stsp return err;
9192 b2118c49 2020-07-28 stsp }
9193 78f5ac24 2022-03-19 op
9194 78f5ac24 2022-03-19 op static const struct got_error *
9195 78f5ac24 2022-03-19 op patch_check_path(const char *p, char **path, unsigned char *status,
9196 78f5ac24 2022-03-19 op unsigned char *staged_status, struct got_fileindex *fileindex,
9197 78f5ac24 2022-03-19 op struct got_worktree *worktree, struct got_repository *repo)
9198 78f5ac24 2022-03-19 op {
9199 78f5ac24 2022-03-19 op const struct got_error *err;
9200 78f5ac24 2022-03-19 op struct got_fileindex_entry *ie;
9201 78f5ac24 2022-03-19 op struct stat sb;
9202 78f5ac24 2022-03-19 op char *ondisk_path = NULL;
9203 78f5ac24 2022-03-19 op
9204 78f5ac24 2022-03-19 op err = got_worktree_resolve_path(path, worktree, p);
9205 78f5ac24 2022-03-19 op if (err)
9206 78f5ac24 2022-03-19 op return err;
9207 78f5ac24 2022-03-19 op
9208 78f5ac24 2022-03-19 op if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9209 78f5ac24 2022-03-19 op *path[0] ? "/" : "", *path) == -1)
9210 78f5ac24 2022-03-19 op return got_error_from_errno("asprintf");
9211 78f5ac24 2022-03-19 op
9212 78f5ac24 2022-03-19 op ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9213 78f5ac24 2022-03-19 op if (ie) {
9214 78f5ac24 2022-03-19 op *staged_status = get_staged_status(ie);
9215 a05fb460 2022-04-23 op err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9216 a05fb460 2022-04-23 op repo);
9217 78f5ac24 2022-03-19 op if (err)
9218 78f5ac24 2022-03-19 op goto done;
9219 78f5ac24 2022-03-19 op } else {
9220 78f5ac24 2022-03-19 op *staged_status = GOT_STATUS_NO_CHANGE;
9221 78f5ac24 2022-03-19 op *status = GOT_STATUS_UNVERSIONED;
9222 78f5ac24 2022-03-19 op if (lstat(ondisk_path, &sb) == -1) {
9223 78f5ac24 2022-03-19 op if (errno != ENOENT) {
9224 78f5ac24 2022-03-19 op err = got_error_from_errno2("lstat",
9225 78f5ac24 2022-03-19 op ondisk_path);
9226 78f5ac24 2022-03-19 op goto done;
9227 78f5ac24 2022-03-19 op }
9228 78f5ac24 2022-03-19 op *status = GOT_STATUS_NONEXISTENT;
9229 78f5ac24 2022-03-19 op }
9230 78f5ac24 2022-03-19 op }
9231 78f5ac24 2022-03-19 op
9232 78f5ac24 2022-03-19 op done:
9233 78f5ac24 2022-03-19 op free(ondisk_path);
9234 78f5ac24 2022-03-19 op return err;
9235 78f5ac24 2022-03-19 op }
9236 78f5ac24 2022-03-19 op
9237 78f5ac24 2022-03-19 op static const struct got_error *
9238 78f5ac24 2022-03-19 op patch_can_rm(const char *path, unsigned char status,
9239 78f5ac24 2022-03-19 op unsigned char staged_status)
9240 78f5ac24 2022-03-19 op {
9241 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9242 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9243 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9244 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9245 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY &&
9246 78f5ac24 2022-03-19 op status != GOT_STATUS_MODE_CHANGE)
9247 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9248 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9249 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9250 78f5ac24 2022-03-19 op return NULL;
9251 78f5ac24 2022-03-19 op }
9252 78f5ac24 2022-03-19 op
9253 78f5ac24 2022-03-19 op static const struct got_error *
9254 78f5ac24 2022-03-19 op patch_can_add(const char *path, unsigned char status)
9255 78f5ac24 2022-03-19 op {
9256 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NONEXISTENT)
9257 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9258 78f5ac24 2022-03-19 op return NULL;
9259 78f5ac24 2022-03-19 op }
9260 78f5ac24 2022-03-19 op
9261 78f5ac24 2022-03-19 op static const struct got_error *
9262 78f5ac24 2022-03-19 op patch_can_edit(const char *path, unsigned char status,
9263 78f5ac24 2022-03-19 op unsigned char staged_status)
9264 78f5ac24 2022-03-19 op {
9265 78f5ac24 2022-03-19 op if (status == GOT_STATUS_NONEXISTENT)
9266 78f5ac24 2022-03-19 op return got_error_set_errno(ENOENT, path);
9267 78f5ac24 2022-03-19 op if (status != GOT_STATUS_NO_CHANGE &&
9268 78f5ac24 2022-03-19 op status != GOT_STATUS_ADD &&
9269 78f5ac24 2022-03-19 op status != GOT_STATUS_MODIFY)
9270 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9271 78f5ac24 2022-03-19 op if (staged_status == GOT_STATUS_DELETE)
9272 78f5ac24 2022-03-19 op return got_error_path(path, GOT_ERR_FILE_STATUS);
9273 78f5ac24 2022-03-19 op return NULL;
9274 78f5ac24 2022-03-19 op }
9275 78f5ac24 2022-03-19 op
9276 78f5ac24 2022-03-19 op const struct got_error *
9277 78f5ac24 2022-03-19 op got_worktree_patch_prepare(struct got_fileindex **fileindex,
9278 f2dd7807 2022-05-17 op char **fileindex_path, struct got_worktree *worktree)
9279 78f5ac24 2022-03-19 op {
9280 f2dd7807 2022-05-17 op return open_fileindex(fileindex, fileindex_path, worktree);
9281 78f5ac24 2022-03-19 op }
9282 78f5ac24 2022-03-19 op
9283 78f5ac24 2022-03-19 op const struct got_error *
9284 78f5ac24 2022-03-19 op got_worktree_patch_check_path(const char *old, const char *new,
9285 78f5ac24 2022-03-19 op char **oldpath, char **newpath, struct got_worktree *worktree,
9286 78f5ac24 2022-03-19 op struct got_repository *repo, struct got_fileindex *fileindex)
9287 78f5ac24 2022-03-19 op {
9288 78f5ac24 2022-03-19 op const struct got_error *err = NULL;
9289 78f5ac24 2022-03-19 op int file_renamed = 0;
9290 78f5ac24 2022-03-19 op unsigned char status_old, staged_status_old;
9291 78f5ac24 2022-03-19 op unsigned char status_new, staged_status_new;
9292 78f5ac24 2022-03-19 op
9293 78f5ac24 2022-03-19 op *oldpath = NULL;
9294 78f5ac24 2022-03-19 op *newpath = NULL;
9295 78f5ac24 2022-03-19 op
9296 78f5ac24 2022-03-19 op err = patch_check_path(old != NULL ? old : new, oldpath,
9297 78f5ac24 2022-03-19 op &status_old, &staged_status_old, fileindex, worktree, repo);
9298 78f5ac24 2022-03-19 op if (err)
9299 78f5ac24 2022-03-19 op goto done;
9300 78f5ac24 2022-03-19 op
9301 78f5ac24 2022-03-19 op err = patch_check_path(new != NULL ? new : old, newpath,
9302 78f5ac24 2022-03-19 op &status_new, &staged_status_new, fileindex, worktree, repo);
9303 78f5ac24 2022-03-19 op if (err)
9304 78f5ac24 2022-03-19 op goto done;
9305 78f5ac24 2022-03-19 op
9306 78f5ac24 2022-03-19 op if (old != NULL && new != NULL && strcmp(old, new) != 0)
9307 78f5ac24 2022-03-19 op file_renamed = 1;
9308 78f5ac24 2022-03-19 op
9309 78f5ac24 2022-03-19 op if (old != NULL && new == NULL)
9310 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9311 78f5ac24 2022-03-19 op else if (file_renamed) {
9312 78f5ac24 2022-03-19 op err = patch_can_rm(*oldpath, status_old, staged_status_old);
9313 78f5ac24 2022-03-19 op if (err == NULL)
9314 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9315 78f5ac24 2022-03-19 op } else if (old == NULL)
9316 78f5ac24 2022-03-19 op err = patch_can_add(*newpath, status_new);
9317 78f5ac24 2022-03-19 op else
9318 78f5ac24 2022-03-19 op err = patch_can_edit(*newpath, status_new, staged_status_new);
9319 78f5ac24 2022-03-19 op
9320 78f5ac24 2022-03-19 op done:
9321 78f5ac24 2022-03-19 op if (err) {
9322 78f5ac24 2022-03-19 op free(*oldpath);
9323 78f5ac24 2022-03-19 op *oldpath = NULL;
9324 78f5ac24 2022-03-19 op free(*newpath);
9325 78f5ac24 2022-03-19 op *newpath = NULL;
9326 78f5ac24 2022-03-19 op }
9327 78f5ac24 2022-03-19 op return err;
9328 78f5ac24 2022-03-19 op }
9329 78f5ac24 2022-03-19 op
9330 78f5ac24 2022-03-19 op const struct got_error *
9331 f2dd7807 2022-05-17 op got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9332 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9333 f2dd7807 2022-05-17 op got_worktree_checkout_cb progress_cb, void *progress_arg)
9334 78f5ac24 2022-03-19 op {
9335 f2dd7807 2022-05-17 op struct schedule_addition_args saa;
9336 f2dd7807 2022-05-17 op
9337 f2dd7807 2022-05-17 op memset(&saa, 0, sizeof(saa));
9338 f2dd7807 2022-05-17 op saa.worktree = worktree;
9339 f2dd7807 2022-05-17 op saa.fileindex = fileindex;
9340 f2dd7807 2022-05-17 op saa.progress_cb = progress_cb;
9341 f2dd7807 2022-05-17 op saa.progress_arg = progress_arg;
9342 f2dd7807 2022-05-17 op saa.repo = repo;
9343 f2dd7807 2022-05-17 op
9344 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9345 f2dd7807 2022-05-17 op schedule_addition, &saa, NULL, NULL, 1, 0);
9346 78f5ac24 2022-03-19 op }
9347 f2dd7807 2022-05-17 op
9348 f2dd7807 2022-05-17 op const struct got_error *
9349 f2dd7807 2022-05-17 op got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9350 f2dd7807 2022-05-17 op struct got_worktree *worktree, struct got_fileindex *fileindex,
9351 f2dd7807 2022-05-17 op got_worktree_delete_cb progress_cb, void *progress_arg)
9352 f2dd7807 2022-05-17 op {
9353 f2dd7807 2022-05-17 op struct schedule_deletion_args sda;
9354 f2dd7807 2022-05-17 op
9355 f2dd7807 2022-05-17 op memset(&sda, 0, sizeof(sda));
9356 f2dd7807 2022-05-17 op sda.worktree = worktree;
9357 f2dd7807 2022-05-17 op sda.fileindex = fileindex;
9358 f2dd7807 2022-05-17 op sda.progress_cb = progress_cb;
9359 f2dd7807 2022-05-17 op sda.progress_arg = progress_arg;
9360 f2dd7807 2022-05-17 op sda.repo = repo;
9361 f2dd7807 2022-05-17 op sda.delete_local_mods = 0;
9362 f2dd7807 2022-05-17 op sda.keep_on_disk = 0;
9363 f2dd7807 2022-05-17 op sda.ignore_missing_paths = 0;
9364 f2dd7807 2022-05-17 op sda.status_codes = NULL;
9365 f2dd7807 2022-05-17 op
9366 f2dd7807 2022-05-17 op return worktree_status(worktree, path, fileindex, repo,
9367 f2dd7807 2022-05-17 op schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9368 f2dd7807 2022-05-17 op }
9369 f2dd7807 2022-05-17 op
9370 f2dd7807 2022-05-17 op const struct got_error *
9371 f2dd7807 2022-05-17 op got_worktree_patch_complete(struct got_fileindex *fileindex,
9372 4bcdc895 2022-05-17 op const char *fileindex_path)
9373 f2dd7807 2022-05-17 op {
9374 f2dd7807 2022-05-17 op const struct got_error *err = NULL;
9375 f2dd7807 2022-05-17 op
9376 4bcdc895 2022-05-17 op err = sync_fileindex(fileindex, fileindex_path);
9377 4bcdc895 2022-05-17 op got_fileindex_free(fileindex);
9378 f2dd7807 2022-05-17 op
9379 f2dd7807 2022-05-17 op return err;
9380 f2dd7807 2022-05-17 op }