Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 1d0f4054 2021-06-17 stsp if (repo) {
440 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
441 1d0f4054 2021-06-17 stsp if (err == NULL)
442 1d0f4054 2021-06-17 stsp err = close_err;
443 1d0f4054 2021-06-17 stsp }
444 7ac97322 2018-03-11 stsp free(path_got);
445 056e7441 2018-03-11 stsp free(path_lock);
446 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
447 c442a90d 2019-03-10 stsp free(uuidstr);
448 bd165944 2019-03-10 stsp free(formatstr);
449 6d9d28c3 2018-03-11 stsp if (err) {
450 6d9d28c3 2018-03-11 stsp if (fd != -1)
451 6d9d28c3 2018-03-11 stsp close(fd);
452 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
453 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
454 6d9d28c3 2018-03-11 stsp *worktree = NULL;
455 6d9d28c3 2018-03-11 stsp } else
456 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
457 6d9d28c3 2018-03-11 stsp
458 6d9d28c3 2018-03-11 stsp return err;
459 86c3caaf 2018-03-09 stsp }
460 247140b2 2019-02-05 stsp
461 247140b2 2019-02-05 stsp const struct got_error *
462 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
463 247140b2 2019-02-05 stsp {
464 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
465 aedea96d 2020-10-20 stsp char *worktree_path;
466 86c3caaf 2018-03-09 stsp
467 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
468 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
469 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp for (;;) {
472 aedea96d 2020-10-20 stsp char *parent_path;
473 aedea96d 2020-10-20 stsp
474 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
475 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 aedea96d 2020-10-20 stsp free(worktree_path);
477 247140b2 2019-02-05 stsp return err;
478 aedea96d 2020-10-20 stsp }
479 aedea96d 2020-10-20 stsp if (*worktree) {
480 aedea96d 2020-10-20 stsp free(worktree_path);
481 aedea96d 2020-10-20 stsp return NULL;
482 aedea96d 2020-10-20 stsp }
483 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 aedea96d 2020-10-20 stsp break;
485 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
486 aedea96d 2020-10-20 stsp if (err) {
487 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 aedea96d 2020-10-20 stsp return err;
490 aedea96d 2020-10-20 stsp }
491 aedea96d 2020-10-20 stsp break;
492 aedea96d 2020-10-20 stsp }
493 aedea96d 2020-10-20 stsp free(worktree_path);
494 aedea96d 2020-10-20 stsp worktree_path = parent_path;
495 aedea96d 2020-10-20 stsp }
496 247140b2 2019-02-05 stsp
497 aedea96d 2020-10-20 stsp free(worktree_path);
498 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
499 247140b2 2019-02-05 stsp }
500 247140b2 2019-02-05 stsp
501 3a6ce05a 2019-02-11 stsp const struct got_error *
502 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
503 86c3caaf 2018-03-09 stsp {
504 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
505 60e40e95 2021-01-22 stsp
506 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
507 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
510 a6b21eef 2021-01-21 stsp }
511 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
512 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
513 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
514 60e40e95 2021-01-22 stsp free(worktree->repo_path);
515 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
516 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
517 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
518 60e40e95 2021-01-22 stsp free(worktree->root_path);
519 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
520 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
521 a06ff56f 2021-01-21 naddy free(worktree);
522 3a6ce05a 2019-02-11 stsp return err;
523 86c3caaf 2018-03-09 stsp }
524 86c3caaf 2018-03-09 stsp
525 2fbdb5ae 2018-12-29 stsp const char *
526 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
527 c7f4312f 2019-02-05 stsp {
528 c7f4312f 2019-02-05 stsp return worktree->root_path;
529 c7f4312f 2019-02-05 stsp }
530 c7f4312f 2019-02-05 stsp
531 c7f4312f 2019-02-05 stsp const char *
532 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
533 86c3caaf 2018-03-09 stsp {
534 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
535 49520a32 2018-12-29 stsp }
536 49520a32 2018-12-29 stsp const char *
537 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
538 49520a32 2018-12-29 stsp {
539 f609be2e 2018-12-29 stsp return worktree->path_prefix;
540 e5dc7198 2018-12-29 stsp }
541 e5dc7198 2018-12-29 stsp
542 e5dc7198 2018-12-29 stsp const struct got_error *
543 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 e5dc7198 2018-12-29 stsp const char *path_prefix)
545 e5dc7198 2018-12-29 stsp {
546 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
547 e5dc7198 2018-12-29 stsp
548 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
549 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
551 e5dc7198 2018-12-29 stsp }
552 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
553 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
554 e5dc7198 2018-12-29 stsp free(absprefix);
555 e5dc7198 2018-12-29 stsp return NULL;
556 86c3caaf 2018-03-09 stsp }
557 86c3caaf 2018-03-09 stsp
558 bc70eb79 2019-05-09 stsp const char *
559 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
560 86c3caaf 2018-03-09 stsp {
561 36a38700 2019-05-10 stsp return worktree->head_ref_name;
562 024e9686 2019-05-14 stsp }
563 024e9686 2019-05-14 stsp
564 024e9686 2019-05-14 stsp const struct got_error *
565 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
566 024e9686 2019-05-14 stsp struct got_reference *head_ref)
567 024e9686 2019-05-14 stsp {
568 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
569 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
570 024e9686 2019-05-14 stsp
571 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
573 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
574 024e9686 2019-05-14 stsp path_got = NULL;
575 024e9686 2019-05-14 stsp goto done;
576 024e9686 2019-05-14 stsp }
577 024e9686 2019-05-14 stsp
578 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
579 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
580 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
581 024e9686 2019-05-14 stsp goto done;
582 024e9686 2019-05-14 stsp }
583 024e9686 2019-05-14 stsp
584 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
585 024e9686 2019-05-14 stsp if (err)
586 024e9686 2019-05-14 stsp goto done;
587 024e9686 2019-05-14 stsp
588 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
589 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
590 024e9686 2019-05-14 stsp done:
591 024e9686 2019-05-14 stsp free(path_got);
592 024e9686 2019-05-14 stsp if (err)
593 024e9686 2019-05-14 stsp free(head_ref_name);
594 024e9686 2019-05-14 stsp return err;
595 507dc3bb 2018-12-29 stsp }
596 507dc3bb 2018-12-29 stsp
597 b72f483a 2019-02-05 stsp struct got_object_id *
598 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
599 507dc3bb 2018-12-29 stsp {
600 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
601 86c3caaf 2018-03-09 stsp }
602 86c3caaf 2018-03-09 stsp
603 507dc3bb 2018-12-29 stsp const struct got_error *
604 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
606 507dc3bb 2018-12-29 stsp {
607 507dc3bb 2018-12-29 stsp const struct got_error *err;
608 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
609 507dc3bb 2018-12-29 stsp char *id_str = NULL;
610 507dc3bb 2018-12-29 stsp char *path_got = NULL;
611 507dc3bb 2018-12-29 stsp
612 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
615 507dc3bb 2018-12-29 stsp path_got = NULL;
616 507dc3bb 2018-12-29 stsp goto done;
617 507dc3bb 2018-12-29 stsp }
618 507dc3bb 2018-12-29 stsp
619 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
620 507dc3bb 2018-12-29 stsp if (err)
621 507dc3bb 2018-12-29 stsp return err;
622 507dc3bb 2018-12-29 stsp
623 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
625 507dc3bb 2018-12-29 stsp goto done;
626 507dc3bb 2018-12-29 stsp }
627 507dc3bb 2018-12-29 stsp
628 507dc3bb 2018-12-29 stsp /* Record our base commit. */
629 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
630 507dc3bb 2018-12-29 stsp if (err)
631 507dc3bb 2018-12-29 stsp goto done;
632 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 507dc3bb 2018-12-29 stsp if (err)
634 507dc3bb 2018-12-29 stsp goto done;
635 507dc3bb 2018-12-29 stsp
636 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
637 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
638 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
640 507dc3bb 2018-12-29 stsp goto done;
641 507dc3bb 2018-12-29 stsp }
642 507dc3bb 2018-12-29 stsp done:
643 507dc3bb 2018-12-29 stsp if (obj)
644 507dc3bb 2018-12-29 stsp got_object_close(obj);
645 507dc3bb 2018-12-29 stsp free(id_str);
646 507dc3bb 2018-12-29 stsp free(path_got);
647 507dc3bb 2018-12-29 stsp return err;
648 50b0790e 2020-09-11 stsp }
649 50b0790e 2020-09-11 stsp
650 50b0790e 2020-09-11 stsp const struct got_gotconfig *
651 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
652 50b0790e 2020-09-11 stsp {
653 50b0790e 2020-09-11 stsp return worktree->gotconfig;
654 507dc3bb 2018-12-29 stsp }
655 507dc3bb 2018-12-29 stsp
656 9d31a1d8 2018-03-11 stsp static const struct got_error *
657 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
658 86c3caaf 2018-03-09 stsp {
659 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
662 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
663 86c3caaf 2018-03-09 stsp return NULL;
664 21908da4 2019-01-13 stsp }
665 21908da4 2019-01-13 stsp
666 21908da4 2019-01-13 stsp static const struct got_error *
667 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
668 4a1ddfc2 2019-01-12 stsp {
669 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
670 4a1ddfc2 2019-01-12 stsp char *abspath;
671 4a1ddfc2 2019-01-12 stsp
672 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
674 4a1ddfc2 2019-01-12 stsp
675 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
676 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 ddcd8544 2019-03-11 stsp struct stat sb;
678 ddcd8544 2019-03-11 stsp err = NULL;
679 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
680 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
681 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
682 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
683 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
684 ddcd8544 2019-03-11 stsp }
685 ddcd8544 2019-03-11 stsp }
686 4a1ddfc2 2019-01-12 stsp free(abspath);
687 68c76935 2019-02-19 stsp return err;
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp
690 68c76935 2019-02-19 stsp static const struct got_error *
691 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
692 68c76935 2019-02-19 stsp {
693 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
694 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
695 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
696 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
697 68c76935 2019-02-19 stsp
698 68c76935 2019-02-19 stsp *same = 1;
699 68c76935 2019-02-19 stsp
700 230a42bd 2019-05-11 jcs for (;;) {
701 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
703 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
704 68c76935 2019-02-19 stsp break;
705 68c76935 2019-02-19 stsp }
706 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
708 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
709 68c76935 2019-02-19 stsp break;
710 68c76935 2019-02-19 stsp }
711 68c76935 2019-02-19 stsp if (flen1 == 0) {
712 68c76935 2019-02-19 stsp if (flen2 != 0)
713 68c76935 2019-02-19 stsp *same = 0;
714 68c76935 2019-02-19 stsp break;
715 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
716 68c76935 2019-02-19 stsp if (flen1 != 0)
717 68c76935 2019-02-19 stsp *same = 0;
718 68c76935 2019-02-19 stsp break;
719 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
720 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 68c76935 2019-02-19 stsp *same = 0;
722 68c76935 2019-02-19 stsp break;
723 68c76935 2019-02-19 stsp }
724 68c76935 2019-02-19 stsp } else {
725 68c76935 2019-02-19 stsp *same = 0;
726 68c76935 2019-02-19 stsp break;
727 68c76935 2019-02-19 stsp }
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp return err;
731 68c76935 2019-02-19 stsp }
732 68c76935 2019-02-19 stsp
733 68c76935 2019-02-19 stsp static const struct got_error *
734 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
735 68c76935 2019-02-19 stsp {
736 68c76935 2019-02-19 stsp struct stat sb;
737 68c76935 2019-02-19 stsp size_t size1, size2;
738 68c76935 2019-02-19 stsp
739 68c76935 2019-02-19 stsp *same = 1;
740 68c76935 2019-02-19 stsp
741 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
742 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
743 68c76935 2019-02-19 stsp size1 = sb.st_size;
744 68c76935 2019-02-19 stsp
745 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
746 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
747 68c76935 2019-02-19 stsp size2 = sb.st_size;
748 68c76935 2019-02-19 stsp
749 68c76935 2019-02-19 stsp if (size1 != size2) {
750 68c76935 2019-02-19 stsp *same = 0;
751 68c76935 2019-02-19 stsp return NULL;
752 68c76935 2019-02-19 stsp }
753 68c76935 2019-02-19 stsp
754 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
755 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
756 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
757 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
758 68c76935 2019-02-19 stsp
759 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
760 6353ad76 2019-02-08 stsp }
761 6353ad76 2019-02-08 stsp
762 6353ad76 2019-02-08 stsp /*
763 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
764 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
765 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
766 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
767 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
768 6353ad76 2019-02-08 stsp */
769 6353ad76 2019-02-08 stsp static const struct got_error *
770 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
773 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
776 6353ad76 2019-02-08 stsp {
777 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
778 6353ad76 2019-02-08 stsp int merged_fd = -1;
779 db590691 2021-06-02 stsp FILE *f_merged = NULL;
780 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
781 234035bc 2019-06-01 stsp int overlapcnt = 0;
782 3524bbf9 2020-10-20 stsp char *parent = NULL;
783 6353ad76 2019-02-08 stsp
784 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
785 234035bc 2019-06-01 stsp
786 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
787 3524bbf9 2020-10-20 stsp if (err)
788 3524bbf9 2020-10-20 stsp return err;
789 af54ae4a 2019-02-19 stsp
790 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
792 3524bbf9 2020-10-20 stsp goto done;
793 3524bbf9 2020-10-20 stsp }
794 af54ae4a 2019-02-19 stsp
795 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 6353ad76 2019-02-08 stsp if (err)
797 6353ad76 2019-02-08 stsp goto done;
798 3b9f0f87 2020-07-23 stsp
799 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 6353ad76 2019-02-08 stsp if (err)
802 6353ad76 2019-02-08 stsp goto done;
803 6353ad76 2019-02-08 stsp
804 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
805 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 1ee397ad 2019-07-12 stsp if (err)
807 1ee397ad 2019-07-12 stsp goto done;
808 6353ad76 2019-02-08 stsp
809 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
811 816dc654 2019-02-16 stsp goto done;
812 68c76935 2019-02-19 stsp }
813 68c76935 2019-02-19 stsp
814 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
815 db590691 2021-06-02 stsp if (f_merged == NULL) {
816 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
817 db590691 2021-06-02 stsp goto done;
818 db590691 2021-06-02 stsp }
819 db590691 2021-06-02 stsp merged_fd = -1;
820 db590691 2021-06-02 stsp
821 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
822 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
823 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
824 db590691 2021-06-02 stsp f_merged);
825 68c76935 2019-02-19 stsp if (err)
826 68c76935 2019-02-19 stsp goto done;
827 816dc654 2019-02-16 stsp }
828 6353ad76 2019-02-08 stsp
829 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
830 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
831 70a0c8ec 2019-02-20 stsp goto done;
832 70a0c8ec 2019-02-20 stsp }
833 70a0c8ec 2019-02-20 stsp
834 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
835 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
836 230a42bd 2019-05-11 jcs ondisk_path);
837 6353ad76 2019-02-08 stsp goto done;
838 6353ad76 2019-02-08 stsp }
839 6353ad76 2019-02-08 stsp done:
840 fdcb7daf 2019-12-15 stsp if (err) {
841 fdcb7daf 2019-12-15 stsp if (merged_path)
842 fdcb7daf 2019-12-15 stsp unlink(merged_path);
843 3b9f0f87 2020-07-23 stsp }
844 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
846 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
848 6353ad76 2019-02-08 stsp free(merged_path);
849 af54ae4a 2019-02-19 stsp free(base_path);
850 3524bbf9 2020-10-20 stsp free(parent);
851 af57b12a 2020-07-23 stsp return err;
852 af57b12a 2020-07-23 stsp }
853 af57b12a 2020-07-23 stsp
854 af57b12a 2020-07-23 stsp static const struct got_error *
855 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
856 af57b12a 2020-07-23 stsp size_t target_len)
857 af57b12a 2020-07-23 stsp {
858 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
859 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
860 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
861 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
862 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
863 af57b12a 2020-07-23 stsp ondisk_path);
864 af57b12a 2020-07-23 stsp return NULL;
865 af57b12a 2020-07-23 stsp }
866 af57b12a 2020-07-23 stsp
867 af57b12a 2020-07-23 stsp /*
868 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
870 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
871 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
872 993e2a1b 2020-07-23 stsp *
873 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
874 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
875 993e2a1b 2020-07-23 stsp *
876 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
877 993e2a1b 2020-07-23 stsp * has deleted this symlink.
878 11cc08c1 2020-07-23 stsp */
879 11cc08c1 2020-07-23 stsp static const struct got_error *
880 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
881 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
883 11cc08c1 2020-07-23 stsp {
884 11cc08c1 2020-07-23 stsp const struct got_error *err;
885 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 11cc08c1 2020-07-23 stsp FILE *f = NULL;
887 11cc08c1 2020-07-23 stsp
888 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
889 11cc08c1 2020-07-23 stsp if (err)
890 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
891 11cc08c1 2020-07-23 stsp
892 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
893 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
895 11cc08c1 2020-07-23 stsp goto done;
896 11cc08c1 2020-07-23 stsp }
897 11cc08c1 2020-07-23 stsp
898 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 11cc08c1 2020-07-23 stsp if (err)
900 3818e3c4 2020-11-01 naddy goto done;
901 3818e3c4 2020-11-01 naddy
902 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
904 11cc08c1 2020-07-23 stsp goto done;
905 3818e3c4 2020-11-01 naddy }
906 11cc08c1 2020-07-23 stsp
907 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
910 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
911 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
912 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
913 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
914 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
915 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
917 11cc08c1 2020-07-23 stsp goto done;
918 11cc08c1 2020-07-23 stsp }
919 11cc08c1 2020-07-23 stsp
920 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
921 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
922 11cc08c1 2020-07-23 stsp goto done;
923 11cc08c1 2020-07-23 stsp }
924 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
925 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
926 11cc08c1 2020-07-23 stsp goto done;
927 11cc08c1 2020-07-23 stsp }
928 11cc08c1 2020-07-23 stsp done:
929 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
930 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
931 11cc08c1 2020-07-23 stsp free(path);
932 11cc08c1 2020-07-23 stsp free(id_str);
933 11cc08c1 2020-07-23 stsp free(label_deriv);
934 11cc08c1 2020-07-23 stsp return err;
935 11cc08c1 2020-07-23 stsp }
936 d219f183 2020-07-23 stsp
937 d219f183 2020-07-23 stsp /* forward declaration */
938 d219f183 2020-07-23 stsp static const struct got_error *
939 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
941 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
942 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
943 11cc08c1 2020-07-23 stsp
944 11cc08c1 2020-07-23 stsp /*
945 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
946 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
947 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
948 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
949 af57b12a 2020-07-23 stsp */
950 af57b12a 2020-07-23 stsp static const struct got_error *
951 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
952 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
953 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
954 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
956 af57b12a 2020-07-23 stsp {
957 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
958 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
959 af57b12a 2020-07-23 stsp struct stat sb;
960 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
961 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
962 11cc08c1 2020-07-23 stsp int have_local_change = 0;
963 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
964 af57b12a 2020-07-23 stsp
965 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
966 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
967 af57b12a 2020-07-23 stsp
968 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
969 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
970 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
971 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
972 af57b12a 2020-07-23 stsp ondisk_path);
973 af57b12a 2020-07-23 stsp goto done;
974 af57b12a 2020-07-23 stsp }
975 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
976 af57b12a 2020-07-23 stsp
977 526a746f 2020-07-23 stsp if (blob_orig) {
978 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 526a746f 2020-07-23 stsp if (err)
980 526a746f 2020-07-23 stsp goto done;
981 526a746f 2020-07-23 stsp }
982 af57b12a 2020-07-23 stsp
983 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
984 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
985 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 11cc08c1 2020-07-23 stsp have_local_change = 1;
987 11cc08c1 2020-07-23 stsp
988 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
993 11cc08c1 2020-07-23 stsp
994 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
995 11cc08c1 2020-07-23 stsp if (ancestor_target) {
996 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
997 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 11cc08c1 2020-07-23 stsp path);
999 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1000 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1002 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 11cc08c1 2020-07-23 stsp path);
1004 11cc08c1 2020-07-23 stsp } else {
1005 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1006 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1007 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1008 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1009 11cc08c1 2020-07-23 stsp if (err)
1010 11cc08c1 2020-07-23 stsp goto done;
1011 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 11cc08c1 2020-07-23 stsp path);
1013 11cc08c1 2020-07-23 stsp }
1014 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1015 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1016 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1017 af57b12a 2020-07-23 stsp strlen(deriv_target));
1018 af57b12a 2020-07-23 stsp if (err)
1019 af57b12a 2020-07-23 stsp goto done;
1020 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1022 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1023 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1025 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 ea7786be 2020-07-23 stsp path);
1027 ea7786be 2020-07-23 stsp } else {
1028 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1031 ea7786be 2020-07-23 stsp if (err)
1032 ea7786be 2020-07-23 stsp goto done;
1033 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 ea7786be 2020-07-23 stsp path);
1035 ea7786be 2020-07-23 stsp }
1036 6353ad76 2019-02-08 stsp }
1037 11cc08c1 2020-07-23 stsp
1038 af57b12a 2020-07-23 stsp done:
1039 af57b12a 2020-07-23 stsp free(ancestor_target);
1040 eec2f5a9 2021-06-03 stsp return err;
1041 eec2f5a9 2021-06-03 stsp }
1042 eec2f5a9 2021-06-03 stsp
1043 eec2f5a9 2021-06-03 stsp static const struct got_error *
1044 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1045 eec2f5a9 2021-06-03 stsp {
1046 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1047 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1048 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1049 eec2f5a9 2021-06-03 stsp size_t n;
1050 eec2f5a9 2021-06-03 stsp FILE *f;
1051 eec2f5a9 2021-06-03 stsp
1052 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1053 eec2f5a9 2021-06-03 stsp
1054 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1055 eec2f5a9 2021-06-03 stsp if (f == NULL)
1056 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1057 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1059 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp }
1062 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1063 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1064 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1065 eec2f5a9 2021-06-03 stsp goto done;
1066 eec2f5a9 2021-06-03 stsp }
1067 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1069 eec2f5a9 2021-06-03 stsp goto done;
1070 eec2f5a9 2021-06-03 stsp }
1071 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1072 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1073 eec2f5a9 2021-06-03 stsp goto done;
1074 eec2f5a9 2021-06-03 stsp }
1075 eec2f5a9 2021-06-03 stsp done:
1076 eec2f5a9 2021-06-03 stsp if (err)
1077 eec2f5a9 2021-06-03 stsp fclose(f);
1078 eec2f5a9 2021-06-03 stsp else
1079 eec2f5a9 2021-06-03 stsp *outfile = f;
1080 14c901f1 2019-08-08 stsp return err;
1081 14c901f1 2019-08-08 stsp }
1082 14c901f1 2019-08-08 stsp
1083 14c901f1 2019-08-08 stsp /*
1084 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1086 14c901f1 2019-08-08 stsp * acts as the second derived version.
1087 14c901f1 2019-08-08 stsp */
1088 14c901f1 2019-08-08 stsp static const struct got_error *
1089 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1091 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1092 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1093 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1095 14c901f1 2019-08-08 stsp {
1096 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1097 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1099 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1101 14c901f1 2019-08-08 stsp
1102 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1103 14c901f1 2019-08-08 stsp
1104 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1105 ed6b5030 2020-10-20 stsp if (err)
1106 ed6b5030 2020-10-20 stsp return err;
1107 14c901f1 2019-08-08 stsp
1108 67a66647 2021-05-31 stsp if (blob_orig) {
1109 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 67a66647 2021-05-31 stsp parent) == -1) {
1111 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1112 67a66647 2021-05-31 stsp base_path = NULL;
1113 67a66647 2021-05-31 stsp goto done;
1114 67a66647 2021-05-31 stsp }
1115 67a66647 2021-05-31 stsp
1116 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 67a66647 2021-05-31 stsp if (err)
1118 67a66647 2021-05-31 stsp goto done;
1119 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 67a66647 2021-05-31 stsp blob_orig);
1121 67a66647 2021-05-31 stsp if (err)
1122 67a66647 2021-05-31 stsp goto done;
1123 67a66647 2021-05-31 stsp free(base_path);
1124 db590691 2021-06-02 stsp } else {
1125 db590691 2021-06-02 stsp /*
1126 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1127 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1128 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1129 db590691 2021-06-02 stsp */
1130 db590691 2021-06-02 stsp f_orig = got_opentemp();
1131 db590691 2021-06-02 stsp if (f_orig == NULL) {
1132 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1133 db590691 2021-06-02 stsp goto done;
1134 db590691 2021-06-02 stsp }
1135 67a66647 2021-05-31 stsp }
1136 67a66647 2021-05-31 stsp
1137 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1139 14c901f1 2019-08-08 stsp base_path = NULL;
1140 14c901f1 2019-08-08 stsp goto done;
1141 14c901f1 2019-08-08 stsp }
1142 14c901f1 2019-08-08 stsp
1143 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 14c901f1 2019-08-08 stsp if (err)
1145 14c901f1 2019-08-08 stsp goto done;
1146 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 14c901f1 2019-08-08 stsp blob_deriv);
1148 14c901f1 2019-08-08 stsp if (err)
1149 14c901f1 2019-08-08 stsp goto done;
1150 14c901f1 2019-08-08 stsp
1151 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 14c901f1 2019-08-08 stsp if (err)
1153 14c901f1 2019-08-08 stsp goto done;
1154 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1155 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1157 14c901f1 2019-08-08 stsp goto done;
1158 eec2f5a9 2021-06-03 stsp }
1159 eec2f5a9 2021-06-03 stsp
1160 eec2f5a9 2021-06-03 stsp /*
1161 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1162 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1163 eec2f5a9 2021-06-03 stsp */
1164 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1165 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 eec2f5a9 2021-06-03 stsp if (err)
1167 eec2f5a9 2021-06-03 stsp goto done;
1168 eec2f5a9 2021-06-03 stsp } else {
1169 eec2f5a9 2021-06-03 stsp int fd;
1170 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1172 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1173 eec2f5a9 2021-06-03 stsp goto done;
1174 eec2f5a9 2021-06-03 stsp }
1175 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1176 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1177 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1178 eec2f5a9 2021-06-03 stsp close(fd);
1179 eec2f5a9 2021-06-03 stsp goto done;
1180 eec2f5a9 2021-06-03 stsp }
1181 14c901f1 2019-08-08 stsp }
1182 14c901f1 2019-08-08 stsp
1183 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 14c901f1 2019-08-08 stsp done:
1187 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1189 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1191 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1193 14c901f1 2019-08-08 stsp free(base_path);
1194 67a66647 2021-05-31 stsp if (blob_orig_path) {
1195 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1196 67a66647 2021-05-31 stsp free(blob_orig_path);
1197 67a66647 2021-05-31 stsp }
1198 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1199 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1200 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1201 14c901f1 2019-08-08 stsp }
1202 6353ad76 2019-02-08 stsp free(id_str);
1203 818c7501 2019-07-11 stsp free(label_deriv);
1204 ed6b5030 2020-10-20 stsp free(parent);
1205 4a1ddfc2 2019-01-12 stsp return err;
1206 4a1ddfc2 2019-01-12 stsp }
1207 4a1ddfc2 2019-01-12 stsp
1208 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1209 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1212 13d9040b 2019-03-27 stsp {
1213 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1214 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1215 13d9040b 2019-03-27 stsp
1216 65b05cec 2020-07-23 stsp *new_iep = NULL;
1217 65b05cec 2020-07-23 stsp
1218 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1219 054041d0 2020-06-24 stsp if (err)
1220 054041d0 2020-06-24 stsp return err;
1221 054041d0 2020-06-24 stsp
1222 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1224 054041d0 2020-06-24 stsp if (err)
1225 054041d0 2020-06-24 stsp goto done;
1226 054041d0 2020-06-24 stsp
1227 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1228 054041d0 2020-06-24 stsp done:
1229 054041d0 2020-06-24 stsp if (err)
1230 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1231 65b05cec 2020-07-23 stsp else
1232 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1233 13d9040b 2019-03-27 stsp return err;
1234 1ebedb77 2019-10-19 stsp }
1235 1ebedb77 2019-10-19 stsp
1236 1ebedb77 2019-10-19 stsp static mode_t
1237 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1238 1ebedb77 2019-10-19 stsp {
1239 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1240 1ebedb77 2019-10-19 stsp
1241 1ebedb77 2019-10-19 stsp if (executable) {
1242 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1243 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1244 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1245 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1246 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1247 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1248 1ebedb77 2019-10-19 stsp }
1249 1ebedb77 2019-10-19 stsp
1250 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1251 13d9040b 2019-03-27 stsp }
1252 13d9040b 2019-03-27 stsp
1253 8ba819a3 2020-07-23 stsp /* forward declaration */
1254 13d9040b 2019-03-27 stsp static const struct got_error *
1255 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1257 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1258 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1259 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1260 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1261 8ba819a3 2020-07-23 stsp
1262 5a1fbc73 2020-07-23 stsp /*
1263 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1264 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1265 5a1fbc73 2020-07-23 stsp */
1266 8ba819a3 2020-07-23 stsp static const struct got_error *
1267 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1269 5a1fbc73 2020-07-23 stsp {
1270 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1271 5a1fbc73 2020-07-23 stsp ssize_t elen;
1272 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1273 5a1fbc73 2020-07-23 stsp int fd;
1274 5a1fbc73 2020-07-23 stsp
1275 c6e8a826 2021-04-05 stsp *did_something = 0;
1276 c6e8a826 2021-04-05 stsp
1277 5a1fbc73 2020-07-23 stsp /*
1278 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1279 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1280 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1281 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1282 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1283 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1284 5a1fbc73 2020-07-23 stsp */
1285 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1287 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
1288 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1289 5a1fbc73 2020-07-23 stsp
1290 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1291 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 5a1fbc73 2020-07-23 stsp if (elen == -1)
1293 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1294 5a1fbc73 2020-07-23 stsp
1295 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1296 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1297 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1298 5a1fbc73 2020-07-23 stsp }
1299 5a1fbc73 2020-07-23 stsp
1300 c6e8a826 2021-04-05 stsp *did_something = 1;
1301 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1302 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1303 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1304 5a1fbc73 2020-07-23 stsp return err;
1305 3c1ec782 2020-07-23 stsp }
1306 3c1ec782 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp static const struct got_error *
1308 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1310 3c1ec782 2020-07-23 stsp {
1311 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1312 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1313 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1314 3c1ec782 2020-07-23 stsp
1315 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1316 3c1ec782 2020-07-23 stsp
1317 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1318 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1319 3c1ec782 2020-07-23 stsp return NULL;
1320 3c1ec782 2020-07-23 stsp }
1321 3c1ec782 2020-07-23 stsp
1322 3c1ec782 2020-07-23 stsp /*
1323 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1324 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1325 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1326 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1327 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1328 3c1ec782 2020-07-23 stsp */
1329 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1330 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1331 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1332 ce031e9e 2020-10-20 stsp if (err)
1333 ce031e9e 2020-10-20 stsp return err;
1334 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 ce031e9e 2020-10-20 stsp free(parent);
1336 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1337 ce031e9e 2020-10-20 stsp }
1338 ce031e9e 2020-10-20 stsp free(parent);
1339 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1340 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 3c1ec782 2020-07-23 stsp free(abspath);
1342 3c1ec782 2020-07-23 stsp return err;
1343 3c1ec782 2020-07-23 stsp }
1344 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 3c1ec782 2020-07-23 stsp free(abspath);
1346 3c1ec782 2020-07-23 stsp if (err)
1347 3c1ec782 2020-07-23 stsp return err;
1348 3c1ec782 2020-07-23 stsp } else {
1349 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 3c1ec782 2020-07-23 stsp if (err)
1351 3c1ec782 2020-07-23 stsp return err;
1352 3c1ec782 2020-07-23 stsp }
1353 3c1ec782 2020-07-23 stsp
1354 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1355 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1357 3c1ec782 2020-07-23 stsp return NULL;
1358 3c1ec782 2020-07-23 stsp }
1359 3c1ec782 2020-07-23 stsp
1360 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1361 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1363 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1364 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1366 3c1ec782 2020-07-23 stsp
1367 3c1ec782 2020-07-23 stsp free(path_got);
1368 3c1ec782 2020-07-23 stsp return NULL;
1369 5a1fbc73 2020-07-23 stsp }
1370 5a1fbc73 2020-07-23 stsp
1371 5a1fbc73 2020-07-23 stsp static const struct got_error *
1372 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1375 5267b9e4 2021-09-26 stsp int path_is_unversioned, int allow_bad_symlinks,
1376 5267b9e4 2021-09-26 stsp struct got_repository *repo,
1377 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1378 9d31a1d8 2018-03-11 stsp {
1379 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1380 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1381 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1382 906c123b 2020-07-23 stsp char *path_got = NULL;
1383 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1384 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1385 8ba819a3 2020-07-23 stsp
1386 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1387 2e1fa222 2020-07-23 stsp
1388 8ba819a3 2020-07-23 stsp /*
1389 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1390 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1391 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1392 8ba819a3 2020-07-23 stsp * in the blob object.
1393 8ba819a3 2020-07-23 stsp */
1394 8ba819a3 2020-07-23 stsp do {
1395 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1396 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1397 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1398 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1399 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1400 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1401 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1403 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1404 3b9f0f87 2020-07-23 stsp progress_arg);
1405 8ba819a3 2020-07-23 stsp }
1406 8ba819a3 2020-07-23 stsp if (len > 0) {
1407 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1408 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1409 8ba819a3 2020-07-23 stsp len - hdrlen);
1410 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1411 8ba819a3 2020-07-23 stsp hdrlen = 0;
1412 8ba819a3 2020-07-23 stsp }
1413 8ba819a3 2020-07-23 stsp } while (len != 0);
1414 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1415 8ba819a3 2020-07-23 stsp
1416 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1417 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1418 3c1ec782 2020-07-23 stsp if (err)
1419 3c1ec782 2020-07-23 stsp return err;
1420 8ba819a3 2020-07-23 stsp
1421 5267b9e4 2021-09-26 stsp if (*is_bad_symlink && !allow_bad_symlinks) {
1422 906c123b 2020-07-23 stsp /* install as a regular file */
1423 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1424 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1425 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1426 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1427 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1428 906c123b 2020-07-23 stsp goto done;
1429 906c123b 2020-07-23 stsp }
1430 906c123b 2020-07-23 stsp
1431 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1432 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1433 c6e8a826 2021-04-05 stsp int symlink_replaced;
1434 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1435 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1436 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1437 c90c8ce3 2020-07-23 stsp goto done;
1438 c90c8ce3 2020-07-23 stsp }
1439 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1440 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1441 5a1fbc73 2020-07-23 stsp if (err)
1442 f35fa46a 2020-07-23 stsp goto done;
1443 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1444 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1445 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1446 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1447 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1448 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1449 c6e8a826 2021-04-05 stsp } else {
1450 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1451 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1452 c6e8a826 2021-04-05 stsp }
1453 f35fa46a 2020-07-23 stsp }
1454 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1455 f35fa46a 2020-07-23 stsp }
1456 f35fa46a 2020-07-23 stsp
1457 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1458 f4994adc 2020-10-20 stsp char *parent;
1459 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1460 f4994adc 2020-10-20 stsp if (err)
1461 f4994adc 2020-10-20 stsp goto done;
1462 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1463 f4994adc 2020-10-20 stsp free(parent);
1464 f35fa46a 2020-07-23 stsp if (err)
1465 f35fa46a 2020-07-23 stsp goto done;
1466 f35fa46a 2020-07-23 stsp /*
1467 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1468 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1469 f35fa46a 2020-07-23 stsp */
1470 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1471 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1472 f35fa46a 2020-07-23 stsp goto done;
1473 f35fa46a 2020-07-23 stsp }
1474 f35fa46a 2020-07-23 stsp }
1475 f35fa46a 2020-07-23 stsp
1476 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1477 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1478 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1479 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1480 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1481 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1482 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1483 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1484 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1485 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1486 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1487 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1488 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1489 8ba819a3 2020-07-23 stsp } else {
1490 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1491 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1492 8ba819a3 2020-07-23 stsp }
1493 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1494 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1495 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1496 8ba819a3 2020-07-23 stsp done:
1497 906c123b 2020-07-23 stsp free(path_got);
1498 8ba819a3 2020-07-23 stsp return err;
1499 8ba819a3 2020-07-23 stsp }
1500 8ba819a3 2020-07-23 stsp
1501 8ba819a3 2020-07-23 stsp static const struct got_error *
1502 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1503 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1504 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1505 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1506 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1507 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1508 8ba819a3 2020-07-23 stsp {
1509 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1510 507dc3bb 2018-12-29 stsp int fd = -1;
1511 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1512 507dc3bb 2018-12-29 stsp int update = 0;
1513 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1514 8ba819a3 2020-07-23 stsp
1515 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1516 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1517 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1518 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1519 f5375317 2020-10-20 stsp char *parent;
1520 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1521 f5375317 2020-10-20 stsp if (err)
1522 f5375317 2020-10-20 stsp return err;
1523 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1524 f5375317 2020-10-20 stsp free(parent);
1525 21908da4 2019-01-13 stsp if (err)
1526 21908da4 2019-01-13 stsp return err;
1527 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1528 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1529 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1530 21908da4 2019-01-13 stsp if (fd == -1)
1531 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1532 230a42bd 2019-05-11 jcs ondisk_path);
1533 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1534 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1535 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1536 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1537 3b9f0f87 2020-07-23 stsp goto done;
1538 3b9f0f87 2020-07-23 stsp }
1539 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1540 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1541 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1542 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1543 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1544 507dc3bb 2018-12-29 stsp goto done;
1545 d70b8e30 2018-12-27 stsp } else {
1546 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1547 507dc3bb 2018-12-29 stsp ondisk_path);
1548 507dc3bb 2018-12-29 stsp if (err)
1549 507dc3bb 2018-12-29 stsp goto done;
1550 507dc3bb 2018-12-29 stsp update = 1;
1551 9d31a1d8 2018-03-11 stsp }
1552 507dc3bb 2018-12-29 stsp } else
1553 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1554 9d31a1d8 2018-03-11 stsp }
1555 9d31a1d8 2018-03-11 stsp
1556 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1557 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1558 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1559 3818e3c4 2020-11-01 naddy goto done;
1560 3818e3c4 2020-11-01 naddy }
1561 3818e3c4 2020-11-01 naddy
1562 bd6aa359 2020-07-23 stsp if (progress_cb) {
1563 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1564 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1565 bd6aa359 2020-07-23 stsp path);
1566 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1567 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1568 bd6aa359 2020-07-23 stsp path);
1569 bd6aa359 2020-07-23 stsp else
1570 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1571 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1572 bd6aa359 2020-07-23 stsp if (err)
1573 bd6aa359 2020-07-23 stsp goto done;
1574 bd6aa359 2020-07-23 stsp }
1575 d7b62c98 2018-12-27 stsp
1576 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1577 9d31a1d8 2018-03-11 stsp do {
1578 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1580 9d31a1d8 2018-03-11 stsp if (err)
1581 9d31a1d8 2018-03-11 stsp break;
1582 9d31a1d8 2018-03-11 stsp if (len > 0) {
1583 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1584 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1585 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1586 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1587 b87c6f83 2018-12-24 stsp goto done;
1588 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1589 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1590 b87c6f83 2018-12-24 stsp goto done;
1591 9d31a1d8 2018-03-11 stsp }
1592 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1593 9d31a1d8 2018-03-11 stsp }
1594 9d31a1d8 2018-03-11 stsp } while (len != 0);
1595 9d31a1d8 2018-03-11 stsp
1596 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1597 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1598 816dc654 2019-02-16 stsp goto done;
1599 816dc654 2019-02-16 stsp }
1600 9d31a1d8 2018-03-11 stsp
1601 507dc3bb 2018-12-29 stsp if (update) {
1602 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1603 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1604 f6d8c0ac 2020-11-09 stsp goto done;
1605 f6d8c0ac 2020-11-09 stsp }
1606 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1607 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1608 230a42bd 2019-05-11 jcs ondisk_path);
1609 68ed9ba5 2019-02-10 stsp goto done;
1610 68ed9ba5 2019-02-10 stsp }
1611 63df146d 2020-11-09 stsp free(tmppath);
1612 63df146d 2020-11-09 stsp tmppath = NULL;
1613 507dc3bb 2018-12-29 stsp }
1614 507dc3bb 2018-12-29 stsp
1615 9d31a1d8 2018-03-11 stsp done:
1616 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1617 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1618 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1619 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1620 507dc3bb 2018-12-29 stsp free(tmppath);
1621 6353ad76 2019-02-08 stsp return err;
1622 6353ad76 2019-02-08 stsp }
1623 6353ad76 2019-02-08 stsp
1624 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1625 6353ad76 2019-02-08 stsp static const struct got_error *
1626 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1627 7154f6ce 2019-03-27 stsp {
1628 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1629 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1630 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1631 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1632 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1633 7154f6ce 2019-03-27 stsp };
1634 7154f6ce 2019-03-27 stsp int i = 0;
1635 9bdd68dd 2021-01-02 naddy char *line = NULL;
1636 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1637 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1638 7154f6ce 2019-03-27 stsp
1639 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1640 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1641 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1642 7154f6ce 2019-03-27 stsp if (feof(f))
1643 7154f6ce 2019-03-27 stsp break;
1644 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1645 7154f6ce 2019-03-27 stsp break;
1646 7154f6ce 2019-03-27 stsp }
1647 7154f6ce 2019-03-27 stsp
1648 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1649 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1650 19332e6d 2019-05-13 stsp == 0)
1651 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1652 7154f6ce 2019-03-27 stsp else
1653 7154f6ce 2019-03-27 stsp i++;
1654 7154f6ce 2019-03-27 stsp }
1655 7154f6ce 2019-03-27 stsp }
1656 9bdd68dd 2021-01-02 naddy free(line);
1657 7154f6ce 2019-03-27 stsp
1658 7154f6ce 2019-03-27 stsp return err;
1659 e2b1e152 2019-07-13 stsp }
1660 e2b1e152 2019-07-13 stsp
1661 e2b1e152 2019-07-13 stsp static int
1662 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1663 1ebedb77 2019-10-19 stsp {
1664 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1665 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1666 1ebedb77 2019-10-19 stsp }
1667 1ebedb77 2019-10-19 stsp
1668 1ebedb77 2019-10-19 stsp static int
1669 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1670 e2b1e152 2019-07-13 stsp {
1671 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1672 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1673 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1674 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1675 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1676 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1677 c363b2c1 2019-08-03 stsp }
1678 c363b2c1 2019-08-03 stsp
1679 c363b2c1 2019-08-03 stsp static unsigned char
1680 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1681 c363b2c1 2019-08-03 stsp {
1682 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1683 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1684 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1685 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1686 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1687 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1688 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1689 c363b2c1 2019-08-03 stsp default:
1690 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1691 a919d5c4 2020-07-23 stsp }
1692 a919d5c4 2020-07-23 stsp }
1693 a919d5c4 2020-07-23 stsp
1694 a919d5c4 2020-07-23 stsp static const struct got_error *
1695 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1696 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1697 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1698 a919d5c4 2020-07-23 stsp {
1699 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1700 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1701 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1702 a919d5c4 2020-07-23 stsp ssize_t elen;
1703 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1704 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1705 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1706 a919d5c4 2020-07-23 stsp
1707 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1708 a919d5c4 2020-07-23 stsp
1709 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1710 a919d5c4 2020-07-23 stsp do {
1711 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1712 a919d5c4 2020-07-23 stsp if (err)
1713 a919d5c4 2020-07-23 stsp return err;
1714 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1715 a919d5c4 2020-07-23 stsp /*
1716 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1717 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1718 a919d5c4 2020-07-23 stsp */
1719 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1720 a919d5c4 2020-07-23 stsp }
1721 a919d5c4 2020-07-23 stsp if (len > 0) {
1722 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1723 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1724 a919d5c4 2020-07-23 stsp len - hdrlen);
1725 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1726 a919d5c4 2020-07-23 stsp hdrlen = 0;
1727 a919d5c4 2020-07-23 stsp }
1728 a919d5c4 2020-07-23 stsp } while (len != 0);
1729 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1730 a919d5c4 2020-07-23 stsp
1731 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1732 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1733 a919d5c4 2020-07-23 stsp if (elen == -1)
1734 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1735 a919d5c4 2020-07-23 stsp } else {
1736 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1737 a919d5c4 2020-07-23 stsp if (elen == -1)
1738 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1739 c363b2c1 2019-08-03 stsp }
1740 a919d5c4 2020-07-23 stsp
1741 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1742 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1743 a919d5c4 2020-07-23 stsp
1744 a919d5c4 2020-07-23 stsp return NULL;
1745 7154f6ce 2019-03-27 stsp }
1746 7154f6ce 2019-03-27 stsp
1747 7154f6ce 2019-03-27 stsp static const struct got_error *
1748 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1749 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1750 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1751 6353ad76 2019-02-08 stsp {
1752 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1753 6353ad76 2019-02-08 stsp struct got_object_id id;
1754 6353ad76 2019-02-08 stsp size_t hdrlen;
1755 1338848f 2019-12-13 stsp int fd = -1;
1756 6353ad76 2019-02-08 stsp FILE *f = NULL;
1757 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1758 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1759 6353ad76 2019-02-08 stsp size_t flen, blen;
1760 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1761 6353ad76 2019-02-08 stsp
1762 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1763 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1764 6353ad76 2019-02-08 stsp
1765 7f91a133 2019-12-13 stsp /*
1766 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1767 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1768 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1769 7f91a133 2019-12-13 stsp */
1770 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1771 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1772 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1773 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1774 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1775 882ef1b9 2019-12-13 stsp else
1776 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1777 882ef1b9 2019-12-13 stsp goto done;
1778 882ef1b9 2019-12-13 stsp }
1779 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1780 3d35a492 2019-12-13 stsp goto done;
1781 3d35a492 2019-12-13 stsp }
1782 7f91a133 2019-12-13 stsp } else {
1783 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1784 5c02d2a5 2021-09-26 stsp if (fd == -1 && errno != ENOENT &&
1785 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
1786 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1787 5c02d2a5 2021-09-26 stsp else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1788 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1789 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1790 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1791 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1792 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1793 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1794 3d35a492 2019-12-13 stsp else
1795 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1796 3d35a492 2019-12-13 stsp goto done;
1797 3d35a492 2019-12-13 stsp }
1798 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1799 1338848f 2019-12-13 stsp goto done;
1800 a378724f 2019-02-10 stsp }
1801 a378724f 2019-02-10 stsp }
1802 6353ad76 2019-02-08 stsp
1803 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1804 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1805 1338848f 2019-12-13 stsp goto done;
1806 3f148bc6 2019-07-27 stsp }
1807 efdd40df 2019-07-27 stsp
1808 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1809 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1810 1338848f 2019-12-13 stsp goto done;
1811 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1812 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1813 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1814 1338848f 2019-12-13 stsp goto done;
1815 d00136be 2019-03-26 stsp }
1816 b8f41171 2019-02-10 stsp
1817 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1818 f179e66d 2020-07-23 stsp goto done;
1819 f179e66d 2020-07-23 stsp
1820 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1821 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1822 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1823 1338848f 2019-12-13 stsp goto done;
1824 f179e66d 2020-07-23 stsp }
1825 6353ad76 2019-02-08 stsp
1826 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1827 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1828 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1829 c363b2c1 2019-08-03 stsp else
1830 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1831 c363b2c1 2019-08-03 stsp
1832 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1833 6353ad76 2019-02-08 stsp if (err)
1834 1338848f 2019-12-13 stsp goto done;
1835 6353ad76 2019-02-08 stsp
1836 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1837 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1838 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1839 a919d5c4 2020-07-23 stsp goto done;
1840 a919d5c4 2020-07-23 stsp }
1841 a919d5c4 2020-07-23 stsp
1842 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1843 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1844 ab0d4361 2019-12-13 stsp if (fd == -1) {
1845 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1846 ab0d4361 2019-12-13 stsp goto done;
1847 ab0d4361 2019-12-13 stsp }
1848 3d35a492 2019-12-13 stsp }
1849 3d35a492 2019-12-13 stsp
1850 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1851 6353ad76 2019-02-08 stsp if (f == NULL) {
1852 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1853 6353ad76 2019-02-08 stsp goto done;
1854 6353ad76 2019-02-08 stsp }
1855 1338848f 2019-12-13 stsp fd = -1;
1856 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1857 656b1f76 2019-05-11 jcs for (;;) {
1858 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1859 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1860 6353ad76 2019-02-08 stsp if (err)
1861 7154f6ce 2019-03-27 stsp goto done;
1862 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1863 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1864 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1865 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1866 7154f6ce 2019-03-27 stsp goto done;
1867 80c5c120 2019-02-19 stsp }
1868 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1869 6353ad76 2019-02-08 stsp if (flen != 0)
1870 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1871 6353ad76 2019-02-08 stsp break;
1872 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1873 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1874 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1875 6353ad76 2019-02-08 stsp break;
1876 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1877 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1878 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1879 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1880 6353ad76 2019-02-08 stsp break;
1881 6353ad76 2019-02-08 stsp }
1882 6353ad76 2019-02-08 stsp } else {
1883 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1884 6353ad76 2019-02-08 stsp break;
1885 6353ad76 2019-02-08 stsp }
1886 6353ad76 2019-02-08 stsp hdrlen = 0;
1887 6353ad76 2019-02-08 stsp }
1888 7154f6ce 2019-03-27 stsp
1889 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1890 7154f6ce 2019-03-27 stsp rewind(f);
1891 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1892 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1893 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1894 6353ad76 2019-02-08 stsp done:
1895 6353ad76 2019-02-08 stsp if (blob)
1896 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1897 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1898 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1899 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1900 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1901 9d31a1d8 2018-03-11 stsp return err;
1902 e2b1e152 2019-07-13 stsp }
1903 e2b1e152 2019-07-13 stsp
1904 e2b1e152 2019-07-13 stsp /*
1905 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1906 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1907 e2b1e152 2019-07-13 stsp */
1908 e2b1e152 2019-07-13 stsp static const struct got_error *
1909 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1910 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1911 e2b1e152 2019-07-13 stsp {
1912 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1913 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1914 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1915 e2b1e152 2019-07-13 stsp
1916 e2b1e152 2019-07-13 stsp return NULL;
1917 9d31a1d8 2018-03-11 stsp }
1918 9d31a1d8 2018-03-11 stsp
1919 9d31a1d8 2018-03-11 stsp static const struct got_error *
1920 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1921 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1922 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1923 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1924 0584f854 2019-04-06 stsp void *progress_arg)
1925 9d31a1d8 2018-03-11 stsp {
1926 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1927 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1928 6353ad76 2019-02-08 stsp char *ondisk_path;
1929 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1930 b8f41171 2019-02-10 stsp struct stat sb;
1931 9d31a1d8 2018-03-11 stsp
1932 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1933 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1934 6353ad76 2019-02-08 stsp
1935 abb4604f 2019-07-27 stsp if (ie) {
1936 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1937 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1938 a76c42e6 2019-08-03 stsp goto done;
1939 a76c42e6 2019-08-03 stsp }
1940 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1941 7f91a133 2019-12-13 stsp repo);
1942 abb4604f 2019-07-27 stsp if (err)
1943 abb4604f 2019-07-27 stsp goto done;
1944 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1945 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1946 c90c8ce3 2020-07-23 stsp } else {
1947 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1948 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1949 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1950 f6764181 2021-09-24 stsp ondisk_path);
1951 f6764181 2021-09-24 stsp goto done;
1952 f6764181 2021-09-24 stsp }
1953 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1954 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1955 f6764181 2021-09-24 stsp } else {
1956 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1957 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1958 f6764181 2021-09-24 stsp else
1959 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1960 f6764181 2021-09-24 stsp }
1961 c90c8ce3 2020-07-23 stsp }
1962 6353ad76 2019-02-08 stsp
1963 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1964 2c41dce7 2021-06-27 stsp if (ie)
1965 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1966 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1967 b8f41171 2019-02-10 stsp goto done;
1968 b8f41171 2019-02-10 stsp }
1969 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1970 a769b60b 2021-06-27 stsp if (ie)
1971 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1972 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1973 5036ab18 2020-04-18 stsp path);
1974 5036ab18 2020-04-18 stsp goto done;
1975 5036ab18 2020-04-18 stsp }
1976 b8f41171 2019-02-10 stsp
1977 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1978 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1979 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1980 c6e8a826 2021-04-05 stsp /*
1981 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1982 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1983 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1984 c6e8a826 2021-04-05 stsp * updating contents of this file.
1985 c6e8a826 2021-04-05 stsp */
1986 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1987 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1988 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1989 c6e8a826 2021-04-05 stsp /* Same commit. */
1990 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1991 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1992 e2b1e152 2019-07-13 stsp if (err)
1993 e2b1e152 2019-07-13 stsp goto done;
1994 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1995 b8f41171 2019-02-10 stsp path);
1996 6353ad76 2019-02-08 stsp goto done;
1997 a378724f 2019-02-10 stsp }
1998 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1999 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
2000 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
2001 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2002 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2003 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2004 0f58026f 2021-04-05 stsp if (err)
2005 0f58026f 2021-04-05 stsp goto done;
2006 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2007 0f58026f 2021-04-05 stsp path);
2008 b8f41171 2019-02-10 stsp goto done;
2009 e2b1e152 2019-07-13 stsp }
2010 9d31a1d8 2018-03-11 stsp }
2011 9d31a1d8 2018-03-11 stsp
2012 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2013 8da9e5f4 2019-01-12 stsp if (err)
2014 6353ad76 2019-02-08 stsp goto done;
2015 512f0d0e 2019-01-02 stsp
2016 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2017 234035bc 2019-06-01 stsp int update_timestamps;
2018 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2019 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2020 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2021 234035bc 2019-06-01 stsp struct got_object_id id2;
2022 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2023 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2024 234035bc 2019-06-01 stsp if (err)
2025 f69721c3 2019-10-21 stsp goto done;
2026 f69721c3 2019-10-21 stsp }
2027 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2028 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2029 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2030 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2031 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2032 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2033 f69721c3 2019-10-21 stsp goto done;
2034 f69721c3 2019-10-21 stsp }
2035 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2036 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2037 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2038 234035bc 2019-06-01 stsp goto done;
2039 f69721c3 2019-10-21 stsp }
2040 234035bc 2019-06-01 stsp }
2041 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2042 36bf999c 2020-07-23 stsp char *link_target;
2043 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2044 36bf999c 2020-07-23 stsp if (err)
2045 36bf999c 2020-07-23 stsp goto done;
2046 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2047 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2048 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2049 36bf999c 2020-07-23 stsp free(link_target);
2050 993e2a1b 2020-07-23 stsp } else {
2051 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2052 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2053 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2054 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2055 993e2a1b 2020-07-23 stsp }
2056 f69721c3 2019-10-21 stsp free(label_orig);
2057 234035bc 2019-06-01 stsp if (blob2)
2058 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2059 909d120e 2019-09-22 stsp if (err)
2060 909d120e 2019-09-22 stsp goto done;
2061 234035bc 2019-06-01 stsp /*
2062 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2063 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2064 234035bc 2019-06-01 stsp * unmodified files again.
2065 234035bc 2019-06-01 stsp */
2066 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2067 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2068 234035bc 2019-06-01 stsp update_timestamps);
2069 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2070 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2071 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2072 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2073 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2074 1ee397ad 2019-07-12 stsp if (err)
2075 1ee397ad 2019-07-12 stsp goto done;
2076 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2077 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2078 13d9040b 2019-03-27 stsp if (err)
2079 13d9040b 2019-03-27 stsp goto done;
2080 13d9040b 2019-03-27 stsp } else {
2081 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2082 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2083 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2084 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2085 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2086 5267b9e4 2021-09-26 stsp status == GOT_STATUS_UNVERSIONED, 0,
2087 5267b9e4 2021-09-26 stsp repo, progress_cb, progress_arg);
2088 2e1fa222 2020-07-23 stsp } else {
2089 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2090 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2091 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2092 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2093 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2094 2e1fa222 2020-07-23 stsp }
2095 13d9040b 2019-03-27 stsp if (err)
2096 13d9040b 2019-03-27 stsp goto done;
2097 65b05cec 2020-07-23 stsp
2098 054041d0 2020-06-24 stsp if (ie) {
2099 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2100 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2101 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2102 054041d0 2020-06-24 stsp } else {
2103 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2104 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2105 054041d0 2020-06-24 stsp &blob->id);
2106 054041d0 2020-06-24 stsp }
2107 13d9040b 2019-03-27 stsp if (err)
2108 13d9040b 2019-03-27 stsp goto done;
2109 65b05cec 2020-07-23 stsp
2110 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2111 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2112 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2113 2e1fa222 2020-07-23 stsp }
2114 13d9040b 2019-03-27 stsp }
2115 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2116 6353ad76 2019-02-08 stsp done:
2117 6353ad76 2019-02-08 stsp free(ondisk_path);
2118 8da9e5f4 2019-01-12 stsp return err;
2119 90285c3b 2019-01-08 stsp }
2120 90285c3b 2019-01-08 stsp
2121 90285c3b 2019-01-08 stsp static const struct got_error *
2122 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2123 90285c3b 2019-01-08 stsp {
2124 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2125 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2126 90285c3b 2019-01-08 stsp
2127 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2128 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2129 90285c3b 2019-01-08 stsp
2130 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2131 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2132 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2133 c97665ae 2019-01-13 stsp } else {
2134 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2135 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2136 1c4cdd89 2021-06-20 stsp if (err)
2137 1c4cdd89 2021-06-20 stsp goto done;
2138 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2139 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2140 fddefe3b 2020-10-20 stsp free(ondisk_path);
2141 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2142 1c4cdd89 2021-06-20 stsp parent = NULL;
2143 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2144 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2145 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2146 fddefe3b 2020-10-20 stsp ondisk_path);
2147 90285c3b 2019-01-08 stsp break;
2148 90285c3b 2019-01-08 stsp }
2149 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2150 1c4cdd89 2021-06-20 stsp if (err)
2151 1c4cdd89 2021-06-20 stsp break;
2152 1c4cdd89 2021-06-20 stsp }
2153 90285c3b 2019-01-08 stsp }
2154 1c4cdd89 2021-06-20 stsp done:
2155 90285c3b 2019-01-08 stsp free(ondisk_path);
2156 1c4cdd89 2021-06-20 stsp free(parent);
2157 90285c3b 2019-01-08 stsp return err;
2158 512f0d0e 2019-01-02 stsp }
2159 512f0d0e 2019-01-02 stsp
2160 708d8e67 2019-03-27 stsp static const struct got_error *
2161 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2163 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2164 708d8e67 2019-03-27 stsp {
2165 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2166 708d8e67 2019-03-27 stsp unsigned char status;
2167 708d8e67 2019-03-27 stsp struct stat sb;
2168 708d8e67 2019-03-27 stsp char *ondisk_path;
2169 708d8e67 2019-03-27 stsp
2170 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2171 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2172 a76c42e6 2019-08-03 stsp
2173 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2174 708d8e67 2019-03-27 stsp == -1)
2175 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2176 708d8e67 2019-03-27 stsp
2177 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2178 708d8e67 2019-03-27 stsp if (err)
2179 5a58a424 2020-06-23 stsp goto done;
2180 708d8e67 2019-03-27 stsp
2181 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2182 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2183 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2184 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2185 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2186 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2187 993e2a1b 2020-07-23 stsp goto done;
2188 993e2a1b 2020-07-23 stsp }
2189 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2190 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2191 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2192 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2193 993e2a1b 2020-07-23 stsp if (err)
2194 993e2a1b 2020-07-23 stsp goto done;
2195 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2196 993e2a1b 2020-07-23 stsp ie->path);
2197 993e2a1b 2020-07-23 stsp goto done;
2198 993e2a1b 2020-07-23 stsp }
2199 993e2a1b 2020-07-23 stsp
2200 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2201 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2202 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2203 1ee397ad 2019-07-12 stsp if (err)
2204 5a58a424 2020-06-23 stsp goto done;
2205 fc6346c4 2019-03-27 stsp /*
2206 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2207 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2208 fc6346c4 2019-03-27 stsp */
2209 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2210 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2211 fc6346c4 2019-03-27 stsp } else {
2212 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2213 1ee397ad 2019-07-12 stsp if (err)
2214 5a58a424 2020-06-23 stsp goto done;
2215 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2216 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2217 fc6346c4 2019-03-27 stsp if (err)
2218 5a58a424 2020-06-23 stsp goto done;
2219 fc6346c4 2019-03-27 stsp }
2220 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2221 708d8e67 2019-03-27 stsp }
2222 5a58a424 2020-06-23 stsp done:
2223 5a58a424 2020-06-23 stsp free(ondisk_path);
2224 fc6346c4 2019-03-27 stsp return err;
2225 708d8e67 2019-03-27 stsp }
2226 708d8e67 2019-03-27 stsp
2227 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2228 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2229 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2230 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2231 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2232 8da9e5f4 2019-01-12 stsp void *progress_arg;
2233 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2234 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2235 8da9e5f4 2019-01-12 stsp };
2236 8da9e5f4 2019-01-12 stsp
2237 512f0d0e 2019-01-02 stsp static const struct got_error *
2238 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2239 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2240 512f0d0e 2019-01-02 stsp {
2241 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2242 0584f854 2019-04-06 stsp
2243 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2245 512f0d0e 2019-01-02 stsp
2246 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2247 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2248 8da9e5f4 2019-01-12 stsp }
2249 512f0d0e 2019-01-02 stsp
2250 8da9e5f4 2019-01-12 stsp static const struct got_error *
2251 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2252 8da9e5f4 2019-01-12 stsp {
2253 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2254 7a9df742 2019-01-08 stsp
2255 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2257 0584f854 2019-04-06 stsp
2258 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2259 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2260 9d31a1d8 2018-03-11 stsp }
2261 9d31a1d8 2018-03-11 stsp
2262 9d31a1d8 2018-03-11 stsp static const struct got_error *
2263 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2264 9d31a1d8 2018-03-11 stsp {
2265 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2266 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2267 8da9e5f4 2019-01-12 stsp char *path;
2268 9d31a1d8 2018-03-11 stsp
2269 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2270 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2271 0584f854 2019-04-06 stsp
2272 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2273 63c5ca5d 2019-08-24 stsp return NULL;
2274 63c5ca5d 2019-08-24 stsp
2275 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2276 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2277 8da9e5f4 2019-01-12 stsp == -1)
2278 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2279 9d31a1d8 2018-03-11 stsp
2280 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2281 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2282 8da9e5f4 2019-01-12 stsp else
2283 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2284 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2285 9d31a1d8 2018-03-11 stsp
2286 8da9e5f4 2019-01-12 stsp free(path);
2287 0cd1c46a 2019-03-11 stsp return err;
2288 0cd1c46a 2019-03-11 stsp }
2289 0cd1c46a 2019-03-11 stsp
2290 b2118c49 2020-07-28 stsp const struct got_error *
2291 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2292 b2118c49 2020-07-28 stsp {
2293 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2294 b2118c49 2020-07-28 stsp
2295 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2296 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2297 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2298 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2299 b2118c49 2020-07-28 stsp }
2300 b2118c49 2020-07-28 stsp
2301 b2118c49 2020-07-28 stsp return NULL;
2302 b2118c49 2020-07-28 stsp }
2303 b2118c49 2020-07-28 stsp
2304 818c7501 2019-07-11 stsp static const struct got_error *
2305 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2306 0cd1c46a 2019-03-11 stsp {
2307 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2308 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2309 0cd1c46a 2019-03-11 stsp
2310 517bab73 2019-03-11 stsp *refname = NULL;
2311 517bab73 2019-03-11 stsp
2312 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2313 b2118c49 2020-07-28 stsp if (err)
2314 b2118c49 2020-07-28 stsp return err;
2315 0cd1c46a 2019-03-11 stsp
2316 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2317 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2318 0647c563 2019-03-11 stsp *refname = NULL;
2319 0cd1c46a 2019-03-11 stsp }
2320 517bab73 2019-03-11 stsp free(uuidstr);
2321 517bab73 2019-03-11 stsp return err;
2322 517bab73 2019-03-11 stsp }
2323 517bab73 2019-03-11 stsp
2324 818c7501 2019-07-11 stsp const struct got_error *
2325 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2326 818c7501 2019-07-11 stsp {
2327 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2328 818c7501 2019-07-11 stsp }
2329 818c7501 2019-07-11 stsp
2330 818c7501 2019-07-11 stsp static const struct got_error *
2331 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2332 818c7501 2019-07-11 stsp {
2333 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2334 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2335 818c7501 2019-07-11 stsp }
2336 818c7501 2019-07-11 stsp
2337 818c7501 2019-07-11 stsp static const struct got_error *
2338 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2339 818c7501 2019-07-11 stsp {
2340 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2341 818c7501 2019-07-11 stsp }
2342 818c7501 2019-07-11 stsp
2343 818c7501 2019-07-11 stsp static const struct got_error *
2344 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2345 818c7501 2019-07-11 stsp {
2346 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2347 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2348 818c7501 2019-07-11 stsp }
2349 818c7501 2019-07-11 stsp
2350 818c7501 2019-07-11 stsp static const struct got_error *
2351 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2352 818c7501 2019-07-11 stsp {
2353 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2354 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2355 0ebf8283 2019-07-24 stsp }
2356 0ebf8283 2019-07-24 stsp
2357 0ebf8283 2019-07-24 stsp static const struct got_error *
2358 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2359 0ebf8283 2019-07-24 stsp {
2360 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2361 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2362 0ebf8283 2019-07-24 stsp }
2363 0ebf8283 2019-07-24 stsp
2364 0ebf8283 2019-07-24 stsp static const struct got_error *
2365 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2366 0ebf8283 2019-07-24 stsp {
2367 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2368 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2369 0ebf8283 2019-07-24 stsp }
2370 0ebf8283 2019-07-24 stsp
2371 0ebf8283 2019-07-24 stsp static const struct got_error *
2372 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 0ebf8283 2019-07-24 stsp {
2374 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2375 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2376 818c7501 2019-07-11 stsp }
2377 818c7501 2019-07-11 stsp
2378 0ebf8283 2019-07-24 stsp static const struct got_error *
2379 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2380 0ebf8283 2019-07-24 stsp {
2381 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2382 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2383 0ebf8283 2019-07-24 stsp }
2384 818c7501 2019-07-11 stsp
2385 0ebf8283 2019-07-24 stsp const struct got_error *
2386 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2387 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2388 0ebf8283 2019-07-24 stsp {
2389 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2390 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2391 0ebf8283 2019-07-24 stsp *path = NULL;
2392 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2393 0ebf8283 2019-07-24 stsp }
2394 0ebf8283 2019-07-24 stsp return NULL;
2395 f259c4c1 2021-09-24 stsp }
2396 f259c4c1 2021-09-24 stsp
2397 f259c4c1 2021-09-24 stsp static const struct got_error *
2398 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2399 f259c4c1 2021-09-24 stsp {
2400 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2401 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2402 0ebf8283 2019-07-24 stsp }
2403 0ebf8283 2019-07-24 stsp
2404 f259c4c1 2021-09-24 stsp static const struct got_error *
2405 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 f259c4c1 2021-09-24 stsp {
2407 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2408 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2409 f259c4c1 2021-09-24 stsp }
2410 f259c4c1 2021-09-24 stsp
2411 517bab73 2019-03-11 stsp /*
2412 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2413 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2414 517bab73 2019-03-11 stsp */
2415 517bab73 2019-03-11 stsp static const struct got_error *
2416 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2417 517bab73 2019-03-11 stsp {
2418 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2419 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2420 517bab73 2019-03-11 stsp char *refname;
2421 517bab73 2019-03-11 stsp
2422 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2423 517bab73 2019-03-11 stsp if (err)
2424 517bab73 2019-03-11 stsp return err;
2425 0cd1c46a 2019-03-11 stsp
2426 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2427 0cd1c46a 2019-03-11 stsp if (err)
2428 0cd1c46a 2019-03-11 stsp goto done;
2429 0cd1c46a 2019-03-11 stsp
2430 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2431 0cd1c46a 2019-03-11 stsp done:
2432 0cd1c46a 2019-03-11 stsp free(refname);
2433 0cd1c46a 2019-03-11 stsp if (ref)
2434 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2435 3e3a69f1 2019-07-25 stsp return err;
2436 3e3a69f1 2019-07-25 stsp }
2437 3e3a69f1 2019-07-25 stsp
2438 3e3a69f1 2019-07-25 stsp static const struct got_error *
2439 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2440 3e3a69f1 2019-07-25 stsp {
2441 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2442 3e3a69f1 2019-07-25 stsp
2443 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2444 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2445 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2446 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2447 3e3a69f1 2019-07-25 stsp }
2448 9d31a1d8 2018-03-11 stsp return err;
2449 9d31a1d8 2018-03-11 stsp }
2450 9d31a1d8 2018-03-11 stsp
2451 3e3a69f1 2019-07-25 stsp
2452 ebf99748 2019-05-09 stsp static const struct got_error *
2453 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2454 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2455 ebf99748 2019-05-09 stsp {
2456 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2457 ebf99748 2019-05-09 stsp FILE *index = NULL;
2458 0cd1c46a 2019-03-11 stsp
2459 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2460 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2461 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2462 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2463 ebf99748 2019-05-09 stsp
2464 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2465 3e3a69f1 2019-07-25 stsp if (err)
2466 ebf99748 2019-05-09 stsp goto done;
2467 ebf99748 2019-05-09 stsp
2468 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2469 ebf99748 2019-05-09 stsp if (index == NULL) {
2470 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2471 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2472 ebf99748 2019-05-09 stsp } else {
2473 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2474 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2475 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2476 ebf99748 2019-05-09 stsp }
2477 ebf99748 2019-05-09 stsp done:
2478 ebf99748 2019-05-09 stsp if (err) {
2479 ebf99748 2019-05-09 stsp free(*fileindex_path);
2480 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2481 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2482 ebf99748 2019-05-09 stsp *fileindex = NULL;
2483 ebf99748 2019-05-09 stsp }
2484 ebf99748 2019-05-09 stsp return err;
2485 ebf99748 2019-05-09 stsp }
2486 c932eeeb 2019-05-22 stsp
2487 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2488 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2489 c932eeeb 2019-05-22 stsp const char *path;
2490 c932eeeb 2019-05-22 stsp size_t path_len;
2491 c932eeeb 2019-05-22 stsp const char *entry_name;
2492 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2493 a484d721 2019-06-10 stsp void *progress_arg;
2494 c932eeeb 2019-05-22 stsp };
2495 ebf99748 2019-05-09 stsp
2496 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2497 c932eeeb 2019-05-22 stsp static const struct got_error *
2498 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2499 c932eeeb 2019-05-22 stsp {
2500 1ee397ad 2019-07-12 stsp const struct got_error *err;
2501 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2502 c932eeeb 2019-05-22 stsp
2503 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2504 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2505 c932eeeb 2019-05-22 stsp return NULL;
2506 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2507 102ff934 2019-06-11 stsp return NULL;
2508 102ff934 2019-06-11 stsp
2509 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2510 a769b60b 2021-06-27 stsp return NULL;
2511 a769b60b 2021-06-27 stsp
2512 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2513 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2514 c932eeeb 2019-05-22 stsp return NULL;
2515 c932eeeb 2019-05-22 stsp
2516 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2517 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2518 edd02c5e 2019-07-11 stsp ie->path);
2519 1ee397ad 2019-07-12 stsp if (err)
2520 1ee397ad 2019-07-12 stsp return err;
2521 1ee397ad 2019-07-12 stsp }
2522 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2523 c932eeeb 2019-05-22 stsp return NULL;
2524 a615e0e7 2020-12-16 stsp }
2525 a615e0e7 2020-12-16 stsp
2526 a615e0e7 2020-12-16 stsp static const struct got_error *
2527 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2528 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2529 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2530 a615e0e7 2020-12-16 stsp {
2531 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2532 a615e0e7 2020-12-16 stsp
2533 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2534 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2535 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2536 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2537 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2538 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2539 a615e0e7 2020-12-16 stsp
2540 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2541 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2542 9c6338c4 2019-06-02 stsp }
2543 9c6338c4 2019-06-02 stsp
2544 9c6338c4 2019-06-02 stsp static const struct got_error *
2545 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2546 9c6338c4 2019-06-02 stsp {
2547 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2548 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2549 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2550 867630bb 2020-01-17 stsp struct timespec timeout;
2551 9c6338c4 2019-06-02 stsp
2552 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2553 9c6338c4 2019-06-02 stsp fileindex_path);
2554 9c6338c4 2019-06-02 stsp if (err)
2555 9c6338c4 2019-06-02 stsp goto done;
2556 9c6338c4 2019-06-02 stsp
2557 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2558 9c6338c4 2019-06-02 stsp if (err)
2559 9c6338c4 2019-06-02 stsp goto done;
2560 9c6338c4 2019-06-02 stsp
2561 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2562 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2563 9c6338c4 2019-06-02 stsp fileindex_path);
2564 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2565 9c6338c4 2019-06-02 stsp }
2566 867630bb 2020-01-17 stsp
2567 867630bb 2020-01-17 stsp /*
2568 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2569 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2570 867630bb 2020-01-17 stsp * was recorded in the file index.
2571 867630bb 2020-01-17 stsp */
2572 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2573 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2574 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2575 9c6338c4 2019-06-02 stsp done:
2576 9c6338c4 2019-06-02 stsp if (new_index)
2577 9c6338c4 2019-06-02 stsp fclose(new_index);
2578 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2579 9c6338c4 2019-06-02 stsp return err;
2580 c932eeeb 2019-05-22 stsp }
2581 6ced4176 2019-07-12 stsp
2582 6ced4176 2019-07-12 stsp static const struct got_error *
2583 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2584 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2585 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2586 6ced4176 2019-07-12 stsp {
2587 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2588 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2589 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2590 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2591 6ced4176 2019-07-12 stsp
2592 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2593 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2594 6ced4176 2019-07-12 stsp *tree_id = NULL;
2595 6ced4176 2019-07-12 stsp
2596 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2597 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2598 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2599 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2600 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2601 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2602 6ced4176 2019-07-12 stsp goto done;
2603 6ced4176 2019-07-12 stsp }
2604 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2605 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2606 6ced4176 2019-07-12 stsp if (err)
2607 6ced4176 2019-07-12 stsp goto done;
2608 6ced4176 2019-07-12 stsp return NULL;
2609 6ced4176 2019-07-12 stsp }
2610 6ced4176 2019-07-12 stsp
2611 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2612 6ced4176 2019-07-12 stsp
2613 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2614 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2615 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2616 6ced4176 2019-07-12 stsp goto done;
2617 6ced4176 2019-07-12 stsp }
2618 6ced4176 2019-07-12 stsp
2619 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2620 6ced4176 2019-07-12 stsp in_repo_path);
2621 6ced4176 2019-07-12 stsp if (err)
2622 6ced4176 2019-07-12 stsp goto done;
2623 6ced4176 2019-07-12 stsp
2624 6ced4176 2019-07-12 stsp free(in_repo_path);
2625 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2626 6ced4176 2019-07-12 stsp
2627 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2628 6ced4176 2019-07-12 stsp if (err)
2629 6ced4176 2019-07-12 stsp goto done;
2630 c932eeeb 2019-05-22 stsp
2631 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2632 6ced4176 2019-07-12 stsp /* Check out a single file. */
2633 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2634 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2635 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2636 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2637 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2638 6ced4176 2019-07-12 stsp goto done;
2639 6ced4176 2019-07-12 stsp }
2640 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2641 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2642 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2643 6ced4176 2019-07-12 stsp goto done;
2644 6ced4176 2019-07-12 stsp }
2645 6ced4176 2019-07-12 stsp } else {
2646 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2647 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2648 6ced4176 2019-07-12 stsp if (err)
2649 6ced4176 2019-07-12 stsp return err;
2650 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2651 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2652 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2653 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2654 6ced4176 2019-07-12 stsp goto done;
2655 6ced4176 2019-07-12 stsp }
2656 6ced4176 2019-07-12 stsp }
2657 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2658 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2659 6ced4176 2019-07-12 stsp } else {
2660 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2661 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2662 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2663 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2664 6ced4176 2019-07-12 stsp goto done;
2665 6ced4176 2019-07-12 stsp }
2666 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2667 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2668 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2669 6ced4176 2019-07-12 stsp goto done;
2670 6ced4176 2019-07-12 stsp }
2671 6ced4176 2019-07-12 stsp }
2672 6ced4176 2019-07-12 stsp done:
2673 6ced4176 2019-07-12 stsp free(id);
2674 6ced4176 2019-07-12 stsp free(in_repo_path);
2675 6ced4176 2019-07-12 stsp if (err) {
2676 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2677 6ced4176 2019-07-12 stsp free(*tree_relpath);
2678 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2679 6ced4176 2019-07-12 stsp free(*tree_id);
2680 6ced4176 2019-07-12 stsp *tree_id = NULL;
2681 6ced4176 2019-07-12 stsp }
2682 07ed472d 2019-07-12 stsp return err;
2683 07ed472d 2019-07-12 stsp }
2684 07ed472d 2019-07-12 stsp
2685 07ed472d 2019-07-12 stsp static const struct got_error *
2686 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2687 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2688 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2689 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2690 07ed472d 2019-07-12 stsp {
2691 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2692 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2693 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2694 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2695 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2696 07ed472d 2019-07-12 stsp
2697 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2698 7f47418f 2019-12-20 stsp if (err) {
2699 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2700 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2701 7f47418f 2019-12-20 stsp goto done;
2702 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2703 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2704 7f47418f 2019-12-20 stsp if (err)
2705 7f47418f 2019-12-20 stsp return err;
2706 7f47418f 2019-12-20 stsp }
2707 07ed472d 2019-07-12 stsp
2708 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2709 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2710 07ed472d 2019-07-12 stsp if (err)
2711 07ed472d 2019-07-12 stsp goto done;
2712 07ed472d 2019-07-12 stsp
2713 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2714 07ed472d 2019-07-12 stsp if (err)
2715 07ed472d 2019-07-12 stsp goto done;
2716 07ed472d 2019-07-12 stsp
2717 07ed472d 2019-07-12 stsp if (entry_name &&
2718 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2719 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2720 07ed472d 2019-07-12 stsp goto done;
2721 07ed472d 2019-07-12 stsp }
2722 07ed472d 2019-07-12 stsp
2723 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2724 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2725 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2726 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2727 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2728 07ed472d 2019-07-12 stsp arg.repo = repo;
2729 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2730 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2731 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2732 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2733 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2734 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2735 07ed472d 2019-07-12 stsp done:
2736 07ed472d 2019-07-12 stsp if (tree)
2737 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2738 07ed472d 2019-07-12 stsp if (commit)
2739 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2740 6ced4176 2019-07-12 stsp return err;
2741 6ced4176 2019-07-12 stsp }
2742 6ced4176 2019-07-12 stsp
2743 9d31a1d8 2018-03-11 stsp const struct got_error *
2744 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2745 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2746 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2747 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2748 9d31a1d8 2018-03-11 stsp {
2749 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2750 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2751 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2752 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2753 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2754 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2755 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2756 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2757 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2758 f2ea84fa 2019-07-27 stsp int entry_type;
2759 f2ea84fa 2019-07-27 stsp char *relpath;
2760 f2ea84fa 2019-07-27 stsp char *entry_name;
2761 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2762 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2763 9d31a1d8 2018-03-11 stsp
2764 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2765 f2ea84fa 2019-07-27 stsp
2766 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2767 9d31a1d8 2018-03-11 stsp if (err)
2768 9d31a1d8 2018-03-11 stsp return err;
2769 93a30277 2018-12-24 stsp
2770 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2771 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2772 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2773 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2774 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2775 f2ea84fa 2019-07-27 stsp goto done;
2776 f2ea84fa 2019-07-27 stsp }
2777 6ced4176 2019-07-12 stsp
2778 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2780 f2ea84fa 2019-07-27 stsp if (err) {
2781 f2ea84fa 2019-07-27 stsp free(tpd);
2782 6ced4176 2019-07-12 stsp goto done;
2783 6ced4176 2019-07-12 stsp }
2784 f2ea84fa 2019-07-27 stsp
2785 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2786 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2787 f2ea84fa 2019-07-27 stsp if (err) {
2788 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2789 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2790 f2ea84fa 2019-07-27 stsp free(tpd);
2791 f2ea84fa 2019-07-27 stsp goto done;
2792 f2ea84fa 2019-07-27 stsp }
2793 f2ea84fa 2019-07-27 stsp } else
2794 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2795 f2ea84fa 2019-07-27 stsp
2796 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2797 6ced4176 2019-07-12 stsp }
2798 6ced4176 2019-07-12 stsp
2799 51514078 2018-12-25 stsp /*
2800 51514078 2018-12-25 stsp * Read the file index.
2801 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2802 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2803 51514078 2018-12-25 stsp */
2804 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2805 8da9e5f4 2019-01-12 stsp if (err)
2806 c4cdcb68 2019-04-03 stsp goto done;
2807 c4cdcb68 2019-04-03 stsp
2808 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2809 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2810 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2811 f2ea84fa 2019-07-27 stsp
2812 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2813 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2814 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2815 f2ea84fa 2019-07-27 stsp if (err)
2816 f2ea84fa 2019-07-27 stsp break;
2817 f2ea84fa 2019-07-27 stsp
2818 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2819 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2820 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2821 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2822 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2823 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2824 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2825 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2826 f2ea84fa 2019-07-27 stsp if (err)
2827 f2ea84fa 2019-07-27 stsp break;
2828 f2ea84fa 2019-07-27 stsp
2829 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2830 711d9cd9 2019-07-12 stsp }
2831 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2832 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2833 af12c6b9 2019-06-04 stsp err = sync_err;
2834 9d31a1d8 2018-03-11 stsp done:
2835 9c6338c4 2019-06-02 stsp free(fileindex_path);
2836 edfa7d7f 2018-09-11 stsp if (tree)
2837 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2838 9d31a1d8 2018-03-11 stsp if (commit)
2839 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2840 5ade8233 2019-07-12 stsp if (fileindex)
2841 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2842 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2843 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2844 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2845 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2846 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2847 f2ea84fa 2019-07-27 stsp free(tpd);
2848 f2ea84fa 2019-07-27 stsp }
2849 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2850 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2851 9d31a1d8 2018-03-11 stsp err = unlockerr;
2852 f8d1f275 2019-02-04 stsp return err;
2853 f8d1f275 2019-02-04 stsp }
2854 f8d1f275 2019-02-04 stsp
2855 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2856 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2857 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2858 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2859 234035bc 2019-06-01 stsp void *progress_arg;
2860 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2861 234035bc 2019-06-01 stsp void *cancel_arg;
2862 f69721c3 2019-10-21 stsp const char *label_orig;
2863 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2864 5267b9e4 2021-09-26 stsp int allow_bad_symlinks;
2865 234035bc 2019-06-01 stsp };
2866 234035bc 2019-06-01 stsp
2867 234035bc 2019-06-01 stsp static const struct got_error *
2868 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2869 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2870 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2871 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2872 234035bc 2019-06-01 stsp {
2873 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2874 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2875 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2876 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2877 234035bc 2019-06-01 stsp struct stat sb;
2878 234035bc 2019-06-01 stsp unsigned char status;
2879 234035bc 2019-06-01 stsp int local_changes_subsumed;
2880 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2881 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2882 234035bc 2019-06-01 stsp
2883 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2884 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2885 d6c87207 2019-08-02 stsp strlen(path2));
2886 1ee397ad 2019-07-12 stsp if (ie == NULL)
2887 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2888 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2889 234035bc 2019-06-01 stsp
2890 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2891 234035bc 2019-06-01 stsp path2) == -1)
2892 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2893 234035bc 2019-06-01 stsp
2894 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2895 7f91a133 2019-12-13 stsp repo);
2896 234035bc 2019-06-01 stsp if (err)
2897 234035bc 2019-06-01 stsp goto done;
2898 234035bc 2019-06-01 stsp
2899 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2900 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2901 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2902 234035bc 2019-06-01 stsp goto done;
2903 234035bc 2019-06-01 stsp }
2904 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2905 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2906 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2907 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2908 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2909 234035bc 2019-06-01 stsp goto done;
2910 234035bc 2019-06-01 stsp }
2911 234035bc 2019-06-01 stsp
2912 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2913 36bf999c 2020-07-23 stsp char *link_target2;
2914 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2915 36bf999c 2020-07-23 stsp if (err)
2916 36bf999c 2020-07-23 stsp goto done;
2917 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2918 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2919 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2920 36bf999c 2020-07-23 stsp free(link_target2);
2921 af57b12a 2020-07-23 stsp } else {
2922 1af628f4 2021-06-03 stsp int fd;
2923 1af628f4 2021-06-03 stsp
2924 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2925 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2926 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2927 1af628f4 2021-06-03 stsp goto done;
2928 1af628f4 2021-06-03 stsp }
2929 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2930 1af628f4 2021-06-03 stsp f_orig, blob1);
2931 1af628f4 2021-06-03 stsp if (err)
2932 1af628f4 2021-06-03 stsp goto done;
2933 1af628f4 2021-06-03 stsp
2934 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2935 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2936 1af628f4 2021-06-03 stsp goto done;
2937 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2939 1af628f4 2021-06-03 stsp if (err)
2940 1af628f4 2021-06-03 stsp goto done;
2941 1af628f4 2021-06-03 stsp
2942 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2943 1af628f4 2021-06-03 stsp if (fd == -1) {
2944 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2945 1af628f4 2021-06-03 stsp ondisk_path);
2946 1af628f4 2021-06-03 stsp goto done;
2947 1af628f4 2021-06-03 stsp }
2948 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2949 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2950 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2951 1af628f4 2021-06-03 stsp ondisk_path);
2952 1af628f4 2021-06-03 stsp close(fd);
2953 1af628f4 2021-06-03 stsp goto done;
2954 1af628f4 2021-06-03 stsp }
2955 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2956 1af628f4 2021-06-03 stsp if (err)
2957 1af628f4 2021-06-03 stsp goto done;
2958 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2959 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2961 1af628f4 2021-06-03 stsp goto done;
2962 1af628f4 2021-06-03 stsp }
2963 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2964 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2966 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2968 af57b12a 2020-07-23 stsp }
2969 234035bc 2019-06-01 stsp } else if (blob1) {
2970 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2971 d6c87207 2019-08-02 stsp strlen(path1));
2972 1ee397ad 2019-07-12 stsp if (ie == NULL)
2973 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2974 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2975 2b92fad7 2019-06-02 stsp
2976 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2977 2b92fad7 2019-06-02 stsp path1) == -1)
2978 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2979 2b92fad7 2019-06-02 stsp
2980 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2981 7f91a133 2019-12-13 stsp repo);
2982 2b92fad7 2019-06-02 stsp if (err)
2983 2b92fad7 2019-06-02 stsp goto done;
2984 2b92fad7 2019-06-02 stsp
2985 2b92fad7 2019-06-02 stsp switch (status) {
2986 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2987 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2988 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2989 1ee397ad 2019-07-12 stsp if (err)
2990 1ee397ad 2019-07-12 stsp goto done;
2991 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2992 2b92fad7 2019-06-02 stsp if (err)
2993 2b92fad7 2019-06-02 stsp goto done;
2994 2b92fad7 2019-06-02 stsp if (ie)
2995 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2996 2b92fad7 2019-06-02 stsp break;
2997 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2998 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2999 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3000 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
3001 1ee397ad 2019-07-12 stsp if (err)
3002 1ee397ad 2019-07-12 stsp goto done;
3003 2b92fad7 2019-06-02 stsp if (ie)
3004 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3005 2b92fad7 2019-06-02 stsp break;
3006 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3007 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3008 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3009 0a22ca1a 2020-09-23 stsp /*
3010 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3011 0a22ca1a 2020-09-23 stsp * exists in the repository.
3012 0a22ca1a 2020-09-23 stsp */
3013 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
3014 0a22ca1a 2020-09-23 stsp if (err)
3015 0a22ca1a 2020-09-23 stsp goto done;
3016 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3017 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3018 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3019 0a22ca1a 2020-09-23 stsp if (err)
3020 0a22ca1a 2020-09-23 stsp goto done;
3021 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3022 0a22ca1a 2020-09-23 stsp path1);
3023 0a22ca1a 2020-09-23 stsp if (err)
3024 0a22ca1a 2020-09-23 stsp goto done;
3025 0a22ca1a 2020-09-23 stsp if (ie)
3026 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3027 0a22ca1a 2020-09-23 stsp ie);
3028 0a22ca1a 2020-09-23 stsp } else {
3029 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3030 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3031 0a22ca1a 2020-09-23 stsp }
3032 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3033 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3034 0a22ca1a 2020-09-23 stsp free(id);
3035 0a22ca1a 2020-09-23 stsp if (err)
3036 0a22ca1a 2020-09-23 stsp goto done;
3037 0a22ca1a 2020-09-23 stsp break;
3038 0a22ca1a 2020-09-23 stsp }
3039 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3040 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3041 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3042 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3043 1ee397ad 2019-07-12 stsp if (err)
3044 1ee397ad 2019-07-12 stsp goto done;
3045 2b92fad7 2019-06-02 stsp break;
3046 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3047 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3048 1ee397ad 2019-07-12 stsp if (err)
3049 1ee397ad 2019-07-12 stsp goto done;
3050 2b92fad7 2019-06-02 stsp break;
3051 2b92fad7 2019-06-02 stsp default:
3052 2b92fad7 2019-06-02 stsp break;
3053 2b92fad7 2019-06-02 stsp }
3054 234035bc 2019-06-01 stsp } else if (blob2) {
3055 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3056 234035bc 2019-06-01 stsp path2) == -1)
3057 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3058 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3059 d6c87207 2019-08-02 stsp strlen(path2));
3060 234035bc 2019-06-01 stsp if (ie) {
3061 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3062 7f91a133 2019-12-13 stsp -1, NULL, repo);
3063 234035bc 2019-06-01 stsp if (err)
3064 234035bc 2019-06-01 stsp goto done;
3065 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3066 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3067 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3068 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3069 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3070 1ee397ad 2019-07-12 stsp status, path2);
3071 234035bc 2019-06-01 stsp goto done;
3072 234035bc 2019-06-01 stsp }
3073 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3074 36bf999c 2020-07-23 stsp char *link_target2;
3075 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3076 36bf999c 2020-07-23 stsp blob2);
3077 36bf999c 2020-07-23 stsp if (err)
3078 36bf999c 2020-07-23 stsp goto done;
3079 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3080 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3081 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3082 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3083 36bf999c 2020-07-23 stsp free(link_target2);
3084 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3085 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3086 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3087 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3088 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3089 526a746f 2020-07-23 stsp a->progress_arg);
3090 dfe9fba0 2020-07-23 stsp } else {
3091 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3092 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3093 526a746f 2020-07-23 stsp }
3094 dfe9fba0 2020-07-23 stsp if (err)
3095 dfe9fba0 2020-07-23 stsp goto done;
3096 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3097 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3098 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3099 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3100 234035bc 2019-06-01 stsp if (err)
3101 234035bc 2019-06-01 stsp goto done;
3102 234035bc 2019-06-01 stsp }
3103 234035bc 2019-06-01 stsp } else {
3104 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3105 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3106 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3107 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3108 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3109 5267b9e4 2021-09-26 stsp 0, 1, a->allow_bad_symlinks, repo,
3110 5267b9e4 2021-09-26 stsp a->progress_cb, a->progress_arg);
3111 2e1fa222 2020-07-23 stsp } else {
3112 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3113 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3114 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3115 2e1fa222 2020-07-23 stsp }
3116 234035bc 2019-06-01 stsp if (err)
3117 234035bc 2019-06-01 stsp goto done;
3118 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3119 234035bc 2019-06-01 stsp if (err)
3120 234035bc 2019-06-01 stsp goto done;
3121 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3122 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3123 3969253a 2020-03-07 stsp if (err) {
3124 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3125 3969253a 2020-03-07 stsp goto done;
3126 3969253a 2020-03-07 stsp }
3127 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3128 2b92fad7 2019-06-02 stsp if (err) {
3129 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3130 2b92fad7 2019-06-02 stsp goto done;
3131 2b92fad7 2019-06-02 stsp }
3132 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3133 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3134 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3135 2e1fa222 2020-07-23 stsp }
3136 234035bc 2019-06-01 stsp }
3137 234035bc 2019-06-01 stsp }
3138 234035bc 2019-06-01 stsp done:
3139 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3140 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3141 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3142 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3143 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3144 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3145 1af628f4 2021-06-03 stsp free(id_str);
3146 54d5be07 2021-06-03 stsp free(label_deriv2);
3147 234035bc 2019-06-01 stsp free(ondisk_path);
3148 234035bc 2019-06-01 stsp return err;
3149 234035bc 2019-06-01 stsp }
3150 234035bc 2019-06-01 stsp
3151 69de9dd4 2021-09-03 stsp static const struct got_error *
3152 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3153 69de9dd4 2021-09-03 stsp {
3154 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3155 69de9dd4 2021-09-03 stsp
3156 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3157 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3158 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3159 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3160 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3161 69de9dd4 2021-09-03 stsp
3162 69de9dd4 2021-09-03 stsp return NULL;
3163 69de9dd4 2021-09-03 stsp }
3164 69de9dd4 2021-09-03 stsp
3165 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3166 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3167 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3168 234035bc 2019-06-01 stsp struct got_repository *repo;
3169 234035bc 2019-06-01 stsp };
3170 234035bc 2019-06-01 stsp
3171 234035bc 2019-06-01 stsp static const struct got_error *
3172 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3173 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3174 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3175 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3176 234035bc 2019-06-01 stsp {
3177 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3178 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3179 234035bc 2019-06-01 stsp unsigned char status;
3180 234035bc 2019-06-01 stsp struct stat sb;
3181 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3182 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3183 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3184 234035bc 2019-06-01 stsp char *ondisk_path;
3185 234035bc 2019-06-01 stsp
3186 69de9dd4 2021-09-03 stsp if (id == NULL)
3187 69de9dd4 2021-09-03 stsp return NULL;
3188 234035bc 2019-06-01 stsp
3189 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3190 69de9dd4 2021-09-03 stsp if (ie == NULL)
3191 69de9dd4 2021-09-03 stsp return NULL;
3192 69de9dd4 2021-09-03 stsp
3193 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3194 234035bc 2019-06-01 stsp == -1)
3195 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3196 234035bc 2019-06-01 stsp
3197 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3198 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3199 5546d466 2021-09-02 stsp free(ondisk_path);
3200 234035bc 2019-06-01 stsp if (err)
3201 234035bc 2019-06-01 stsp return err;
3202 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3203 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3204 234035bc 2019-06-01 stsp
3205 234035bc 2019-06-01 stsp return NULL;
3206 234035bc 2019-06-01 stsp }
3207 234035bc 2019-06-01 stsp
3208 818c7501 2019-07-11 stsp static const struct got_error *
3209 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3210 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3211 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3212 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3213 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3214 234035bc 2019-06-01 stsp {
3215 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3216 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3217 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3218 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3219 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3220 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3221 234035bc 2019-06-01 stsp
3222 03415a1a 2019-06-02 stsp if (commit_id1) {
3223 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3224 03415a1a 2019-06-02 stsp worktree->path_prefix);
3225 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3226 03415a1a 2019-06-02 stsp goto done;
3227 69d57f3d 2020-07-31 stsp }
3228 69d57f3d 2020-07-31 stsp if (tree_id1) {
3229 69d57f3d 2020-07-31 stsp char *id_str;
3230 03415a1a 2019-06-02 stsp
3231 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3232 03415a1a 2019-06-02 stsp if (err)
3233 03415a1a 2019-06-02 stsp goto done;
3234 f69721c3 2019-10-21 stsp
3235 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3236 f69721c3 2019-10-21 stsp if (err)
3237 f69721c3 2019-10-21 stsp goto done;
3238 f69721c3 2019-10-21 stsp
3239 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3240 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3241 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3242 f69721c3 2019-10-21 stsp free(id_str);
3243 f69721c3 2019-10-21 stsp goto done;
3244 f69721c3 2019-10-21 stsp }
3245 f69721c3 2019-10-21 stsp free(id_str);
3246 03415a1a 2019-06-02 stsp }
3247 234035bc 2019-06-01 stsp
3248 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3249 234035bc 2019-06-01 stsp worktree->path_prefix);
3250 234035bc 2019-06-01 stsp if (err)
3251 234035bc 2019-06-01 stsp goto done;
3252 234035bc 2019-06-01 stsp
3253 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3254 234035bc 2019-06-01 stsp if (err)
3255 234035bc 2019-06-01 stsp goto done;
3256 234035bc 2019-06-01 stsp
3257 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3258 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3259 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3260 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3261 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3262 69de9dd4 2021-09-03 stsp if (err)
3263 69de9dd4 2021-09-03 stsp goto done;
3264 69de9dd4 2021-09-03 stsp
3265 234035bc 2019-06-01 stsp arg.worktree = worktree;
3266 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3267 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3268 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3269 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3270 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3271 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3272 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3273 5267b9e4 2021-09-26 stsp arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3274 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3275 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3276 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3277 af12c6b9 2019-06-04 stsp err = sync_err;
3278 234035bc 2019-06-01 stsp done:
3279 234035bc 2019-06-01 stsp if (tree1)
3280 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3281 234035bc 2019-06-01 stsp if (tree2)
3282 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3283 f69721c3 2019-10-21 stsp free(label_orig);
3284 818c7501 2019-07-11 stsp return err;
3285 818c7501 2019-07-11 stsp }
3286 234035bc 2019-06-01 stsp
3287 818c7501 2019-07-11 stsp const struct got_error *
3288 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3289 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3290 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3291 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3292 818c7501 2019-07-11 stsp {
3293 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3294 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3295 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3296 818c7501 2019-07-11 stsp
3297 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3298 818c7501 2019-07-11 stsp if (err)
3299 818c7501 2019-07-11 stsp return err;
3300 818c7501 2019-07-11 stsp
3301 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3302 818c7501 2019-07-11 stsp if (err)
3303 818c7501 2019-07-11 stsp goto done;
3304 818c7501 2019-07-11 stsp
3305 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3306 69de9dd4 2021-09-03 stsp worktree);
3307 818c7501 2019-07-11 stsp if (err)
3308 818c7501 2019-07-11 stsp goto done;
3309 818c7501 2019-07-11 stsp
3310 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3311 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3312 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3313 818c7501 2019-07-11 stsp done:
3314 818c7501 2019-07-11 stsp if (fileindex)
3315 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3316 818c7501 2019-07-11 stsp free(fileindex_path);
3317 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3318 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3319 234035bc 2019-06-01 stsp err = unlockerr;
3320 234035bc 2019-06-01 stsp return err;
3321 234035bc 2019-06-01 stsp }
3322 234035bc 2019-06-01 stsp
3323 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3324 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3325 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3326 927df6b7 2019-02-10 stsp const char *status_path;
3327 927df6b7 2019-02-10 stsp size_t status_path_len;
3328 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3329 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3330 f8d1f275 2019-02-04 stsp void *status_arg;
3331 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3332 f8d1f275 2019-02-04 stsp void *cancel_arg;
3333 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3334 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3335 f2a9dc41 2019-12-13 tracey int report_unchanged;
3336 3143d852 2020-06-25 stsp int no_ignores;
3337 f8d1f275 2019-02-04 stsp };
3338 88d0e355 2019-08-03 stsp
3339 f8d1f275 2019-02-04 stsp static const struct got_error *
3340 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3341 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3342 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3343 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3344 927df6b7 2019-02-10 stsp {
3345 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3346 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3347 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3348 927df6b7 2019-02-10 stsp struct stat sb;
3349 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3350 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3351 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3352 927df6b7 2019-02-10 stsp
3353 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3354 98eaaa12 2019-08-03 stsp if (err)
3355 98eaaa12 2019-08-03 stsp return err;
3356 98eaaa12 2019-08-03 stsp
3357 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3358 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3359 98eaaa12 2019-08-03 stsp return NULL;
3360 98eaaa12 2019-08-03 stsp
3361 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3362 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3363 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3364 98eaaa12 2019-08-03 stsp }
3365 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3366 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3367 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3368 927df6b7 2019-02-10 stsp }
3369 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3370 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3371 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3372 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3373 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3374 98eaaa12 2019-08-03 stsp }
3375 98eaaa12 2019-08-03 stsp
3376 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3377 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3378 927df6b7 2019-02-10 stsp }
3379 927df6b7 2019-02-10 stsp
3380 927df6b7 2019-02-10 stsp static const struct got_error *
3381 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3382 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3383 f8d1f275 2019-02-04 stsp {
3384 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3385 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3386 f8d1f275 2019-02-04 stsp char *abspath;
3387 f8d1f275 2019-02-04 stsp
3388 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3389 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3390 0584f854 2019-04-06 stsp
3391 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3392 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3393 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3394 927df6b7 2019-02-10 stsp return NULL;
3395 927df6b7 2019-02-10 stsp
3396 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3397 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3398 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3399 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3400 f8d1f275 2019-02-04 stsp } else {
3401 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3402 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3403 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3404 f8d1f275 2019-02-04 stsp }
3405 f8d1f275 2019-02-04 stsp
3406 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3407 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3408 f8d1f275 2019-02-04 stsp free(abspath);
3409 9d31a1d8 2018-03-11 stsp return err;
3410 9d31a1d8 2018-03-11 stsp }
3411 f8d1f275 2019-02-04 stsp
3412 f8d1f275 2019-02-04 stsp static const struct got_error *
3413 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3414 f8d1f275 2019-02-04 stsp {
3415 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3416 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3417 2ec1f75b 2019-03-26 stsp unsigned char status;
3418 927df6b7 2019-02-10 stsp
3419 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3420 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3421 0584f854 2019-04-06 stsp
3422 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3423 927df6b7 2019-02-10 stsp return NULL;
3424 927df6b7 2019-02-10 stsp
3425 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3426 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3427 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3428 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3429 2ec1f75b 2019-03-26 stsp else
3430 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3431 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3432 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3433 6841da00 2019-08-08 stsp }
3434 6841da00 2019-08-08 stsp
3435 6841da00 2019-08-08 stsp void
3436 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3437 6841da00 2019-08-08 stsp {
3438 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3439 6841da00 2019-08-08 stsp
3440 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3441 6841da00 2019-08-08 stsp free((char *)pe->path);
3442 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3443 6841da00 2019-08-08 stsp }
3444 6841da00 2019-08-08 stsp
3445 6841da00 2019-08-08 stsp void
3446 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3447 6841da00 2019-08-08 stsp {
3448 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3449 6841da00 2019-08-08 stsp
3450 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3451 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3452 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3453 6841da00 2019-08-08 stsp free((char *)pe->path);
3454 6841da00 2019-08-08 stsp }
3455 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3456 6841da00 2019-08-08 stsp }
3457 6841da00 2019-08-08 stsp
3458 6841da00 2019-08-08 stsp static const struct got_error *
3459 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3460 6841da00 2019-08-08 stsp {
3461 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3462 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3463 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3464 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3465 6841da00 2019-08-08 stsp size_t linesize = 0;
3466 6841da00 2019-08-08 stsp ssize_t linelen;
3467 6841da00 2019-08-08 stsp
3468 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3469 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3470 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3471 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3472 6841da00 2019-08-08 stsp
3473 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3474 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3475 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3476 bd8de430 2019-10-04 stsp
3477 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3478 bd8de430 2019-10-04 stsp if (line[0] == '#')
3479 bd8de430 2019-10-04 stsp continue;
3480 bd8de430 2019-10-04 stsp
3481 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3482 bd8de430 2019-10-04 stsp if (line[0] == '!')
3483 bd8de430 2019-10-04 stsp continue;
3484 bd8de430 2019-10-04 stsp
3485 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3486 6841da00 2019-08-08 stsp line) == -1) {
3487 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3488 6841da00 2019-08-08 stsp goto done;
3489 6841da00 2019-08-08 stsp }
3490 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3491 6841da00 2019-08-08 stsp if (err)
3492 6841da00 2019-08-08 stsp goto done;
3493 6841da00 2019-08-08 stsp }
3494 6841da00 2019-08-08 stsp if (ferror(f)) {
3495 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3496 6841da00 2019-08-08 stsp goto done;
3497 6841da00 2019-08-08 stsp }
3498 6841da00 2019-08-08 stsp
3499 6841da00 2019-08-08 stsp dirpath = strdup(path);
3500 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3501 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3502 6841da00 2019-08-08 stsp goto done;
3503 6841da00 2019-08-08 stsp }
3504 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3505 6841da00 2019-08-08 stsp done:
3506 6841da00 2019-08-08 stsp free(line);
3507 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3508 6841da00 2019-08-08 stsp free(dirpath);
3509 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3510 6841da00 2019-08-08 stsp }
3511 6841da00 2019-08-08 stsp return err;
3512 6841da00 2019-08-08 stsp }
3513 6841da00 2019-08-08 stsp
3514 6841da00 2019-08-08 stsp int
3515 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3516 6841da00 2019-08-08 stsp {
3517 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3518 bd8de430 2019-10-04 stsp
3519 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3520 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3521 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3522 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3523 bd8de430 2019-10-04 stsp
3524 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3525 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3526 6841da00 2019-08-08 stsp
3527 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3528 bd8de430 2019-10-04 stsp continue;
3529 bd8de430 2019-10-04 stsp pattern += 3;
3530 bd8de430 2019-10-04 stsp p = path;
3531 bd8de430 2019-10-04 stsp while (*p) {
3532 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3533 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3534 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3535 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3536 bd8de430 2019-10-04 stsp p++;
3537 bd8de430 2019-10-04 stsp while (*p == '/')
3538 bd8de430 2019-10-04 stsp p++;
3539 bd8de430 2019-10-04 stsp continue;
3540 bd8de430 2019-10-04 stsp }
3541 bd8de430 2019-10-04 stsp return 1;
3542 bd8de430 2019-10-04 stsp }
3543 bd8de430 2019-10-04 stsp }
3544 bd8de430 2019-10-04 stsp }
3545 bd8de430 2019-10-04 stsp
3546 6841da00 2019-08-08 stsp /*
3547 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3548 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3549 6841da00 2019-08-08 stsp * ignores backwards.
3550 6841da00 2019-08-08 stsp */
3551 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3552 6841da00 2019-08-08 stsp while (pe) {
3553 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3554 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3555 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3556 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3557 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3558 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3559 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3560 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3561 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3562 6841da00 2019-08-08 stsp continue;
3563 6841da00 2019-08-08 stsp return 1;
3564 6841da00 2019-08-08 stsp }
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3567 6841da00 2019-08-08 stsp }
3568 6841da00 2019-08-08 stsp
3569 6841da00 2019-08-08 stsp return 0;
3570 6841da00 2019-08-08 stsp }
3571 6841da00 2019-08-08 stsp
3572 6841da00 2019-08-08 stsp static const struct got_error *
3573 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3574 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3575 6841da00 2019-08-08 stsp {
3576 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3577 6841da00 2019-08-08 stsp char *ignorespath;
3578 886cec17 2019-12-15 stsp int fd = -1;
3579 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3580 6841da00 2019-08-08 stsp
3581 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3582 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3583 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3584 6841da00 2019-08-08 stsp
3585 886cec17 2019-12-15 stsp if (dirfd != -1) {
3586 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3587 886cec17 2019-12-15 stsp if (fd == -1) {
3588 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3589 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3590 886cec17 2019-12-15 stsp ignorespath);
3591 886cec17 2019-12-15 stsp } else {
3592 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3593 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3594 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3595 886cec17 2019-12-15 stsp ignorespath);
3596 886cec17 2019-12-15 stsp else {
3597 886cec17 2019-12-15 stsp fd = -1;
3598 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3599 886cec17 2019-12-15 stsp }
3600 886cec17 2019-12-15 stsp }
3601 886cec17 2019-12-15 stsp } else {
3602 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3603 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3604 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3605 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3606 886cec17 2019-12-15 stsp ignorespath);
3607 886cec17 2019-12-15 stsp } else
3608 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3609 886cec17 2019-12-15 stsp }
3610 6841da00 2019-08-08 stsp
3611 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3612 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3613 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3614 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3615 6841da00 2019-08-08 stsp free(ignorespath);
3616 6841da00 2019-08-08 stsp return err;
3617 f8d1f275 2019-02-04 stsp }
3618 f8d1f275 2019-02-04 stsp
3619 f8d1f275 2019-02-04 stsp static const struct got_error *
3620 62da3196 2021-10-01 stsp status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3621 62da3196 2021-10-01 stsp int dirfd)
3622 f8d1f275 2019-02-04 stsp {
3623 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3624 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3625 f8d1f275 2019-02-04 stsp char *path = NULL;
3626 62da3196 2021-10-01 stsp
3627 62da3196 2021-10-01 stsp if (ignore != NULL)
3628 62da3196 2021-10-01 stsp *ignore = 0;
3629 f8d1f275 2019-02-04 stsp
3630 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3631 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3632 0584f854 2019-04-06 stsp
3633 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3634 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3635 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3636 f8d1f275 2019-02-04 stsp } else {
3637 f8d1f275 2019-02-04 stsp path = de->d_name;
3638 f8d1f275 2019-02-04 stsp }
3639 f8d1f275 2019-02-04 stsp
3640 62da3196 2021-10-01 stsp if (de->d_type == DT_DIR) {
3641 62da3196 2021-10-01 stsp if (!a->no_ignores && ignore != NULL &&
3642 62da3196 2021-10-01 stsp match_ignores(a->ignores, path))
3643 62da3196 2021-10-01 stsp *ignore = 1;
3644 62da3196 2021-10-01 stsp } else if (!match_ignores(a->ignores, path) &&
3645 62da3196 2021-10-01 stsp got_path_is_child(path, a->status_path, a->status_path_len))
3646 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3647 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3648 f8d1f275 2019-02-04 stsp if (parent_path[0])
3649 f8d1f275 2019-02-04 stsp free(path);
3650 b72f483a 2019-02-05 stsp return err;
3651 f8d1f275 2019-02-04 stsp }
3652 f8d1f275 2019-02-04 stsp
3653 347d1d3e 2019-07-12 stsp static const struct got_error *
3654 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3655 3143d852 2020-06-25 stsp {
3656 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3657 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3658 3143d852 2020-06-25 stsp
3659 3143d852 2020-06-25 stsp if (a->no_ignores)
3660 3143d852 2020-06-25 stsp return NULL;
3661 3143d852 2020-06-25 stsp
3662 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3663 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3664 3143d852 2020-06-25 stsp if (err)
3665 3143d852 2020-06-25 stsp return err;
3666 3143d852 2020-06-25 stsp
3667 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3668 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3669 3143d852 2020-06-25 stsp
3670 3143d852 2020-06-25 stsp return err;
3671 3143d852 2020-06-25 stsp }
3672 3143d852 2020-06-25 stsp
3673 3143d852 2020-06-25 stsp static const struct got_error *
3674 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3675 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3676 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3677 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3678 abb4604f 2019-07-27 stsp {
3679 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3680 abb4604f 2019-07-27 stsp struct stat sb;
3681 abb4604f 2019-07-27 stsp
3682 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3683 ff56836b 2021-07-08 stsp return NULL;
3684 ff56836b 2021-07-08 stsp
3685 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3686 abb4604f 2019-07-27 stsp if (ie)
3687 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3688 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3689 abb4604f 2019-07-27 stsp
3690 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3691 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3692 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3693 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3694 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3695 abb4604f 2019-07-27 stsp return NULL;
3696 abb4604f 2019-07-27 stsp }
3697 abb4604f 2019-07-27 stsp
3698 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3699 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3700 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3701 abb4604f 2019-07-27 stsp
3702 abb4604f 2019-07-27 stsp return NULL;
3703 3143d852 2020-06-25 stsp }
3704 3143d852 2020-06-25 stsp
3705 3143d852 2020-06-25 stsp static const struct got_error *
3706 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3707 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3708 3143d852 2020-06-25 stsp {
3709 3143d852 2020-06-25 stsp const struct got_error *err;
3710 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3711 3143d852 2020-06-25 stsp
3712 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3713 3143d852 2020-06-25 stsp ".cvsignore");
3714 3143d852 2020-06-25 stsp if (err)
3715 3143d852 2020-06-25 stsp return err;
3716 3143d852 2020-06-25 stsp
3717 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3718 3143d852 2020-06-25 stsp ".gitignore");
3719 3143d852 2020-06-25 stsp if (err)
3720 3143d852 2020-06-25 stsp return err;
3721 3143d852 2020-06-25 stsp
3722 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3723 3143d852 2020-06-25 stsp if (err) {
3724 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3725 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3726 3143d852 2020-06-25 stsp return err;
3727 3143d852 2020-06-25 stsp }
3728 3143d852 2020-06-25 stsp for (;;) {
3729 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3730 3143d852 2020-06-25 stsp ".cvsignore");
3731 3143d852 2020-06-25 stsp if (err)
3732 3143d852 2020-06-25 stsp break;
3733 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3734 3143d852 2020-06-25 stsp ".gitignore");
3735 3143d852 2020-06-25 stsp if (err)
3736 3143d852 2020-06-25 stsp break;
3737 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3738 3143d852 2020-06-25 stsp if (err) {
3739 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3740 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3741 3143d852 2020-06-25 stsp break;
3742 3143d852 2020-06-25 stsp }
3743 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3744 ff56836b 2021-07-08 stsp break;
3745 b737c85a 2020-06-26 stsp free(parent_path);
3746 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3747 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3748 3143d852 2020-06-25 stsp }
3749 3143d852 2020-06-25 stsp
3750 b737c85a 2020-06-26 stsp free(parent_path);
3751 b737c85a 2020-06-26 stsp free(next_parent_path);
3752 3143d852 2020-06-25 stsp return err;
3753 abb4604f 2019-07-27 stsp }
3754 abb4604f 2019-07-27 stsp
3755 abb4604f 2019-07-27 stsp static const struct got_error *
3756 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3757 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3758 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3759 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3760 f2a9dc41 2019-12-13 tracey int report_unchanged)
3761 f8d1f275 2019-02-04 stsp {
3762 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3763 6fc93f37 2019-12-13 stsp int fd = -1;
3764 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3765 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3766 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3767 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3768 3143d852 2020-06-25 stsp
3769 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3770 f8d1f275 2019-02-04 stsp
3771 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3772 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3773 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3774 8dc303cc 2019-07-27 stsp
3775 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3776 6fc93f37 2019-12-13 stsp if (fd == -1) {
3777 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3778 5c02d2a5 2021-09-26 stsp !got_err_open_nofollow_on_symlink())
3779 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3780 ff56836b 2021-07-08 stsp else {
3781 ff56836b 2021-07-08 stsp if (!no_ignores) {
3782 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3783 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3784 ff56836b 2021-07-08 stsp if (err)
3785 ff56836b 2021-07-08 stsp goto done;
3786 ff56836b 2021-07-08 stsp }
3787 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3788 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3789 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3790 ff56836b 2021-07-08 stsp }
3791 abb4604f 2019-07-27 stsp } else {
3792 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3793 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3794 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3795 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3796 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3797 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3798 abb4604f 2019-07-27 stsp arg.status_path = path;
3799 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3800 abb4604f 2019-07-27 stsp arg.repo = repo;
3801 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3802 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3803 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3804 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3805 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3806 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3807 022fae89 2019-12-06 tracey if (!no_ignores) {
3808 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3809 3143d852 2020-06-25 stsp worktree->root_path, path);
3810 3143d852 2020-06-25 stsp if (err)
3811 3143d852 2020-06-25 stsp goto done;
3812 022fae89 2019-12-06 tracey }
3813 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3814 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3815 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3816 927df6b7 2019-02-10 stsp }
3817 3143d852 2020-06-25 stsp done:
3818 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3819 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3820 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3821 927df6b7 2019-02-10 stsp free(ondisk_path);
3822 347d1d3e 2019-07-12 stsp return err;
3823 347d1d3e 2019-07-12 stsp }
3824 347d1d3e 2019-07-12 stsp
3825 347d1d3e 2019-07-12 stsp const struct got_error *
3826 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3827 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3828 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3829 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3830 347d1d3e 2019-07-12 stsp {
3831 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3832 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3833 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3834 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3835 347d1d3e 2019-07-12 stsp
3836 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3837 347d1d3e 2019-07-12 stsp if (err)
3838 347d1d3e 2019-07-12 stsp return err;
3839 347d1d3e 2019-07-12 stsp
3840 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3841 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3842 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3843 f6343036 2021-06-22 stsp no_ignores, 0);
3844 72ea6654 2019-07-27 stsp if (err)
3845 72ea6654 2019-07-27 stsp break;
3846 72ea6654 2019-07-27 stsp }
3847 f8d1f275 2019-02-04 stsp free(fileindex_path);
3848 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3849 f8d1f275 2019-02-04 stsp return err;
3850 f8d1f275 2019-02-04 stsp }
3851 6c7ab921 2019-03-18 stsp
3852 6c7ab921 2019-03-18 stsp const struct got_error *
3853 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3854 6c7ab921 2019-03-18 stsp const char *arg)
3855 6c7ab921 2019-03-18 stsp {
3856 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3857 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3858 6c7ab921 2019-03-18 stsp size_t len;
3859 00bb5ea0 2020-07-23 stsp struct stat sb;
3860 01740607 2020-11-04 stsp char *abspath = NULL;
3861 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3862 6c7ab921 2019-03-18 stsp
3863 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3864 6c7ab921 2019-03-18 stsp
3865 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3866 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3867 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3868 00bb5ea0 2020-07-23 stsp
3869 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3870 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3871 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3872 00bb5ea0 2020-07-23 stsp goto done;
3873 00bb5ea0 2020-07-23 stsp }
3874 727173c3 2020-11-06 stsp sb.st_mode = 0;
3875 00bb5ea0 2020-07-23 stsp }
3876 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3877 00bb5ea0 2020-07-23 stsp /*
3878 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3879 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3880 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3881 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3882 00bb5ea0 2020-07-23 stsp */
3883 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3884 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3885 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3886 00bb5ea0 2020-07-23 stsp goto done;
3887 00bb5ea0 2020-07-23 stsp }
3888 00bb5ea0 2020-07-23 stsp
3889 00bb5ea0 2020-07-23 stsp }
3890 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3891 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3892 00bb5ea0 2020-07-23 stsp if (err)
3893 d0710d08 2019-07-22 stsp goto done;
3894 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3895 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3896 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3897 00bb5ea0 2020-07-23 stsp goto done;
3898 00bb5ea0 2020-07-23 stsp }
3899 00bb5ea0 2020-07-23 stsp } else {
3900 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3901 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3902 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3903 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3904 00bb5ea0 2020-07-23 stsp goto done;
3905 00bb5ea0 2020-07-23 stsp }
3906 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3907 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3908 01740607 2020-11-04 stsp goto done;
3909 01740607 2020-11-04 stsp }
3910 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3911 01740607 2020-11-04 stsp sizeof(canonpath));
3912 01740607 2020-11-04 stsp if (err)
3913 00bb5ea0 2020-07-23 stsp goto done;
3914 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3915 01740607 2020-11-04 stsp if (resolved == NULL) {
3916 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3917 01740607 2020-11-04 stsp goto done;
3918 00bb5ea0 2020-07-23 stsp }
3919 d0710d08 2019-07-22 stsp }
3920 d0710d08 2019-07-22 stsp }
3921 6c7ab921 2019-03-18 stsp
3922 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3923 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3924 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3925 6c7ab921 2019-03-18 stsp goto done;
3926 6c7ab921 2019-03-18 stsp }
3927 6c7ab921 2019-03-18 stsp
3928 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3929 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3930 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3931 f6d88e1a 2019-05-29 stsp if (err)
3932 f6d88e1a 2019-05-29 stsp goto done;
3933 f6d88e1a 2019-05-29 stsp } else {
3934 f6d88e1a 2019-05-29 stsp path = strdup("");
3935 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3936 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3937 f6d88e1a 2019-05-29 stsp goto done;
3938 f6d88e1a 2019-05-29 stsp }
3939 6c7ab921 2019-03-18 stsp }
3940 6c7ab921 2019-03-18 stsp
3941 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3942 6c7ab921 2019-03-18 stsp len = strlen(path);
3943 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3944 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3945 6c7ab921 2019-03-18 stsp len--;
3946 6c7ab921 2019-03-18 stsp }
3947 6c7ab921 2019-03-18 stsp done:
3948 01740607 2020-11-04 stsp free(abspath);
3949 6c7ab921 2019-03-18 stsp free(resolved);
3950 d0710d08 2019-07-22 stsp free(cwd);
3951 6c7ab921 2019-03-18 stsp if (err == NULL)
3952 6c7ab921 2019-03-18 stsp *wt_path = path;
3953 6c7ab921 2019-03-18 stsp else
3954 6c7ab921 2019-03-18 stsp free(path);
3955 d00136be 2019-03-26 stsp return err;
3956 d00136be 2019-03-26 stsp }
3957 d00136be 2019-03-26 stsp
3958 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3959 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3960 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3961 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3962 4e68cba3 2019-11-23 stsp void *progress_arg;
3963 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3964 4e68cba3 2019-11-23 stsp };
3965 4e68cba3 2019-11-23 stsp
3966 4e68cba3 2019-11-23 stsp static const struct got_error *
3967 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3968 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3969 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3970 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3971 07f5b47a 2019-06-02 stsp {
3972 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3973 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3974 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3975 6d022e97 2019-08-04 stsp struct stat sb;
3976 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3977 07f5b47a 2019-06-02 stsp
3978 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3979 dbb83fbd 2019-12-12 stsp relpath) == -1)
3980 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3981 dbb83fbd 2019-12-12 stsp
3982 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3983 6d022e97 2019-08-04 stsp if (ie) {
3984 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3985 12463d8b 2019-12-13 stsp de_name, a->repo);
3986 6d022e97 2019-08-04 stsp if (err)
3987 dbb83fbd 2019-12-12 stsp goto done;
3988 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3989 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3990 dbb83fbd 2019-12-12 stsp goto done;
3991 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3992 dbb83fbd 2019-12-12 stsp if (err)
3993 dbb83fbd 2019-12-12 stsp goto done;
3994 6d022e97 2019-08-04 stsp }
3995 07f5b47a 2019-06-02 stsp
3996 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3997 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3998 dbb83fbd 2019-12-12 stsp goto done;
3999 4e68cba3 2019-11-23 stsp }
4000 07f5b47a 2019-06-02 stsp
4001 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
4002 4e68cba3 2019-11-23 stsp if (err)
4003 4e68cba3 2019-11-23 stsp goto done;
4004 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4005 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
4006 3969253a 2020-03-07 stsp if (err) {
4007 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4008 3969253a 2020-03-07 stsp goto done;
4009 3969253a 2020-03-07 stsp }
4010 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
4011 07f5b47a 2019-06-02 stsp if (err) {
4012 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4013 4e68cba3 2019-11-23 stsp goto done;
4014 07f5b47a 2019-06-02 stsp }
4015 4e68cba3 2019-11-23 stsp done:
4016 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4017 dbb83fbd 2019-12-12 stsp if (err)
4018 dbb83fbd 2019-12-12 stsp return err;
4019 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4020 dbb83fbd 2019-12-12 stsp return NULL;
4021 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4022 07f5b47a 2019-06-02 stsp }
4023 07f5b47a 2019-06-02 stsp
4024 d00136be 2019-03-26 stsp const struct got_error *
4025 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4026 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4027 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4028 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4029 d00136be 2019-03-26 stsp {
4030 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4031 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4032 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4033 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4034 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4035 d00136be 2019-03-26 stsp
4036 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4037 d00136be 2019-03-26 stsp if (err)
4038 d00136be 2019-03-26 stsp return err;
4039 d00136be 2019-03-26 stsp
4040 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4041 d00136be 2019-03-26 stsp if (err)
4042 d00136be 2019-03-26 stsp goto done;
4043 d00136be 2019-03-26 stsp
4044 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4045 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4046 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4047 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4048 4e68cba3 2019-11-23 stsp saa.repo = repo;
4049 4e68cba3 2019-11-23 stsp
4050 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4051 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4052 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4053 1dd54920 2019-05-11 stsp if (err)
4054 af12c6b9 2019-06-04 stsp break;
4055 1dd54920 2019-05-11 stsp }
4056 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4057 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4058 af12c6b9 2019-06-04 stsp err = sync_err;
4059 d00136be 2019-03-26 stsp done:
4060 fb399478 2019-07-12 stsp free(fileindex_path);
4061 d00136be 2019-03-26 stsp if (fileindex)
4062 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4063 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4064 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4065 d00136be 2019-03-26 stsp err = unlockerr;
4066 6c7ab921 2019-03-18 stsp return err;
4067 6c7ab921 2019-03-18 stsp }
4068 17ed4618 2019-06-02 stsp
4069 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4070 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4071 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4072 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4073 f2a9dc41 2019-12-13 tracey void *progress_arg;
4074 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4075 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4076 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4077 766841c2 2020-08-13 stsp const char *status_codes;
4078 f2a9dc41 2019-12-13 tracey };
4079 f2a9dc41 2019-12-13 tracey
4080 f2a9dc41 2019-12-13 tracey static const struct got_error *
4081 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4082 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4083 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4084 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4085 17ed4618 2019-06-02 stsp {
4086 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4087 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4088 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4089 17ed4618 2019-06-02 stsp struct stat sb;
4090 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4091 17ed4618 2019-06-02 stsp
4092 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4093 17ed4618 2019-06-02 stsp if (ie == NULL)
4094 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4095 2ec1f75b 2019-03-26 stsp
4096 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4097 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4098 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4099 9acbc4fa 2019-08-03 stsp return NULL;
4100 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4101 9acbc4fa 2019-08-03 stsp }
4102 9acbc4fa 2019-08-03 stsp
4103 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4104 f2a9dc41 2019-12-13 tracey relpath) == -1)
4105 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4106 f2a9dc41 2019-12-13 tracey
4107 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4108 12463d8b 2019-12-13 stsp a->repo);
4109 17ed4618 2019-06-02 stsp if (err)
4110 f2a9dc41 2019-12-13 tracey goto done;
4111 17ed4618 2019-06-02 stsp
4112 766841c2 2020-08-13 stsp if (a->status_codes) {
4113 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4114 766841c2 2020-08-13 stsp int i;
4115 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4116 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4117 766841c2 2020-08-13 stsp break;
4118 766841c2 2020-08-13 stsp }
4119 766841c2 2020-08-13 stsp if (i == ncodes) {
4120 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4121 766841c2 2020-08-13 stsp free(ondisk_path);
4122 766841c2 2020-08-13 stsp return NULL;
4123 766841c2 2020-08-13 stsp }
4124 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4125 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4126 766841c2 2020-08-13 stsp static char msg[64];
4127 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4128 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4129 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4130 766841c2 2020-08-13 stsp goto done;
4131 766841c2 2020-08-13 stsp }
4132 766841c2 2020-08-13 stsp }
4133 766841c2 2020-08-13 stsp
4134 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4135 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4136 f2a9dc41 2019-12-13 tracey goto done;
4137 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4138 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4139 f2a9dc41 2019-12-13 tracey goto done;
4140 f2a9dc41 2019-12-13 tracey }
4141 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4142 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4143 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 f2a9dc41 2019-12-13 tracey goto done;
4145 f2a9dc41 2019-12-13 tracey }
4146 17ed4618 2019-06-02 stsp }
4147 17ed4618 2019-06-02 stsp
4148 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 20a2ad1c 2020-10-20 stsp size_t root_len;
4150 20a2ad1c 2020-10-20 stsp
4151 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4152 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4153 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4154 12463d8b 2019-12-13 stsp ondisk_path);
4155 12463d8b 2019-12-13 stsp goto done;
4156 12463d8b 2019-12-13 stsp }
4157 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4158 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4159 12463d8b 2019-12-13 stsp goto done;
4160 15341bfd 2020-03-05 tracey }
4161 15341bfd 2020-03-05 tracey
4162 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4163 20a2ad1c 2020-10-20 stsp do {
4164 20a2ad1c 2020-10-20 stsp char *parent;
4165 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4166 20a2ad1c 2020-10-20 stsp if (err)
4167 2513f20a 2020-10-20 stsp goto done;
4168 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4169 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4170 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4171 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4172 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4173 20a2ad1c 2020-10-20 stsp ondisk_path);
4174 15341bfd 2020-03-05 tracey break;
4175 15341bfd 2020-03-05 tracey }
4176 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4178 f2a9dc41 2019-12-13 tracey }
4179 17ed4618 2019-06-02 stsp
4180 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4181 f2a9dc41 2019-12-13 tracey done:
4182 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4183 f2a9dc41 2019-12-13 tracey if (err)
4184 f2a9dc41 2019-12-13 tracey return err;
4185 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4186 f2a9dc41 2019-12-13 tracey return NULL;
4187 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4189 17ed4618 2019-06-02 stsp }
4190 17ed4618 2019-06-02 stsp
4191 2ec1f75b 2019-03-26 stsp const struct got_error *
4192 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4193 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4194 766841c2 2020-08-13 stsp const char *status_codes,
4195 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4196 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4197 2ec1f75b 2019-03-26 stsp {
4198 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4199 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4200 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4202 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4203 2ec1f75b 2019-03-26 stsp
4204 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4205 2ec1f75b 2019-03-26 stsp if (err)
4206 2ec1f75b 2019-03-26 stsp return err;
4207 2ec1f75b 2019-03-26 stsp
4208 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 2ec1f75b 2019-03-26 stsp if (err)
4210 2ec1f75b 2019-03-26 stsp goto done;
4211 f2a9dc41 2019-12-13 tracey
4212 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4213 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4214 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4215 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4216 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4217 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4218 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4219 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4220 2ec1f75b 2019-03-26 stsp
4221 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4222 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4223 62da3196 2021-10-01 stsp schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4224 17ed4618 2019-06-02 stsp if (err)
4225 af12c6b9 2019-06-04 stsp break;
4226 2ec1f75b 2019-03-26 stsp }
4227 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4228 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4229 af12c6b9 2019-06-04 stsp err = sync_err;
4230 2ec1f75b 2019-03-26 stsp done:
4231 fb399478 2019-07-12 stsp free(fileindex_path);
4232 2ec1f75b 2019-03-26 stsp if (fileindex)
4233 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4234 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4235 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4236 2ec1f75b 2019-03-26 stsp err = unlockerr;
4237 33aa809d 2019-08-08 stsp return err;
4238 33aa809d 2019-08-08 stsp }
4239 33aa809d 2019-08-08 stsp
4240 33aa809d 2019-08-08 stsp static const struct got_error *
4241 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4242 33aa809d 2019-08-08 stsp {
4243 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4244 33aa809d 2019-08-08 stsp char *line = NULL;
4245 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4246 33aa809d 2019-08-08 stsp ssize_t linelen;
4247 33aa809d 2019-08-08 stsp
4248 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4249 33aa809d 2019-08-08 stsp if (linelen == -1) {
4250 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4251 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4252 33aa809d 2019-08-08 stsp goto done;
4253 33aa809d 2019-08-08 stsp }
4254 33aa809d 2019-08-08 stsp return NULL;
4255 33aa809d 2019-08-08 stsp }
4256 33aa809d 2019-08-08 stsp if (outfile) {
4257 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4258 33aa809d 2019-08-08 stsp if (n != linelen) {
4259 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4260 33aa809d 2019-08-08 stsp goto done;
4261 33aa809d 2019-08-08 stsp }
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp if (rejectfile) {
4264 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4265 33aa809d 2019-08-08 stsp if (n != linelen)
4266 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4267 33aa809d 2019-08-08 stsp }
4268 33aa809d 2019-08-08 stsp done:
4269 33aa809d 2019-08-08 stsp free(line);
4270 2ec1f75b 2019-03-26 stsp return err;
4271 2ec1f75b 2019-03-26 stsp }
4272 1f1abb7e 2019-08-08 stsp
4273 33aa809d 2019-08-08 stsp static const struct got_error *
4274 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4275 33aa809d 2019-08-08 stsp {
4276 33aa809d 2019-08-08 stsp char *line = NULL;
4277 33aa809d 2019-08-08 stsp size_t linesize = 0;
4278 33aa809d 2019-08-08 stsp ssize_t linelen;
4279 33aa809d 2019-08-08 stsp
4280 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4281 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4282 500467ff 2019-09-25 hiltjo if (ferror(f))
4283 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4284 500467ff 2019-09-25 hiltjo return NULL;
4285 500467ff 2019-09-25 hiltjo }
4286 33aa809d 2019-08-08 stsp free(line);
4287 33aa809d 2019-08-08 stsp return NULL;
4288 33aa809d 2019-08-08 stsp }
4289 33aa809d 2019-08-08 stsp
4290 33aa809d 2019-08-08 stsp static const struct got_error *
4291 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4292 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4293 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4294 abc59930 2021-09-05 naddy {
4295 33aa809d 2019-08-08 stsp const struct got_error *err;
4296 33aa809d 2019-08-08 stsp
4297 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4298 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4299 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4300 33aa809d 2019-08-08 stsp if (err)
4301 33aa809d 2019-08-08 stsp return err;
4302 33aa809d 2019-08-08 stsp (*line_cur1)++;
4303 33aa809d 2019-08-08 stsp }
4304 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4305 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4306 33aa809d 2019-08-08 stsp if (rejectfile)
4307 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4308 33aa809d 2019-08-08 stsp else
4309 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4310 33aa809d 2019-08-08 stsp if (err)
4311 33aa809d 2019-08-08 stsp return err;
4312 33aa809d 2019-08-08 stsp (*line_cur2)++;
4313 33aa809d 2019-08-08 stsp }
4314 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4315 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4316 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4317 33aa809d 2019-08-08 stsp if (err)
4318 33aa809d 2019-08-08 stsp return err;
4319 33aa809d 2019-08-08 stsp (*line_cur2)++;
4320 33aa809d 2019-08-08 stsp }
4321 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4322 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4323 33aa809d 2019-08-08 stsp if (rejectfile)
4324 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4325 33aa809d 2019-08-08 stsp else
4326 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4327 33aa809d 2019-08-08 stsp if (err)
4328 33aa809d 2019-08-08 stsp return err;
4329 33aa809d 2019-08-08 stsp (*line_cur1)++;
4330 33aa809d 2019-08-08 stsp }
4331 f1e81a05 2019-08-10 stsp
4332 f1e81a05 2019-08-10 stsp return NULL;
4333 f1e81a05 2019-08-10 stsp }
4334 f1e81a05 2019-08-10 stsp
4335 f1e81a05 2019-08-10 stsp static const struct got_error *
4336 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4338 f1e81a05 2019-08-10 stsp {
4339 f1e81a05 2019-08-10 stsp const struct got_error *err;
4340 f1e81a05 2019-08-10 stsp
4341 f1e81a05 2019-08-10 stsp if (outfile) {
4342 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4343 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4344 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4345 f1e81a05 2019-08-10 stsp if (err)
4346 f1e81a05 2019-08-10 stsp return err;
4347 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4348 f1e81a05 2019-08-10 stsp }
4349 f1e81a05 2019-08-10 stsp }
4350 f1e81a05 2019-08-10 stsp if (rejectfile) {
4351 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4352 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4353 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4354 f1e81a05 2019-08-10 stsp if (err)
4355 f1e81a05 2019-08-10 stsp return err;
4356 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4357 f1e81a05 2019-08-10 stsp }
4358 33aa809d 2019-08-08 stsp }
4359 33aa809d 2019-08-08 stsp
4360 33aa809d 2019-08-08 stsp return NULL;
4361 33aa809d 2019-08-08 stsp }
4362 33aa809d 2019-08-08 stsp
4363 33aa809d 2019-08-08 stsp static const struct got_error *
4364 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4365 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4366 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4367 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4368 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4369 33aa809d 2019-08-08 stsp {
4370 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4371 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4372 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4373 33aa809d 2019-08-08 stsp FILE *hunkfile;
4374 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4375 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4376 fe621944 2020-11-10 stsp int rc;
4377 33aa809d 2019-08-08 stsp
4378 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4379 33aa809d 2019-08-08 stsp
4380 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4381 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4382 fe621944 2020-11-10 stsp start_old = cc.left.start;
4383 fe621944 2020-11-10 stsp end_old = cc.left.end;
4384 fe621944 2020-11-10 stsp start_new = cc.right.start;
4385 fe621944 2020-11-10 stsp end_new = cc.right.end;
4386 33aa809d 2019-08-08 stsp
4387 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4388 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4389 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4390 33aa809d 2019-08-08 stsp
4391 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4392 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4393 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4394 33aa809d 2019-08-08 stsp
4395 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4396 fe621944 2020-11-10 stsp if (diff_state == NULL)
4397 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4398 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4399 fe621944 2020-11-10 stsp
4400 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4401 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4402 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4403 33aa809d 2019-08-08 stsp goto done;
4404 33aa809d 2019-08-08 stsp }
4405 fe621944 2020-11-10 stsp
4406 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4407 fe621944 2020-11-10 stsp diff_result, &cc);
4408 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4409 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4410 33aa809d 2019-08-08 stsp goto done;
4411 33aa809d 2019-08-08 stsp }
4412 fe621944 2020-11-10 stsp
4413 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4414 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4415 33aa809d 2019-08-08 stsp goto done;
4416 33aa809d 2019-08-08 stsp }
4417 33aa809d 2019-08-08 stsp
4418 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4419 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4420 33aa809d 2019-08-08 stsp if (err)
4421 33aa809d 2019-08-08 stsp goto done;
4422 33aa809d 2019-08-08 stsp
4423 33aa809d 2019-08-08 stsp switch (*choice) {
4424 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4425 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4427 33aa809d 2019-08-08 stsp break;
4428 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4429 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4430 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4431 33aa809d 2019-08-08 stsp break;
4432 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4433 33aa809d 2019-08-08 stsp break;
4434 33aa809d 2019-08-08 stsp default:
4435 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4436 33aa809d 2019-08-08 stsp break;
4437 33aa809d 2019-08-08 stsp }
4438 33aa809d 2019-08-08 stsp done:
4439 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4440 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4441 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4442 33aa809d 2019-08-08 stsp return err;
4443 33aa809d 2019-08-08 stsp }
4444 33aa809d 2019-08-08 stsp
4445 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4446 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4447 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4448 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4449 1f1abb7e 2019-08-08 stsp void *progress_arg;
4450 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4451 33aa809d 2019-08-08 stsp void *patch_arg;
4452 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4453 f259c4c1 2021-09-24 stsp int unlink_added_files;
4454 1f1abb7e 2019-08-08 stsp };
4455 a129376b 2019-03-28 stsp
4456 e20a8b6f 2019-06-04 stsp static const struct got_error *
4457 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4458 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4459 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4460 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4461 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4462 33aa809d 2019-08-08 stsp {
4463 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4464 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4465 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4466 1ebedb77 2019-10-19 stsp int fd2 = -1;
4467 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4468 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4469 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4470 fe621944 2020-11-10 stsp struct stat sb2;
4471 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4472 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4473 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4474 33aa809d 2019-08-08 stsp
4475 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4476 33aa809d 2019-08-08 stsp
4477 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4478 33aa809d 2019-08-08 stsp if (err)
4479 33aa809d 2019-08-08 stsp return err;
4480 33aa809d 2019-08-08 stsp
4481 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4482 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4483 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4484 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4485 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4486 fa3cef63 2020-07-23 stsp goto done;
4487 fa3cef63 2020-07-23 stsp }
4488 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4489 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4490 fa3cef63 2020-07-23 stsp if (link_len == -1)
4491 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4492 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4493 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4494 12463d8b 2019-12-13 stsp }
4495 12463d8b 2019-12-13 stsp } else {
4496 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4497 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4498 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4499 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4500 fa3cef63 2020-07-23 stsp goto done;
4501 fa3cef63 2020-07-23 stsp }
4502 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4503 fa3cef63 2020-07-23 stsp sizeof(link_target));
4504 fa3cef63 2020-07-23 stsp if (link_len == -1)
4505 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4506 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4507 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4508 12463d8b 2019-12-13 stsp }
4509 1ebedb77 2019-10-19 stsp }
4510 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4511 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4512 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4513 fa3cef63 2020-07-23 stsp goto done;
4514 fa3cef63 2020-07-23 stsp }
4515 1ebedb77 2019-10-19 stsp
4516 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4517 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4518 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4519 fa3cef63 2020-07-23 stsp goto done;
4520 fa3cef63 2020-07-23 stsp }
4521 fa3cef63 2020-07-23 stsp fd2 = -1;
4522 fa3cef63 2020-07-23 stsp } else {
4523 fa3cef63 2020-07-23 stsp size_t n;
4524 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4525 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4526 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4527 fa3cef63 2020-07-23 stsp goto done;
4528 fa3cef63 2020-07-23 stsp }
4529 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4530 fa3cef63 2020-07-23 stsp if (n != link_len) {
4531 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4532 fa3cef63 2020-07-23 stsp goto done;
4533 fa3cef63 2020-07-23 stsp }
4534 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4535 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4536 fa3cef63 2020-07-23 stsp goto done;
4537 fa3cef63 2020-07-23 stsp }
4538 fa3cef63 2020-07-23 stsp rewind(f2);
4539 33aa809d 2019-08-08 stsp }
4540 33aa809d 2019-08-08 stsp
4541 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4542 33aa809d 2019-08-08 stsp if (err)
4543 33aa809d 2019-08-08 stsp goto done;
4544 33aa809d 2019-08-08 stsp
4545 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4546 33aa809d 2019-08-08 stsp if (err)
4547 33aa809d 2019-08-08 stsp goto done;
4548 33aa809d 2019-08-08 stsp
4549 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4550 33aa809d 2019-08-08 stsp if (err)
4551 33aa809d 2019-08-08 stsp goto done;
4552 33aa809d 2019-08-08 stsp
4553 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4554 fe621944 2020-11-10 stsp NULL);
4555 33aa809d 2019-08-08 stsp if (err)
4556 33aa809d 2019-08-08 stsp goto done;
4557 33aa809d 2019-08-08 stsp
4558 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4559 33aa809d 2019-08-08 stsp if (err)
4560 33aa809d 2019-08-08 stsp goto done;
4561 33aa809d 2019-08-08 stsp
4562 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4563 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4564 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4565 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4566 fe621944 2020-11-10 stsp
4567 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4568 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4570 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4571 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4572 fe621944 2020-11-10 stsp nchanges++;
4573 fe621944 2020-11-10 stsp }
4574 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4575 33aa809d 2019-08-08 stsp int choice;
4576 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4577 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4578 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4579 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4580 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4581 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4582 33aa809d 2019-08-08 stsp if (err)
4583 33aa809d 2019-08-08 stsp goto done;
4584 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4585 33aa809d 2019-08-08 stsp have_content = 1;
4586 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4587 33aa809d 2019-08-08 stsp break;
4588 33aa809d 2019-08-08 stsp }
4589 1ebedb77 2019-10-19 stsp if (have_content) {
4590 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4591 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4592 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4593 1ebedb77 2019-10-19 stsp if (err)
4594 1ebedb77 2019-10-19 stsp goto done;
4595 1ebedb77 2019-10-19 stsp
4596 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4597 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4598 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4599 fa3cef63 2020-07-23 stsp goto done;
4600 fa3cef63 2020-07-23 stsp }
4601 1ebedb77 2019-10-19 stsp }
4602 1ebedb77 2019-10-19 stsp }
4603 33aa809d 2019-08-08 stsp done:
4604 33aa809d 2019-08-08 stsp free(id_str);
4605 33aa809d 2019-08-08 stsp if (blob)
4606 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4607 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4608 fe621944 2020-11-10 stsp if (err == NULL)
4609 fe621944 2020-11-10 stsp err = free_err;
4610 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4611 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4612 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4613 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4614 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4615 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4616 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4617 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4618 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4619 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4620 33aa809d 2019-08-08 stsp if (err || !have_content) {
4621 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4622 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4623 33aa809d 2019-08-08 stsp free(*path_outfile);
4624 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4625 33aa809d 2019-08-08 stsp }
4626 33aa809d 2019-08-08 stsp free(path1);
4627 33aa809d 2019-08-08 stsp return err;
4628 33aa809d 2019-08-08 stsp }
4629 33aa809d 2019-08-08 stsp
4630 33aa809d 2019-08-08 stsp static const struct got_error *
4631 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4632 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4633 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4634 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4635 a129376b 2019-03-28 stsp {
4636 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4637 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4638 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4639 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4640 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4641 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4642 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4643 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4644 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4645 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4646 a129376b 2019-03-28 stsp
4647 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4648 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4649 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4650 d3bcc3d1 2019-08-08 stsp return NULL;
4651 3d69ad8d 2019-08-17 semarie
4652 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4653 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4654 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4655 d3bcc3d1 2019-08-08 stsp
4656 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4657 65084dad 2019-08-08 stsp if (ie == NULL)
4658 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4659 a129376b 2019-03-28 stsp
4660 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4661 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4662 a129376b 2019-03-28 stsp if (err) {
4663 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4664 a129376b 2019-03-28 stsp goto done;
4665 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4666 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4667 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4668 e20a8b6f 2019-06-04 stsp goto done;
4669 e20a8b6f 2019-06-04 stsp }
4670 a129376b 2019-03-28 stsp }
4671 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4673 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4674 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4675 a129376b 2019-03-28 stsp goto done;
4676 a129376b 2019-03-28 stsp }
4677 a129376b 2019-03-28 stsp } else {
4678 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4679 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4680 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4681 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4682 a129376b 2019-03-28 stsp goto done;
4683 a129376b 2019-03-28 stsp }
4684 a129376b 2019-03-28 stsp } else {
4685 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4686 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4687 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4688 a129376b 2019-03-28 stsp goto done;
4689 a129376b 2019-03-28 stsp }
4690 a129376b 2019-03-28 stsp }
4691 a129376b 2019-03-28 stsp }
4692 a129376b 2019-03-28 stsp
4693 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4694 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4695 a9fa2909 2019-07-27 stsp if (err) {
4696 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4697 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4698 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4699 a9fa2909 2019-07-27 stsp goto done;
4700 a9fa2909 2019-07-27 stsp } else {
4701 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4702 a9fa2909 2019-07-27 stsp if (err)
4703 a9fa2909 2019-07-27 stsp goto done;
4704 a9fa2909 2019-07-27 stsp
4705 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4706 1233e6b6 2020-10-19 stsp if (err)
4707 a9fa2909 2019-07-27 stsp goto done;
4708 a9fa2909 2019-07-27 stsp
4709 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4710 1233e6b6 2020-10-19 stsp free(te_name);
4711 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4712 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4713 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4714 a9fa2909 2019-07-27 stsp goto done;
4715 a9fa2909 2019-07-27 stsp }
4716 a129376b 2019-03-28 stsp }
4717 a129376b 2019-03-28 stsp
4718 a129376b 2019-03-28 stsp switch (status) {
4719 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4720 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4721 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4722 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4723 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4724 33aa809d 2019-08-08 stsp if (err)
4725 33aa809d 2019-08-08 stsp goto done;
4726 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4727 33aa809d 2019-08-08 stsp break;
4728 33aa809d 2019-08-08 stsp }
4729 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4730 1f1abb7e 2019-08-08 stsp ie->path);
4731 1ee397ad 2019-07-12 stsp if (err)
4732 1ee397ad 2019-07-12 stsp goto done;
4733 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4734 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4735 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4736 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4737 f259c4c1 2021-09-24 stsp relpath) == -1) {
4738 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4739 f259c4c1 2021-09-24 stsp goto done;
4740 f259c4c1 2021-09-24 stsp }
4741 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4742 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4743 f259c4c1 2021-09-24 stsp ondisk_path);
4744 f259c4c1 2021-09-24 stsp break;
4745 f259c4c1 2021-09-24 stsp }
4746 f259c4c1 2021-09-24 stsp }
4747 a129376b 2019-03-28 stsp break;
4748 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4749 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4750 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4751 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4752 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4753 33aa809d 2019-08-08 stsp if (err)
4754 33aa809d 2019-08-08 stsp goto done;
4755 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4756 33aa809d 2019-08-08 stsp break;
4757 33aa809d 2019-08-08 stsp }
4758 33aa809d 2019-08-08 stsp /* fall through */
4759 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4760 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4761 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4762 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4763 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4764 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4765 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4766 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4767 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4768 24278f30 2019-08-03 stsp } else
4769 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4770 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4771 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4772 a129376b 2019-03-28 stsp if (err)
4773 65084dad 2019-08-08 stsp goto done;
4774 65084dad 2019-08-08 stsp
4775 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4776 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4777 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4778 a129376b 2019-03-28 stsp goto done;
4779 65084dad 2019-08-08 stsp }
4780 65084dad 2019-08-08 stsp
4781 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4782 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4783 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4784 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4785 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4786 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4787 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4788 33aa809d 2019-08-08 stsp break;
4789 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4790 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4791 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4792 369fd7e5 2020-07-23 stsp path_content);
4793 369fd7e5 2020-07-23 stsp break;
4794 369fd7e5 2020-07-23 stsp }
4795 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4796 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4797 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4798 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4799 369fd7e5 2020-07-23 stsp } else {
4800 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4801 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4802 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4803 369fd7e5 2020-07-23 stsp goto done;
4804 369fd7e5 2020-07-23 stsp }
4805 33aa809d 2019-08-08 stsp }
4806 33aa809d 2019-08-08 stsp } else {
4807 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4808 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4809 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4810 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4811 5267b9e4 2021-09-26 stsp blob, 0, 1, 0, 0, a->repo,
4812 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4813 2e1fa222 2020-07-23 stsp } else {
4814 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4815 2e1fa222 2020-07-23 stsp ie->path,
4816 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4817 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4818 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4819 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4820 2e1fa222 2020-07-23 stsp }
4821 a129376b 2019-03-28 stsp if (err)
4822 a129376b 2019-03-28 stsp goto done;
4823 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4824 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4825 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4826 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4827 437adc9d 2020-12-10 yzhong blob->id.sha1,
4828 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4829 2e1fa222 2020-07-23 stsp if (err)
4830 2e1fa222 2020-07-23 stsp goto done;
4831 2e1fa222 2020-07-23 stsp }
4832 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4833 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4834 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4835 33aa809d 2019-08-08 stsp }
4836 a129376b 2019-03-28 stsp }
4837 a129376b 2019-03-28 stsp break;
4838 e20a8b6f 2019-06-04 stsp }
4839 a129376b 2019-03-28 stsp default:
4840 1f1abb7e 2019-08-08 stsp break;
4841 a129376b 2019-03-28 stsp }
4842 a129376b 2019-03-28 stsp done:
4843 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4844 33aa809d 2019-08-08 stsp free(path_content);
4845 e20a8b6f 2019-06-04 stsp free(parent_path);
4846 a129376b 2019-03-28 stsp free(tree_path);
4847 a129376b 2019-03-28 stsp if (blob)
4848 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4849 a129376b 2019-03-28 stsp if (tree)
4850 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4851 a129376b 2019-03-28 stsp free(tree_id);
4852 e20a8b6f 2019-06-04 stsp return err;
4853 e20a8b6f 2019-06-04 stsp }
4854 e20a8b6f 2019-06-04 stsp
4855 e20a8b6f 2019-06-04 stsp const struct got_error *
4856 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4857 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4858 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4859 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4860 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4861 e20a8b6f 2019-06-04 stsp {
4862 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4863 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4864 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4865 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4866 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4867 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4868 e20a8b6f 2019-06-04 stsp
4869 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4870 e20a8b6f 2019-06-04 stsp if (err)
4871 e20a8b6f 2019-06-04 stsp return err;
4872 e20a8b6f 2019-06-04 stsp
4873 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4874 e20a8b6f 2019-06-04 stsp if (err)
4875 e20a8b6f 2019-06-04 stsp goto done;
4876 e20a8b6f 2019-06-04 stsp
4877 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4878 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4879 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4880 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4881 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4882 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4883 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4884 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4885 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4886 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4887 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
4888 e20a8b6f 2019-06-04 stsp if (err)
4889 af12c6b9 2019-06-04 stsp break;
4890 e20a8b6f 2019-06-04 stsp }
4891 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4892 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4893 e20a8b6f 2019-06-04 stsp err = sync_err;
4894 af12c6b9 2019-06-04 stsp done:
4895 fb399478 2019-07-12 stsp free(fileindex_path);
4896 a129376b 2019-03-28 stsp if (fileindex)
4897 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4898 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4899 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4900 a129376b 2019-03-28 stsp err = unlockerr;
4901 c4296144 2019-05-09 stsp return err;
4902 c4296144 2019-05-09 stsp }
4903 c4296144 2019-05-09 stsp
4904 cf066bf8 2019-05-09 stsp static void
4905 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4906 cf066bf8 2019-05-09 stsp {
4907 24519714 2019-05-09 stsp free(ct->path);
4908 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4909 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4910 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4911 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4912 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4913 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4914 cf066bf8 2019-05-09 stsp free(ct);
4915 cf066bf8 2019-05-09 stsp }
4916 24519714 2019-05-09 stsp
4917 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4918 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4919 24519714 2019-05-09 stsp struct got_repository *repo;
4920 24519714 2019-05-09 stsp struct got_worktree *worktree;
4921 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4922 5f8a88c6 2019-08-03 stsp int have_staged_files;
4923 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4924 24519714 2019-05-09 stsp };
4925 cf066bf8 2019-05-09 stsp
4926 c4296144 2019-05-09 stsp static const struct got_error *
4927 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4928 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4929 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4930 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4931 c4296144 2019-05-09 stsp {
4932 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4933 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4934 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4935 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4936 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4937 768aea60 2019-05-09 stsp struct stat sb;
4938 c4296144 2019-05-09 stsp
4939 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4940 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4941 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4942 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4943 5f8a88c6 2019-08-03 stsp return NULL;
4944 5f8a88c6 2019-08-03 stsp } else {
4945 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4946 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4947 c4296144 2019-05-09 stsp
4948 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4949 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4950 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4951 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4952 5f8a88c6 2019-08-03 stsp return NULL;
4953 5f8a88c6 2019-08-03 stsp }
4954 0b5cc0d6 2019-05-09 stsp
4955 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4956 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4957 036813ee 2019-05-09 stsp goto done;
4958 036813ee 2019-05-09 stsp }
4959 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4960 036813ee 2019-05-09 stsp parent_path = strdup("");
4961 69960a46 2019-05-09 stsp if (parent_path == NULL)
4962 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4963 69960a46 2019-05-09 stsp } else {
4964 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4965 69960a46 2019-05-09 stsp if (err)
4966 69960a46 2019-05-09 stsp return err;
4967 69960a46 2019-05-09 stsp }
4968 c4296144 2019-05-09 stsp
4969 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4970 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4971 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4972 c4296144 2019-05-09 stsp goto done;
4973 768aea60 2019-05-09 stsp }
4974 768aea60 2019-05-09 stsp
4975 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4976 768aea60 2019-05-09 stsp relpath) == -1) {
4977 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4978 768aea60 2019-05-09 stsp goto done;
4979 768aea60 2019-05-09 stsp }
4980 0aeb8099 2020-07-23 stsp
4981 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4982 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4983 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4984 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4985 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4986 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4987 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4988 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4989 0aeb8099 2020-07-23 stsp break;
4990 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4991 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4992 0aeb8099 2020-07-23 stsp break;
4993 0aeb8099 2020-07-23 stsp default:
4994 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4995 0aeb8099 2020-07-23 stsp goto done;
4996 0aeb8099 2020-07-23 stsp }
4997 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4998 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4999 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
5000 12463d8b 2019-12-13 stsp if (dirfd != -1) {
5001 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
5002 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
5003 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
5004 12463d8b 2019-12-13 stsp ct->ondisk_path);
5005 12463d8b 2019-12-13 stsp goto done;
5006 12463d8b 2019-12-13 stsp }
5007 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
5008 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
5009 768aea60 2019-05-09 stsp goto done;
5010 768aea60 2019-05-09 stsp }
5011 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5012 c4296144 2019-05-09 stsp }
5013 c4296144 2019-05-09 stsp
5014 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5015 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5016 44d03001 2019-05-09 stsp relpath) == -1) {
5017 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5018 44d03001 2019-05-09 stsp goto done;
5019 44d03001 2019-05-09 stsp }
5020 44d03001 2019-05-09 stsp
5021 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5022 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5023 35213c7c 2020-07-23 stsp int is_bad_symlink;
5024 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5025 35213c7c 2020-07-23 stsp ssize_t target_len;
5026 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5027 35213c7c 2020-07-23 stsp sizeof(target_path));
5028 35213c7c 2020-07-23 stsp if (target_len == -1) {
5029 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5030 35213c7c 2020-07-23 stsp ct->ondisk_path);
5031 35213c7c 2020-07-23 stsp goto done;
5032 35213c7c 2020-07-23 stsp }
5033 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5034 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5035 35213c7c 2020-07-23 stsp if (err)
5036 35213c7c 2020-07-23 stsp goto done;
5037 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5038 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5039 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5040 35213c7c 2020-07-23 stsp goto done;
5041 35213c7c 2020-07-23 stsp }
5042 35213c7c 2020-07-23 stsp }
5043 35213c7c 2020-07-23 stsp
5044 35213c7c 2020-07-23 stsp
5045 cf066bf8 2019-05-09 stsp ct->status = status;
5046 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5047 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5048 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5049 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5050 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5051 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5052 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5053 b416585c 2019-05-13 stsp goto done;
5054 b416585c 2019-05-13 stsp }
5055 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5056 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5057 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5058 f0b75401 2019-08-03 stsp goto done;
5059 f0b75401 2019-08-03 stsp }
5060 f0b75401 2019-08-03 stsp }
5061 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5062 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5063 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5064 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5065 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5066 036813ee 2019-05-09 stsp goto done;
5067 036813ee 2019-05-09 stsp }
5068 ca2503ea 2019-05-09 stsp }
5069 24519714 2019-05-09 stsp ct->path = strdup(path);
5070 24519714 2019-05-09 stsp if (ct->path == NULL) {
5071 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5072 24519714 2019-05-09 stsp goto done;
5073 24519714 2019-05-09 stsp }
5074 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5075 c4296144 2019-05-09 stsp done:
5076 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5077 ed175427 2019-05-09 stsp free_commitable(ct);
5078 24519714 2019-05-09 stsp free(parent_path);
5079 036813ee 2019-05-09 stsp free(path);
5080 a129376b 2019-03-28 stsp return err;
5081 a129376b 2019-03-28 stsp }
5082 c4296144 2019-05-09 stsp
5083 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5084 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5085 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5086 036813ee 2019-05-09 stsp struct got_repository *);
5087 ed175427 2019-05-09 stsp
5088 ed175427 2019-05-09 stsp static const struct got_error *
5089 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5090 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5091 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5092 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5093 afa376bf 2019-05-09 stsp struct got_repository *repo)
5094 ed175427 2019-05-09 stsp {
5095 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5096 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5097 036813ee 2019-05-09 stsp char *subpath;
5098 ed175427 2019-05-09 stsp
5099 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5100 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5101 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5102 ed175427 2019-05-09 stsp
5103 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5104 ed175427 2019-05-09 stsp if (err)
5105 ed175427 2019-05-09 stsp return err;
5106 ed175427 2019-05-09 stsp
5107 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5108 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5109 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5110 036813ee 2019-05-09 stsp free(subpath);
5111 036813ee 2019-05-09 stsp return err;
5112 036813ee 2019-05-09 stsp }
5113 ed175427 2019-05-09 stsp
5114 036813ee 2019-05-09 stsp static const struct got_error *
5115 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5116 036813ee 2019-05-09 stsp {
5117 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5118 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5119 ed175427 2019-05-09 stsp
5120 036813ee 2019-05-09 stsp *match = 0;
5121 ed175427 2019-05-09 stsp
5122 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5123 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5124 0f63689d 2019-05-10 stsp return NULL;
5125 036813ee 2019-05-09 stsp }
5126 0b5cc0d6 2019-05-09 stsp
5127 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5128 0f63689d 2019-05-10 stsp if (err)
5129 0f63689d 2019-05-10 stsp return err;
5130 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5131 036813ee 2019-05-09 stsp free(ct_parent_path);
5132 036813ee 2019-05-09 stsp return err;
5133 036813ee 2019-05-09 stsp }
5134 0b5cc0d6 2019-05-09 stsp
5135 768aea60 2019-05-09 stsp static mode_t
5136 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5137 768aea60 2019-05-09 stsp {
5138 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5139 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5140 3d9a4ec4 2020-07-23 stsp
5141 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5142 768aea60 2019-05-09 stsp }
5143 768aea60 2019-05-09 stsp
5144 036813ee 2019-05-09 stsp static const struct got_error *
5145 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5146 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5147 036813ee 2019-05-09 stsp {
5148 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5149 ca2503ea 2019-05-09 stsp
5150 036813ee 2019-05-09 stsp *new_te = NULL;
5151 0b5cc0d6 2019-05-09 stsp
5152 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5153 036813ee 2019-05-09 stsp if (err)
5154 036813ee 2019-05-09 stsp goto done;
5155 0b5cc0d6 2019-05-09 stsp
5156 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5157 036813ee 2019-05-09 stsp
5158 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5159 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5160 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5161 5f8a88c6 2019-08-03 stsp else
5162 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5163 036813ee 2019-05-09 stsp done:
5164 036813ee 2019-05-09 stsp if (err && *new_te) {
5165 56e0773d 2019-11-28 stsp free(*new_te);
5166 036813ee 2019-05-09 stsp *new_te = NULL;
5167 036813ee 2019-05-09 stsp }
5168 036813ee 2019-05-09 stsp return err;
5169 036813ee 2019-05-09 stsp }
5170 036813ee 2019-05-09 stsp
5171 036813ee 2019-05-09 stsp static const struct got_error *
5172 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5173 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5174 036813ee 2019-05-09 stsp {
5175 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5176 102b254e 2020-10-19 stsp char *ct_name = NULL;
5177 036813ee 2019-05-09 stsp
5178 036813ee 2019-05-09 stsp *new_te = NULL;
5179 036813ee 2019-05-09 stsp
5180 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5181 036813ee 2019-05-09 stsp if (*new_te == NULL)
5182 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5183 036813ee 2019-05-09 stsp
5184 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5185 102b254e 2020-10-19 stsp if (err)
5186 036813ee 2019-05-09 stsp goto done;
5187 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5188 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5189 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5190 036813ee 2019-05-09 stsp goto done;
5191 036813ee 2019-05-09 stsp }
5192 036813ee 2019-05-09 stsp
5193 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5194 036813ee 2019-05-09 stsp
5195 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5196 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5197 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5198 5f8a88c6 2019-08-03 stsp else
5199 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5200 036813ee 2019-05-09 stsp done:
5201 102b254e 2020-10-19 stsp free(ct_name);
5202 036813ee 2019-05-09 stsp if (err && *new_te) {
5203 56e0773d 2019-11-28 stsp free(*new_te);
5204 036813ee 2019-05-09 stsp *new_te = NULL;
5205 036813ee 2019-05-09 stsp }
5206 036813ee 2019-05-09 stsp return err;
5207 036813ee 2019-05-09 stsp }
5208 036813ee 2019-05-09 stsp
5209 036813ee 2019-05-09 stsp static const struct got_error *
5210 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5211 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5212 036813ee 2019-05-09 stsp {
5213 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5214 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5215 036813ee 2019-05-09 stsp
5216 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5217 036813ee 2019-05-09 stsp if (err)
5218 036813ee 2019-05-09 stsp return err;
5219 036813ee 2019-05-09 stsp if (new_pe == NULL)
5220 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5221 036813ee 2019-05-09 stsp return NULL;
5222 afa376bf 2019-05-09 stsp }
5223 afa376bf 2019-05-09 stsp
5224 afa376bf 2019-05-09 stsp static const struct got_error *
5225 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5226 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5227 afa376bf 2019-05-09 stsp {
5228 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5229 5f8a88c6 2019-08-03 stsp unsigned char status;
5230 0ff8d236 2021-09-28 stsp
5231 0ff8d236 2021-09-28 stsp if (status_cb == NULL) /* no commit progress output desired */
5232 0ff8d236 2021-09-28 stsp return NULL;
5233 5f8a88c6 2019-08-03 stsp
5234 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5235 afa376bf 2019-05-09 stsp ct_path++;
5236 5f8a88c6 2019-08-03 stsp
5237 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5238 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5239 5f8a88c6 2019-08-03 stsp else
5240 5f8a88c6 2019-08-03 stsp status = ct->status;
5241 5f8a88c6 2019-08-03 stsp
5242 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5243 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5244 036813ee 2019-05-09 stsp }
5245 036813ee 2019-05-09 stsp
5246 036813ee 2019-05-09 stsp static const struct got_error *
5247 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5248 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5249 44d03001 2019-05-09 stsp {
5250 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5251 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5252 44d03001 2019-05-09 stsp char *te_path;
5253 44d03001 2019-05-09 stsp
5254 44d03001 2019-05-09 stsp *modified = 0;
5255 44d03001 2019-05-09 stsp
5256 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5257 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5258 44d03001 2019-05-09 stsp te->name) == -1)
5259 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5260 44d03001 2019-05-09 stsp
5261 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5262 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5263 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5264 44d03001 2019-05-09 stsp strlen(te_path));
5265 62d463ca 2020-10-20 naddy if (*modified)
5266 44d03001 2019-05-09 stsp break;
5267 44d03001 2019-05-09 stsp }
5268 44d03001 2019-05-09 stsp
5269 44d03001 2019-05-09 stsp free(te_path);
5270 44d03001 2019-05-09 stsp return err;
5271 44d03001 2019-05-09 stsp }
5272 44d03001 2019-05-09 stsp
5273 44d03001 2019-05-09 stsp static const struct got_error *
5274 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5275 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5276 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5277 036813ee 2019-05-09 stsp {
5278 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5279 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5280 036813ee 2019-05-09 stsp
5281 036813ee 2019-05-09 stsp *ctp = NULL;
5282 036813ee 2019-05-09 stsp
5283 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5284 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5285 036813ee 2019-05-09 stsp char *ct_name = NULL;
5286 036813ee 2019-05-09 stsp int path_matches;
5287 036813ee 2019-05-09 stsp
5288 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5289 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5290 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5291 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5292 5f8a88c6 2019-08-03 stsp continue;
5293 5f8a88c6 2019-08-03 stsp } else {
5294 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5295 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5296 5f8a88c6 2019-08-03 stsp continue;
5297 5f8a88c6 2019-08-03 stsp }
5298 036813ee 2019-05-09 stsp
5299 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5300 036813ee 2019-05-09 stsp continue;
5301 036813ee 2019-05-09 stsp
5302 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5303 62d463ca 2020-10-20 naddy if (err)
5304 036813ee 2019-05-09 stsp return err;
5305 036813ee 2019-05-09 stsp if (!path_matches)
5306 036813ee 2019-05-09 stsp continue;
5307 036813ee 2019-05-09 stsp
5308 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5309 d34b633e 2020-10-19 stsp if (err)
5310 d34b633e 2020-10-19 stsp return err;
5311 d34b633e 2020-10-19 stsp
5312 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5313 d34b633e 2020-10-19 stsp free(ct_name);
5314 036813ee 2019-05-09 stsp continue;
5315 d34b633e 2020-10-19 stsp }
5316 d34b633e 2020-10-19 stsp free(ct_name);
5317 036813ee 2019-05-09 stsp
5318 036813ee 2019-05-09 stsp *ctp = ct;
5319 036813ee 2019-05-09 stsp break;
5320 036813ee 2019-05-09 stsp }
5321 2b496619 2019-07-10 stsp
5322 2b496619 2019-07-10 stsp return err;
5323 2b496619 2019-07-10 stsp }
5324 2b496619 2019-07-10 stsp
5325 2b496619 2019-07-10 stsp static const struct got_error *
5326 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5327 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5328 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5329 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5330 2b496619 2019-07-10 stsp struct got_repository *repo)
5331 2b496619 2019-07-10 stsp {
5332 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5333 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5334 2b496619 2019-07-10 stsp char *subtree_path;
5335 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5336 ba580f68 2020-03-22 stsp int nentries;
5337 2b496619 2019-07-10 stsp
5338 2b496619 2019-07-10 stsp *new_tep = NULL;
5339 2b496619 2019-07-10 stsp
5340 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5341 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5342 2b496619 2019-07-10 stsp child_path) == -1)
5343 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5344 036813ee 2019-05-09 stsp
5345 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5346 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5347 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5348 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5349 56e0773d 2019-11-28 stsp
5350 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5351 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5352 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5353 2b496619 2019-07-10 stsp goto done;
5354 2b496619 2019-07-10 stsp }
5355 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5356 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5357 2b496619 2019-07-10 stsp if (err) {
5358 56e0773d 2019-11-28 stsp free(new_te);
5359 2b496619 2019-07-10 stsp goto done;
5360 2b496619 2019-07-10 stsp }
5361 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5362 2b496619 2019-07-10 stsp done:
5363 56e0773d 2019-11-28 stsp free(id);
5364 2b496619 2019-07-10 stsp free(subtree_path);
5365 2b496619 2019-07-10 stsp if (err == NULL)
5366 2b496619 2019-07-10 stsp *new_tep = new_te;
5367 036813ee 2019-05-09 stsp return err;
5368 036813ee 2019-05-09 stsp }
5369 036813ee 2019-05-09 stsp
5370 036813ee 2019-05-09 stsp static const struct got_error *
5371 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5372 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5373 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5374 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5375 036813ee 2019-05-09 stsp struct got_repository *repo)
5376 036813ee 2019-05-09 stsp {
5377 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5378 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5379 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5380 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5381 036813ee 2019-05-09 stsp
5382 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5383 ba580f68 2020-03-22 stsp *nentries = 0;
5384 036813ee 2019-05-09 stsp
5385 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5386 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5387 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5388 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5389 036813ee 2019-05-09 stsp
5390 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5391 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5392 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5393 036813ee 2019-05-09 stsp continue;
5394 036813ee 2019-05-09 stsp
5395 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5396 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5397 036813ee 2019-05-09 stsp continue;
5398 036813ee 2019-05-09 stsp
5399 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5400 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5401 036813ee 2019-05-09 stsp if (err)
5402 036813ee 2019-05-09 stsp goto done;
5403 036813ee 2019-05-09 stsp
5404 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5405 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5406 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5407 036813ee 2019-05-09 stsp if (err)
5408 036813ee 2019-05-09 stsp goto done;
5409 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5410 afa376bf 2019-05-09 stsp if (err)
5411 afa376bf 2019-05-09 stsp goto done;
5412 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5413 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5414 2b496619 2019-07-10 stsp if (err)
5415 2f51b5b3 2019-05-09 stsp goto done;
5416 ba580f68 2020-03-22 stsp (*nentries)++;
5417 2b496619 2019-07-10 stsp } else {
5418 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5419 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5420 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5421 2b496619 2019-07-10 stsp == NULL) {
5422 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5423 2b496619 2019-07-10 stsp child_path, path_base_tree,
5424 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5425 2b496619 2019-07-10 stsp repo);
5426 2b496619 2019-07-10 stsp if (err)
5427 2b496619 2019-07-10 stsp goto done;
5428 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5429 2b496619 2019-07-10 stsp if (err)
5430 2b496619 2019-07-10 stsp goto done;
5431 ba580f68 2020-03-22 stsp (*nentries)++;
5432 9ba0479c 2019-05-10 stsp }
5433 ed175427 2019-05-09 stsp }
5434 2f51b5b3 2019-05-09 stsp }
5435 2f51b5b3 2019-05-09 stsp
5436 2f51b5b3 2019-05-09 stsp if (base_tree) {
5437 56e0773d 2019-11-28 stsp int i, nbase_entries;
5438 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5439 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5440 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5441 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5442 63c5ca5d 2019-08-24 stsp
5443 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5444 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5445 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5446 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5447 63c5ca5d 2019-08-24 stsp if (err)
5448 63c5ca5d 2019-08-24 stsp goto done;
5449 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5450 63c5ca5d 2019-08-24 stsp if (err)
5451 63c5ca5d 2019-08-24 stsp goto done;
5452 ba580f68 2020-03-22 stsp (*nentries)++;
5453 63c5ca5d 2019-08-24 stsp continue;
5454 63c5ca5d 2019-08-24 stsp }
5455 2f51b5b3 2019-05-09 stsp
5456 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5457 44d03001 2019-05-09 stsp int modified;
5458 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5459 036813ee 2019-05-09 stsp if (err)
5460 036813ee 2019-05-09 stsp goto done;
5461 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5462 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5463 2f51b5b3 2019-05-09 stsp if (err)
5464 2f51b5b3 2019-05-09 stsp goto done;
5465 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5466 44d03001 2019-05-09 stsp if (modified) {
5467 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5468 ba580f68 2020-03-22 stsp int nsubentries;
5469 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5470 ba580f68 2020-03-22 stsp &nsubentries, te,
5471 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5472 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5473 44d03001 2019-05-09 stsp if (err)
5474 44d03001 2019-05-09 stsp goto done;
5475 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5476 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5477 ba580f68 2020-03-22 stsp free(new_id);
5478 ba580f68 2020-03-22 stsp continue;
5479 ba580f68 2020-03-22 stsp }
5480 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5481 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5482 56e0773d 2019-11-28 stsp free(new_id);
5483 44d03001 2019-05-09 stsp }
5484 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5485 036813ee 2019-05-09 stsp if (err)
5486 036813ee 2019-05-09 stsp goto done;
5487 ba580f68 2020-03-22 stsp (*nentries)++;
5488 2f51b5b3 2019-05-09 stsp continue;
5489 036813ee 2019-05-09 stsp }
5490 2f51b5b3 2019-05-09 stsp
5491 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5492 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5493 f66c734c 2019-09-22 stsp if (err)
5494 f66c734c 2019-09-22 stsp goto done;
5495 2f51b5b3 2019-05-09 stsp if (ct) {
5496 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5497 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5498 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5499 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5500 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5501 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5502 2f51b5b3 2019-05-09 stsp if (err)
5503 2f51b5b3 2019-05-09 stsp goto done;
5504 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5505 2f51b5b3 2019-05-09 stsp if (err)
5506 2f51b5b3 2019-05-09 stsp goto done;
5507 ba580f68 2020-03-22 stsp (*nentries)++;
5508 2f51b5b3 2019-05-09 stsp }
5509 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5510 b416585c 2019-05-13 stsp status_arg);
5511 afa376bf 2019-05-09 stsp if (err)
5512 afa376bf 2019-05-09 stsp goto done;
5513 2f51b5b3 2019-05-09 stsp } else {
5514 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5515 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5516 2f51b5b3 2019-05-09 stsp if (err)
5517 2f51b5b3 2019-05-09 stsp goto done;
5518 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5519 2f51b5b3 2019-05-09 stsp if (err)
5520 2f51b5b3 2019-05-09 stsp goto done;
5521 ba580f68 2020-03-22 stsp (*nentries)++;
5522 2f51b5b3 2019-05-09 stsp }
5523 ca2503ea 2019-05-09 stsp }
5524 ed175427 2019-05-09 stsp }
5525 0b5cc0d6 2019-05-09 stsp
5526 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5527 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5528 ed175427 2019-05-09 stsp done:
5529 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5530 2e1fa222 2020-07-23 stsp return err;
5531 2e1fa222 2020-07-23 stsp }
5532 2e1fa222 2020-07-23 stsp
5533 2e1fa222 2020-07-23 stsp static const struct got_error *
5534 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5535 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5536 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5537 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5538 ebf99748 2019-05-09 stsp {
5539 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5540 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5541 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5542 ebf99748 2019-05-09 stsp
5543 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5544 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5545 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5546 ebf99748 2019-05-09 stsp
5547 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5548 437adc9d 2020-12-10 yzhong
5549 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5550 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5551 437adc9d 2020-12-10 yzhong if (err)
5552 437adc9d 2020-12-10 yzhong goto done;
5553 437adc9d 2020-12-10 yzhong
5554 ebf99748 2019-05-09 stsp if (ie) {
5555 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5556 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5557 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5558 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5559 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5560 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5561 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5562 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5563 437adc9d 2020-12-10 yzhong
5564 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5565 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5566 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5567 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5568 72fd46fa 2019-09-06 stsp !have_staged_files);
5569 ebf99748 2019-05-09 stsp } else
5570 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5571 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5572 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5573 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5574 72fd46fa 2019-09-06 stsp !have_staged_files);
5575 ebf99748 2019-05-09 stsp } else {
5576 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5577 ebf99748 2019-05-09 stsp if (err)
5578 437adc9d 2020-12-10 yzhong goto done;
5579 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5580 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5581 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5582 3969253a 2020-03-07 stsp if (err) {
5583 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5584 437adc9d 2020-12-10 yzhong goto done;
5585 3969253a 2020-03-07 stsp }
5586 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5587 3969253a 2020-03-07 stsp if (err) {
5588 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5589 437adc9d 2020-12-10 yzhong goto done;
5590 3969253a 2020-03-07 stsp }
5591 ebf99748 2019-05-09 stsp }
5592 437adc9d 2020-12-10 yzhong free(relpath);
5593 437adc9d 2020-12-10 yzhong relpath = NULL;
5594 ebf99748 2019-05-09 stsp }
5595 437adc9d 2020-12-10 yzhong done:
5596 437adc9d 2020-12-10 yzhong free(relpath);
5597 d56d26ce 2019-05-10 stsp return err;
5598 d56d26ce 2019-05-10 stsp }
5599 735ef5ac 2019-08-03 stsp
5600 d56d26ce 2019-05-10 stsp
5601 d56d26ce 2019-05-10 stsp static const struct got_error *
5602 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5603 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5604 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5605 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5606 735ef5ac 2019-08-03 stsp int ood_errcode)
5607 d56d26ce 2019-05-10 stsp {
5608 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5609 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5610 1a36436d 2019-06-10 stsp
5611 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5612 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5613 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5614 1a36436d 2019-06-10 stsp return NULL;
5615 9bead371 2019-07-28 stsp /*
5616 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5617 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5618 9bead371 2019-07-28 stsp */
5619 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5620 735ef5ac 2019-08-03 stsp in_repo_path);
5621 9bead371 2019-07-28 stsp if (err) {
5622 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5623 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5624 1a36436d 2019-06-10 stsp goto done;
5625 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5626 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5627 1a36436d 2019-06-10 stsp } else {
5628 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5629 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5630 735ef5ac 2019-08-03 stsp in_repo_path);
5631 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5632 1a36436d 2019-06-10 stsp goto done;
5633 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5634 1a36436d 2019-06-10 stsp }
5635 1a36436d 2019-06-10 stsp done:
5636 1a36436d 2019-06-10 stsp free(id);
5637 1a36436d 2019-06-10 stsp return err;
5638 ebf99748 2019-05-09 stsp }
5639 ebf99748 2019-05-09 stsp
5640 c4296144 2019-05-09 stsp const struct got_error *
5641 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5642 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5643 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5644 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5645 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5646 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5647 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5648 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5649 afa376bf 2019-05-09 stsp struct got_repository *repo)
5650 c4296144 2019-05-09 stsp {
5651 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5652 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5653 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5654 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5655 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5656 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5657 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5658 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5659 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5660 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5661 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5662 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5663 c4296144 2019-05-09 stsp
5664 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5665 c4296144 2019-05-09 stsp
5666 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5667 675c7539 2019-05-09 stsp
5668 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5669 588edf97 2019-05-10 stsp if (err)
5670 588edf97 2019-05-10 stsp goto done;
5671 de18fc63 2019-05-09 stsp
5672 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5673 588edf97 2019-05-10 stsp if (err)
5674 588edf97 2019-05-10 stsp goto done;
5675 588edf97 2019-05-10 stsp
5676 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5677 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5678 33ad4cbe 2019-05-12 jcs if (err)
5679 33ad4cbe 2019-05-12 jcs goto done;
5680 33ad4cbe 2019-05-12 jcs }
5681 c4296144 2019-05-09 stsp
5682 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5683 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5684 33ad4cbe 2019-05-12 jcs goto done;
5685 33ad4cbe 2019-05-12 jcs }
5686 33ad4cbe 2019-05-12 jcs
5687 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5688 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5689 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5690 cf066bf8 2019-05-09 stsp char *ondisk_path;
5691 cf066bf8 2019-05-09 stsp
5692 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5693 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5694 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5695 5f8a88c6 2019-08-03 stsp continue;
5696 5f8a88c6 2019-08-03 stsp
5697 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5698 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5699 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5700 cf066bf8 2019-05-09 stsp continue;
5701 cf066bf8 2019-05-09 stsp
5702 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5703 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5704 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5705 cf066bf8 2019-05-09 stsp goto done;
5706 cf066bf8 2019-05-09 stsp }
5707 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5708 2e1fa222 2020-07-23 stsp free(ondisk_path);
5709 2e1fa222 2020-07-23 stsp if (err)
5710 cf066bf8 2019-05-09 stsp goto done;
5711 cf066bf8 2019-05-09 stsp }
5712 036813ee 2019-05-09 stsp
5713 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5714 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5715 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5716 036813ee 2019-05-09 stsp if (err)
5717 036813ee 2019-05-09 stsp goto done;
5718 036813ee 2019-05-09 stsp
5719 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5720 de18fc63 2019-05-09 stsp if (err)
5721 de18fc63 2019-05-09 stsp goto done;
5722 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5723 f259c4c1 2021-09-24 stsp nparents++;
5724 f259c4c1 2021-09-24 stsp if (parent_id2) {
5725 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5726 f259c4c1 2021-09-24 stsp if (err)
5727 f259c4c1 2021-09-24 stsp goto done;
5728 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5729 f259c4c1 2021-09-24 stsp nparents++;
5730 f259c4c1 2021-09-24 stsp }
5731 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5732 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5733 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5734 33ad4cbe 2019-05-12 jcs free(logmsg);
5735 9d40349a 2019-05-09 stsp if (err)
5736 9d40349a 2019-05-09 stsp goto done;
5737 9d40349a 2019-05-09 stsp
5738 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5739 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5740 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5741 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5742 09f5bd90 2019-05-09 stsp goto done;
5743 09f5bd90 2019-05-09 stsp }
5744 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5745 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5746 09f5bd90 2019-05-09 stsp if (err)
5747 09f5bd90 2019-05-09 stsp goto done;
5748 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5749 ebf99748 2019-05-09 stsp if (err)
5750 ebf99748 2019-05-09 stsp goto done;
5751 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5752 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5753 09f5bd90 2019-05-09 stsp goto done;
5754 09f5bd90 2019-05-09 stsp }
5755 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5756 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5757 f2c16586 2019-05-09 stsp if (err)
5758 f2c16586 2019-05-09 stsp goto done;
5759 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5760 f2c16586 2019-05-09 stsp if (err)
5761 f2c16586 2019-05-09 stsp goto done;
5762 f2c16586 2019-05-09 stsp
5763 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5764 09f5bd90 2019-05-09 stsp if (err)
5765 09f5bd90 2019-05-09 stsp goto done;
5766 09f5bd90 2019-05-09 stsp
5767 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5768 ebf99748 2019-05-09 stsp if (err)
5769 ebf99748 2019-05-09 stsp goto done;
5770 c4296144 2019-05-09 stsp done:
5771 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5772 588edf97 2019-05-10 stsp if (head_tree)
5773 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5774 588edf97 2019-05-10 stsp if (head_commit)
5775 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5776 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5777 2f17228e 2019-05-12 stsp if (head_ref2) {
5778 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5779 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5780 2f17228e 2019-05-12 stsp err = unlockerr;
5781 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5782 39cd0ff6 2019-07-12 stsp }
5783 39cd0ff6 2019-07-12 stsp return err;
5784 39cd0ff6 2019-07-12 stsp }
5785 5c1e53bc 2019-07-28 stsp
5786 5c1e53bc 2019-07-28 stsp static const struct got_error *
5787 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5788 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5789 5c1e53bc 2019-07-28 stsp {
5790 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5791 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5792 39cd0ff6 2019-07-12 stsp
5793 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5794 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5795 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5796 5c1e53bc 2019-07-28 stsp
5797 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5798 5c1e53bc 2019-07-28 stsp ct_path++;
5799 5c1e53bc 2019-07-28 stsp
5800 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5801 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5802 5c1e53bc 2019-07-28 stsp break;
5803 5c1e53bc 2019-07-28 stsp }
5804 5c1e53bc 2019-07-28 stsp
5805 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5806 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5807 5c1e53bc 2019-07-28 stsp
5808 5c1e53bc 2019-07-28 stsp return NULL;
5809 5c1e53bc 2019-07-28 stsp }
5810 5c1e53bc 2019-07-28 stsp
5811 f0b75401 2019-08-03 stsp static const struct got_error *
5812 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5813 f0b75401 2019-08-03 stsp {
5814 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5815 f0b75401 2019-08-03 stsp
5816 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5817 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5818 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5819 f0b75401 2019-08-03 stsp }
5820 f0b75401 2019-08-03 stsp
5821 f0b75401 2019-08-03 stsp return NULL;
5822 f0b75401 2019-08-03 stsp }
5823 f0b75401 2019-08-03 stsp
5824 5f8a88c6 2019-08-03 stsp static const struct got_error *
5825 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5826 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5827 5f8a88c6 2019-08-03 stsp {
5828 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5829 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5830 5f8a88c6 2019-08-03 stsp
5831 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5832 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5833 5f8a88c6 2019-08-03 stsp continue;
5834 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5835 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5836 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5837 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5838 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5839 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5840 5f8a88c6 2019-08-03 stsp }
5841 5f8a88c6 2019-08-03 stsp
5842 5f8a88c6 2019-08-03 stsp return NULL;
5843 5f8a88c6 2019-08-03 stsp }
5844 5f8a88c6 2019-08-03 stsp
5845 39cd0ff6 2019-07-12 stsp const struct got_error *
5846 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5847 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5848 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5849 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5850 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5851 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5852 39cd0ff6 2019-07-12 stsp {
5853 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5854 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5855 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5856 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5857 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5858 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5859 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5860 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5861 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5862 39cd0ff6 2019-07-12 stsp
5863 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5864 39cd0ff6 2019-07-12 stsp
5865 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5866 39cd0ff6 2019-07-12 stsp
5867 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5868 39cd0ff6 2019-07-12 stsp if (err)
5869 39cd0ff6 2019-07-12 stsp goto done;
5870 39cd0ff6 2019-07-12 stsp
5871 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5872 39cd0ff6 2019-07-12 stsp if (err)
5873 39cd0ff6 2019-07-12 stsp goto done;
5874 39cd0ff6 2019-07-12 stsp
5875 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5876 39cd0ff6 2019-07-12 stsp if (err)
5877 39cd0ff6 2019-07-12 stsp goto done;
5878 39cd0ff6 2019-07-12 stsp
5879 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5880 39cd0ff6 2019-07-12 stsp if (err)
5881 39cd0ff6 2019-07-12 stsp goto done;
5882 39cd0ff6 2019-07-12 stsp
5883 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5884 5f8a88c6 2019-08-03 stsp &have_staged_files);
5885 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5886 5f8a88c6 2019-08-03 stsp goto done;
5887 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5888 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5889 5f8a88c6 2019-08-03 stsp if (err)
5890 5f8a88c6 2019-08-03 stsp goto done;
5891 5f8a88c6 2019-08-03 stsp }
5892 5f8a88c6 2019-08-03 stsp
5893 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5894 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5895 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5896 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5897 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5898 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5899 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5900 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5901 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5902 5c1e53bc 2019-07-28 stsp if (err)
5903 5c1e53bc 2019-07-28 stsp goto done;
5904 5c1e53bc 2019-07-28 stsp }
5905 39cd0ff6 2019-07-12 stsp
5906 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5907 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5908 39cd0ff6 2019-07-12 stsp goto done;
5909 5c1e53bc 2019-07-28 stsp }
5910 5c1e53bc 2019-07-28 stsp
5911 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5912 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5913 5c1e53bc 2019-07-28 stsp if (err)
5914 5c1e53bc 2019-07-28 stsp goto done;
5915 39cd0ff6 2019-07-12 stsp }
5916 f0b75401 2019-08-03 stsp
5917 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5918 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5919 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5920 f0b75401 2019-08-03 stsp
5921 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5922 f0b75401 2019-08-03 stsp ct_path++;
5923 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5924 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5925 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5926 b50cabdf 2019-07-12 stsp if (err)
5927 b50cabdf 2019-07-12 stsp goto done;
5928 f0b75401 2019-08-03 stsp
5929 b50cabdf 2019-07-12 stsp }
5930 b50cabdf 2019-07-12 stsp
5931 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5932 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5933 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5934 39cd0ff6 2019-07-12 stsp if (err)
5935 39cd0ff6 2019-07-12 stsp goto done;
5936 39cd0ff6 2019-07-12 stsp
5937 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5938 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5939 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5940 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5941 39cd0ff6 2019-07-12 stsp err = sync_err;
5942 39cd0ff6 2019-07-12 stsp done:
5943 39cd0ff6 2019-07-12 stsp if (fileindex)
5944 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5945 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5946 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5947 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5948 39cd0ff6 2019-07-12 stsp err = unlockerr;
5949 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5950 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5951 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5952 39cd0ff6 2019-07-12 stsp }
5953 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5954 c4296144 2019-05-09 stsp return err;
5955 8656d6c4 2019-05-20 stsp }
5956 8656d6c4 2019-05-20 stsp
5957 8656d6c4 2019-05-20 stsp const char *
5958 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5959 8656d6c4 2019-05-20 stsp {
5960 8656d6c4 2019-05-20 stsp return ct->path;
5961 8656d6c4 2019-05-20 stsp }
5962 8656d6c4 2019-05-20 stsp
5963 8656d6c4 2019-05-20 stsp unsigned int
5964 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5965 8656d6c4 2019-05-20 stsp {
5966 8656d6c4 2019-05-20 stsp return ct->status;
5967 818c7501 2019-07-11 stsp }
5968 818c7501 2019-07-11 stsp
5969 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5970 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5971 818c7501 2019-07-11 stsp struct got_repository *repo;
5972 818c7501 2019-07-11 stsp };
5973 818c7501 2019-07-11 stsp
5974 818c7501 2019-07-11 stsp static const struct got_error *
5975 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5976 818c7501 2019-07-11 stsp {
5977 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5978 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5979 818c7501 2019-07-11 stsp unsigned char status;
5980 818c7501 2019-07-11 stsp struct stat sb;
5981 818c7501 2019-07-11 stsp char *ondisk_path;
5982 818c7501 2019-07-11 stsp
5983 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5984 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5985 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5986 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5987 818c7501 2019-07-11 stsp
5988 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5989 818c7501 2019-07-11 stsp == -1)
5990 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5991 818c7501 2019-07-11 stsp
5992 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5993 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5994 818c7501 2019-07-11 stsp free(ondisk_path);
5995 818c7501 2019-07-11 stsp if (err)
5996 818c7501 2019-07-11 stsp return err;
5997 818c7501 2019-07-11 stsp
5998 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5999 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
6000 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6001 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6002 818c7501 2019-07-11 stsp
6003 818c7501 2019-07-11 stsp return NULL;
6004 818c7501 2019-07-11 stsp }
6005 818c7501 2019-07-11 stsp
6006 818c7501 2019-07-11 stsp const struct got_error *
6007 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6008 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6009 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
6010 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6011 818c7501 2019-07-11 stsp {
6012 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
6013 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6014 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6015 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6016 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6017 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6018 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6019 818c7501 2019-07-11 stsp
6020 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6021 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6022 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6023 818c7501 2019-07-11 stsp
6024 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6025 818c7501 2019-07-11 stsp if (err)
6026 818c7501 2019-07-11 stsp return err;
6027 818c7501 2019-07-11 stsp
6028 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6029 818c7501 2019-07-11 stsp if (err)
6030 818c7501 2019-07-11 stsp goto done;
6031 818c7501 2019-07-11 stsp
6032 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6033 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6034 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6035 818c7501 2019-07-11 stsp &ok_arg);
6036 818c7501 2019-07-11 stsp if (err)
6037 818c7501 2019-07-11 stsp goto done;
6038 818c7501 2019-07-11 stsp
6039 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6040 818c7501 2019-07-11 stsp if (err)
6041 818c7501 2019-07-11 stsp goto done;
6042 818c7501 2019-07-11 stsp
6043 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6044 818c7501 2019-07-11 stsp if (err)
6045 818c7501 2019-07-11 stsp goto done;
6046 818c7501 2019-07-11 stsp
6047 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6048 818c7501 2019-07-11 stsp if (err)
6049 818c7501 2019-07-11 stsp goto done;
6050 818c7501 2019-07-11 stsp
6051 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6052 818c7501 2019-07-11 stsp 0);
6053 e51d7b55 2020-01-04 stsp if (err)
6054 e51d7b55 2020-01-04 stsp goto done;
6055 e51d7b55 2020-01-04 stsp
6056 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6057 818c7501 2019-07-11 stsp if (err)
6058 818c7501 2019-07-11 stsp goto done;
6059 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6060 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6061 e51d7b55 2020-01-04 stsp goto done;
6062 e51d7b55 2020-01-04 stsp }
6063 818c7501 2019-07-11 stsp
6064 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6065 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6066 818c7501 2019-07-11 stsp if (err)
6067 818c7501 2019-07-11 stsp goto done;
6068 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6069 818c7501 2019-07-11 stsp if (err)
6070 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp
6072 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6073 818c7501 2019-07-11 stsp
6074 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6075 818c7501 2019-07-11 stsp if (err)
6076 818c7501 2019-07-11 stsp goto done;
6077 818c7501 2019-07-11 stsp
6078 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6079 818c7501 2019-07-11 stsp if (err)
6080 818c7501 2019-07-11 stsp goto done;
6081 818c7501 2019-07-11 stsp
6082 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6083 818c7501 2019-07-11 stsp worktree->base_commit_id);
6084 818c7501 2019-07-11 stsp if (err)
6085 818c7501 2019-07-11 stsp goto done;
6086 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6087 818c7501 2019-07-11 stsp if (err)
6088 818c7501 2019-07-11 stsp goto done;
6089 818c7501 2019-07-11 stsp
6090 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6091 818c7501 2019-07-11 stsp if (err)
6092 818c7501 2019-07-11 stsp goto done;
6093 818c7501 2019-07-11 stsp done:
6094 818c7501 2019-07-11 stsp free(fileindex_path);
6095 818c7501 2019-07-11 stsp free(tmp_branch_name);
6096 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6097 818c7501 2019-07-11 stsp free(branch_ref_name);
6098 818c7501 2019-07-11 stsp if (branch_ref)
6099 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6100 818c7501 2019-07-11 stsp if (wt_branch)
6101 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6102 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6103 818c7501 2019-07-11 stsp if (err) {
6104 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6105 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6106 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6107 818c7501 2019-07-11 stsp }
6108 818c7501 2019-07-11 stsp if (*tmp_branch) {
6109 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6110 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6111 818c7501 2019-07-11 stsp }
6112 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6113 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6114 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6115 3e3a69f1 2019-07-25 stsp }
6116 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6117 818c7501 2019-07-11 stsp }
6118 818c7501 2019-07-11 stsp return err;
6119 818c7501 2019-07-11 stsp }
6120 818c7501 2019-07-11 stsp
6121 818c7501 2019-07-11 stsp const struct got_error *
6122 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6123 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6124 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6125 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6126 818c7501 2019-07-11 stsp {
6127 818c7501 2019-07-11 stsp const struct got_error *err;
6128 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6129 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6130 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6131 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6132 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6133 818c7501 2019-07-11 stsp
6134 818c7501 2019-07-11 stsp *commit_id = NULL;
6135 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6136 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6137 3e3a69f1 2019-07-25 stsp *branch = NULL;
6138 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6139 3e3a69f1 2019-07-25 stsp
6140 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6141 3e3a69f1 2019-07-25 stsp if (err)
6142 3e3a69f1 2019-07-25 stsp return err;
6143 818c7501 2019-07-11 stsp
6144 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6145 3e3a69f1 2019-07-25 stsp if (err)
6146 f032f1f7 2019-08-04 stsp goto done;
6147 f032f1f7 2019-08-04 stsp
6148 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6149 f032f1f7 2019-08-04 stsp &have_staged_files);
6150 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6151 f032f1f7 2019-08-04 stsp goto done;
6152 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6153 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6154 3e3a69f1 2019-07-25 stsp goto done;
6155 f032f1f7 2019-08-04 stsp }
6156 3e3a69f1 2019-07-25 stsp
6157 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6158 818c7501 2019-07-11 stsp if (err)
6159 f032f1f7 2019-08-04 stsp goto done;
6160 818c7501 2019-07-11 stsp
6161 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6162 818c7501 2019-07-11 stsp if (err)
6163 818c7501 2019-07-11 stsp goto done;
6164 818c7501 2019-07-11 stsp
6165 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6166 818c7501 2019-07-11 stsp if (err)
6167 818c7501 2019-07-11 stsp goto done;
6168 818c7501 2019-07-11 stsp
6169 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6170 818c7501 2019-07-11 stsp if (err)
6171 818c7501 2019-07-11 stsp goto done;
6172 818c7501 2019-07-11 stsp
6173 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6174 818c7501 2019-07-11 stsp if (err)
6175 818c7501 2019-07-11 stsp goto done;
6176 818c7501 2019-07-11 stsp
6177 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6178 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6179 818c7501 2019-07-11 stsp if (err)
6180 818c7501 2019-07-11 stsp goto done;
6181 818c7501 2019-07-11 stsp
6182 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6183 818c7501 2019-07-11 stsp if (err)
6184 818c7501 2019-07-11 stsp goto done;
6185 818c7501 2019-07-11 stsp
6186 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6187 818c7501 2019-07-11 stsp if (err)
6188 818c7501 2019-07-11 stsp goto done;
6189 818c7501 2019-07-11 stsp
6190 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6191 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6192 818c7501 2019-07-11 stsp if (err)
6193 818c7501 2019-07-11 stsp goto done;
6194 818c7501 2019-07-11 stsp
6195 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6196 818c7501 2019-07-11 stsp if (err)
6197 818c7501 2019-07-11 stsp goto done;
6198 818c7501 2019-07-11 stsp done:
6199 818c7501 2019-07-11 stsp free(commit_ref_name);
6200 818c7501 2019-07-11 stsp free(branch_ref_name);
6201 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6202 818c7501 2019-07-11 stsp if (commit_ref)
6203 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6204 818c7501 2019-07-11 stsp if (branch_ref)
6205 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6206 818c7501 2019-07-11 stsp if (err) {
6207 818c7501 2019-07-11 stsp free(*commit_id);
6208 818c7501 2019-07-11 stsp *commit_id = NULL;
6209 818c7501 2019-07-11 stsp if (*tmp_branch) {
6210 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6211 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6212 818c7501 2019-07-11 stsp }
6213 818c7501 2019-07-11 stsp if (*new_base_branch) {
6214 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6215 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6216 818c7501 2019-07-11 stsp }
6217 818c7501 2019-07-11 stsp if (*branch) {
6218 818c7501 2019-07-11 stsp got_ref_close(*branch);
6219 818c7501 2019-07-11 stsp *branch = NULL;
6220 818c7501 2019-07-11 stsp }
6221 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6222 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6223 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6224 3e3a69f1 2019-07-25 stsp }
6225 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6226 818c7501 2019-07-11 stsp }
6227 818c7501 2019-07-11 stsp return err;
6228 c4296144 2019-05-09 stsp }
6229 818c7501 2019-07-11 stsp
6230 818c7501 2019-07-11 stsp const struct got_error *
6231 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6232 818c7501 2019-07-11 stsp {
6233 818c7501 2019-07-11 stsp const struct got_error *err;
6234 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6235 818c7501 2019-07-11 stsp
6236 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6237 818c7501 2019-07-11 stsp if (err)
6238 818c7501 2019-07-11 stsp return err;
6239 818c7501 2019-07-11 stsp
6240 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6241 818c7501 2019-07-11 stsp free(tmp_branch_name);
6242 818c7501 2019-07-11 stsp return NULL;
6243 818c7501 2019-07-11 stsp }
6244 818c7501 2019-07-11 stsp
6245 818c7501 2019-07-11 stsp static const struct got_error *
6246 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6247 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6248 818c7501 2019-07-11 stsp {
6249 0ebf8283 2019-07-24 stsp *logmsg = arg;
6250 818c7501 2019-07-11 stsp return NULL;
6251 818c7501 2019-07-11 stsp }
6252 818c7501 2019-07-11 stsp
6253 818c7501 2019-07-11 stsp static const struct got_error *
6254 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6255 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6256 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6257 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6258 818c7501 2019-07-11 stsp {
6259 818c7501 2019-07-11 stsp return NULL;
6260 01757395 2019-07-12 stsp }
6261 01757395 2019-07-12 stsp
6262 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6263 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6264 01757395 2019-07-12 stsp void *progress_arg;
6265 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6266 01757395 2019-07-12 stsp };
6267 01757395 2019-07-12 stsp
6268 01757395 2019-07-12 stsp static const struct got_error *
6269 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6270 01757395 2019-07-12 stsp {
6271 01757395 2019-07-12 stsp const struct got_error *err;
6272 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6273 01757395 2019-07-12 stsp char *p;
6274 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6275 01757395 2019-07-12 stsp
6276 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6277 01757395 2019-07-12 stsp if (err)
6278 01757395 2019-07-12 stsp return err;
6279 01757395 2019-07-12 stsp
6280 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6281 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6282 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6283 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6284 01757395 2019-07-12 stsp return NULL;
6285 01757395 2019-07-12 stsp
6286 01757395 2019-07-12 stsp p = strdup(path);
6287 01757395 2019-07-12 stsp if (p == NULL)
6288 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6289 01757395 2019-07-12 stsp
6290 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6291 01757395 2019-07-12 stsp if (err || new == NULL)
6292 01757395 2019-07-12 stsp free(p);
6293 01757395 2019-07-12 stsp return err;
6294 01757395 2019-07-12 stsp }
6295 01757395 2019-07-12 stsp
6296 01757395 2019-07-12 stsp void
6297 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6298 01757395 2019-07-12 stsp {
6299 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6300 01757395 2019-07-12 stsp
6301 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6302 01757395 2019-07-12 stsp free((char *)pe->path);
6303 01757395 2019-07-12 stsp
6304 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6305 818c7501 2019-07-11 stsp }
6306 818c7501 2019-07-11 stsp
6307 0ebf8283 2019-07-24 stsp static const struct got_error *
6308 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6309 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6310 818c7501 2019-07-11 stsp {
6311 818c7501 2019-07-11 stsp const struct got_error *err;
6312 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6313 818c7501 2019-07-11 stsp
6314 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6315 818c7501 2019-07-11 stsp if (err) {
6316 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6317 818c7501 2019-07-11 stsp goto done;
6318 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6319 818c7501 2019-07-11 stsp if (err)
6320 818c7501 2019-07-11 stsp goto done;
6321 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6322 818c7501 2019-07-11 stsp if (err)
6323 818c7501 2019-07-11 stsp goto done;
6324 de05890f 2020-03-05 stsp } else if (is_rebase) {
6325 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6326 818c7501 2019-07-11 stsp int cmp;
6327 818c7501 2019-07-11 stsp
6328 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6329 818c7501 2019-07-11 stsp if (err)
6330 818c7501 2019-07-11 stsp goto done;
6331 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6332 818c7501 2019-07-11 stsp free(stored_id);
6333 818c7501 2019-07-11 stsp if (cmp != 0) {
6334 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6335 818c7501 2019-07-11 stsp goto done;
6336 818c7501 2019-07-11 stsp }
6337 818c7501 2019-07-11 stsp }
6338 0ebf8283 2019-07-24 stsp done:
6339 0ebf8283 2019-07-24 stsp if (commit_ref)
6340 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6341 0ebf8283 2019-07-24 stsp return err;
6342 0ebf8283 2019-07-24 stsp }
6343 0ebf8283 2019-07-24 stsp
6344 0ebf8283 2019-07-24 stsp static const struct got_error *
6345 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6346 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6347 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6348 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6349 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6350 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6351 0ebf8283 2019-07-24 stsp {
6352 0ebf8283 2019-07-24 stsp const struct got_error *err;
6353 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6354 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6355 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6356 818c7501 2019-07-11 stsp
6357 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6358 0ebf8283 2019-07-24 stsp
6359 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6360 0ebf8283 2019-07-24 stsp if (err)
6361 0ebf8283 2019-07-24 stsp return err;
6362 0ebf8283 2019-07-24 stsp
6363 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6364 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6365 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6366 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6367 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6368 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6369 818c7501 2019-07-11 stsp if (commit_ref)
6370 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6371 818c7501 2019-07-11 stsp return err;
6372 818c7501 2019-07-11 stsp }
6373 818c7501 2019-07-11 stsp
6374 818c7501 2019-07-11 stsp const struct got_error *
6375 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6376 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6377 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6378 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6379 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6380 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6381 818c7501 2019-07-11 stsp {
6382 0ebf8283 2019-07-24 stsp const struct got_error *err;
6383 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6384 0ebf8283 2019-07-24 stsp
6385 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6386 0ebf8283 2019-07-24 stsp if (err)
6387 0ebf8283 2019-07-24 stsp return err;
6388 0ebf8283 2019-07-24 stsp
6389 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6390 0ebf8283 2019-07-24 stsp if (err)
6391 0ebf8283 2019-07-24 stsp goto done;
6392 0ebf8283 2019-07-24 stsp
6393 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6394 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6395 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6396 0ebf8283 2019-07-24 stsp done:
6397 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6398 0ebf8283 2019-07-24 stsp return err;
6399 0ebf8283 2019-07-24 stsp }
6400 0ebf8283 2019-07-24 stsp
6401 0ebf8283 2019-07-24 stsp const struct got_error *
6402 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6403 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6404 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6405 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6406 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6407 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6408 0ebf8283 2019-07-24 stsp {
6409 0ebf8283 2019-07-24 stsp const struct got_error *err;
6410 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6411 0ebf8283 2019-07-24 stsp
6412 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6413 0ebf8283 2019-07-24 stsp if (err)
6414 0ebf8283 2019-07-24 stsp return err;
6415 0ebf8283 2019-07-24 stsp
6416 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6417 0ebf8283 2019-07-24 stsp if (err)
6418 0ebf8283 2019-07-24 stsp goto done;
6419 0ebf8283 2019-07-24 stsp
6420 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6421 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6422 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6423 0ebf8283 2019-07-24 stsp done:
6424 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6425 0ebf8283 2019-07-24 stsp return err;
6426 0ebf8283 2019-07-24 stsp }
6427 0ebf8283 2019-07-24 stsp
6428 0ebf8283 2019-07-24 stsp static const struct got_error *
6429 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6430 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6431 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6432 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6433 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6434 0ebf8283 2019-07-24 stsp {
6435 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6436 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6437 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6438 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6439 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6440 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6441 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6442 818c7501 2019-07-11 stsp
6443 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6444 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6445 a0e95631 2019-07-12 stsp
6446 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6447 818c7501 2019-07-11 stsp
6448 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6449 a0e95631 2019-07-12 stsp if (err)
6450 3e3a69f1 2019-07-25 stsp return err;
6451 a0e95631 2019-07-12 stsp
6452 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6453 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6454 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6455 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6456 01757395 2019-07-12 stsp /*
6457 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6458 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6459 6c13b005 2021-09-02 stsp *
6460 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6461 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6462 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6463 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6464 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6465 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6466 01757395 2019-07-12 stsp */
6467 01757395 2019-07-12 stsp if (merged_paths) {
6468 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6469 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6470 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6471 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6472 f2a9dc41 2019-12-13 tracey 0);
6473 01757395 2019-07-12 stsp if (err)
6474 01757395 2019-07-12 stsp goto done;
6475 01757395 2019-07-12 stsp }
6476 01757395 2019-07-12 stsp } else {
6477 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6478 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6479 01757395 2019-07-12 stsp if (err)
6480 01757395 2019-07-12 stsp goto done;
6481 01757395 2019-07-12 stsp }
6482 a0e95631 2019-07-12 stsp
6483 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6484 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6485 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6486 a0e95631 2019-07-12 stsp if (err)
6487 a0e95631 2019-07-12 stsp goto done;
6488 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6489 a0e95631 2019-07-12 stsp goto done;
6490 ff0d2220 2019-07-11 stsp }
6491 818c7501 2019-07-11 stsp
6492 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6493 edd02c5e 2019-07-11 stsp if (err)
6494 edd02c5e 2019-07-11 stsp goto done;
6495 edd02c5e 2019-07-11 stsp
6496 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6497 818c7501 2019-07-11 stsp if (err)
6498 818c7501 2019-07-11 stsp goto done;
6499 818c7501 2019-07-11 stsp
6500 5943eee2 2019-08-13 stsp if (new_logmsg) {
6501 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6502 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6503 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6504 5943eee2 2019-08-13 stsp goto done;
6505 5943eee2 2019-08-13 stsp }
6506 5943eee2 2019-08-13 stsp } else {
6507 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6508 5943eee2 2019-08-13 stsp if (err)
6509 5943eee2 2019-08-13 stsp goto done;
6510 5943eee2 2019-08-13 stsp }
6511 0ebf8283 2019-07-24 stsp
6512 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6513 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6514 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6515 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6516 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6517 a0e95631 2019-07-12 stsp if (err)
6518 a0e95631 2019-07-12 stsp goto done;
6519 a0e95631 2019-07-12 stsp
6520 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6521 a0e95631 2019-07-12 stsp if (err)
6522 a0e95631 2019-07-12 stsp goto done;
6523 a0e95631 2019-07-12 stsp
6524 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6525 a0e95631 2019-07-12 stsp if (err)
6526 a0e95631 2019-07-12 stsp goto done;
6527 a0e95631 2019-07-12 stsp
6528 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6529 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6530 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6531 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6532 a0e95631 2019-07-12 stsp err = sync_err;
6533 818c7501 2019-07-11 stsp done:
6534 a0e95631 2019-07-12 stsp free(fileindex_path);
6535 a0e95631 2019-07-12 stsp free(head_commit_id);
6536 a0e95631 2019-07-12 stsp if (head_ref)
6537 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6538 818c7501 2019-07-11 stsp if (err) {
6539 818c7501 2019-07-11 stsp free(*new_commit_id);
6540 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6541 818c7501 2019-07-11 stsp }
6542 818c7501 2019-07-11 stsp return err;
6543 818c7501 2019-07-11 stsp }
6544 818c7501 2019-07-11 stsp
6545 818c7501 2019-07-11 stsp const struct got_error *
6546 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6547 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6548 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6549 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6550 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6551 0ebf8283 2019-07-24 stsp {
6552 0ebf8283 2019-07-24 stsp const struct got_error *err;
6553 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6554 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6555 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6558 0ebf8283 2019-07-24 stsp if (err)
6559 0ebf8283 2019-07-24 stsp return err;
6560 0ebf8283 2019-07-24 stsp
6561 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6562 0ebf8283 2019-07-24 stsp if (err)
6563 0ebf8283 2019-07-24 stsp goto done;
6564 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6565 0ebf8283 2019-07-24 stsp if (err)
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6568 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6569 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp }
6571 0ebf8283 2019-07-24 stsp
6572 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6573 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6574 0ebf8283 2019-07-24 stsp done:
6575 0ebf8283 2019-07-24 stsp if (commit_ref)
6576 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6577 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6578 0ebf8283 2019-07-24 stsp free(commit_id);
6579 0ebf8283 2019-07-24 stsp return err;
6580 0ebf8283 2019-07-24 stsp }
6581 0ebf8283 2019-07-24 stsp
6582 0ebf8283 2019-07-24 stsp const struct got_error *
6583 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6584 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6585 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6586 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6587 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6588 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6589 0ebf8283 2019-07-24 stsp {
6590 0ebf8283 2019-07-24 stsp const struct got_error *err;
6591 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6592 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6593 0ebf8283 2019-07-24 stsp
6594 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6595 0ebf8283 2019-07-24 stsp if (err)
6596 0ebf8283 2019-07-24 stsp return err;
6597 0ebf8283 2019-07-24 stsp
6598 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6599 0ebf8283 2019-07-24 stsp if (err)
6600 0ebf8283 2019-07-24 stsp goto done;
6601 0ebf8283 2019-07-24 stsp
6602 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6603 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6604 0ebf8283 2019-07-24 stsp done:
6605 0ebf8283 2019-07-24 stsp if (commit_ref)
6606 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6607 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6608 0ebf8283 2019-07-24 stsp return err;
6609 0ebf8283 2019-07-24 stsp }
6610 0ebf8283 2019-07-24 stsp
6611 0ebf8283 2019-07-24 stsp const struct got_error *
6612 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6613 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6614 818c7501 2019-07-11 stsp {
6615 3e3a69f1 2019-07-25 stsp if (fileindex)
6616 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6617 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6618 69844fba 2019-07-11 stsp }
6619 69844fba 2019-07-11 stsp
6620 69844fba 2019-07-11 stsp static const struct got_error *
6621 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6622 69844fba 2019-07-11 stsp {
6623 69844fba 2019-07-11 stsp const struct got_error *err;
6624 69844fba 2019-07-11 stsp struct got_reference *ref;
6625 69844fba 2019-07-11 stsp
6626 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6627 69844fba 2019-07-11 stsp if (err) {
6628 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6629 69844fba 2019-07-11 stsp return NULL;
6630 69844fba 2019-07-11 stsp return err;
6631 69844fba 2019-07-11 stsp }
6632 69844fba 2019-07-11 stsp
6633 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6634 69844fba 2019-07-11 stsp got_ref_close(ref);
6635 69844fba 2019-07-11 stsp return err;
6636 818c7501 2019-07-11 stsp }
6637 818c7501 2019-07-11 stsp
6638 69844fba 2019-07-11 stsp static const struct got_error *
6639 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6640 69844fba 2019-07-11 stsp {
6641 69844fba 2019-07-11 stsp const struct got_error *err;
6642 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6643 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6644 69844fba 2019-07-11 stsp
6645 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6646 69844fba 2019-07-11 stsp if (err)
6647 69844fba 2019-07-11 stsp goto done;
6648 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6649 69844fba 2019-07-11 stsp if (err)
6650 69844fba 2019-07-11 stsp goto done;
6651 69844fba 2019-07-11 stsp
6652 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6653 69844fba 2019-07-11 stsp if (err)
6654 69844fba 2019-07-11 stsp goto done;
6655 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6656 69844fba 2019-07-11 stsp if (err)
6657 69844fba 2019-07-11 stsp goto done;
6658 69844fba 2019-07-11 stsp
6659 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6660 69844fba 2019-07-11 stsp if (err)
6661 69844fba 2019-07-11 stsp goto done;
6662 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6663 69844fba 2019-07-11 stsp if (err)
6664 69844fba 2019-07-11 stsp goto done;
6665 69844fba 2019-07-11 stsp
6666 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6667 69844fba 2019-07-11 stsp if (err)
6668 69844fba 2019-07-11 stsp goto done;
6669 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6670 69844fba 2019-07-11 stsp if (err)
6671 69844fba 2019-07-11 stsp goto done;
6672 69844fba 2019-07-11 stsp
6673 69844fba 2019-07-11 stsp done:
6674 69844fba 2019-07-11 stsp free(tmp_branch_name);
6675 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6676 69844fba 2019-07-11 stsp free(branch_ref_name);
6677 69844fba 2019-07-11 stsp free(commit_ref_name);
6678 e600f124 2021-03-21 stsp return err;
6679 e600f124 2021-03-21 stsp }
6680 e600f124 2021-03-21 stsp
6681 e600f124 2021-03-21 stsp const struct got_error *
6682 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6683 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6684 e600f124 2021-03-21 stsp {
6685 e600f124 2021-03-21 stsp const struct got_error *err;
6686 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6687 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6688 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6689 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6690 e600f124 2021-03-21 stsp char *refname = NULL;
6691 e600f124 2021-03-21 stsp
6692 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6693 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6694 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6695 e600f124 2021-03-21 stsp branch_name += 11;
6696 e600f124 2021-03-21 stsp
6697 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6698 e600f124 2021-03-21 stsp if (err)
6699 e600f124 2021-03-21 stsp return err;
6700 e600f124 2021-03-21 stsp
6701 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6702 e600f124 2021-03-21 stsp new_id_str) == -1) {
6703 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6704 e600f124 2021-03-21 stsp goto done;
6705 e600f124 2021-03-21 stsp }
6706 e600f124 2021-03-21 stsp
6707 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6708 e600f124 2021-03-21 stsp if (err)
6709 e600f124 2021-03-21 stsp goto done;
6710 e600f124 2021-03-21 stsp
6711 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6712 e600f124 2021-03-21 stsp if (err)
6713 e600f124 2021-03-21 stsp goto done;
6714 e600f124 2021-03-21 stsp
6715 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6716 e600f124 2021-03-21 stsp done:
6717 e600f124 2021-03-21 stsp free(new_id_str);
6718 e600f124 2021-03-21 stsp free(refname);
6719 e600f124 2021-03-21 stsp free(old_commit_id);
6720 e600f124 2021-03-21 stsp if (ref)
6721 e600f124 2021-03-21 stsp got_ref_close(ref);
6722 69844fba 2019-07-11 stsp return err;
6723 69844fba 2019-07-11 stsp }
6724 69844fba 2019-07-11 stsp
6725 818c7501 2019-07-11 stsp const struct got_error *
6726 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6727 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6728 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6729 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6730 818c7501 2019-07-11 stsp {
6731 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6732 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6733 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6736 818c7501 2019-07-11 stsp if (err)
6737 818c7501 2019-07-11 stsp return err;
6738 e600f124 2021-03-21 stsp
6739 e600f124 2021-03-21 stsp if (create_backup) {
6740 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6741 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6742 e600f124 2021-03-21 stsp if (err)
6743 e600f124 2021-03-21 stsp goto done;
6744 e600f124 2021-03-21 stsp }
6745 818c7501 2019-07-11 stsp
6746 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6747 818c7501 2019-07-11 stsp if (err)
6748 818c7501 2019-07-11 stsp goto done;
6749 818c7501 2019-07-11 stsp
6750 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6751 818c7501 2019-07-11 stsp if (err)
6752 818c7501 2019-07-11 stsp goto done;
6753 818c7501 2019-07-11 stsp
6754 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6755 818c7501 2019-07-11 stsp if (err)
6756 818c7501 2019-07-11 stsp goto done;
6757 818c7501 2019-07-11 stsp
6758 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6759 a615e0e7 2020-12-16 stsp if (err)
6760 a615e0e7 2020-12-16 stsp goto done;
6761 a615e0e7 2020-12-16 stsp
6762 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6763 a615e0e7 2020-12-16 stsp if (err)
6764 a615e0e7 2020-12-16 stsp goto done;
6765 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6766 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6767 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6768 a615e0e7 2020-12-16 stsp err = sync_err;
6769 818c7501 2019-07-11 stsp done:
6770 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6771 a615e0e7 2020-12-16 stsp free(fileindex_path);
6772 818c7501 2019-07-11 stsp free(new_head_commit_id);
6773 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6774 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6775 818c7501 2019-07-11 stsp err = unlockerr;
6776 818c7501 2019-07-11 stsp return err;
6777 818c7501 2019-07-11 stsp }
6778 818c7501 2019-07-11 stsp
6779 818c7501 2019-07-11 stsp const struct got_error *
6780 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6781 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6782 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6783 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6784 818c7501 2019-07-11 stsp {
6785 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6786 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6787 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6788 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6789 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6790 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6791 818c7501 2019-07-11 stsp
6792 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6793 818c7501 2019-07-11 stsp if (err)
6794 818c7501 2019-07-11 stsp return err;
6795 818c7501 2019-07-11 stsp
6796 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6797 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6798 818c7501 2019-07-11 stsp if (err)
6799 818c7501 2019-07-11 stsp goto done;
6800 818c7501 2019-07-11 stsp
6801 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6802 818c7501 2019-07-11 stsp if (err)
6803 818c7501 2019-07-11 stsp goto done;
6804 818c7501 2019-07-11 stsp
6805 818c7501 2019-07-11 stsp /*
6806 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6807 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6808 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6809 818c7501 2019-07-11 stsp */
6810 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6811 818c7501 2019-07-11 stsp if (err)
6812 818c7501 2019-07-11 stsp goto done;
6813 818c7501 2019-07-11 stsp
6814 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6815 818c7501 2019-07-11 stsp if (err)
6816 818c7501 2019-07-11 stsp goto done;
6817 818c7501 2019-07-11 stsp
6818 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6819 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6820 ca355955 2019-07-12 stsp if (err)
6821 ca355955 2019-07-12 stsp goto done;
6822 ca355955 2019-07-12 stsp
6823 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6824 ca355955 2019-07-12 stsp if (err)
6825 ca355955 2019-07-12 stsp goto done;
6826 ca355955 2019-07-12 stsp
6827 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6828 818c7501 2019-07-11 stsp if (err)
6829 818c7501 2019-07-11 stsp goto done;
6830 818c7501 2019-07-11 stsp
6831 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6832 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6833 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6834 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6835 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6836 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6837 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6838 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6839 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6840 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
6841 55bd499d 2019-07-12 stsp if (err)
6842 1f1abb7e 2019-08-08 stsp goto sync;
6843 55bd499d 2019-07-12 stsp
6844 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6845 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6846 ca355955 2019-07-12 stsp sync:
6847 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6848 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6849 ca355955 2019-07-12 stsp err = sync_err;
6850 818c7501 2019-07-11 stsp done:
6851 818c7501 2019-07-11 stsp got_ref_close(resolved);
6852 a3a2faf2 2019-07-12 stsp free(tree_id);
6853 818c7501 2019-07-11 stsp free(commit_id);
6854 0ebf8283 2019-07-24 stsp if (fileindex)
6855 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6856 0ebf8283 2019-07-24 stsp free(fileindex_path);
6857 0ebf8283 2019-07-24 stsp
6858 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6859 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6860 0ebf8283 2019-07-24 stsp err = unlockerr;
6861 0ebf8283 2019-07-24 stsp return err;
6862 0ebf8283 2019-07-24 stsp }
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp const struct got_error *
6865 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6866 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6867 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6868 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6869 0ebf8283 2019-07-24 stsp {
6870 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6871 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6872 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6873 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6874 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6875 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6876 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6877 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6878 0ebf8283 2019-07-24 stsp
6879 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6880 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6881 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6882 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6883 0ebf8283 2019-07-24 stsp
6884 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6885 0ebf8283 2019-07-24 stsp if (err)
6886 0ebf8283 2019-07-24 stsp return err;
6887 0ebf8283 2019-07-24 stsp
6888 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6889 0ebf8283 2019-07-24 stsp if (err)
6890 0ebf8283 2019-07-24 stsp goto done;
6891 0ebf8283 2019-07-24 stsp
6892 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6893 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6894 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6895 0ebf8283 2019-07-24 stsp &ok_arg);
6896 0ebf8283 2019-07-24 stsp if (err)
6897 0ebf8283 2019-07-24 stsp goto done;
6898 0ebf8283 2019-07-24 stsp
6899 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6900 0ebf8283 2019-07-24 stsp if (err)
6901 0ebf8283 2019-07-24 stsp goto done;
6902 0ebf8283 2019-07-24 stsp
6903 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6904 0ebf8283 2019-07-24 stsp if (err)
6905 0ebf8283 2019-07-24 stsp goto done;
6906 0ebf8283 2019-07-24 stsp
6907 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6908 0ebf8283 2019-07-24 stsp worktree);
6909 0ebf8283 2019-07-24 stsp if (err)
6910 0ebf8283 2019-07-24 stsp goto done;
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6913 0ebf8283 2019-07-24 stsp 0);
6914 0ebf8283 2019-07-24 stsp if (err)
6915 0ebf8283 2019-07-24 stsp goto done;
6916 0ebf8283 2019-07-24 stsp
6917 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6918 0ebf8283 2019-07-24 stsp if (err)
6919 0ebf8283 2019-07-24 stsp goto done;
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp goto done;
6924 0ebf8283 2019-07-24 stsp
6925 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6926 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6927 0ebf8283 2019-07-24 stsp if (err)
6928 0ebf8283 2019-07-24 stsp goto done;
6929 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6933 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6934 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6935 0ebf8283 2019-07-24 stsp goto done;
6936 0ebf8283 2019-07-24 stsp }
6937 0ebf8283 2019-07-24 stsp
6938 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6939 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6940 0ebf8283 2019-07-24 stsp if (err)
6941 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6943 0ebf8283 2019-07-24 stsp if (err)
6944 0ebf8283 2019-07-24 stsp goto done;
6945 0ebf8283 2019-07-24 stsp
6946 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6947 0ebf8283 2019-07-24 stsp if (err)
6948 0ebf8283 2019-07-24 stsp goto done;
6949 0ebf8283 2019-07-24 stsp done:
6950 0ebf8283 2019-07-24 stsp free(fileindex_path);
6951 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6952 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6953 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6954 0ebf8283 2019-07-24 stsp if (wt_branch)
6955 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6956 0ebf8283 2019-07-24 stsp if (err) {
6957 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6958 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6959 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6960 0ebf8283 2019-07-24 stsp }
6961 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6962 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6963 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6964 0ebf8283 2019-07-24 stsp }
6965 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6966 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6967 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6968 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6969 3e3a69f1 2019-07-25 stsp }
6970 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6971 0ebf8283 2019-07-24 stsp }
6972 0ebf8283 2019-07-24 stsp return err;
6973 0ebf8283 2019-07-24 stsp }
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp const struct got_error *
6976 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6977 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6978 0ebf8283 2019-07-24 stsp {
6979 3e3a69f1 2019-07-25 stsp if (fileindex)
6980 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6981 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6982 0ebf8283 2019-07-24 stsp }
6983 0ebf8283 2019-07-24 stsp
6984 0ebf8283 2019-07-24 stsp const struct got_error *
6985 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6986 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6987 0ebf8283 2019-07-24 stsp {
6988 0ebf8283 2019-07-24 stsp const struct got_error *err;
6989 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6990 0ebf8283 2019-07-24 stsp
6991 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6992 0ebf8283 2019-07-24 stsp if (err)
6993 0ebf8283 2019-07-24 stsp return err;
6994 0ebf8283 2019-07-24 stsp
6995 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6996 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6997 0ebf8283 2019-07-24 stsp return NULL;
6998 0ebf8283 2019-07-24 stsp }
6999 0ebf8283 2019-07-24 stsp
7000 0ebf8283 2019-07-24 stsp const struct got_error *
7001 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
7002 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
7003 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7004 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7005 0ebf8283 2019-07-24 stsp {
7006 0ebf8283 2019-07-24 stsp const struct got_error *err;
7007 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7008 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7009 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
7010 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
7011 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
7012 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
7013 0ebf8283 2019-07-24 stsp
7014 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7015 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7016 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7017 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7018 0ebf8283 2019-07-24 stsp
7019 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7020 3e3a69f1 2019-07-25 stsp if (err)
7021 3e3a69f1 2019-07-25 stsp return err;
7022 3e3a69f1 2019-07-25 stsp
7023 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7024 3e3a69f1 2019-07-25 stsp if (err)
7025 f032f1f7 2019-08-04 stsp goto done;
7026 f032f1f7 2019-08-04 stsp
7027 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7028 f032f1f7 2019-08-04 stsp &have_staged_files);
7029 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7030 f032f1f7 2019-08-04 stsp goto done;
7031 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7032 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7033 3e3a69f1 2019-07-25 stsp goto done;
7034 f032f1f7 2019-08-04 stsp }
7035 3e3a69f1 2019-07-25 stsp
7036 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7037 0ebf8283 2019-07-24 stsp if (err)
7038 f032f1f7 2019-08-04 stsp goto done;
7039 0ebf8283 2019-07-24 stsp
7040 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7041 0ebf8283 2019-07-24 stsp if (err)
7042 0ebf8283 2019-07-24 stsp goto done;
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7045 0ebf8283 2019-07-24 stsp if (err)
7046 0ebf8283 2019-07-24 stsp goto done;
7047 0ebf8283 2019-07-24 stsp
7048 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7049 0ebf8283 2019-07-24 stsp worktree);
7050 0ebf8283 2019-07-24 stsp if (err)
7051 0ebf8283 2019-07-24 stsp goto done;
7052 0ebf8283 2019-07-24 stsp
7053 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7054 0ebf8283 2019-07-24 stsp if (err)
7055 0ebf8283 2019-07-24 stsp goto done;
7056 0ebf8283 2019-07-24 stsp
7057 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7058 0ebf8283 2019-07-24 stsp if (err)
7059 0ebf8283 2019-07-24 stsp goto done;
7060 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7061 0ebf8283 2019-07-24 stsp if (err)
7062 0ebf8283 2019-07-24 stsp goto done;
7063 0ebf8283 2019-07-24 stsp
7064 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7065 0ebf8283 2019-07-24 stsp if (err)
7066 0ebf8283 2019-07-24 stsp goto done;
7067 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7068 0ebf8283 2019-07-24 stsp if (err)
7069 0ebf8283 2019-07-24 stsp goto done;
7070 0ebf8283 2019-07-24 stsp
7071 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7072 0ebf8283 2019-07-24 stsp if (err)
7073 0ebf8283 2019-07-24 stsp goto done;
7074 0ebf8283 2019-07-24 stsp done:
7075 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7076 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7077 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7078 0ebf8283 2019-07-24 stsp if (commit_ref)
7079 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7080 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7081 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7082 0ebf8283 2019-07-24 stsp if (err) {
7083 0ebf8283 2019-07-24 stsp free(*commit_id);
7084 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7085 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7086 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7087 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7088 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7089 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7090 0ebf8283 2019-07-24 stsp }
7091 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7092 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7093 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7094 3e3a69f1 2019-07-25 stsp }
7095 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7096 0ebf8283 2019-07-24 stsp }
7097 0ebf8283 2019-07-24 stsp return err;
7098 0ebf8283 2019-07-24 stsp }
7099 0ebf8283 2019-07-24 stsp
7100 0ebf8283 2019-07-24 stsp static const struct got_error *
7101 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7102 0ebf8283 2019-07-24 stsp {
7103 0ebf8283 2019-07-24 stsp const struct got_error *err;
7104 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7105 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7106 0ebf8283 2019-07-24 stsp
7107 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7108 0ebf8283 2019-07-24 stsp if (err)
7109 0ebf8283 2019-07-24 stsp goto done;
7110 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7111 0ebf8283 2019-07-24 stsp if (err)
7112 0ebf8283 2019-07-24 stsp goto done;
7113 0ebf8283 2019-07-24 stsp
7114 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7115 0ebf8283 2019-07-24 stsp worktree);
7116 0ebf8283 2019-07-24 stsp if (err)
7117 0ebf8283 2019-07-24 stsp goto done;
7118 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7119 0ebf8283 2019-07-24 stsp if (err)
7120 0ebf8283 2019-07-24 stsp goto done;
7121 0ebf8283 2019-07-24 stsp
7122 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7123 0ebf8283 2019-07-24 stsp if (err)
7124 0ebf8283 2019-07-24 stsp goto done;
7125 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7126 0ebf8283 2019-07-24 stsp if (err)
7127 0ebf8283 2019-07-24 stsp goto done;
7128 0ebf8283 2019-07-24 stsp
7129 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7130 0ebf8283 2019-07-24 stsp if (err)
7131 0ebf8283 2019-07-24 stsp goto done;
7132 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7133 0ebf8283 2019-07-24 stsp if (err)
7134 0ebf8283 2019-07-24 stsp goto done;
7135 0ebf8283 2019-07-24 stsp done:
7136 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7137 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7138 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7139 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7140 0ebf8283 2019-07-24 stsp return err;
7141 0ebf8283 2019-07-24 stsp }
7142 0ebf8283 2019-07-24 stsp
7143 0ebf8283 2019-07-24 stsp const struct got_error *
7144 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7145 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7146 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7147 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7148 0ebf8283 2019-07-24 stsp {
7149 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7150 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7151 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7152 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7153 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7154 0ebf8283 2019-07-24 stsp
7155 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7156 0ebf8283 2019-07-24 stsp if (err)
7157 0ebf8283 2019-07-24 stsp return err;
7158 0ebf8283 2019-07-24 stsp
7159 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7160 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7161 0ebf8283 2019-07-24 stsp if (err)
7162 0ebf8283 2019-07-24 stsp goto done;
7163 0ebf8283 2019-07-24 stsp
7164 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7165 0ebf8283 2019-07-24 stsp if (err)
7166 0ebf8283 2019-07-24 stsp goto done;
7167 0ebf8283 2019-07-24 stsp
7168 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7169 0ebf8283 2019-07-24 stsp if (err)
7170 0ebf8283 2019-07-24 stsp goto done;
7171 0ebf8283 2019-07-24 stsp
7172 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7173 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7174 0ebf8283 2019-07-24 stsp if (err)
7175 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp
7177 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7178 0ebf8283 2019-07-24 stsp if (err)
7179 0ebf8283 2019-07-24 stsp goto done;
7180 0ebf8283 2019-07-24 stsp
7181 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7182 0ebf8283 2019-07-24 stsp if (err)
7183 0ebf8283 2019-07-24 stsp goto done;
7184 0ebf8283 2019-07-24 stsp
7185 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7186 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7187 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7188 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7189 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7190 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7191 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7192 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7193 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7194 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7195 0ebf8283 2019-07-24 stsp if (err)
7196 1f1abb7e 2019-08-08 stsp goto sync;
7197 0ebf8283 2019-07-24 stsp
7198 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7199 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7200 0ebf8283 2019-07-24 stsp sync:
7201 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7202 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7203 0ebf8283 2019-07-24 stsp err = sync_err;
7204 0ebf8283 2019-07-24 stsp done:
7205 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7206 0ebf8283 2019-07-24 stsp free(tree_id);
7207 818c7501 2019-07-11 stsp free(fileindex_path);
7208 818c7501 2019-07-11 stsp
7209 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7210 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7211 818c7501 2019-07-11 stsp err = unlockerr;
7212 818c7501 2019-07-11 stsp return err;
7213 818c7501 2019-07-11 stsp }
7214 0ebf8283 2019-07-24 stsp
7215 0ebf8283 2019-07-24 stsp const struct got_error *
7216 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7217 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7218 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7219 0ebf8283 2019-07-24 stsp {
7220 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7221 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7222 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7223 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7224 0ebf8283 2019-07-24 stsp
7225 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7226 0ebf8283 2019-07-24 stsp if (err)
7227 0ebf8283 2019-07-24 stsp return err;
7228 0ebf8283 2019-07-24 stsp
7229 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7230 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7231 e600f124 2021-03-21 stsp if (err)
7232 e600f124 2021-03-21 stsp goto done;
7233 e600f124 2021-03-21 stsp
7234 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7235 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7236 0ebf8283 2019-07-24 stsp if (err)
7237 0ebf8283 2019-07-24 stsp goto done;
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7240 0ebf8283 2019-07-24 stsp if (err)
7241 0ebf8283 2019-07-24 stsp goto done;
7242 0ebf8283 2019-07-24 stsp
7243 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7244 0ebf8283 2019-07-24 stsp if (err)
7245 0ebf8283 2019-07-24 stsp goto done;
7246 0ebf8283 2019-07-24 stsp
7247 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7248 0ebf8283 2019-07-24 stsp if (err)
7249 0ebf8283 2019-07-24 stsp goto done;
7250 0ebf8283 2019-07-24 stsp
7251 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7252 a615e0e7 2020-12-16 stsp if (err)
7253 a615e0e7 2020-12-16 stsp goto done;
7254 a615e0e7 2020-12-16 stsp
7255 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7256 a615e0e7 2020-12-16 stsp if (err)
7257 a615e0e7 2020-12-16 stsp goto done;
7258 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7259 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7260 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7261 a615e0e7 2020-12-16 stsp err = sync_err;
7262 0ebf8283 2019-07-24 stsp done:
7263 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7264 a615e0e7 2020-12-16 stsp free(fileindex_path);
7265 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7266 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7267 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7268 0ebf8283 2019-07-24 stsp err = unlockerr;
7269 0ebf8283 2019-07-24 stsp return err;
7270 0ebf8283 2019-07-24 stsp }
7271 0ebf8283 2019-07-24 stsp
7272 0ebf8283 2019-07-24 stsp const struct got_error *
7273 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7274 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7275 0ebf8283 2019-07-24 stsp {
7276 0ebf8283 2019-07-24 stsp const struct got_error *err;
7277 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7278 0ebf8283 2019-07-24 stsp
7279 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7280 0ebf8283 2019-07-24 stsp if (err)
7281 0ebf8283 2019-07-24 stsp return err;
7282 0ebf8283 2019-07-24 stsp
7283 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7284 0ebf8283 2019-07-24 stsp if (err)
7285 0ebf8283 2019-07-24 stsp goto done;
7286 0ebf8283 2019-07-24 stsp
7287 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7288 0ebf8283 2019-07-24 stsp done:
7289 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7290 2822a352 2019-10-15 stsp return err;
7291 2822a352 2019-10-15 stsp }
7292 2822a352 2019-10-15 stsp
7293 2822a352 2019-10-15 stsp const struct got_error *
7294 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7295 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7296 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7297 2822a352 2019-10-15 stsp struct got_repository *repo)
7298 2822a352 2019-10-15 stsp {
7299 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7300 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7301 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7302 2822a352 2019-10-15 stsp
7303 2822a352 2019-10-15 stsp *fileindex = NULL;
7304 2822a352 2019-10-15 stsp *branch_ref = NULL;
7305 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7306 2822a352 2019-10-15 stsp
7307 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7308 2822a352 2019-10-15 stsp if (err)
7309 2822a352 2019-10-15 stsp return err;
7310 2822a352 2019-10-15 stsp
7311 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7312 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7313 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7314 2822a352 2019-10-15 stsp "update -b or different branch name required");
7315 2822a352 2019-10-15 stsp goto done;
7316 2822a352 2019-10-15 stsp }
7317 2822a352 2019-10-15 stsp
7318 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7319 2822a352 2019-10-15 stsp if (err)
7320 2822a352 2019-10-15 stsp goto done;
7321 2822a352 2019-10-15 stsp
7322 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7323 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7324 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7325 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7326 2822a352 2019-10-15 stsp &ok_arg);
7327 2822a352 2019-10-15 stsp if (err)
7328 2822a352 2019-10-15 stsp goto done;
7329 2822a352 2019-10-15 stsp
7330 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7331 2822a352 2019-10-15 stsp if (err)
7332 2822a352 2019-10-15 stsp goto done;
7333 2822a352 2019-10-15 stsp
7334 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7335 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7336 2822a352 2019-10-15 stsp done:
7337 2822a352 2019-10-15 stsp if (err) {
7338 2822a352 2019-10-15 stsp if (*branch_ref) {
7339 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7340 2822a352 2019-10-15 stsp *branch_ref = NULL;
7341 2822a352 2019-10-15 stsp }
7342 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7343 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7344 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7345 2822a352 2019-10-15 stsp }
7346 2822a352 2019-10-15 stsp if (*fileindex) {
7347 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7348 2822a352 2019-10-15 stsp *fileindex = NULL;
7349 2822a352 2019-10-15 stsp }
7350 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7351 2822a352 2019-10-15 stsp }
7352 0cb83759 2019-08-03 stsp return err;
7353 0cb83759 2019-08-03 stsp }
7354 0cb83759 2019-08-03 stsp
7355 2822a352 2019-10-15 stsp const struct got_error *
7356 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7357 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7358 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7359 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7360 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7361 2822a352 2019-10-15 stsp {
7362 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7363 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7364 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7365 2822a352 2019-10-15 stsp
7366 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7367 2822a352 2019-10-15 stsp if (err)
7368 2822a352 2019-10-15 stsp goto done;
7369 2822a352 2019-10-15 stsp
7370 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7371 2822a352 2019-10-15 stsp if (err)
7372 2822a352 2019-10-15 stsp goto done;
7373 2822a352 2019-10-15 stsp
7374 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7375 2822a352 2019-10-15 stsp worktree->path_prefix);
7376 2822a352 2019-10-15 stsp if (err)
7377 2822a352 2019-10-15 stsp goto done;
7378 2822a352 2019-10-15 stsp
7379 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7380 2822a352 2019-10-15 stsp if (err)
7381 2822a352 2019-10-15 stsp goto done;
7382 2822a352 2019-10-15 stsp
7383 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7384 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7385 2822a352 2019-10-15 stsp if (err)
7386 2822a352 2019-10-15 stsp goto sync;
7387 2822a352 2019-10-15 stsp
7388 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7389 2822a352 2019-10-15 stsp if (err)
7390 2822a352 2019-10-15 stsp goto sync;
7391 2822a352 2019-10-15 stsp
7392 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7393 6e210706 2021-01-22 stsp if (err)
7394 6e210706 2021-01-22 stsp goto sync;
7395 6e210706 2021-01-22 stsp
7396 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7397 2822a352 2019-10-15 stsp sync:
7398 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7399 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7400 2822a352 2019-10-15 stsp err = sync_err;
7401 2822a352 2019-10-15 stsp
7402 2822a352 2019-10-15 stsp done:
7403 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7404 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7405 2822a352 2019-10-15 stsp err = unlockerr;
7406 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7407 2822a352 2019-10-15 stsp
7408 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7409 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7410 2822a352 2019-10-15 stsp err = unlockerr;
7411 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7412 2822a352 2019-10-15 stsp
7413 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7414 2822a352 2019-10-15 stsp free(fileindex_path);
7415 2822a352 2019-10-15 stsp free(tree_id);
7416 2822a352 2019-10-15 stsp
7417 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7418 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7419 2822a352 2019-10-15 stsp err = unlockerr;
7420 2822a352 2019-10-15 stsp return err;
7421 2822a352 2019-10-15 stsp }
7422 2822a352 2019-10-15 stsp
7423 2822a352 2019-10-15 stsp const struct got_error *
7424 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7425 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7426 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7427 2822a352 2019-10-15 stsp {
7428 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7429 8b692cd0 2019-10-21 stsp
7430 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7431 8b692cd0 2019-10-21 stsp
7432 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7433 8b692cd0 2019-10-21 stsp
7434 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7435 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7436 8b692cd0 2019-10-21 stsp err = unlockerr;
7437 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7438 8b692cd0 2019-10-21 stsp
7439 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7440 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7441 8b692cd0 2019-10-21 stsp err = unlockerr;
7442 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7443 f259c4c1 2021-09-24 stsp
7444 f259c4c1 2021-09-24 stsp return err;
7445 f259c4c1 2021-09-24 stsp }
7446 f259c4c1 2021-09-24 stsp
7447 f259c4c1 2021-09-24 stsp const struct got_error *
7448 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7449 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7450 f259c4c1 2021-09-24 stsp {
7451 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7452 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7453 f259c4c1 2021-09-24 stsp
7454 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7455 f259c4c1 2021-09-24 stsp if (err)
7456 f259c4c1 2021-09-24 stsp goto done;
7457 8b692cd0 2019-10-21 stsp
7458 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7459 f259c4c1 2021-09-24 stsp
7460 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7461 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7462 f259c4c1 2021-09-24 stsp err = sync_err;
7463 f259c4c1 2021-09-24 stsp done:
7464 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7465 f259c4c1 2021-09-24 stsp free(fileindex_path);
7466 8b692cd0 2019-10-21 stsp return err;
7467 2822a352 2019-10-15 stsp }
7468 2822a352 2019-10-15 stsp
7469 f259c4c1 2021-09-24 stsp static const struct got_error *
7470 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7471 f259c4c1 2021-09-24 stsp {
7472 f259c4c1 2021-09-24 stsp const struct got_error *err;
7473 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7474 f259c4c1 2021-09-24 stsp
7475 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7476 f259c4c1 2021-09-24 stsp if (err)
7477 f259c4c1 2021-09-24 stsp goto done;
7478 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7479 f259c4c1 2021-09-24 stsp if (err)
7480 f259c4c1 2021-09-24 stsp goto done;
7481 f259c4c1 2021-09-24 stsp
7482 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7483 f259c4c1 2021-09-24 stsp if (err)
7484 f259c4c1 2021-09-24 stsp goto done;
7485 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7486 f259c4c1 2021-09-24 stsp if (err)
7487 f259c4c1 2021-09-24 stsp goto done;
7488 f259c4c1 2021-09-24 stsp
7489 f259c4c1 2021-09-24 stsp done:
7490 f259c4c1 2021-09-24 stsp free(branch_refname);
7491 f259c4c1 2021-09-24 stsp free(commit_refname);
7492 f259c4c1 2021-09-24 stsp return err;
7493 f259c4c1 2021-09-24 stsp }
7494 f259c4c1 2021-09-24 stsp
7495 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7496 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7497 f259c4c1 2021-09-24 stsp const char *branch_name;
7498 f259c4c1 2021-09-24 stsp };
7499 f259c4c1 2021-09-24 stsp
7500 f259c4c1 2021-09-24 stsp static const struct got_error *
7501 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7502 f259c4c1 2021-09-24 stsp void *arg)
7503 f259c4c1 2021-09-24 stsp {
7504 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7505 f259c4c1 2021-09-24 stsp
7506 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7507 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7508 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7509 f259c4c1 2021-09-24 stsp
7510 f259c4c1 2021-09-24 stsp return NULL;
7511 f259c4c1 2021-09-24 stsp }
7512 f259c4c1 2021-09-24 stsp
7513 f259c4c1 2021-09-24 stsp
7514 f259c4c1 2021-09-24 stsp const struct got_error *
7515 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7516 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7517 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7518 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7519 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7520 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7521 f259c4c1 2021-09-24 stsp {
7522 f259c4c1 2021-09-24 stsp const struct got_error *err;
7523 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7524 f259c4c1 2021-09-24 stsp
7525 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7526 f259c4c1 2021-09-24 stsp if (err)
7527 f259c4c1 2021-09-24 stsp goto done;
7528 f259c4c1 2021-09-24 stsp
7529 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7530 f259c4c1 2021-09-24 stsp worktree);
7531 f259c4c1 2021-09-24 stsp if (err)
7532 f259c4c1 2021-09-24 stsp goto done;
7533 f259c4c1 2021-09-24 stsp
7534 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7535 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7536 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7537 f259c4c1 2021-09-24 stsp done:
7538 f259c4c1 2021-09-24 stsp free(fileindex_path);
7539 f259c4c1 2021-09-24 stsp return err;
7540 f259c4c1 2021-09-24 stsp }
7541 f259c4c1 2021-09-24 stsp
7542 f259c4c1 2021-09-24 stsp const struct got_error *
7543 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7544 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7545 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7546 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7547 0ff8d236 2021-09-28 stsp struct got_repository *repo,
7548 0ff8d236 2021-09-28 stsp got_worktree_status_cb status_cb, void *status_arg)
7549 0ff8d236 2021-09-28 stsp
7550 f259c4c1 2021-09-24 stsp {
7551 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7552 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7553 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7554 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7555 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7556 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7557 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7558 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7559 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7560 f259c4c1 2021-09-24 stsp
7561 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7562 f259c4c1 2021-09-24 stsp
7563 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7564 f259c4c1 2021-09-24 stsp
7565 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7566 f259c4c1 2021-09-24 stsp if (err)
7567 f259c4c1 2021-09-24 stsp goto done;
7568 f259c4c1 2021-09-24 stsp
7569 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7570 f259c4c1 2021-09-24 stsp if (err)
7571 f259c4c1 2021-09-24 stsp goto done;
7572 f259c4c1 2021-09-24 stsp
7573 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7574 f259c4c1 2021-09-24 stsp if (err)
7575 f259c4c1 2021-09-24 stsp goto done;
7576 f259c4c1 2021-09-24 stsp
7577 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7578 f259c4c1 2021-09-24 stsp &have_staged_files);
7579 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7580 f259c4c1 2021-09-24 stsp goto done;
7581 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7582 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7583 f259c4c1 2021-09-24 stsp goto done;
7584 f259c4c1 2021-09-24 stsp }
7585 f259c4c1 2021-09-24 stsp
7586 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7587 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7588 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7589 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7590 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7591 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7592 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7593 62da3196 2021-10-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7594 f259c4c1 2021-09-24 stsp if (err)
7595 f259c4c1 2021-09-24 stsp goto done;
7596 f259c4c1 2021-09-24 stsp
7597 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7598 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7599 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7600 f259c4c1 2021-09-24 stsp goto done;
7601 f259c4c1 2021-09-24 stsp }
7602 f259c4c1 2021-09-24 stsp
7603 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7604 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7605 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7606 f259c4c1 2021-09-24 stsp
7607 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7608 f259c4c1 2021-09-24 stsp ct_path++;
7609 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7610 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7611 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7612 f259c4c1 2021-09-24 stsp if (err)
7613 f259c4c1 2021-09-24 stsp goto done;
7614 f259c4c1 2021-09-24 stsp
7615 f259c4c1 2021-09-24 stsp }
7616 f259c4c1 2021-09-24 stsp
7617 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7618 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7619 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7620 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7621 0ff8d236 2021-09-28 stsp merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7622 f259c4c1 2021-09-24 stsp if (err)
7623 f259c4c1 2021-09-24 stsp goto done;
7624 f259c4c1 2021-09-24 stsp
7625 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7626 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7627 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7628 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7629 f259c4c1 2021-09-24 stsp err = sync_err;
7630 f259c4c1 2021-09-24 stsp done:
7631 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7632 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7633 f259c4c1 2021-09-24 stsp free_commitable(ct);
7634 f259c4c1 2021-09-24 stsp }
7635 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7636 f259c4c1 2021-09-24 stsp free(fileindex_path);
7637 f259c4c1 2021-09-24 stsp return err;
7638 f259c4c1 2021-09-24 stsp }
7639 f259c4c1 2021-09-24 stsp
7640 f259c4c1 2021-09-24 stsp const struct got_error *
7641 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7642 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7643 f259c4c1 2021-09-24 stsp {
7644 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7645 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7646 f259c4c1 2021-09-24 stsp
7647 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7648 f259c4c1 2021-09-24 stsp if (err)
7649 f259c4c1 2021-09-24 stsp goto done;
7650 f259c4c1 2021-09-24 stsp
7651 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7652 f259c4c1 2021-09-24 stsp if (err)
7653 f259c4c1 2021-09-24 stsp goto done;
7654 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7655 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7656 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7657 f259c4c1 2021-09-24 stsp err = sync_err;
7658 f259c4c1 2021-09-24 stsp done:
7659 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7660 f259c4c1 2021-09-24 stsp free(fileindex_path);
7661 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7662 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7663 f259c4c1 2021-09-24 stsp err = unlockerr;
7664 f259c4c1 2021-09-24 stsp return err;
7665 f259c4c1 2021-09-24 stsp }
7666 f259c4c1 2021-09-24 stsp
7667 f259c4c1 2021-09-24 stsp const struct got_error *
7668 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7669 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7670 f259c4c1 2021-09-24 stsp {
7671 f259c4c1 2021-09-24 stsp const struct got_error *err;
7672 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7673 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7674 f259c4c1 2021-09-24 stsp
7675 f259c4c1 2021-09-24 stsp *in_progress = 0;
7676 f259c4c1 2021-09-24 stsp
7677 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7678 f259c4c1 2021-09-24 stsp if (err)
7679 f259c4c1 2021-09-24 stsp return err;
7680 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7681 793fcac3 2021-09-24 stsp free(branch_refname);
7682 f259c4c1 2021-09-24 stsp if (err) {
7683 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7684 f259c4c1 2021-09-24 stsp return err;
7685 f259c4c1 2021-09-24 stsp } else
7686 f259c4c1 2021-09-24 stsp *in_progress = 1;
7687 f259c4c1 2021-09-24 stsp
7688 f259c4c1 2021-09-24 stsp return NULL;
7689 f259c4c1 2021-09-24 stsp }
7690 f259c4c1 2021-09-24 stsp
7691 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7692 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7693 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7694 f259c4c1 2021-09-24 stsp {
7695 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7696 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7697 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7698 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7699 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7700 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7701 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7702 f259c4c1 2021-09-24 stsp
7703 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7704 f259c4c1 2021-09-24 stsp
7705 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7706 f259c4c1 2021-09-24 stsp if (err)
7707 f259c4c1 2021-09-24 stsp return err;
7708 f259c4c1 2021-09-24 stsp
7709 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7710 f259c4c1 2021-09-24 stsp if (err)
7711 f259c4c1 2021-09-24 stsp goto done;
7712 f259c4c1 2021-09-24 stsp
7713 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7714 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7715 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7716 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7717 f259c4c1 2021-09-24 stsp &ok_arg);
7718 f259c4c1 2021-09-24 stsp if (err)
7719 f259c4c1 2021-09-24 stsp goto done;
7720 f259c4c1 2021-09-24 stsp
7721 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7722 f259c4c1 2021-09-24 stsp if (err)
7723 f259c4c1 2021-09-24 stsp return err;
7724 f259c4c1 2021-09-24 stsp
7725 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7726 f259c4c1 2021-09-24 stsp if (err)
7727 f259c4c1 2021-09-24 stsp return err;
7728 f259c4c1 2021-09-24 stsp
7729 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7730 f259c4c1 2021-09-24 stsp 0);
7731 f259c4c1 2021-09-24 stsp if (err)
7732 f259c4c1 2021-09-24 stsp goto done;
7733 f259c4c1 2021-09-24 stsp
7734 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7735 f259c4c1 2021-09-24 stsp if (err)
7736 f259c4c1 2021-09-24 stsp goto done;
7737 f259c4c1 2021-09-24 stsp
7738 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7739 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7740 f259c4c1 2021-09-24 stsp goto done;
7741 f259c4c1 2021-09-24 stsp }
7742 f259c4c1 2021-09-24 stsp
7743 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7744 f259c4c1 2021-09-24 stsp if (err)
7745 f259c4c1 2021-09-24 stsp goto done;
7746 f259c4c1 2021-09-24 stsp
7747 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7748 f259c4c1 2021-09-24 stsp if (err)
7749 f259c4c1 2021-09-24 stsp goto done;
7750 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7751 f259c4c1 2021-09-24 stsp if (err)
7752 f259c4c1 2021-09-24 stsp goto done;
7753 f259c4c1 2021-09-24 stsp
7754 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7755 f259c4c1 2021-09-24 stsp if (err)
7756 f259c4c1 2021-09-24 stsp goto done;
7757 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7758 f259c4c1 2021-09-24 stsp if (err)
7759 f259c4c1 2021-09-24 stsp goto done;
7760 f259c4c1 2021-09-24 stsp
7761 f259c4c1 2021-09-24 stsp done:
7762 f259c4c1 2021-09-24 stsp free(branch_refname);
7763 f259c4c1 2021-09-24 stsp free(commit_refname);
7764 f259c4c1 2021-09-24 stsp free(fileindex_path);
7765 f259c4c1 2021-09-24 stsp if (branch_ref)
7766 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7767 f259c4c1 2021-09-24 stsp if (commit_ref)
7768 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7769 f259c4c1 2021-09-24 stsp if (wt_branch)
7770 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7771 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7772 f259c4c1 2021-09-24 stsp if (err) {
7773 f259c4c1 2021-09-24 stsp if (*fileindex) {
7774 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7775 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7776 f259c4c1 2021-09-24 stsp }
7777 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7778 f259c4c1 2021-09-24 stsp }
7779 f259c4c1 2021-09-24 stsp return err;
7780 f259c4c1 2021-09-24 stsp }
7781 f259c4c1 2021-09-24 stsp
7782 f259c4c1 2021-09-24 stsp const struct got_error *
7783 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7784 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7785 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7786 f259c4c1 2021-09-24 stsp {
7787 f259c4c1 2021-09-24 stsp const struct got_error *err;
7788 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7789 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7790 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7791 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7792 f259c4c1 2021-09-24 stsp
7793 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7794 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7795 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7796 f259c4c1 2021-09-24 stsp
7797 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7798 f259c4c1 2021-09-24 stsp if (err)
7799 f259c4c1 2021-09-24 stsp return err;
7800 f259c4c1 2021-09-24 stsp
7801 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7802 f259c4c1 2021-09-24 stsp if (err)
7803 f259c4c1 2021-09-24 stsp goto done;
7804 f259c4c1 2021-09-24 stsp
7805 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7806 f259c4c1 2021-09-24 stsp &have_staged_files);
7807 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7808 f259c4c1 2021-09-24 stsp goto done;
7809 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7810 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7811 f259c4c1 2021-09-24 stsp goto done;
7812 f259c4c1 2021-09-24 stsp }
7813 f259c4c1 2021-09-24 stsp
7814 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7815 f259c4c1 2021-09-24 stsp if (err)
7816 f259c4c1 2021-09-24 stsp goto done;
7817 f259c4c1 2021-09-24 stsp
7818 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7819 f259c4c1 2021-09-24 stsp if (err)
7820 f259c4c1 2021-09-24 stsp goto done;
7821 f259c4c1 2021-09-24 stsp
7822 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7823 f259c4c1 2021-09-24 stsp if (err)
7824 f259c4c1 2021-09-24 stsp goto done;
7825 f259c4c1 2021-09-24 stsp
7826 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7827 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7828 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7829 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7830 f259c4c1 2021-09-24 stsp goto done;
7831 f259c4c1 2021-09-24 stsp }
7832 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7833 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7834 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7835 f259c4c1 2021-09-24 stsp goto done;
7836 f259c4c1 2021-09-24 stsp }
7837 f259c4c1 2021-09-24 stsp
7838 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7839 f259c4c1 2021-09-24 stsp if (err)
7840 f259c4c1 2021-09-24 stsp goto done;
7841 f259c4c1 2021-09-24 stsp
7842 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7843 f259c4c1 2021-09-24 stsp if (err)
7844 f259c4c1 2021-09-24 stsp goto done;
7845 f259c4c1 2021-09-24 stsp done:
7846 f259c4c1 2021-09-24 stsp free(commit_refname);
7847 f259c4c1 2021-09-24 stsp free(branch_refname);
7848 f259c4c1 2021-09-24 stsp free(fileindex_path);
7849 f259c4c1 2021-09-24 stsp if (commit_ref)
7850 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7851 f259c4c1 2021-09-24 stsp if (branch_ref)
7852 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7853 f259c4c1 2021-09-24 stsp if (err) {
7854 f259c4c1 2021-09-24 stsp if (*branch_name) {
7855 f259c4c1 2021-09-24 stsp free(*branch_name);
7856 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7857 f259c4c1 2021-09-24 stsp }
7858 f259c4c1 2021-09-24 stsp free(*branch_tip);
7859 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7860 f259c4c1 2021-09-24 stsp if (*fileindex) {
7861 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7862 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7863 f259c4c1 2021-09-24 stsp }
7864 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7865 f259c4c1 2021-09-24 stsp }
7866 f259c4c1 2021-09-24 stsp return err;
7867 f259c4c1 2021-09-24 stsp }
7868 f259c4c1 2021-09-24 stsp
7869 f259c4c1 2021-09-24 stsp const struct got_error *
7870 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7871 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7872 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7873 f259c4c1 2021-09-24 stsp {
7874 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7875 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7876 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7877 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7878 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7879 f259c4c1 2021-09-24 stsp
7880 f259c4c1 2021-09-24 stsp err = got_object_id_by_path(&tree_id, repo,
7881 f259c4c1 2021-09-24 stsp worktree->base_commit_id, worktree->path_prefix);
7882 f259c4c1 2021-09-24 stsp if (err)
7883 f259c4c1 2021-09-24 stsp goto done;
7884 f259c4c1 2021-09-24 stsp
7885 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7886 f259c4c1 2021-09-24 stsp if (err)
7887 f259c4c1 2021-09-24 stsp goto done;
7888 f259c4c1 2021-09-24 stsp
7889 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7890 f259c4c1 2021-09-24 stsp if (err)
7891 f259c4c1 2021-09-24 stsp goto done;
7892 f259c4c1 2021-09-24 stsp
7893 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7894 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7895 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7896 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7897 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7898 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7899 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7900 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7901 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7902 62da3196 2021-10-01 stsp revert_file, &rfa, NULL, NULL, 1, 0);
7903 f259c4c1 2021-09-24 stsp if (err)
7904 f259c4c1 2021-09-24 stsp goto sync;
7905 f259c4c1 2021-09-24 stsp
7906 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7907 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7908 f259c4c1 2021-09-24 stsp sync:
7909 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7910 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7911 f259c4c1 2021-09-24 stsp err = sync_err;
7912 f259c4c1 2021-09-24 stsp done:
7913 f259c4c1 2021-09-24 stsp free(tree_id);
7914 f259c4c1 2021-09-24 stsp free(commit_id);
7915 f259c4c1 2021-09-24 stsp if (fileindex)
7916 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7917 f259c4c1 2021-09-24 stsp free(fileindex_path);
7918 f259c4c1 2021-09-24 stsp
7919 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7920 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7921 f259c4c1 2021-09-24 stsp err = unlockerr;
7922 f259c4c1 2021-09-24 stsp return err;
7923 f259c4c1 2021-09-24 stsp }
7924 f259c4c1 2021-09-24 stsp
7925 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7926 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7927 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7928 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7929 2db2652d 2019-08-07 stsp struct got_repository *repo;
7930 2db2652d 2019-08-07 stsp int have_changes;
7931 2db2652d 2019-08-07 stsp };
7932 2db2652d 2019-08-07 stsp
7933 2db2652d 2019-08-07 stsp const struct got_error *
7934 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7935 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7936 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7937 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7938 735ef5ac 2019-08-03 stsp {
7939 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7940 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7941 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7942 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7943 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7944 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7945 8b13ce36 2019-08-08 stsp
7946 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7947 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7948 8b13ce36 2019-08-08 stsp return NULL;
7949 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7950 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7951 735ef5ac 2019-08-03 stsp
7952 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7953 735ef5ac 2019-08-03 stsp if (ie == NULL)
7954 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7955 735ef5ac 2019-08-03 stsp
7956 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7957 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7958 735ef5ac 2019-08-03 stsp relpath) == -1)
7959 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7960 735ef5ac 2019-08-03 stsp
7961 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7962 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7963 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7964 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7965 735ef5ac 2019-08-03 stsp }
7966 735ef5ac 2019-08-03 stsp
7967 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7968 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7969 3aa5969e 2019-08-06 stsp goto done;
7970 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7971 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7972 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7973 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7974 735ef5ac 2019-08-03 stsp goto done;
7975 3aa5969e 2019-08-06 stsp }
7976 735ef5ac 2019-08-03 stsp
7977 2db2652d 2019-08-07 stsp a->have_changes = 1;
7978 2db2652d 2019-08-07 stsp
7979 735ef5ac 2019-08-03 stsp p = in_repo_path;
7980 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7981 735ef5ac 2019-08-03 stsp p++;
7982 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7983 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7984 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7985 735ef5ac 2019-08-03 stsp done:
7986 735ef5ac 2019-08-03 stsp free(in_repo_path);
7987 dc424a06 2019-08-07 stsp return err;
7988 dc424a06 2019-08-07 stsp }
7989 dc424a06 2019-08-07 stsp
7990 2db2652d 2019-08-07 stsp struct stage_path_arg {
7991 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7992 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7993 2db2652d 2019-08-07 stsp struct got_repository *repo;
7994 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7995 2db2652d 2019-08-07 stsp void *status_arg;
7996 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7997 2db2652d 2019-08-07 stsp void *patch_arg;
7998 7b5dc508 2019-10-28 stsp int staged_something;
7999 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
8000 2db2652d 2019-08-07 stsp };
8001 2db2652d 2019-08-07 stsp
8002 2db2652d 2019-08-07 stsp static const struct got_error *
8003 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
8004 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
8005 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8006 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8007 0cb83759 2019-08-03 stsp {
8008 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8009 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8010 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8011 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8012 0cb83759 2019-08-03 stsp uint32_t stage;
8013 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8014 0aeb8099 2020-07-23 stsp struct stat sb;
8015 8b13ce36 2019-08-08 stsp
8016 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8017 8b13ce36 2019-08-08 stsp return NULL;
8018 0cb83759 2019-08-03 stsp
8019 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8020 d3e7c587 2019-08-03 stsp if (ie == NULL)
8021 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8022 0cb83759 2019-08-03 stsp
8023 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8024 2db2652d 2019-08-07 stsp relpath)== -1)
8025 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8026 0cb83759 2019-08-03 stsp
8027 0cb83759 2019-08-03 stsp switch (status) {
8028 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8029 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8030 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8031 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8032 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8033 0aeb8099 2020-07-23 stsp break;
8034 0aeb8099 2020-07-23 stsp }
8035 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8036 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8037 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8038 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8039 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8040 dc424a06 2019-08-07 stsp if (err)
8041 dc424a06 2019-08-07 stsp break;
8042 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8043 dc424a06 2019-08-07 stsp break;
8044 dc424a06 2019-08-07 stsp } else {
8045 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8046 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8047 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8048 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8049 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8050 dc424a06 2019-08-07 stsp break;
8051 dc424a06 2019-08-07 stsp }
8052 dc424a06 2019-08-07 stsp }
8053 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8054 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8055 0cb83759 2019-08-03 stsp if (err)
8056 dc424a06 2019-08-07 stsp break;
8057 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8058 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8059 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8060 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8061 0cb83759 2019-08-03 stsp else
8062 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8063 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8064 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8065 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8066 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8067 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8068 35213c7c 2020-07-23 stsp ssize_t target_len;
8069 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8070 35213c7c 2020-07-23 stsp sizeof(target_path));
8071 35213c7c 2020-07-23 stsp if (target_len == -1) {
8072 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8073 35213c7c 2020-07-23 stsp ondisk_path);
8074 35213c7c 2020-07-23 stsp break;
8075 35213c7c 2020-07-23 stsp }
8076 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8077 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8078 35213c7c 2020-07-23 stsp a->worktree->root_path);
8079 35213c7c 2020-07-23 stsp if (err)
8080 35213c7c 2020-07-23 stsp break;
8081 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8082 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8083 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8084 35213c7c 2020-07-23 stsp break;
8085 35213c7c 2020-07-23 stsp }
8086 35213c7c 2020-07-23 stsp }
8087 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8088 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8089 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8090 35213c7c 2020-07-23 stsp else
8091 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8092 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8093 0aeb8099 2020-07-23 stsp } else {
8094 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8095 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8096 0aeb8099 2020-07-23 stsp }
8097 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8098 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8099 dc424a06 2019-08-07 stsp break;
8100 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8101 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8102 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8103 0cb83759 2019-08-03 stsp break;
8104 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8105 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8106 d3e7c587 2019-08-03 stsp break;
8107 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8108 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8109 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8110 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8111 dc424a06 2019-08-07 stsp if (err)
8112 dc424a06 2019-08-07 stsp break;
8113 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8114 88f33a19 2019-08-08 stsp break;
8115 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8116 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8117 dc424a06 2019-08-07 stsp break;
8118 88f33a19 2019-08-08 stsp }
8119 dc424a06 2019-08-07 stsp }
8120 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8121 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8122 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8123 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8124 dc424a06 2019-08-07 stsp break;
8125 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8126 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8127 12463d8b 2019-12-13 stsp de_name);
8128 0cb83759 2019-08-03 stsp break;
8129 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8130 d3e7c587 2019-08-03 stsp break;
8131 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8132 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8133 ebf48fd5 2019-08-03 stsp break;
8134 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8135 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8136 2a06fe5f 2019-08-24 stsp break;
8137 0cb83759 2019-08-03 stsp default:
8138 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8139 537ac44b 2019-08-03 stsp break;
8140 0cb83759 2019-08-03 stsp }
8141 dc424a06 2019-08-07 stsp
8142 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8143 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8144 dc424a06 2019-08-07 stsp free(path_content);
8145 2db2652d 2019-08-07 stsp free(ondisk_path);
8146 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8147 0ebf8283 2019-07-24 stsp return err;
8148 0ebf8283 2019-07-24 stsp }
8149 0cb83759 2019-08-03 stsp
8150 0cb83759 2019-08-03 stsp const struct got_error *
8151 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8152 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8153 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8154 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8155 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8156 0cb83759 2019-08-03 stsp {
8157 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8158 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8159 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8160 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8161 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8162 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8163 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8164 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8165 0cb83759 2019-08-03 stsp
8166 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8167 0cb83759 2019-08-03 stsp if (err)
8168 0cb83759 2019-08-03 stsp return err;
8169 0cb83759 2019-08-03 stsp
8170 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8171 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8172 735ef5ac 2019-08-03 stsp if (err)
8173 735ef5ac 2019-08-03 stsp goto done;
8174 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8175 735ef5ac 2019-08-03 stsp if (err)
8176 735ef5ac 2019-08-03 stsp goto done;
8177 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8178 0cb83759 2019-08-03 stsp if (err)
8179 0cb83759 2019-08-03 stsp goto done;
8180 0cb83759 2019-08-03 stsp
8181 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8182 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8183 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8184 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8185 2db2652d 2019-08-07 stsp oka.repo = repo;
8186 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8187 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8188 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8189 62da3196 2021-10-01 stsp check_stage_ok, &oka, NULL, NULL, 1, 0);
8190 735ef5ac 2019-08-03 stsp if (err)
8191 735ef5ac 2019-08-03 stsp goto done;
8192 735ef5ac 2019-08-03 stsp }
8193 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8194 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8195 2db2652d 2019-08-07 stsp goto done;
8196 2db2652d 2019-08-07 stsp }
8197 735ef5ac 2019-08-03 stsp
8198 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8199 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8200 2db2652d 2019-08-07 stsp spa.repo = repo;
8201 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8202 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8203 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8204 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8205 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8206 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8207 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8208 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8209 62da3196 2021-10-01 stsp stage_path, &spa, NULL, NULL, 1, 0);
8210 42005733 2019-08-03 stsp if (err)
8211 2db2652d 2019-08-07 stsp goto done;
8212 7b5dc508 2019-10-28 stsp }
8213 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8214 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8215 7b5dc508 2019-10-28 stsp goto done;
8216 0cb83759 2019-08-03 stsp }
8217 0cb83759 2019-08-03 stsp
8218 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8219 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8220 0cb83759 2019-08-03 stsp err = sync_err;
8221 0cb83759 2019-08-03 stsp done:
8222 735ef5ac 2019-08-03 stsp if (head_ref)
8223 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8224 735ef5ac 2019-08-03 stsp free(head_commit_id);
8225 0cb83759 2019-08-03 stsp free(fileindex_path);
8226 0cb83759 2019-08-03 stsp if (fileindex)
8227 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8228 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8229 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8230 0cb83759 2019-08-03 stsp err = unlockerr;
8231 0cb83759 2019-08-03 stsp return err;
8232 0cb83759 2019-08-03 stsp }
8233 ad493afc 2019-08-03 stsp
8234 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8235 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8236 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8237 ad493afc 2019-08-03 stsp struct got_repository *repo;
8238 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8239 ad493afc 2019-08-03 stsp void *progress_arg;
8240 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8241 2e1f37b0 2019-08-08 stsp void *patch_arg;
8242 ad493afc 2019-08-03 stsp };
8243 2e1f37b0 2019-08-08 stsp
8244 2e1f37b0 2019-08-08 stsp static const struct got_error *
8245 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8246 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8247 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8248 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8249 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8250 2e1f37b0 2019-08-08 stsp {
8251 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8252 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8253 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8254 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8255 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8256 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8257 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8258 2e1f37b0 2019-08-08 stsp
8259 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8260 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8261 2e1f37b0 2019-08-08 stsp
8262 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8263 2e1f37b0 2019-08-08 stsp if (err)
8264 2e1f37b0 2019-08-08 stsp return err;
8265 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8266 2e1f37b0 2019-08-08 stsp if (err)
8267 2e1f37b0 2019-08-08 stsp goto done;
8268 2e1f37b0 2019-08-08 stsp
8269 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8270 2e1f37b0 2019-08-08 stsp if (err)
8271 2e1f37b0 2019-08-08 stsp goto done;
8272 2e1f37b0 2019-08-08 stsp
8273 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8274 2e1f37b0 2019-08-08 stsp if (err)
8275 2e1f37b0 2019-08-08 stsp goto done;
8276 2e1f37b0 2019-08-08 stsp
8277 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8278 2e1f37b0 2019-08-08 stsp if (err)
8279 2e1f37b0 2019-08-08 stsp goto done;
8280 2e1f37b0 2019-08-08 stsp
8281 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8282 2e1f37b0 2019-08-08 stsp if (err)
8283 2e1f37b0 2019-08-08 stsp goto done;
8284 ad493afc 2019-08-03 stsp
8285 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8286 2e1f37b0 2019-08-08 stsp if (err)
8287 2e1f37b0 2019-08-08 stsp goto done;
8288 2e1f37b0 2019-08-08 stsp
8289 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8290 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8291 2e1f37b0 2019-08-08 stsp if (err)
8292 2e1f37b0 2019-08-08 stsp goto done;
8293 2e1f37b0 2019-08-08 stsp
8294 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8295 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8296 2e1f37b0 2019-08-08 stsp if (err)
8297 2e1f37b0 2019-08-08 stsp goto done;
8298 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8299 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8300 2e1f37b0 2019-08-08 stsp if (err)
8301 2e1f37b0 2019-08-08 stsp goto done;
8302 2e1f37b0 2019-08-08 stsp
8303 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8304 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8305 2e1f37b0 2019-08-08 stsp goto done;
8306 2e1f37b0 2019-08-08 stsp }
8307 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8308 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8309 2e1f37b0 2019-08-08 stsp goto done;
8310 2e1f37b0 2019-08-08 stsp }
8311 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8312 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8313 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8314 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8315 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8316 fe621944 2020-11-10 stsp nchanges++;
8317 fe621944 2020-11-10 stsp }
8318 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8319 2e1f37b0 2019-08-08 stsp int choice;
8320 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8321 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8322 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8323 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8324 2e1f37b0 2019-08-08 stsp if (err)
8325 2e1f37b0 2019-08-08 stsp goto done;
8326 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8327 2e1f37b0 2019-08-08 stsp have_content = 1;
8328 19e4b907 2019-08-08 stsp else
8329 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8330 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8331 2e1f37b0 2019-08-08 stsp break;
8332 2e1f37b0 2019-08-08 stsp }
8333 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8334 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8335 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8336 2e1f37b0 2019-08-08 stsp done:
8337 2e1f37b0 2019-08-08 stsp free(label1);
8338 2e1f37b0 2019-08-08 stsp if (blob)
8339 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8340 2e1f37b0 2019-08-08 stsp if (staged_blob)
8341 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8342 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8343 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8344 fe621944 2020-11-10 stsp err = free_err;
8345 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8346 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8347 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8348 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8349 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8350 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8351 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8352 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8353 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8354 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8355 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8356 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8357 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8358 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8359 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8360 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8361 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8362 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8363 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8364 2e1f37b0 2019-08-08 stsp }
8365 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8366 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8367 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8368 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8369 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8370 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8371 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8372 2e1f37b0 2019-08-08 stsp }
8373 2e1f37b0 2019-08-08 stsp free(path1);
8374 2e1f37b0 2019-08-08 stsp free(path2);
8375 fda8017d 2020-07-23 stsp return err;
8376 fda8017d 2020-07-23 stsp }
8377 fda8017d 2020-07-23 stsp
8378 fda8017d 2020-07-23 stsp static const struct got_error *
8379 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8380 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8381 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8382 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8383 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8384 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8385 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8386 fda8017d 2020-07-23 stsp {
8387 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8388 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8389 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8390 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8391 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8392 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8393 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8394 fda8017d 2020-07-23 stsp struct stat sb;
8395 fda8017d 2020-07-23 stsp
8396 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8397 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8398 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8399 fda8017d 2020-07-23 stsp if (err)
8400 fda8017d 2020-07-23 stsp return err;
8401 fda8017d 2020-07-23 stsp
8402 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8403 fda8017d 2020-07-23 stsp return NULL;
8404 fda8017d 2020-07-23 stsp
8405 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8406 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8407 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8408 fda8017d 2020-07-23 stsp if (err)
8409 fda8017d 2020-07-23 stsp goto done;
8410 fda8017d 2020-07-23 stsp }
8411 fda8017d 2020-07-23 stsp
8412 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
8413 fda8017d 2020-07-23 stsp if (f == NULL) {
8414 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8415 fda8017d 2020-07-23 stsp path_unstaged_content);
8416 fda8017d 2020-07-23 stsp goto done;
8417 fda8017d 2020-07-23 stsp }
8418 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8419 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8420 fda8017d 2020-07-23 stsp goto done;
8421 fda8017d 2020-07-23 stsp }
8422 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8423 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8424 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8425 fda8017d 2020-07-23 stsp size_t r;
8426 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8427 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8428 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8429 fda8017d 2020-07-23 stsp goto done;
8430 fda8017d 2020-07-23 stsp }
8431 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8432 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8433 fda8017d 2020-07-23 stsp goto done;
8434 fda8017d 2020-07-23 stsp }
8435 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8436 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8437 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8438 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8439 fda8017d 2020-07-23 stsp progress_arg);
8440 fda8017d 2020-07-23 stsp } else {
8441 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8442 67a66647 2021-05-31 stsp
8443 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8444 67a66647 2021-05-31 stsp if (err)
8445 67a66647 2021-05-31 stsp return err;
8446 67a66647 2021-05-31 stsp
8447 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8448 67a66647 2021-05-31 stsp parent) == -1) {
8449 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8450 67a66647 2021-05-31 stsp base_path = NULL;
8451 67a66647 2021-05-31 stsp goto done;
8452 67a66647 2021-05-31 stsp }
8453 67a66647 2021-05-31 stsp
8454 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8455 67a66647 2021-05-31 stsp if (err)
8456 67a66647 2021-05-31 stsp goto done;
8457 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8458 67a66647 2021-05-31 stsp blob_base);
8459 67a66647 2021-05-31 stsp if (err)
8460 67a66647 2021-05-31 stsp goto done;
8461 67a66647 2021-05-31 stsp
8462 eec2f5a9 2021-06-03 stsp /*
8463 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8464 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8465 eec2f5a9 2021-06-03 stsp */
8466 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8467 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8468 eec2f5a9 2021-06-03 stsp ondisk_path);
8469 eec2f5a9 2021-06-03 stsp if (err)
8470 eec2f5a9 2021-06-03 stsp goto done;
8471 eec2f5a9 2021-06-03 stsp } else {
8472 eec2f5a9 2021-06-03 stsp int fd;
8473 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8474 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8475 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8476 eec2f5a9 2021-06-03 stsp goto done;
8477 eec2f5a9 2021-06-03 stsp }
8478 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8479 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8480 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8481 eec2f5a9 2021-06-03 stsp close(fd);
8482 eec2f5a9 2021-06-03 stsp goto done;
8483 eec2f5a9 2021-06-03 stsp }
8484 eec2f5a9 2021-06-03 stsp }
8485 eec2f5a9 2021-06-03 stsp
8486 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8487 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8488 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8489 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8490 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8491 fda8017d 2020-07-23 stsp }
8492 fda8017d 2020-07-23 stsp if (err)
8493 fda8017d 2020-07-23 stsp goto done;
8494 fda8017d 2020-07-23 stsp
8495 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8496 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8497 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8498 2ac8aa02 2020-11-09 stsp } else {
8499 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8500 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8501 2ac8aa02 2020-11-09 stsp }
8502 fda8017d 2020-07-23 stsp done:
8503 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8504 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8505 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8506 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8507 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8508 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8509 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8510 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8511 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8512 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8513 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8514 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8515 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8516 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8517 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8518 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8519 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8520 67a66647 2021-05-31 stsp free(blob_base_path);
8521 67a66647 2021-05-31 stsp free(parent);
8522 67a66647 2021-05-31 stsp free(base_path);
8523 2e1f37b0 2019-08-08 stsp return err;
8524 2e1f37b0 2019-08-08 stsp }
8525 2e1f37b0 2019-08-08 stsp
8526 ad493afc 2019-08-03 stsp static const struct got_error *
8527 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8528 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8529 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8530 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8531 ad493afc 2019-08-03 stsp {
8532 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8533 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8534 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8535 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8536 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8537 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8538 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8539 9bc94a15 2019-08-03 stsp struct stat sb;
8540 ad493afc 2019-08-03 stsp
8541 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8542 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8543 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8544 2e1f37b0 2019-08-08 stsp return NULL;
8545 2e1f37b0 2019-08-08 stsp
8546 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8547 ad493afc 2019-08-03 stsp if (ie == NULL)
8548 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8549 9bc94a15 2019-08-03 stsp
8550 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8551 9bc94a15 2019-08-03 stsp == -1)
8552 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8553 ad493afc 2019-08-03 stsp
8554 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8555 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8556 f69721c3 2019-10-21 stsp if (err)
8557 f69721c3 2019-10-21 stsp goto done;
8558 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8559 f69721c3 2019-10-21 stsp id_str) == -1) {
8560 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8561 f69721c3 2019-10-21 stsp goto done;
8562 f69721c3 2019-10-21 stsp }
8563 f69721c3 2019-10-21 stsp
8564 ad493afc 2019-08-03 stsp switch (staged_status) {
8565 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8566 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8567 ad493afc 2019-08-03 stsp blob_id, 8192);
8568 ad493afc 2019-08-03 stsp if (err)
8569 ad493afc 2019-08-03 stsp break;
8570 ad493afc 2019-08-03 stsp /* fall through */
8571 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8572 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8573 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8574 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8575 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8576 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8577 2e1f37b0 2019-08-08 stsp if (err)
8578 2e1f37b0 2019-08-08 stsp break;
8579 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8580 2e1f37b0 2019-08-08 stsp break;
8581 2e1f37b0 2019-08-08 stsp } else {
8582 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8583 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8584 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8585 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8586 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8587 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8588 2e1f37b0 2019-08-08 stsp }
8589 2e1f37b0 2019-08-08 stsp }
8590 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8591 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8592 ad493afc 2019-08-03 stsp if (err)
8593 ad493afc 2019-08-03 stsp break;
8594 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8595 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8596 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8597 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8598 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8599 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8600 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8601 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8602 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8603 ea7786be 2020-07-23 stsp break;
8604 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8605 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8606 36bf999c 2020-07-23 stsp char *staged_target;
8607 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8608 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8609 36bf999c 2020-07-23 stsp if (err)
8610 36bf999c 2020-07-23 stsp goto done;
8611 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8612 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8613 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8614 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8615 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8616 36bf999c 2020-07-23 stsp free(staged_target);
8617 dfe9fba0 2020-07-23 stsp } else {
8618 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8619 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8620 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8621 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8622 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8623 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8624 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8625 dfe9fba0 2020-07-23 stsp }
8626 ea7786be 2020-07-23 stsp break;
8627 ea7786be 2020-07-23 stsp default:
8628 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8629 ea7786be 2020-07-23 stsp break;
8630 ea7786be 2020-07-23 stsp }
8631 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8632 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8633 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8634 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8635 2ac8aa02 2020-11-09 stsp }
8636 ad493afc 2019-08-03 stsp break;
8637 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8638 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8639 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8640 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8641 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8642 2e1f37b0 2019-08-08 stsp if (err)
8643 2e1f37b0 2019-08-08 stsp break;
8644 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8645 2e1f37b0 2019-08-08 stsp break;
8646 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8647 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8648 2e1f37b0 2019-08-08 stsp break;
8649 2e1f37b0 2019-08-08 stsp }
8650 2e1f37b0 2019-08-08 stsp }
8651 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8652 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8653 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8654 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8655 9bc94a15 2019-08-03 stsp if (err)
8656 9bc94a15 2019-08-03 stsp break;
8657 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8658 ad493afc 2019-08-03 stsp break;
8659 ad493afc 2019-08-03 stsp }
8660 f69721c3 2019-10-21 stsp done:
8661 ad493afc 2019-08-03 stsp free(ondisk_path);
8662 ad493afc 2019-08-03 stsp if (blob_base)
8663 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8664 ad493afc 2019-08-03 stsp if (blob_staged)
8665 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8666 f69721c3 2019-10-21 stsp free(id_str);
8667 f69721c3 2019-10-21 stsp free(label_orig);
8668 ad493afc 2019-08-03 stsp return err;
8669 ad493afc 2019-08-03 stsp }
8670 ad493afc 2019-08-03 stsp
8671 ad493afc 2019-08-03 stsp const struct got_error *
8672 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8673 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8674 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8675 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8676 ad493afc 2019-08-03 stsp struct got_repository *repo)
8677 ad493afc 2019-08-03 stsp {
8678 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8679 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8680 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8681 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8682 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8683 ad493afc 2019-08-03 stsp
8684 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8685 ad493afc 2019-08-03 stsp if (err)
8686 ad493afc 2019-08-03 stsp return err;
8687 ad493afc 2019-08-03 stsp
8688 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8689 ad493afc 2019-08-03 stsp if (err)
8690 ad493afc 2019-08-03 stsp goto done;
8691 ad493afc 2019-08-03 stsp
8692 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8693 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8694 ad493afc 2019-08-03 stsp upa.repo = repo;
8695 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8696 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8697 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8698 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8699 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8700 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8701 62da3196 2021-10-01 stsp unstage_path, &upa, NULL, NULL, 1, 0);
8702 ad493afc 2019-08-03 stsp if (err)
8703 ad493afc 2019-08-03 stsp goto done;
8704 ad493afc 2019-08-03 stsp }
8705 ad493afc 2019-08-03 stsp
8706 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8707 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8708 ad493afc 2019-08-03 stsp err = sync_err;
8709 ad493afc 2019-08-03 stsp done:
8710 ad493afc 2019-08-03 stsp free(fileindex_path);
8711 ad493afc 2019-08-03 stsp if (fileindex)
8712 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8713 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8714 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8715 ad493afc 2019-08-03 stsp err = unlockerr;
8716 ad493afc 2019-08-03 stsp return err;
8717 ad493afc 2019-08-03 stsp }
8718 b2118c49 2020-07-28 stsp
8719 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8720 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8721 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8722 b2118c49 2020-07-28 stsp void *info_arg;
8723 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8724 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8725 b2118c49 2020-07-28 stsp void *cancel_arg;
8726 b2118c49 2020-07-28 stsp };
8727 b2118c49 2020-07-28 stsp
8728 b2118c49 2020-07-28 stsp static const struct got_error *
8729 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8730 b2118c49 2020-07-28 stsp {
8731 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8732 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8733 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8734 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8735 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8736 b2118c49 2020-07-28 stsp int stage;
8737 b2118c49 2020-07-28 stsp
8738 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8739 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8740 b2118c49 2020-07-28 stsp
8741 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8742 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8743 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8744 b2118c49 2020-07-28 stsp break;
8745 b2118c49 2020-07-28 stsp }
8746 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8747 b2118c49 2020-07-28 stsp return NULL;
8748 b2118c49 2020-07-28 stsp
8749 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8750 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8751 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8752 b2118c49 2020-07-28 stsp }
8753 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8754 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8755 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8756 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8757 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8758 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8759 b2118c49 2020-07-28 stsp }
8760 b2118c49 2020-07-28 stsp
8761 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8762 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8763 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8764 b2118c49 2020-07-28 stsp }
8765 b2118c49 2020-07-28 stsp
8766 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8767 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8768 b2118c49 2020-07-28 stsp }
8769 b2118c49 2020-07-28 stsp
8770 b2118c49 2020-07-28 stsp const struct got_error *
8771 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8772 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8773 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8774 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8775 b2118c49 2020-07-28 stsp
8776 b2118c49 2020-07-28 stsp {
8777 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8778 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8779 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8780 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8781 b2118c49 2020-07-28 stsp
8782 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8783 b2118c49 2020-07-28 stsp if (err)
8784 b2118c49 2020-07-28 stsp return err;
8785 b2118c49 2020-07-28 stsp
8786 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8787 b2118c49 2020-07-28 stsp if (err)
8788 b2118c49 2020-07-28 stsp goto done;
8789 b2118c49 2020-07-28 stsp
8790 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8791 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8792 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8793 b2118c49 2020-07-28 stsp arg.paths = paths;
8794 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8795 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8796 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8797 b2118c49 2020-07-28 stsp &arg);
8798 b2118c49 2020-07-28 stsp done:
8799 b2118c49 2020-07-28 stsp free(fileindex_path);
8800 b2118c49 2020-07-28 stsp if (fileindex)
8801 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8802 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8803 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8804 b2118c49 2020-07-28 stsp err = unlockerr;
8805 b2118c49 2020-07-28 stsp return err;
8806 b2118c49 2020-07-28 stsp }