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 86c3caaf 2018-03-09 stsp
19 d1f6d47b 2019-02-04 stsp #include <dirent.h>
20 12ce7a6c 2019-08-12 stsp #include <limits.h>
21 f5c49f82 2019-01-06 stsp #include <stddef.h>
22 86c3caaf 2018-03-09 stsp #include <string.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 303e14b5 2019-09-23 stsp #include <time.h>
26 86c3caaf 2018-03-09 stsp #include <fcntl.h>
27 86c3caaf 2018-03-09 stsp #include <errno.h>
28 86c3caaf 2018-03-09 stsp #include <unistd.h>
29 9d31a1d8 2018-03-11 stsp #include <sha1.h>
30 9d31a1d8 2018-03-11 stsp #include <zlib.h>
31 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
32 512f0d0e 2019-01-02 stsp #include <libgen.h>
33 dd038bc6 2021-09-21 thomas.ad
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
35 86c3caaf 2018-03-09 stsp
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 1dd54920 2019-05-11 stsp #include "got_path.h"
41 e6209546 2019-08-22 stsp #include "got_cancel.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
57 9d31a1d8 2018-03-11 stsp
58 9d31a1d8 2018-03-11 stsp #ifndef MIN
59 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 9d31a1d8 2018-03-11 stsp #endif
61 f69721c3 2019-10-21 stsp
62 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 86c3caaf 2018-03-09 stsp
65 99724ed4 2018-03-10 stsp static const struct got_error *
66 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
67 99724ed4 2018-03-10 stsp {
68 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
69 99724ed4 2018-03-10 stsp char *path;
70 99724ed4 2018-03-10 stsp
71 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
72 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
73 99724ed4 2018-03-10 stsp
74 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
75 99724ed4 2018-03-10 stsp free(path);
76 507dc3bb 2018-12-29 stsp return err;
77 507dc3bb 2018-12-29 stsp }
78 507dc3bb 2018-12-29 stsp
79 507dc3bb 2018-12-29 stsp static const struct got_error *
80 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
81 507dc3bb 2018-12-29 stsp {
82 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
83 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
84 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
85 507dc3bb 2018-12-29 stsp char *path = NULL;
86 507dc3bb 2018-12-29 stsp
87 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
89 507dc3bb 2018-12-29 stsp path = NULL;
90 507dc3bb 2018-12-29 stsp goto done;
91 507dc3bb 2018-12-29 stsp }
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
94 507dc3bb 2018-12-29 stsp if (err)
95 507dc3bb 2018-12-29 stsp goto done;
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp if (content) {
98 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
99 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
100 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
101 507dc3bb 2018-12-29 stsp goto done;
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp
105 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
106 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
107 2a57020b 2019-02-20 stsp unlink(tmppath);
108 507dc3bb 2018-12-29 stsp goto done;
109 507dc3bb 2018-12-29 stsp }
110 507dc3bb 2018-12-29 stsp
111 507dc3bb 2018-12-29 stsp done:
112 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
113 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
114 230a42bd 2019-05-11 jcs free(tmppath);
115 99724ed4 2018-03-10 stsp return err;
116 99724ed4 2018-03-10 stsp }
117 99724ed4 2018-03-10 stsp
118 09fe317a 2018-03-11 stsp static const struct got_error *
119 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
120 09fe317a 2018-03-11 stsp {
121 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
122 09fe317a 2018-03-11 stsp char *path;
123 09fe317a 2018-03-11 stsp int fd = -1;
124 09fe317a 2018-03-11 stsp ssize_t n;
125 09fe317a 2018-03-11 stsp struct stat sb;
126 09fe317a 2018-03-11 stsp
127 09fe317a 2018-03-11 stsp *content = NULL;
128 09fe317a 2018-03-11 stsp
129 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
131 09fe317a 2018-03-11 stsp path = NULL;
132 09fe317a 2018-03-11 stsp goto done;
133 09fe317a 2018-03-11 stsp }
134 09fe317a 2018-03-11 stsp
135 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
136 09fe317a 2018-03-11 stsp if (fd == -1) {
137 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
138 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
139 f02eaa22 2019-03-11 stsp else
140 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
141 ef99fdb1 2018-03-11 stsp goto done;
142 ef99fdb1 2018-03-11 stsp }
143 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
144 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
145 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
146 09fe317a 2018-03-11 stsp goto done;
147 09fe317a 2018-03-11 stsp }
148 09fe317a 2018-03-11 stsp
149 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
150 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
151 d10c9b58 2019-02-19 stsp goto done;
152 d10c9b58 2019-02-19 stsp }
153 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
154 09fe317a 2018-03-11 stsp if (*content == NULL) {
155 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
156 09fe317a 2018-03-11 stsp goto done;
157 09fe317a 2018-03-11 stsp }
158 09fe317a 2018-03-11 stsp
159 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
160 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
161 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
162 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
163 09fe317a 2018-03-11 stsp goto done;
164 09fe317a 2018-03-11 stsp }
165 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
166 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
167 09fe317a 2018-03-11 stsp goto done;
168 09fe317a 2018-03-11 stsp }
169 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
170 09fe317a 2018-03-11 stsp
171 09fe317a 2018-03-11 stsp done:
172 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
173 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
174 09fe317a 2018-03-11 stsp free(path);
175 09fe317a 2018-03-11 stsp if (err) {
176 09fe317a 2018-03-11 stsp free(*content);
177 09fe317a 2018-03-11 stsp *content = NULL;
178 09fe317a 2018-03-11 stsp }
179 09fe317a 2018-03-11 stsp return err;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp
182 024e9686 2019-05-14 stsp static const struct got_error *
183 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
184 024e9686 2019-05-14 stsp {
185 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
186 024e9686 2019-05-14 stsp char *refstr = NULL;
187 024e9686 2019-05-14 stsp
188 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
189 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
192 024e9686 2019-05-14 stsp } else {
193 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
194 024e9686 2019-05-14 stsp if (refstr == NULL)
195 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
196 024e9686 2019-05-14 stsp }
197 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 024e9686 2019-05-14 stsp free(refstr);
199 024e9686 2019-05-14 stsp return err;
200 024e9686 2019-05-14 stsp }
201 024e9686 2019-05-14 stsp
202 86c3caaf 2018-03-09 stsp const struct got_error *
203 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
204 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
205 86c3caaf 2018-03-09 stsp {
206 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
207 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
208 ec22038e 2019-03-10 stsp uuid_t uuid;
209 ec22038e 2019-03-10 stsp uint32_t uuid_status;
210 65596e15 2018-12-24 stsp int obj_type;
211 7ac97322 2018-03-11 stsp char *path_got = NULL;
212 1451e70d 2018-03-10 stsp char *formatstr = NULL;
213 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
214 65596e15 2018-12-24 stsp char *basestr = NULL;
215 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
216 65596e15 2018-12-24 stsp
217 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
218 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
219 0c48fee2 2019-03-11 stsp goto done;
220 0c48fee2 2019-03-11 stsp }
221 0c48fee2 2019-03-11 stsp
222 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
223 65596e15 2018-12-24 stsp if (err)
224 65596e15 2018-12-24 stsp return err;
225 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
226 65596e15 2018-12-24 stsp if (err)
227 65596e15 2018-12-24 stsp return err;
228 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
229 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
230 86c3caaf 2018-03-09 stsp
231 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
232 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
233 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
234 0bb8a95e 2018-03-12 stsp }
235 577ec78f 2018-03-11 stsp
236 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
237 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
238 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
239 86c3caaf 2018-03-09 stsp goto done;
240 86c3caaf 2018-03-09 stsp }
241 86c3caaf 2018-03-09 stsp
242 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
243 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
245 86c3caaf 2018-03-09 stsp goto done;
246 86c3caaf 2018-03-09 stsp }
247 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
248 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
249 86c3caaf 2018-03-09 stsp goto done;
250 86c3caaf 2018-03-09 stsp }
251 86c3caaf 2018-03-09 stsp
252 056e7441 2018-03-11 stsp /* Create an empty lock file. */
253 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 056e7441 2018-03-11 stsp if (err)
255 056e7441 2018-03-11 stsp goto done;
256 056e7441 2018-03-11 stsp
257 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
258 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 99724ed4 2018-03-10 stsp if (err)
260 86c3caaf 2018-03-09 stsp goto done;
261 86c3caaf 2018-03-09 stsp
262 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
263 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
264 65596e15 2018-12-24 stsp if (err)
265 65596e15 2018-12-24 stsp goto done;
266 65596e15 2018-12-24 stsp
267 65596e15 2018-12-24 stsp /* Record our base commit. */
268 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
269 65596e15 2018-12-24 stsp if (err)
270 65596e15 2018-12-24 stsp goto done;
271 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
272 99724ed4 2018-03-10 stsp if (err)
273 86c3caaf 2018-03-09 stsp goto done;
274 86c3caaf 2018-03-09 stsp
275 1451e70d 2018-03-10 stsp /* Store path to repository. */
276 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
278 99724ed4 2018-03-10 stsp if (err)
279 86c3caaf 2018-03-09 stsp goto done;
280 86c3caaf 2018-03-09 stsp
281 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
282 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
284 ec22038e 2019-03-10 stsp if (err)
285 ec22038e 2019-03-10 stsp goto done;
286 ec22038e 2019-03-10 stsp
287 ec22038e 2019-03-10 stsp /* Generate UUID. */
288 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
289 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
290 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
291 ec22038e 2019-03-10 stsp goto done;
292 ec22038e 2019-03-10 stsp }
293 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
295 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
296 ec22038e 2019-03-10 stsp goto done;
297 ec22038e 2019-03-10 stsp }
298 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
299 577ec78f 2018-03-11 stsp if (err)
300 577ec78f 2018-03-11 stsp goto done;
301 577ec78f 2018-03-11 stsp
302 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
303 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
305 1451e70d 2018-03-10 stsp goto done;
306 1451e70d 2018-03-10 stsp }
307 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
308 99724ed4 2018-03-10 stsp if (err)
309 1451e70d 2018-03-10 stsp goto done;
310 1451e70d 2018-03-10 stsp
311 86c3caaf 2018-03-09 stsp done:
312 65596e15 2018-12-24 stsp free(commit_id);
313 7ac97322 2018-03-11 stsp free(path_got);
314 1451e70d 2018-03-10 stsp free(formatstr);
315 0bb8a95e 2018-03-12 stsp free(absprefix);
316 65596e15 2018-12-24 stsp free(basestr);
317 ec22038e 2019-03-10 stsp free(uuidstr);
318 86c3caaf 2018-03-09 stsp return err;
319 86c3caaf 2018-03-09 stsp }
320 86c3caaf 2018-03-09 stsp
321 247140b2 2019-02-05 stsp static const struct got_error *
322 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
323 86c3caaf 2018-03-09 stsp {
324 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
325 7ac97322 2018-03-11 stsp char *path_got;
326 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
327 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
328 056e7441 2018-03-11 stsp char *path_lock = NULL;
329 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
330 6d9d28c3 2018-03-11 stsp int version, fd = -1;
331 6d9d28c3 2018-03-11 stsp const char *errstr;
332 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
333 c442a90d 2019-03-10 stsp uint32_t uuid_status;
334 6d9d28c3 2018-03-11 stsp
335 6d9d28c3 2018-03-11 stsp *worktree = NULL;
336 6d9d28c3 2018-03-11 stsp
337 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
339 7ac97322 2018-03-11 stsp path_got = NULL;
340 6d9d28c3 2018-03-11 stsp goto done;
341 6d9d28c3 2018-03-11 stsp }
342 6d9d28c3 2018-03-11 stsp
343 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
345 056e7441 2018-03-11 stsp path_lock = NULL;
346 6d9d28c3 2018-03-11 stsp goto done;
347 6d9d28c3 2018-03-11 stsp }
348 6d9d28c3 2018-03-11 stsp
349 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 6d9d28c3 2018-03-11 stsp if (fd == -1) {
351 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
353 6d9d28c3 2018-03-11 stsp goto done;
354 6d9d28c3 2018-03-11 stsp }
355 6d9d28c3 2018-03-11 stsp
356 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
357 6d9d28c3 2018-03-11 stsp if (err)
358 6d9d28c3 2018-03-11 stsp goto done;
359 6d9d28c3 2018-03-11 stsp
360 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 6d9d28c3 2018-03-11 stsp if (errstr) {
362 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
363 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
364 6d9d28c3 2018-03-11 stsp goto done;
365 6d9d28c3 2018-03-11 stsp }
366 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
368 6d9d28c3 2018-03-11 stsp goto done;
369 6d9d28c3 2018-03-11 stsp }
370 6d9d28c3 2018-03-11 stsp
371 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
372 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
373 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
374 6d9d28c3 2018-03-11 stsp goto done;
375 6d9d28c3 2018-03-11 stsp }
376 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
377 6d9d28c3 2018-03-11 stsp
378 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
379 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
380 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
381 6d9d28c3 2018-03-11 stsp goto done;
382 6d9d28c3 2018-03-11 stsp }
383 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
384 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
385 6d9d28c3 2018-03-11 stsp if (err)
386 6d9d28c3 2018-03-11 stsp goto done;
387 eaccb85f 2018-12-25 stsp
388 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
390 93a30277 2018-12-24 stsp if (err)
391 93a30277 2018-12-24 stsp goto done;
392 93a30277 2018-12-24 stsp
393 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
394 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
395 c442a90d 2019-03-10 stsp if (err)
396 c442a90d 2019-03-10 stsp goto done;
397 c442a90d 2019-03-10 stsp
398 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 eaccb85f 2018-12-25 stsp if (err)
400 c442a90d 2019-03-10 stsp goto done;
401 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
403 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
404 eaccb85f 2018-12-25 stsp goto done;
405 c442a90d 2019-03-10 stsp }
406 eaccb85f 2018-12-25 stsp
407 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
408 eaccb85f 2018-12-25 stsp if (err)
409 eaccb85f 2018-12-25 stsp goto done;
410 eaccb85f 2018-12-25 stsp
411 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 eaccb85f 2018-12-25 stsp base_commit_id_str);
413 f5baf295 2018-03-11 stsp if (err)
414 6d9d28c3 2018-03-11 stsp goto done;
415 6d9d28c3 2018-03-11 stsp
416 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
417 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
418 50b0790e 2020-09-11 stsp if (err)
419 50b0790e 2020-09-11 stsp goto done;
420 50b0790e 2020-09-11 stsp
421 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
422 50b0790e 2020-09-11 stsp (*worktree)->root_path,
423 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
424 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
425 50b0790e 2020-09-11 stsp goto done;
426 50b0790e 2020-09-11 stsp }
427 50b0790e 2020-09-11 stsp
428 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
429 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
430 437adc9d 2020-12-10 yzhong
431 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
432 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
433 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
434 437adc9d 2020-12-10 yzhong goto done;
435 437adc9d 2020-12-10 yzhong }
436 6d9d28c3 2018-03-11 stsp done:
437 1d0f4054 2021-06-17 stsp if (repo) {
438 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
439 1d0f4054 2021-06-17 stsp if (err == NULL)
440 1d0f4054 2021-06-17 stsp err = close_err;
441 1d0f4054 2021-06-17 stsp }
442 7ac97322 2018-03-11 stsp free(path_got);
443 056e7441 2018-03-11 stsp free(path_lock);
444 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
445 c442a90d 2019-03-10 stsp free(uuidstr);
446 bd165944 2019-03-10 stsp free(formatstr);
447 6d9d28c3 2018-03-11 stsp if (err) {
448 6d9d28c3 2018-03-11 stsp if (fd != -1)
449 6d9d28c3 2018-03-11 stsp close(fd);
450 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
451 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
452 6d9d28c3 2018-03-11 stsp *worktree = NULL;
453 6d9d28c3 2018-03-11 stsp } else
454 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
455 6d9d28c3 2018-03-11 stsp
456 6d9d28c3 2018-03-11 stsp return err;
457 86c3caaf 2018-03-09 stsp }
458 247140b2 2019-02-05 stsp
459 247140b2 2019-02-05 stsp const struct got_error *
460 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
461 247140b2 2019-02-05 stsp {
462 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
463 aedea96d 2020-10-20 stsp char *worktree_path;
464 86c3caaf 2018-03-09 stsp
465 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
466 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
467 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
468 aedea96d 2020-10-20 stsp
469 aedea96d 2020-10-20 stsp for (;;) {
470 aedea96d 2020-10-20 stsp char *parent_path;
471 aedea96d 2020-10-20 stsp
472 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
473 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
474 aedea96d 2020-10-20 stsp free(worktree_path);
475 247140b2 2019-02-05 stsp return err;
476 aedea96d 2020-10-20 stsp }
477 aedea96d 2020-10-20 stsp if (*worktree) {
478 aedea96d 2020-10-20 stsp free(worktree_path);
479 aedea96d 2020-10-20 stsp return NULL;
480 aedea96d 2020-10-20 stsp }
481 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 aedea96d 2020-10-20 stsp break;
483 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
484 aedea96d 2020-10-20 stsp if (err) {
485 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
486 aedea96d 2020-10-20 stsp free(worktree_path);
487 aedea96d 2020-10-20 stsp return err;
488 aedea96d 2020-10-20 stsp }
489 aedea96d 2020-10-20 stsp break;
490 aedea96d 2020-10-20 stsp }
491 aedea96d 2020-10-20 stsp free(worktree_path);
492 aedea96d 2020-10-20 stsp worktree_path = parent_path;
493 aedea96d 2020-10-20 stsp }
494 247140b2 2019-02-05 stsp
495 aedea96d 2020-10-20 stsp free(worktree_path);
496 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
497 247140b2 2019-02-05 stsp }
498 247140b2 2019-02-05 stsp
499 3a6ce05a 2019-02-11 stsp const struct got_error *
500 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
501 86c3caaf 2018-03-09 stsp {
502 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
503 60e40e95 2021-01-22 stsp
504 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
505 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
506 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
507 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
508 a6b21eef 2021-01-21 stsp }
509 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
510 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
511 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
512 60e40e95 2021-01-22 stsp free(worktree->repo_path);
513 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
514 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
515 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
516 60e40e95 2021-01-22 stsp free(worktree->root_path);
517 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
518 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
519 a06ff56f 2021-01-21 naddy free(worktree);
520 3a6ce05a 2019-02-11 stsp return err;
521 86c3caaf 2018-03-09 stsp }
522 86c3caaf 2018-03-09 stsp
523 2fbdb5ae 2018-12-29 stsp const char *
524 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
525 c7f4312f 2019-02-05 stsp {
526 c7f4312f 2019-02-05 stsp return worktree->root_path;
527 c7f4312f 2019-02-05 stsp }
528 c7f4312f 2019-02-05 stsp
529 c7f4312f 2019-02-05 stsp const char *
530 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
531 86c3caaf 2018-03-09 stsp {
532 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
533 49520a32 2018-12-29 stsp }
534 49520a32 2018-12-29 stsp const char *
535 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
536 49520a32 2018-12-29 stsp {
537 f609be2e 2018-12-29 stsp return worktree->path_prefix;
538 e5dc7198 2018-12-29 stsp }
539 e5dc7198 2018-12-29 stsp
540 e5dc7198 2018-12-29 stsp const struct got_error *
541 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
542 e5dc7198 2018-12-29 stsp const char *path_prefix)
543 e5dc7198 2018-12-29 stsp {
544 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
545 e5dc7198 2018-12-29 stsp
546 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
547 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
548 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
549 e5dc7198 2018-12-29 stsp }
550 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
551 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
552 e5dc7198 2018-12-29 stsp free(absprefix);
553 e5dc7198 2018-12-29 stsp return NULL;
554 86c3caaf 2018-03-09 stsp }
555 86c3caaf 2018-03-09 stsp
556 bc70eb79 2019-05-09 stsp const char *
557 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 86c3caaf 2018-03-09 stsp {
559 36a38700 2019-05-10 stsp return worktree->head_ref_name;
560 024e9686 2019-05-14 stsp }
561 024e9686 2019-05-14 stsp
562 024e9686 2019-05-14 stsp const struct got_error *
563 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
564 024e9686 2019-05-14 stsp struct got_reference *head_ref)
565 024e9686 2019-05-14 stsp {
566 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
567 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
568 024e9686 2019-05-14 stsp
569 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
570 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
571 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
572 024e9686 2019-05-14 stsp path_got = NULL;
573 024e9686 2019-05-14 stsp goto done;
574 024e9686 2019-05-14 stsp }
575 024e9686 2019-05-14 stsp
576 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
577 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
578 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
579 024e9686 2019-05-14 stsp goto done;
580 024e9686 2019-05-14 stsp }
581 024e9686 2019-05-14 stsp
582 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
583 024e9686 2019-05-14 stsp if (err)
584 024e9686 2019-05-14 stsp goto done;
585 024e9686 2019-05-14 stsp
586 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
587 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
588 024e9686 2019-05-14 stsp done:
589 024e9686 2019-05-14 stsp free(path_got);
590 024e9686 2019-05-14 stsp if (err)
591 024e9686 2019-05-14 stsp free(head_ref_name);
592 024e9686 2019-05-14 stsp return err;
593 507dc3bb 2018-12-29 stsp }
594 507dc3bb 2018-12-29 stsp
595 b72f483a 2019-02-05 stsp struct got_object_id *
596 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 507dc3bb 2018-12-29 stsp {
598 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
599 86c3caaf 2018-03-09 stsp }
600 86c3caaf 2018-03-09 stsp
601 507dc3bb 2018-12-29 stsp const struct got_error *
602 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
603 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
604 507dc3bb 2018-12-29 stsp {
605 507dc3bb 2018-12-29 stsp const struct got_error *err;
606 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
607 507dc3bb 2018-12-29 stsp char *id_str = NULL;
608 507dc3bb 2018-12-29 stsp char *path_got = NULL;
609 507dc3bb 2018-12-29 stsp
610 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
611 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
612 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
613 507dc3bb 2018-12-29 stsp path_got = NULL;
614 507dc3bb 2018-12-29 stsp goto done;
615 507dc3bb 2018-12-29 stsp }
616 507dc3bb 2018-12-29 stsp
617 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
618 507dc3bb 2018-12-29 stsp if (err)
619 507dc3bb 2018-12-29 stsp return err;
620 507dc3bb 2018-12-29 stsp
621 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
622 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
623 507dc3bb 2018-12-29 stsp goto done;
624 507dc3bb 2018-12-29 stsp }
625 507dc3bb 2018-12-29 stsp
626 507dc3bb 2018-12-29 stsp /* Record our base commit. */
627 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
628 507dc3bb 2018-12-29 stsp if (err)
629 507dc3bb 2018-12-29 stsp goto done;
630 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
631 507dc3bb 2018-12-29 stsp if (err)
632 507dc3bb 2018-12-29 stsp goto done;
633 507dc3bb 2018-12-29 stsp
634 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
635 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
636 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
637 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
638 507dc3bb 2018-12-29 stsp goto done;
639 507dc3bb 2018-12-29 stsp }
640 507dc3bb 2018-12-29 stsp done:
641 507dc3bb 2018-12-29 stsp if (obj)
642 507dc3bb 2018-12-29 stsp got_object_close(obj);
643 507dc3bb 2018-12-29 stsp free(id_str);
644 507dc3bb 2018-12-29 stsp free(path_got);
645 507dc3bb 2018-12-29 stsp return err;
646 50b0790e 2020-09-11 stsp }
647 50b0790e 2020-09-11 stsp
648 50b0790e 2020-09-11 stsp const struct got_gotconfig *
649 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
650 50b0790e 2020-09-11 stsp {
651 50b0790e 2020-09-11 stsp return worktree->gotconfig;
652 507dc3bb 2018-12-29 stsp }
653 507dc3bb 2018-12-29 stsp
654 9d31a1d8 2018-03-11 stsp static const struct got_error *
655 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
656 86c3caaf 2018-03-09 stsp {
657 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
658 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
659 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
660 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
661 86c3caaf 2018-03-09 stsp return NULL;
662 21908da4 2019-01-13 stsp }
663 21908da4 2019-01-13 stsp
664 21908da4 2019-01-13 stsp static const struct got_error *
665 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 4a1ddfc2 2019-01-12 stsp {
667 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
668 4a1ddfc2 2019-01-12 stsp char *abspath;
669 4a1ddfc2 2019-01-12 stsp
670 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
671 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
672 4a1ddfc2 2019-01-12 stsp
673 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
674 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
675 ddcd8544 2019-03-11 stsp struct stat sb;
676 ddcd8544 2019-03-11 stsp err = NULL;
677 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
678 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
679 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
680 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
681 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
682 ddcd8544 2019-03-11 stsp }
683 ddcd8544 2019-03-11 stsp }
684 4a1ddfc2 2019-01-12 stsp free(abspath);
685 68c76935 2019-02-19 stsp return err;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp static const struct got_error *
689 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 68c76935 2019-02-19 stsp {
691 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
692 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
693 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
694 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
695 68c76935 2019-02-19 stsp
696 68c76935 2019-02-19 stsp *same = 1;
697 68c76935 2019-02-19 stsp
698 230a42bd 2019-05-11 jcs for (;;) {
699 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
700 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
701 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
702 68c76935 2019-02-19 stsp break;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
705 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
706 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
707 68c76935 2019-02-19 stsp break;
708 68c76935 2019-02-19 stsp }
709 68c76935 2019-02-19 stsp if (flen1 == 0) {
710 68c76935 2019-02-19 stsp if (flen2 != 0)
711 68c76935 2019-02-19 stsp *same = 0;
712 68c76935 2019-02-19 stsp break;
713 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
714 68c76935 2019-02-19 stsp if (flen1 != 0)
715 68c76935 2019-02-19 stsp *same = 0;
716 68c76935 2019-02-19 stsp break;
717 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
718 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
719 68c76935 2019-02-19 stsp *same = 0;
720 68c76935 2019-02-19 stsp break;
721 68c76935 2019-02-19 stsp }
722 68c76935 2019-02-19 stsp } else {
723 68c76935 2019-02-19 stsp *same = 0;
724 68c76935 2019-02-19 stsp break;
725 68c76935 2019-02-19 stsp }
726 68c76935 2019-02-19 stsp }
727 68c76935 2019-02-19 stsp
728 68c76935 2019-02-19 stsp return err;
729 68c76935 2019-02-19 stsp }
730 68c76935 2019-02-19 stsp
731 68c76935 2019-02-19 stsp static const struct got_error *
732 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
733 68c76935 2019-02-19 stsp {
734 68c76935 2019-02-19 stsp struct stat sb;
735 68c76935 2019-02-19 stsp size_t size1, size2;
736 68c76935 2019-02-19 stsp
737 68c76935 2019-02-19 stsp *same = 1;
738 68c76935 2019-02-19 stsp
739 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
740 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
741 68c76935 2019-02-19 stsp size1 = sb.st_size;
742 68c76935 2019-02-19 stsp
743 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
744 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
745 68c76935 2019-02-19 stsp size2 = sb.st_size;
746 68c76935 2019-02-19 stsp
747 68c76935 2019-02-19 stsp if (size1 != size2) {
748 68c76935 2019-02-19 stsp *same = 0;
749 68c76935 2019-02-19 stsp return NULL;
750 68c76935 2019-02-19 stsp }
751 68c76935 2019-02-19 stsp
752 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
753 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
754 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
755 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
756 68c76935 2019-02-19 stsp
757 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
758 6353ad76 2019-02-08 stsp }
759 6353ad76 2019-02-08 stsp
760 6353ad76 2019-02-08 stsp /*
761 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
762 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
763 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
764 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
765 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
766 6353ad76 2019-02-08 stsp */
767 6353ad76 2019-02-08 stsp static const struct got_error *
768 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
769 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
770 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
771 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
772 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
773 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
774 6353ad76 2019-02-08 stsp {
775 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
776 6353ad76 2019-02-08 stsp int merged_fd = -1;
777 db590691 2021-06-02 stsp FILE *f_merged = NULL;
778 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
779 234035bc 2019-06-01 stsp int overlapcnt = 0;
780 3524bbf9 2020-10-20 stsp char *parent = NULL;
781 6353ad76 2019-02-08 stsp
782 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
783 234035bc 2019-06-01 stsp
784 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
785 3524bbf9 2020-10-20 stsp if (err)
786 3524bbf9 2020-10-20 stsp return err;
787 af54ae4a 2019-02-19 stsp
788 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
789 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
790 3524bbf9 2020-10-20 stsp goto done;
791 3524bbf9 2020-10-20 stsp }
792 af54ae4a 2019-02-19 stsp
793 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
794 6353ad76 2019-02-08 stsp if (err)
795 6353ad76 2019-02-08 stsp goto done;
796 3b9f0f87 2020-07-23 stsp
797 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
798 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
799 6353ad76 2019-02-08 stsp if (err)
800 6353ad76 2019-02-08 stsp goto done;
801 6353ad76 2019-02-08 stsp
802 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
803 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
804 1ee397ad 2019-07-12 stsp if (err)
805 1ee397ad 2019-07-12 stsp goto done;
806 6353ad76 2019-02-08 stsp
807 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
808 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
809 816dc654 2019-02-16 stsp goto done;
810 68c76935 2019-02-19 stsp }
811 68c76935 2019-02-19 stsp
812 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
813 db590691 2021-06-02 stsp if (f_merged == NULL) {
814 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
815 db590691 2021-06-02 stsp goto done;
816 db590691 2021-06-02 stsp }
817 db590691 2021-06-02 stsp merged_fd = -1;
818 db590691 2021-06-02 stsp
819 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
820 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
821 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
822 db590691 2021-06-02 stsp f_merged);
823 68c76935 2019-02-19 stsp if (err)
824 68c76935 2019-02-19 stsp goto done;
825 816dc654 2019-02-16 stsp }
826 6353ad76 2019-02-08 stsp
827 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
828 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
829 70a0c8ec 2019-02-20 stsp goto done;
830 70a0c8ec 2019-02-20 stsp }
831 70a0c8ec 2019-02-20 stsp
832 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
833 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
834 230a42bd 2019-05-11 jcs ondisk_path);
835 6353ad76 2019-02-08 stsp goto done;
836 6353ad76 2019-02-08 stsp }
837 6353ad76 2019-02-08 stsp done:
838 fdcb7daf 2019-12-15 stsp if (err) {
839 fdcb7daf 2019-12-15 stsp if (merged_path)
840 fdcb7daf 2019-12-15 stsp unlink(merged_path);
841 3b9f0f87 2020-07-23 stsp }
842 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
843 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
844 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
845 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
846 6353ad76 2019-02-08 stsp free(merged_path);
847 af54ae4a 2019-02-19 stsp free(base_path);
848 3524bbf9 2020-10-20 stsp free(parent);
849 af57b12a 2020-07-23 stsp return err;
850 af57b12a 2020-07-23 stsp }
851 af57b12a 2020-07-23 stsp
852 af57b12a 2020-07-23 stsp static const struct got_error *
853 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
854 af57b12a 2020-07-23 stsp size_t target_len)
855 af57b12a 2020-07-23 stsp {
856 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
857 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
858 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
859 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
860 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
861 af57b12a 2020-07-23 stsp ondisk_path);
862 af57b12a 2020-07-23 stsp return NULL;
863 af57b12a 2020-07-23 stsp }
864 af57b12a 2020-07-23 stsp
865 af57b12a 2020-07-23 stsp /*
866 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
867 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
868 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
869 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
870 993e2a1b 2020-07-23 stsp *
871 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
872 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
873 993e2a1b 2020-07-23 stsp *
874 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
875 993e2a1b 2020-07-23 stsp * has deleted this symlink.
876 11cc08c1 2020-07-23 stsp */
877 11cc08c1 2020-07-23 stsp static const struct got_error *
878 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
879 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
880 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
881 11cc08c1 2020-07-23 stsp {
882 11cc08c1 2020-07-23 stsp const struct got_error *err;
883 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
884 11cc08c1 2020-07-23 stsp FILE *f = NULL;
885 11cc08c1 2020-07-23 stsp
886 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
887 11cc08c1 2020-07-23 stsp if (err)
888 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
889 11cc08c1 2020-07-23 stsp
890 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
891 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
892 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
893 11cc08c1 2020-07-23 stsp goto done;
894 11cc08c1 2020-07-23 stsp }
895 11cc08c1 2020-07-23 stsp
896 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
897 11cc08c1 2020-07-23 stsp if (err)
898 3818e3c4 2020-11-01 naddy goto done;
899 3818e3c4 2020-11-01 naddy
900 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
901 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
902 11cc08c1 2020-07-23 stsp goto done;
903 3818e3c4 2020-11-01 naddy }
904 11cc08c1 2020-07-23 stsp
905 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
906 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
907 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
908 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
909 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
910 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
911 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
912 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
913 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
914 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
915 11cc08c1 2020-07-23 stsp goto done;
916 11cc08c1 2020-07-23 stsp }
917 11cc08c1 2020-07-23 stsp
918 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
919 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
920 11cc08c1 2020-07-23 stsp goto done;
921 11cc08c1 2020-07-23 stsp }
922 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
923 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
924 11cc08c1 2020-07-23 stsp goto done;
925 11cc08c1 2020-07-23 stsp }
926 11cc08c1 2020-07-23 stsp done:
927 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
928 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
929 11cc08c1 2020-07-23 stsp free(path);
930 11cc08c1 2020-07-23 stsp free(id_str);
931 11cc08c1 2020-07-23 stsp free(label_deriv);
932 11cc08c1 2020-07-23 stsp return err;
933 11cc08c1 2020-07-23 stsp }
934 d219f183 2020-07-23 stsp
935 d219f183 2020-07-23 stsp /* forward declaration */
936 d219f183 2020-07-23 stsp static const struct got_error *
937 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
938 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
939 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
940 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
941 11cc08c1 2020-07-23 stsp
942 11cc08c1 2020-07-23 stsp /*
943 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
944 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
945 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
946 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
947 af57b12a 2020-07-23 stsp */
948 af57b12a 2020-07-23 stsp static const struct got_error *
949 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
950 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
951 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
952 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
953 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
954 af57b12a 2020-07-23 stsp {
955 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
956 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
957 af57b12a 2020-07-23 stsp struct stat sb;
958 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
959 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
960 11cc08c1 2020-07-23 stsp int have_local_change = 0;
961 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
962 af57b12a 2020-07-23 stsp
963 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
964 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
965 af57b12a 2020-07-23 stsp
966 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
967 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
968 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
969 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
970 af57b12a 2020-07-23 stsp ondisk_path);
971 af57b12a 2020-07-23 stsp goto done;
972 af57b12a 2020-07-23 stsp }
973 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
974 af57b12a 2020-07-23 stsp
975 526a746f 2020-07-23 stsp if (blob_orig) {
976 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
977 526a746f 2020-07-23 stsp if (err)
978 526a746f 2020-07-23 stsp goto done;
979 526a746f 2020-07-23 stsp }
980 af57b12a 2020-07-23 stsp
981 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
982 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
983 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
984 11cc08c1 2020-07-23 stsp have_local_change = 1;
985 11cc08c1 2020-07-23 stsp
986 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
987 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
988 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
989 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
990 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
991 11cc08c1 2020-07-23 stsp
992 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
993 11cc08c1 2020-07-23 stsp if (ancestor_target) {
994 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
995 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
996 11cc08c1 2020-07-23 stsp path);
997 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
998 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
999 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1000 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1001 11cc08c1 2020-07-23 stsp path);
1002 11cc08c1 2020-07-23 stsp } else {
1003 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1004 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1005 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1006 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1007 11cc08c1 2020-07-23 stsp if (err)
1008 11cc08c1 2020-07-23 stsp goto done;
1009 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1010 11cc08c1 2020-07-23 stsp path);
1011 11cc08c1 2020-07-23 stsp }
1012 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1013 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1014 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1015 af57b12a 2020-07-23 stsp strlen(deriv_target));
1016 af57b12a 2020-07-23 stsp if (err)
1017 af57b12a 2020-07-23 stsp goto done;
1018 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1019 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1020 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1021 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1022 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1023 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1024 ea7786be 2020-07-23 stsp path);
1025 ea7786be 2020-07-23 stsp } else {
1026 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1027 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1028 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1029 ea7786be 2020-07-23 stsp if (err)
1030 ea7786be 2020-07-23 stsp goto done;
1031 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1032 ea7786be 2020-07-23 stsp path);
1033 ea7786be 2020-07-23 stsp }
1034 6353ad76 2019-02-08 stsp }
1035 11cc08c1 2020-07-23 stsp
1036 af57b12a 2020-07-23 stsp done:
1037 af57b12a 2020-07-23 stsp free(ancestor_target);
1038 eec2f5a9 2021-06-03 stsp return err;
1039 eec2f5a9 2021-06-03 stsp }
1040 eec2f5a9 2021-06-03 stsp
1041 eec2f5a9 2021-06-03 stsp static const struct got_error *
1042 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1043 eec2f5a9 2021-06-03 stsp {
1044 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1045 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1046 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1047 eec2f5a9 2021-06-03 stsp size_t n;
1048 eec2f5a9 2021-06-03 stsp FILE *f;
1049 eec2f5a9 2021-06-03 stsp
1050 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1051 eec2f5a9 2021-06-03 stsp
1052 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1053 eec2f5a9 2021-06-03 stsp if (f == NULL)
1054 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1055 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1056 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1057 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1058 eec2f5a9 2021-06-03 stsp goto done;
1059 eec2f5a9 2021-06-03 stsp }
1060 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1061 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1062 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1063 eec2f5a9 2021-06-03 stsp goto done;
1064 eec2f5a9 2021-06-03 stsp }
1065 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1066 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1067 eec2f5a9 2021-06-03 stsp goto done;
1068 eec2f5a9 2021-06-03 stsp }
1069 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1070 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1071 eec2f5a9 2021-06-03 stsp goto done;
1072 eec2f5a9 2021-06-03 stsp }
1073 eec2f5a9 2021-06-03 stsp done:
1074 eec2f5a9 2021-06-03 stsp if (err)
1075 eec2f5a9 2021-06-03 stsp fclose(f);
1076 eec2f5a9 2021-06-03 stsp else
1077 eec2f5a9 2021-06-03 stsp *outfile = f;
1078 14c901f1 2019-08-08 stsp return err;
1079 14c901f1 2019-08-08 stsp }
1080 14c901f1 2019-08-08 stsp
1081 14c901f1 2019-08-08 stsp /*
1082 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1083 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1084 14c901f1 2019-08-08 stsp * acts as the second derived version.
1085 14c901f1 2019-08-08 stsp */
1086 14c901f1 2019-08-08 stsp static const struct got_error *
1087 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1088 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1089 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1090 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1091 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1092 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1093 14c901f1 2019-08-08 stsp {
1094 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1095 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1096 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1097 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1098 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1099 14c901f1 2019-08-08 stsp
1100 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1101 14c901f1 2019-08-08 stsp
1102 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1103 ed6b5030 2020-10-20 stsp if (err)
1104 ed6b5030 2020-10-20 stsp return err;
1105 14c901f1 2019-08-08 stsp
1106 67a66647 2021-05-31 stsp if (blob_orig) {
1107 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1108 67a66647 2021-05-31 stsp parent) == -1) {
1109 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1110 67a66647 2021-05-31 stsp base_path = NULL;
1111 67a66647 2021-05-31 stsp goto done;
1112 67a66647 2021-05-31 stsp }
1113 67a66647 2021-05-31 stsp
1114 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1115 67a66647 2021-05-31 stsp if (err)
1116 67a66647 2021-05-31 stsp goto done;
1117 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1118 67a66647 2021-05-31 stsp blob_orig);
1119 67a66647 2021-05-31 stsp if (err)
1120 67a66647 2021-05-31 stsp goto done;
1121 67a66647 2021-05-31 stsp free(base_path);
1122 db590691 2021-06-02 stsp } else {
1123 db590691 2021-06-02 stsp /*
1124 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1125 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1126 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1127 db590691 2021-06-02 stsp */
1128 db590691 2021-06-02 stsp f_orig = got_opentemp();
1129 db590691 2021-06-02 stsp if (f_orig == NULL) {
1130 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1131 db590691 2021-06-02 stsp goto done;
1132 db590691 2021-06-02 stsp }
1133 67a66647 2021-05-31 stsp }
1134 67a66647 2021-05-31 stsp
1135 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1136 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1137 14c901f1 2019-08-08 stsp base_path = NULL;
1138 14c901f1 2019-08-08 stsp goto done;
1139 14c901f1 2019-08-08 stsp }
1140 14c901f1 2019-08-08 stsp
1141 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1142 14c901f1 2019-08-08 stsp if (err)
1143 14c901f1 2019-08-08 stsp goto done;
1144 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1145 14c901f1 2019-08-08 stsp blob_deriv);
1146 14c901f1 2019-08-08 stsp if (err)
1147 14c901f1 2019-08-08 stsp goto done;
1148 14c901f1 2019-08-08 stsp
1149 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1150 14c901f1 2019-08-08 stsp if (err)
1151 14c901f1 2019-08-08 stsp goto done;
1152 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1153 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1154 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1155 14c901f1 2019-08-08 stsp goto done;
1156 eec2f5a9 2021-06-03 stsp }
1157 eec2f5a9 2021-06-03 stsp
1158 eec2f5a9 2021-06-03 stsp /*
1159 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1160 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1161 eec2f5a9 2021-06-03 stsp */
1162 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1163 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1164 eec2f5a9 2021-06-03 stsp if (err)
1165 eec2f5a9 2021-06-03 stsp goto done;
1166 eec2f5a9 2021-06-03 stsp } else {
1167 eec2f5a9 2021-06-03 stsp int fd;
1168 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1169 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1170 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1171 eec2f5a9 2021-06-03 stsp goto done;
1172 eec2f5a9 2021-06-03 stsp }
1173 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1174 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1175 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1176 eec2f5a9 2021-06-03 stsp close(fd);
1177 eec2f5a9 2021-06-03 stsp goto done;
1178 eec2f5a9 2021-06-03 stsp }
1179 14c901f1 2019-08-08 stsp }
1180 14c901f1 2019-08-08 stsp
1181 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1182 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1183 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1184 14c901f1 2019-08-08 stsp done:
1185 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1186 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1187 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1188 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1189 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1190 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1191 14c901f1 2019-08-08 stsp free(base_path);
1192 67a66647 2021-05-31 stsp if (blob_orig_path) {
1193 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1194 67a66647 2021-05-31 stsp free(blob_orig_path);
1195 67a66647 2021-05-31 stsp }
1196 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1197 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1198 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1199 14c901f1 2019-08-08 stsp }
1200 6353ad76 2019-02-08 stsp free(id_str);
1201 818c7501 2019-07-11 stsp free(label_deriv);
1202 ed6b5030 2020-10-20 stsp free(parent);
1203 4a1ddfc2 2019-01-12 stsp return err;
1204 4a1ddfc2 2019-01-12 stsp }
1205 4a1ddfc2 2019-01-12 stsp
1206 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1207 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1208 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1209 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1210 13d9040b 2019-03-27 stsp {
1211 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1212 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1213 13d9040b 2019-03-27 stsp
1214 65b05cec 2020-07-23 stsp *new_iep = NULL;
1215 65b05cec 2020-07-23 stsp
1216 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1217 054041d0 2020-06-24 stsp if (err)
1218 054041d0 2020-06-24 stsp return err;
1219 054041d0 2020-06-24 stsp
1220 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1221 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1222 054041d0 2020-06-24 stsp if (err)
1223 054041d0 2020-06-24 stsp goto done;
1224 054041d0 2020-06-24 stsp
1225 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1226 054041d0 2020-06-24 stsp done:
1227 054041d0 2020-06-24 stsp if (err)
1228 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1229 65b05cec 2020-07-23 stsp else
1230 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1231 13d9040b 2019-03-27 stsp return err;
1232 1ebedb77 2019-10-19 stsp }
1233 1ebedb77 2019-10-19 stsp
1234 1ebedb77 2019-10-19 stsp static mode_t
1235 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1236 1ebedb77 2019-10-19 stsp {
1237 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1238 1ebedb77 2019-10-19 stsp
1239 1ebedb77 2019-10-19 stsp if (executable) {
1240 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1241 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1242 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1243 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1244 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1245 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1246 1ebedb77 2019-10-19 stsp }
1247 1ebedb77 2019-10-19 stsp
1248 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1249 13d9040b 2019-03-27 stsp }
1250 13d9040b 2019-03-27 stsp
1251 8ba819a3 2020-07-23 stsp /* forward declaration */
1252 13d9040b 2019-03-27 stsp static const struct got_error *
1253 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1254 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1255 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1256 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1257 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1258 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1259 8ba819a3 2020-07-23 stsp
1260 5a1fbc73 2020-07-23 stsp /*
1261 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1262 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1263 5a1fbc73 2020-07-23 stsp */
1264 8ba819a3 2020-07-23 stsp static const struct got_error *
1265 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1266 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1267 5a1fbc73 2020-07-23 stsp {
1268 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1269 5a1fbc73 2020-07-23 stsp ssize_t elen;
1270 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1271 5a1fbc73 2020-07-23 stsp int fd;
1272 5a1fbc73 2020-07-23 stsp
1273 c6e8a826 2021-04-05 stsp *did_something = 0;
1274 c6e8a826 2021-04-05 stsp
1275 5a1fbc73 2020-07-23 stsp /*
1276 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1277 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1278 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1279 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1280 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1281 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1282 5a1fbc73 2020-07-23 stsp */
1283 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1284 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1285 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1286 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1287 5a1fbc73 2020-07-23 stsp
1288 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1289 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1290 5a1fbc73 2020-07-23 stsp if (elen == -1)
1291 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1292 5a1fbc73 2020-07-23 stsp
1293 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1294 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1295 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1296 5a1fbc73 2020-07-23 stsp }
1297 5a1fbc73 2020-07-23 stsp
1298 c6e8a826 2021-04-05 stsp *did_something = 1;
1299 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1300 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1301 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1302 5a1fbc73 2020-07-23 stsp return err;
1303 3c1ec782 2020-07-23 stsp }
1304 3c1ec782 2020-07-23 stsp
1305 3c1ec782 2020-07-23 stsp static const struct got_error *
1306 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1307 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1308 3c1ec782 2020-07-23 stsp {
1309 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1310 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1311 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1312 3c1ec782 2020-07-23 stsp
1313 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1314 3c1ec782 2020-07-23 stsp
1315 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1316 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1317 3c1ec782 2020-07-23 stsp return NULL;
1318 3c1ec782 2020-07-23 stsp }
1319 3c1ec782 2020-07-23 stsp
1320 3c1ec782 2020-07-23 stsp /*
1321 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1322 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1323 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1324 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1325 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1326 3c1ec782 2020-07-23 stsp */
1327 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1328 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1329 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1330 ce031e9e 2020-10-20 stsp if (err)
1331 ce031e9e 2020-10-20 stsp return err;
1332 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1333 ce031e9e 2020-10-20 stsp free(parent);
1334 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1335 ce031e9e 2020-10-20 stsp }
1336 ce031e9e 2020-10-20 stsp free(parent);
1337 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1338 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1339 3c1ec782 2020-07-23 stsp free(abspath);
1340 3c1ec782 2020-07-23 stsp return err;
1341 3c1ec782 2020-07-23 stsp }
1342 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1343 3c1ec782 2020-07-23 stsp free(abspath);
1344 3c1ec782 2020-07-23 stsp if (err)
1345 3c1ec782 2020-07-23 stsp return err;
1346 3c1ec782 2020-07-23 stsp } else {
1347 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1348 3c1ec782 2020-07-23 stsp if (err)
1349 3c1ec782 2020-07-23 stsp return err;
1350 3c1ec782 2020-07-23 stsp }
1351 3c1ec782 2020-07-23 stsp
1352 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1353 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1354 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1355 3c1ec782 2020-07-23 stsp return NULL;
1356 3c1ec782 2020-07-23 stsp }
1357 3c1ec782 2020-07-23 stsp
1358 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1359 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1360 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1361 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1362 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1363 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1364 3c1ec782 2020-07-23 stsp
1365 3c1ec782 2020-07-23 stsp free(path_got);
1366 3c1ec782 2020-07-23 stsp return NULL;
1367 5a1fbc73 2020-07-23 stsp }
1368 5a1fbc73 2020-07-23 stsp
1369 5a1fbc73 2020-07-23 stsp static const struct got_error *
1370 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1371 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1372 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1373 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1374 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1375 9d31a1d8 2018-03-11 stsp {
1376 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1377 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1378 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1379 906c123b 2020-07-23 stsp char *path_got = NULL;
1380 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1381 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1382 8ba819a3 2020-07-23 stsp
1383 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1384 2e1fa222 2020-07-23 stsp
1385 8ba819a3 2020-07-23 stsp /*
1386 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1387 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1388 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1389 8ba819a3 2020-07-23 stsp * in the blob object.
1390 8ba819a3 2020-07-23 stsp */
1391 8ba819a3 2020-07-23 stsp do {
1392 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1393 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1394 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1395 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1396 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1397 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1398 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1399 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1400 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1401 3b9f0f87 2020-07-23 stsp progress_arg);
1402 8ba819a3 2020-07-23 stsp }
1403 8ba819a3 2020-07-23 stsp if (len > 0) {
1404 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1405 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1406 8ba819a3 2020-07-23 stsp len - hdrlen);
1407 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1408 8ba819a3 2020-07-23 stsp hdrlen = 0;
1409 8ba819a3 2020-07-23 stsp }
1410 8ba819a3 2020-07-23 stsp } while (len != 0);
1411 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1412 8ba819a3 2020-07-23 stsp
1413 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1414 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1415 3c1ec782 2020-07-23 stsp if (err)
1416 3c1ec782 2020-07-23 stsp return err;
1417 8ba819a3 2020-07-23 stsp
1418 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1419 906c123b 2020-07-23 stsp /* install as a regular file */
1420 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1421 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1422 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1423 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1424 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1425 906c123b 2020-07-23 stsp goto done;
1426 906c123b 2020-07-23 stsp }
1427 906c123b 2020-07-23 stsp
1428 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1429 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1430 c6e8a826 2021-04-05 stsp int symlink_replaced;
1431 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1432 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1433 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1434 c90c8ce3 2020-07-23 stsp goto done;
1435 c90c8ce3 2020-07-23 stsp }
1436 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1437 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1438 5a1fbc73 2020-07-23 stsp if (err)
1439 f35fa46a 2020-07-23 stsp goto done;
1440 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1441 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1442 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1443 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1444 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1445 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1446 c6e8a826 2021-04-05 stsp } else {
1447 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1448 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1449 c6e8a826 2021-04-05 stsp }
1450 f35fa46a 2020-07-23 stsp }
1451 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1452 f35fa46a 2020-07-23 stsp }
1453 f35fa46a 2020-07-23 stsp
1454 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1455 f4994adc 2020-10-20 stsp char *parent;
1456 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1457 f4994adc 2020-10-20 stsp if (err)
1458 f4994adc 2020-10-20 stsp goto done;
1459 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1460 f4994adc 2020-10-20 stsp free(parent);
1461 f35fa46a 2020-07-23 stsp if (err)
1462 f35fa46a 2020-07-23 stsp goto done;
1463 f35fa46a 2020-07-23 stsp /*
1464 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1465 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1466 f35fa46a 2020-07-23 stsp */
1467 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1468 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1469 f35fa46a 2020-07-23 stsp goto done;
1470 f35fa46a 2020-07-23 stsp }
1471 f35fa46a 2020-07-23 stsp }
1472 f35fa46a 2020-07-23 stsp
1473 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1474 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1475 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1476 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1477 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1478 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1479 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1480 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1481 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1482 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1483 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1484 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1485 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1486 8ba819a3 2020-07-23 stsp } else {
1487 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1488 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1489 8ba819a3 2020-07-23 stsp }
1490 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1491 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1492 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1493 8ba819a3 2020-07-23 stsp done:
1494 906c123b 2020-07-23 stsp free(path_got);
1495 8ba819a3 2020-07-23 stsp return err;
1496 8ba819a3 2020-07-23 stsp }
1497 8ba819a3 2020-07-23 stsp
1498 8ba819a3 2020-07-23 stsp static const struct got_error *
1499 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1500 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1501 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1502 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1503 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1504 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1505 8ba819a3 2020-07-23 stsp {
1506 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1507 507dc3bb 2018-12-29 stsp int fd = -1;
1508 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1509 507dc3bb 2018-12-29 stsp int update = 0;
1510 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1511 8ba819a3 2020-07-23 stsp
1512 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1513 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1514 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1515 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1516 f5375317 2020-10-20 stsp char *parent;
1517 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1518 f5375317 2020-10-20 stsp if (err)
1519 f5375317 2020-10-20 stsp return err;
1520 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1521 f5375317 2020-10-20 stsp free(parent);
1522 21908da4 2019-01-13 stsp if (err)
1523 21908da4 2019-01-13 stsp return err;
1524 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1525 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1526 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1527 21908da4 2019-01-13 stsp if (fd == -1)
1528 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1529 230a42bd 2019-05-11 jcs ondisk_path);
1530 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1531 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1532 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1533 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1534 3b9f0f87 2020-07-23 stsp goto done;
1535 3b9f0f87 2020-07-23 stsp }
1536 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1537 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1538 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1539 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1540 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1541 507dc3bb 2018-12-29 stsp goto done;
1542 d70b8e30 2018-12-27 stsp } else {
1543 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1544 507dc3bb 2018-12-29 stsp ondisk_path);
1545 507dc3bb 2018-12-29 stsp if (err)
1546 507dc3bb 2018-12-29 stsp goto done;
1547 507dc3bb 2018-12-29 stsp update = 1;
1548 9d31a1d8 2018-03-11 stsp }
1549 507dc3bb 2018-12-29 stsp } else
1550 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1551 9d31a1d8 2018-03-11 stsp }
1552 9d31a1d8 2018-03-11 stsp
1553 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1554 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1555 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1556 3818e3c4 2020-11-01 naddy goto done;
1557 3818e3c4 2020-11-01 naddy }
1558 3818e3c4 2020-11-01 naddy
1559 bd6aa359 2020-07-23 stsp if (progress_cb) {
1560 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1561 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1562 bd6aa359 2020-07-23 stsp path);
1563 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1564 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1565 bd6aa359 2020-07-23 stsp path);
1566 bd6aa359 2020-07-23 stsp else
1567 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1568 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1569 bd6aa359 2020-07-23 stsp if (err)
1570 bd6aa359 2020-07-23 stsp goto done;
1571 bd6aa359 2020-07-23 stsp }
1572 d7b62c98 2018-12-27 stsp
1573 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1574 9d31a1d8 2018-03-11 stsp do {
1575 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1576 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1577 9d31a1d8 2018-03-11 stsp if (err)
1578 9d31a1d8 2018-03-11 stsp break;
1579 9d31a1d8 2018-03-11 stsp if (len > 0) {
1580 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1581 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1582 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1583 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1584 b87c6f83 2018-12-24 stsp goto done;
1585 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1586 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1587 b87c6f83 2018-12-24 stsp goto done;
1588 9d31a1d8 2018-03-11 stsp }
1589 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1590 9d31a1d8 2018-03-11 stsp }
1591 9d31a1d8 2018-03-11 stsp } while (len != 0);
1592 9d31a1d8 2018-03-11 stsp
1593 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1594 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1595 816dc654 2019-02-16 stsp goto done;
1596 816dc654 2019-02-16 stsp }
1597 9d31a1d8 2018-03-11 stsp
1598 507dc3bb 2018-12-29 stsp if (update) {
1599 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1600 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1601 f6d8c0ac 2020-11-09 stsp goto done;
1602 f6d8c0ac 2020-11-09 stsp }
1603 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1604 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1605 230a42bd 2019-05-11 jcs ondisk_path);
1606 68ed9ba5 2019-02-10 stsp goto done;
1607 68ed9ba5 2019-02-10 stsp }
1608 63df146d 2020-11-09 stsp free(tmppath);
1609 63df146d 2020-11-09 stsp tmppath = NULL;
1610 507dc3bb 2018-12-29 stsp }
1611 507dc3bb 2018-12-29 stsp
1612 9d31a1d8 2018-03-11 stsp done:
1613 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1614 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1615 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1616 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1617 507dc3bb 2018-12-29 stsp free(tmppath);
1618 6353ad76 2019-02-08 stsp return err;
1619 6353ad76 2019-02-08 stsp }
1620 6353ad76 2019-02-08 stsp
1621 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1622 6353ad76 2019-02-08 stsp static const struct got_error *
1623 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1624 7154f6ce 2019-03-27 stsp {
1625 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1626 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1627 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1628 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1629 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1630 7154f6ce 2019-03-27 stsp };
1631 7154f6ce 2019-03-27 stsp int i = 0;
1632 9bdd68dd 2020-01-02 naddy char *line = NULL;
1633 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1634 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1635 7154f6ce 2019-03-27 stsp
1636 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1637 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1638 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1639 7154f6ce 2019-03-27 stsp if (feof(f))
1640 7154f6ce 2019-03-27 stsp break;
1641 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1642 7154f6ce 2019-03-27 stsp break;
1643 7154f6ce 2019-03-27 stsp }
1644 7154f6ce 2019-03-27 stsp
1645 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1646 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1647 19332e6d 2019-05-13 stsp == 0)
1648 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1649 7154f6ce 2019-03-27 stsp else
1650 7154f6ce 2019-03-27 stsp i++;
1651 7154f6ce 2019-03-27 stsp }
1652 7154f6ce 2019-03-27 stsp }
1653 9bdd68dd 2020-01-02 naddy free(line);
1654 7154f6ce 2019-03-27 stsp
1655 7154f6ce 2019-03-27 stsp return err;
1656 e2b1e152 2019-07-13 stsp }
1657 e2b1e152 2019-07-13 stsp
1658 e2b1e152 2019-07-13 stsp static int
1659 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1660 1ebedb77 2019-10-19 stsp {
1661 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1662 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1663 1ebedb77 2019-10-19 stsp }
1664 1ebedb77 2019-10-19 stsp
1665 1ebedb77 2019-10-19 stsp static int
1666 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1667 e2b1e152 2019-07-13 stsp {
1668 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1669 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1670 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1671 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1672 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1673 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1674 c363b2c1 2019-08-03 stsp }
1675 c363b2c1 2019-08-03 stsp
1676 c363b2c1 2019-08-03 stsp static unsigned char
1677 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1678 c363b2c1 2019-08-03 stsp {
1679 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1680 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1681 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1682 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1683 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1684 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1685 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1686 c363b2c1 2019-08-03 stsp default:
1687 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1688 a919d5c4 2020-07-23 stsp }
1689 a919d5c4 2020-07-23 stsp }
1690 a919d5c4 2020-07-23 stsp
1691 a919d5c4 2020-07-23 stsp static const struct got_error *
1692 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1693 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1694 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1695 a919d5c4 2020-07-23 stsp {
1696 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1697 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1698 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1699 a919d5c4 2020-07-23 stsp ssize_t elen;
1700 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1701 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1702 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1703 a919d5c4 2020-07-23 stsp
1704 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1705 a919d5c4 2020-07-23 stsp
1706 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1707 a919d5c4 2020-07-23 stsp do {
1708 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1709 a919d5c4 2020-07-23 stsp if (err)
1710 a919d5c4 2020-07-23 stsp return err;
1711 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1712 a919d5c4 2020-07-23 stsp /*
1713 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1714 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1715 a919d5c4 2020-07-23 stsp */
1716 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1717 a919d5c4 2020-07-23 stsp }
1718 a919d5c4 2020-07-23 stsp if (len > 0) {
1719 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1720 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1721 a919d5c4 2020-07-23 stsp len - hdrlen);
1722 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1723 a919d5c4 2020-07-23 stsp hdrlen = 0;
1724 a919d5c4 2020-07-23 stsp }
1725 a919d5c4 2020-07-23 stsp } while (len != 0);
1726 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1727 a919d5c4 2020-07-23 stsp
1728 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1729 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1730 a919d5c4 2020-07-23 stsp if (elen == -1)
1731 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1732 a919d5c4 2020-07-23 stsp } else {
1733 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1734 a919d5c4 2020-07-23 stsp if (elen == -1)
1735 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1736 c363b2c1 2019-08-03 stsp }
1737 a919d5c4 2020-07-23 stsp
1738 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1739 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1740 a919d5c4 2020-07-23 stsp
1741 a919d5c4 2020-07-23 stsp return NULL;
1742 7154f6ce 2019-03-27 stsp }
1743 7154f6ce 2019-03-27 stsp
1744 7154f6ce 2019-03-27 stsp static const struct got_error *
1745 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1746 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1747 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1748 6353ad76 2019-02-08 stsp {
1749 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1750 6353ad76 2019-02-08 stsp struct got_object_id id;
1751 6353ad76 2019-02-08 stsp size_t hdrlen;
1752 1338848f 2019-12-13 stsp int fd = -1;
1753 6353ad76 2019-02-08 stsp FILE *f = NULL;
1754 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1755 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1756 6353ad76 2019-02-08 stsp size_t flen, blen;
1757 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1758 6353ad76 2019-02-08 stsp
1759 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1760 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1761 6353ad76 2019-02-08 stsp
1762 7f91a133 2019-12-13 stsp /*
1763 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1764 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1765 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1766 7f91a133 2019-12-13 stsp */
1767 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1768 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1769 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1770 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1771 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1772 882ef1b9 2019-12-13 stsp else
1773 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1774 882ef1b9 2019-12-13 stsp goto done;
1775 882ef1b9 2019-12-13 stsp }
1776 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1777 3d35a492 2019-12-13 stsp goto done;
1778 3d35a492 2019-12-13 stsp }
1779 7f91a133 2019-12-13 stsp } else {
1780 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1781 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1782 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1783 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1784 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1785 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1786 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1787 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1788 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1789 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1790 3d35a492 2019-12-13 stsp else
1791 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1792 3d35a492 2019-12-13 stsp goto done;
1793 3d35a492 2019-12-13 stsp }
1794 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1795 1338848f 2019-12-13 stsp goto done;
1796 a378724f 2019-02-10 stsp }
1797 a378724f 2019-02-10 stsp }
1798 6353ad76 2019-02-08 stsp
1799 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1800 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1801 1338848f 2019-12-13 stsp goto done;
1802 3f148bc6 2019-07-27 stsp }
1803 efdd40df 2019-07-27 stsp
1804 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1805 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1806 1338848f 2019-12-13 stsp goto done;
1807 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1808 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1809 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1810 1338848f 2019-12-13 stsp goto done;
1811 d00136be 2019-03-26 stsp }
1812 b8f41171 2019-02-10 stsp
1813 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1814 f179e66d 2020-07-23 stsp goto done;
1815 f179e66d 2020-07-23 stsp
1816 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1817 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1818 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1819 1338848f 2019-12-13 stsp goto done;
1820 f179e66d 2020-07-23 stsp }
1821 6353ad76 2019-02-08 stsp
1822 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1823 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1824 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1825 c363b2c1 2019-08-03 stsp else
1826 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1827 c363b2c1 2019-08-03 stsp
1828 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1829 6353ad76 2019-02-08 stsp if (err)
1830 1338848f 2019-12-13 stsp goto done;
1831 6353ad76 2019-02-08 stsp
1832 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1833 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1834 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1835 a919d5c4 2020-07-23 stsp goto done;
1836 a919d5c4 2020-07-23 stsp }
1837 a919d5c4 2020-07-23 stsp
1838 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1839 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1840 ab0d4361 2019-12-13 stsp if (fd == -1) {
1841 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1842 ab0d4361 2019-12-13 stsp goto done;
1843 ab0d4361 2019-12-13 stsp }
1844 3d35a492 2019-12-13 stsp }
1845 3d35a492 2019-12-13 stsp
1846 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1847 6353ad76 2019-02-08 stsp if (f == NULL) {
1848 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1849 6353ad76 2019-02-08 stsp goto done;
1850 6353ad76 2019-02-08 stsp }
1851 1338848f 2019-12-13 stsp fd = -1;
1852 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1853 656b1f76 2019-05-11 jcs for (;;) {
1854 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1855 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1856 6353ad76 2019-02-08 stsp if (err)
1857 7154f6ce 2019-03-27 stsp goto done;
1858 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1859 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1860 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1861 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1862 7154f6ce 2019-03-27 stsp goto done;
1863 80c5c120 2019-02-19 stsp }
1864 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1865 6353ad76 2019-02-08 stsp if (flen != 0)
1866 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1867 6353ad76 2019-02-08 stsp break;
1868 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1869 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 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 (blen - hdrlen == flen) {
1873 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1874 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1875 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1876 6353ad76 2019-02-08 stsp break;
1877 6353ad76 2019-02-08 stsp }
1878 6353ad76 2019-02-08 stsp } else {
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 hdrlen = 0;
1883 6353ad76 2019-02-08 stsp }
1884 7154f6ce 2019-03-27 stsp
1885 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1886 7154f6ce 2019-03-27 stsp rewind(f);
1887 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1888 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1889 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1890 6353ad76 2019-02-08 stsp done:
1891 6353ad76 2019-02-08 stsp if (blob)
1892 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1893 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1894 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1895 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1896 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1897 9d31a1d8 2018-03-11 stsp return err;
1898 e2b1e152 2019-07-13 stsp }
1899 e2b1e152 2019-07-13 stsp
1900 e2b1e152 2019-07-13 stsp /*
1901 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1902 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1903 e2b1e152 2019-07-13 stsp */
1904 e2b1e152 2019-07-13 stsp static const struct got_error *
1905 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1906 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1907 e2b1e152 2019-07-13 stsp {
1908 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1909 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1910 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1911 e2b1e152 2019-07-13 stsp
1912 e2b1e152 2019-07-13 stsp return NULL;
1913 9d31a1d8 2018-03-11 stsp }
1914 9d31a1d8 2018-03-11 stsp
1915 9d31a1d8 2018-03-11 stsp static const struct got_error *
1916 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1917 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1918 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1919 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1920 0584f854 2019-04-06 stsp void *progress_arg)
1921 9d31a1d8 2018-03-11 stsp {
1922 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1923 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1924 6353ad76 2019-02-08 stsp char *ondisk_path;
1925 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1926 b8f41171 2019-02-10 stsp struct stat sb;
1927 9d31a1d8 2018-03-11 stsp
1928 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1929 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1930 6353ad76 2019-02-08 stsp
1931 abb4604f 2019-07-27 stsp if (ie) {
1932 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1933 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1934 a76c42e6 2019-08-03 stsp goto done;
1935 a76c42e6 2019-08-03 stsp }
1936 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1937 7f91a133 2019-12-13 stsp repo);
1938 abb4604f 2019-07-27 stsp if (err)
1939 abb4604f 2019-07-27 stsp goto done;
1940 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1941 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1942 c90c8ce3 2020-07-23 stsp } else {
1943 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1944 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1945 c90c8ce3 2020-07-23 stsp }
1946 6353ad76 2019-02-08 stsp
1947 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1948 2c41dce7 2021-06-27 stsp if (ie)
1949 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1950 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1951 b8f41171 2019-02-10 stsp goto done;
1952 b8f41171 2019-02-10 stsp }
1953 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1954 a769b60b 2021-06-27 stsp if (ie)
1955 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1956 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1957 5036ab18 2020-04-18 stsp path);
1958 5036ab18 2020-04-18 stsp goto done;
1959 5036ab18 2020-04-18 stsp }
1960 b8f41171 2019-02-10 stsp
1961 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1963 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1964 c6e8a826 2021-04-05 stsp /*
1965 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1966 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1967 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1968 c6e8a826 2021-04-05 stsp * updating contents of this file.
1969 c6e8a826 2021-04-05 stsp */
1970 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1971 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1973 c6e8a826 2021-04-05 stsp /* Same commit. */
1974 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1975 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1976 e2b1e152 2019-07-13 stsp if (err)
1977 e2b1e152 2019-07-13 stsp goto done;
1978 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 b8f41171 2019-02-10 stsp path);
1980 6353ad76 2019-02-08 stsp goto done;
1981 a378724f 2019-02-10 stsp }
1982 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1983 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1984 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1985 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1986 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1987 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1988 0f58026f 2021-04-05 stsp if (err)
1989 0f58026f 2021-04-05 stsp goto done;
1990 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1991 0f58026f 2021-04-05 stsp path);
1992 b8f41171 2019-02-10 stsp goto done;
1993 e2b1e152 2019-07-13 stsp }
1994 9d31a1d8 2018-03-11 stsp }
1995 9d31a1d8 2018-03-11 stsp
1996 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1997 8da9e5f4 2019-01-12 stsp if (err)
1998 6353ad76 2019-02-08 stsp goto done;
1999 512f0d0e 2019-01-02 stsp
2000 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 234035bc 2019-06-01 stsp int update_timestamps;
2002 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2003 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2004 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2005 234035bc 2019-06-01 stsp struct got_object_id id2;
2006 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2008 234035bc 2019-06-01 stsp if (err)
2009 f69721c3 2019-10-21 stsp goto done;
2010 f69721c3 2019-10-21 stsp }
2011 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2012 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2015 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2016 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2017 f69721c3 2019-10-21 stsp goto done;
2018 f69721c3 2019-10-21 stsp }
2019 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2020 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2022 234035bc 2019-06-01 stsp goto done;
2023 f69721c3 2019-10-21 stsp }
2024 234035bc 2019-06-01 stsp }
2025 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2026 36bf999c 2020-07-23 stsp char *link_target;
2027 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2028 36bf999c 2020-07-23 stsp if (err)
2029 36bf999c 2020-07-23 stsp goto done;
2030 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2032 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2033 36bf999c 2020-07-23 stsp free(link_target);
2034 993e2a1b 2020-07-23 stsp } else {
2035 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2036 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2037 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2038 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2039 993e2a1b 2020-07-23 stsp }
2040 f69721c3 2019-10-21 stsp free(label_orig);
2041 234035bc 2019-06-01 stsp if (blob2)
2042 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2043 909d120e 2019-09-22 stsp if (err)
2044 909d120e 2019-09-22 stsp goto done;
2045 234035bc 2019-06-01 stsp /*
2046 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2047 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2048 234035bc 2019-06-01 stsp * unmodified files again.
2049 234035bc 2019-06-01 stsp */
2050 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2052 234035bc 2019-06-01 stsp update_timestamps);
2053 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2057 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2058 1ee397ad 2019-07-12 stsp if (err)
2059 1ee397ad 2019-07-12 stsp goto done;
2060 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2062 13d9040b 2019-03-27 stsp if (err)
2063 13d9040b 2019-03-27 stsp goto done;
2064 13d9040b 2019-03-27 stsp } else {
2065 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2066 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2067 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2068 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2069 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2070 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2071 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2072 2e1fa222 2020-07-23 stsp } else {
2073 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2074 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2075 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2076 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2077 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2078 2e1fa222 2020-07-23 stsp }
2079 13d9040b 2019-03-27 stsp if (err)
2080 13d9040b 2019-03-27 stsp goto done;
2081 65b05cec 2020-07-23 stsp
2082 054041d0 2020-06-24 stsp if (ie) {
2083 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2084 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2085 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2086 054041d0 2020-06-24 stsp } else {
2087 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2088 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2089 054041d0 2020-06-24 stsp &blob->id);
2090 054041d0 2020-06-24 stsp }
2091 13d9040b 2019-03-27 stsp if (err)
2092 13d9040b 2019-03-27 stsp goto done;
2093 65b05cec 2020-07-23 stsp
2094 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2095 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2096 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 2e1fa222 2020-07-23 stsp }
2098 13d9040b 2019-03-27 stsp }
2099 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2100 6353ad76 2019-02-08 stsp done:
2101 6353ad76 2019-02-08 stsp free(ondisk_path);
2102 8da9e5f4 2019-01-12 stsp return err;
2103 90285c3b 2019-01-08 stsp }
2104 90285c3b 2019-01-08 stsp
2105 90285c3b 2019-01-08 stsp static const struct got_error *
2106 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2107 90285c3b 2019-01-08 stsp {
2108 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2109 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2110 90285c3b 2019-01-08 stsp
2111 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2113 90285c3b 2019-01-08 stsp
2114 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2115 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2116 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2117 c97665ae 2019-01-13 stsp } else {
2118 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2119 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2120 1c4cdd89 2021-06-20 stsp if (err)
2121 1c4cdd89 2021-06-20 stsp goto done;
2122 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2123 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2124 fddefe3b 2020-10-20 stsp free(ondisk_path);
2125 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2126 1c4cdd89 2021-06-20 stsp parent = NULL;
2127 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2128 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2129 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2130 fddefe3b 2020-10-20 stsp ondisk_path);
2131 90285c3b 2019-01-08 stsp break;
2132 90285c3b 2019-01-08 stsp }
2133 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2134 1c4cdd89 2021-06-20 stsp if (err)
2135 1c4cdd89 2021-06-20 stsp break;
2136 1c4cdd89 2021-06-20 stsp }
2137 90285c3b 2019-01-08 stsp }
2138 1c4cdd89 2021-06-20 stsp done:
2139 90285c3b 2019-01-08 stsp free(ondisk_path);
2140 1c4cdd89 2021-06-20 stsp free(parent);
2141 90285c3b 2019-01-08 stsp return err;
2142 512f0d0e 2019-01-02 stsp }
2143 512f0d0e 2019-01-02 stsp
2144 708d8e67 2019-03-27 stsp static const struct got_error *
2145 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2146 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2147 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2148 708d8e67 2019-03-27 stsp {
2149 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2150 708d8e67 2019-03-27 stsp unsigned char status;
2151 708d8e67 2019-03-27 stsp struct stat sb;
2152 708d8e67 2019-03-27 stsp char *ondisk_path;
2153 708d8e67 2019-03-27 stsp
2154 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2155 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2156 a76c42e6 2019-08-03 stsp
2157 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2158 708d8e67 2019-03-27 stsp == -1)
2159 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2160 708d8e67 2019-03-27 stsp
2161 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2162 708d8e67 2019-03-27 stsp if (err)
2163 5a58a424 2020-06-23 stsp goto done;
2164 708d8e67 2019-03-27 stsp
2165 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2166 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2167 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2168 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2169 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2170 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2171 993e2a1b 2020-07-23 stsp goto done;
2172 993e2a1b 2020-07-23 stsp }
2173 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2174 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2175 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2176 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2177 993e2a1b 2020-07-23 stsp if (err)
2178 993e2a1b 2020-07-23 stsp goto done;
2179 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2180 993e2a1b 2020-07-23 stsp ie->path);
2181 993e2a1b 2020-07-23 stsp goto done;
2182 993e2a1b 2020-07-23 stsp }
2183 993e2a1b 2020-07-23 stsp
2184 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2185 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2186 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2187 1ee397ad 2019-07-12 stsp if (err)
2188 5a58a424 2020-06-23 stsp goto done;
2189 fc6346c4 2019-03-27 stsp /*
2190 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2191 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2192 fc6346c4 2019-03-27 stsp */
2193 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2194 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2195 fc6346c4 2019-03-27 stsp } else {
2196 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2197 1ee397ad 2019-07-12 stsp if (err)
2198 5a58a424 2020-06-23 stsp goto done;
2199 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2200 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2201 fc6346c4 2019-03-27 stsp if (err)
2202 5a58a424 2020-06-23 stsp goto done;
2203 fc6346c4 2019-03-27 stsp }
2204 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2205 708d8e67 2019-03-27 stsp }
2206 5a58a424 2020-06-23 stsp done:
2207 5a58a424 2020-06-23 stsp free(ondisk_path);
2208 fc6346c4 2019-03-27 stsp return err;
2209 708d8e67 2019-03-27 stsp }
2210 708d8e67 2019-03-27 stsp
2211 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2212 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2213 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2214 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2215 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2216 8da9e5f4 2019-01-12 stsp void *progress_arg;
2217 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2218 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2219 8da9e5f4 2019-01-12 stsp };
2220 8da9e5f4 2019-01-12 stsp
2221 512f0d0e 2019-01-02 stsp static const struct got_error *
2222 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2223 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2224 512f0d0e 2019-01-02 stsp {
2225 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2226 0584f854 2019-04-06 stsp
2227 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2228 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2229 512f0d0e 2019-01-02 stsp
2230 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2231 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2232 8da9e5f4 2019-01-12 stsp }
2233 512f0d0e 2019-01-02 stsp
2234 8da9e5f4 2019-01-12 stsp static const struct got_error *
2235 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2236 8da9e5f4 2019-01-12 stsp {
2237 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2238 7a9df742 2019-01-08 stsp
2239 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2241 0584f854 2019-04-06 stsp
2242 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2243 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2244 9d31a1d8 2018-03-11 stsp }
2245 9d31a1d8 2018-03-11 stsp
2246 9d31a1d8 2018-03-11 stsp static const struct got_error *
2247 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2248 9d31a1d8 2018-03-11 stsp {
2249 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2250 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2251 8da9e5f4 2019-01-12 stsp char *path;
2252 9d31a1d8 2018-03-11 stsp
2253 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2255 0584f854 2019-04-06 stsp
2256 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2257 63c5ca5d 2019-08-24 stsp return NULL;
2258 63c5ca5d 2019-08-24 stsp
2259 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2260 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2261 8da9e5f4 2019-01-12 stsp == -1)
2262 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2263 9d31a1d8 2018-03-11 stsp
2264 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2265 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2266 8da9e5f4 2019-01-12 stsp else
2267 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2268 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2269 9d31a1d8 2018-03-11 stsp
2270 8da9e5f4 2019-01-12 stsp free(path);
2271 0cd1c46a 2019-03-11 stsp return err;
2272 0cd1c46a 2019-03-11 stsp }
2273 0cd1c46a 2019-03-11 stsp
2274 b2118c49 2020-07-28 stsp const struct got_error *
2275 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2276 b2118c49 2020-07-28 stsp {
2277 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2278 b2118c49 2020-07-28 stsp
2279 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2280 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2281 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2282 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2283 b2118c49 2020-07-28 stsp }
2284 b2118c49 2020-07-28 stsp
2285 b2118c49 2020-07-28 stsp return NULL;
2286 b2118c49 2020-07-28 stsp }
2287 b2118c49 2020-07-28 stsp
2288 818c7501 2019-07-11 stsp static const struct got_error *
2289 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2290 0cd1c46a 2019-03-11 stsp {
2291 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2292 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2293 0cd1c46a 2019-03-11 stsp
2294 517bab73 2019-03-11 stsp *refname = NULL;
2295 517bab73 2019-03-11 stsp
2296 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2297 b2118c49 2020-07-28 stsp if (err)
2298 b2118c49 2020-07-28 stsp return err;
2299 0cd1c46a 2019-03-11 stsp
2300 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2301 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2302 0647c563 2019-03-11 stsp *refname = NULL;
2303 0cd1c46a 2019-03-11 stsp }
2304 517bab73 2019-03-11 stsp free(uuidstr);
2305 517bab73 2019-03-11 stsp return err;
2306 517bab73 2019-03-11 stsp }
2307 517bab73 2019-03-11 stsp
2308 818c7501 2019-07-11 stsp const struct got_error *
2309 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2310 818c7501 2019-07-11 stsp {
2311 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2312 818c7501 2019-07-11 stsp }
2313 818c7501 2019-07-11 stsp
2314 818c7501 2019-07-11 stsp static const struct got_error *
2315 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2316 818c7501 2019-07-11 stsp {
2317 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2318 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2319 818c7501 2019-07-11 stsp }
2320 818c7501 2019-07-11 stsp
2321 818c7501 2019-07-11 stsp static const struct got_error *
2322 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2323 818c7501 2019-07-11 stsp {
2324 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2325 818c7501 2019-07-11 stsp }
2326 818c7501 2019-07-11 stsp
2327 818c7501 2019-07-11 stsp static const struct got_error *
2328 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2329 818c7501 2019-07-11 stsp {
2330 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2331 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2332 818c7501 2019-07-11 stsp }
2333 818c7501 2019-07-11 stsp
2334 818c7501 2019-07-11 stsp static const struct got_error *
2335 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2336 818c7501 2019-07-11 stsp {
2337 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2338 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2339 0ebf8283 2019-07-24 stsp }
2340 0ebf8283 2019-07-24 stsp
2341 0ebf8283 2019-07-24 stsp static const struct got_error *
2342 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2343 0ebf8283 2019-07-24 stsp {
2344 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2345 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2346 0ebf8283 2019-07-24 stsp }
2347 0ebf8283 2019-07-24 stsp
2348 0ebf8283 2019-07-24 stsp static const struct got_error *
2349 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2350 0ebf8283 2019-07-24 stsp {
2351 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2352 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2353 0ebf8283 2019-07-24 stsp }
2354 0ebf8283 2019-07-24 stsp
2355 0ebf8283 2019-07-24 stsp static const struct got_error *
2356 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2357 0ebf8283 2019-07-24 stsp {
2358 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2359 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2360 818c7501 2019-07-11 stsp }
2361 818c7501 2019-07-11 stsp
2362 0ebf8283 2019-07-24 stsp static const struct got_error *
2363 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2364 0ebf8283 2019-07-24 stsp {
2365 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2366 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2367 0ebf8283 2019-07-24 stsp }
2368 818c7501 2019-07-11 stsp
2369 0ebf8283 2019-07-24 stsp const struct got_error *
2370 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2371 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2372 0ebf8283 2019-07-24 stsp {
2373 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2374 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2375 0ebf8283 2019-07-24 stsp *path = NULL;
2376 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2377 0ebf8283 2019-07-24 stsp }
2378 0ebf8283 2019-07-24 stsp return NULL;
2379 0ebf8283 2019-07-24 stsp }
2380 0ebf8283 2019-07-24 stsp
2381 517bab73 2019-03-11 stsp /*
2382 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2383 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2384 517bab73 2019-03-11 stsp */
2385 517bab73 2019-03-11 stsp static const struct got_error *
2386 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2387 517bab73 2019-03-11 stsp {
2388 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2389 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2390 517bab73 2019-03-11 stsp char *refname;
2391 517bab73 2019-03-11 stsp
2392 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2393 517bab73 2019-03-11 stsp if (err)
2394 517bab73 2019-03-11 stsp return err;
2395 0cd1c46a 2019-03-11 stsp
2396 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2397 0cd1c46a 2019-03-11 stsp if (err)
2398 0cd1c46a 2019-03-11 stsp goto done;
2399 0cd1c46a 2019-03-11 stsp
2400 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2401 0cd1c46a 2019-03-11 stsp done:
2402 0cd1c46a 2019-03-11 stsp free(refname);
2403 0cd1c46a 2019-03-11 stsp if (ref)
2404 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2405 3e3a69f1 2019-07-25 stsp return err;
2406 3e3a69f1 2019-07-25 stsp }
2407 3e3a69f1 2019-07-25 stsp
2408 3e3a69f1 2019-07-25 stsp static const struct got_error *
2409 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2410 3e3a69f1 2019-07-25 stsp {
2411 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2412 3e3a69f1 2019-07-25 stsp
2413 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2414 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2415 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2416 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2417 3e3a69f1 2019-07-25 stsp }
2418 9d31a1d8 2018-03-11 stsp return err;
2419 9d31a1d8 2018-03-11 stsp }
2420 9d31a1d8 2018-03-11 stsp
2421 3e3a69f1 2019-07-25 stsp
2422 ebf99748 2019-05-09 stsp static const struct got_error *
2423 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2424 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2425 ebf99748 2019-05-09 stsp {
2426 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2427 ebf99748 2019-05-09 stsp FILE *index = NULL;
2428 0cd1c46a 2019-03-11 stsp
2429 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2430 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2431 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2432 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2433 ebf99748 2019-05-09 stsp
2434 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2435 3e3a69f1 2019-07-25 stsp if (err)
2436 ebf99748 2019-05-09 stsp goto done;
2437 ebf99748 2019-05-09 stsp
2438 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2439 ebf99748 2019-05-09 stsp if (index == NULL) {
2440 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2441 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2442 ebf99748 2019-05-09 stsp } else {
2443 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2444 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2445 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2446 ebf99748 2019-05-09 stsp }
2447 ebf99748 2019-05-09 stsp done:
2448 ebf99748 2019-05-09 stsp if (err) {
2449 ebf99748 2019-05-09 stsp free(*fileindex_path);
2450 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2451 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2452 ebf99748 2019-05-09 stsp *fileindex = NULL;
2453 ebf99748 2019-05-09 stsp }
2454 ebf99748 2019-05-09 stsp return err;
2455 ebf99748 2019-05-09 stsp }
2456 c932eeeb 2019-05-22 stsp
2457 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2458 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2459 c932eeeb 2019-05-22 stsp const char *path;
2460 c932eeeb 2019-05-22 stsp size_t path_len;
2461 c932eeeb 2019-05-22 stsp const char *entry_name;
2462 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2463 a484d721 2019-06-10 stsp void *progress_arg;
2464 c932eeeb 2019-05-22 stsp };
2465 ebf99748 2019-05-09 stsp
2466 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2467 c932eeeb 2019-05-22 stsp static const struct got_error *
2468 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2469 c932eeeb 2019-05-22 stsp {
2470 1ee397ad 2019-07-12 stsp const struct got_error *err;
2471 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2472 c932eeeb 2019-05-22 stsp
2473 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2474 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2475 c932eeeb 2019-05-22 stsp return NULL;
2476 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2477 102ff934 2019-06-11 stsp return NULL;
2478 102ff934 2019-06-11 stsp
2479 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2480 a769b60b 2021-06-27 stsp return NULL;
2481 a769b60b 2021-06-27 stsp
2482 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2483 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2484 c932eeeb 2019-05-22 stsp return NULL;
2485 c932eeeb 2019-05-22 stsp
2486 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2487 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2488 edd02c5e 2019-07-11 stsp ie->path);
2489 1ee397ad 2019-07-12 stsp if (err)
2490 1ee397ad 2019-07-12 stsp return err;
2491 1ee397ad 2019-07-12 stsp }
2492 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2493 c932eeeb 2019-05-22 stsp return NULL;
2494 a615e0e7 2020-12-16 stsp }
2495 a615e0e7 2020-12-16 stsp
2496 a615e0e7 2020-12-16 stsp static const struct got_error *
2497 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2498 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2499 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2500 a615e0e7 2020-12-16 stsp {
2501 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2502 a615e0e7 2020-12-16 stsp
2503 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2504 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2505 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2506 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2507 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2508 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2509 a615e0e7 2020-12-16 stsp
2510 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2511 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2512 9c6338c4 2019-06-02 stsp }
2513 9c6338c4 2019-06-02 stsp
2514 9c6338c4 2019-06-02 stsp static const struct got_error *
2515 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2516 9c6338c4 2019-06-02 stsp {
2517 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2518 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2519 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2520 867630bb 2020-01-17 stsp struct timespec timeout;
2521 9c6338c4 2019-06-02 stsp
2522 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2523 9c6338c4 2019-06-02 stsp fileindex_path);
2524 9c6338c4 2019-06-02 stsp if (err)
2525 9c6338c4 2019-06-02 stsp goto done;
2526 9c6338c4 2019-06-02 stsp
2527 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2528 9c6338c4 2019-06-02 stsp if (err)
2529 9c6338c4 2019-06-02 stsp goto done;
2530 9c6338c4 2019-06-02 stsp
2531 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2532 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2533 9c6338c4 2019-06-02 stsp fileindex_path);
2534 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2535 9c6338c4 2019-06-02 stsp }
2536 867630bb 2020-01-17 stsp
2537 867630bb 2020-01-17 stsp /*
2538 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2539 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2540 867630bb 2020-01-17 stsp * was recorded in the file index.
2541 867630bb 2020-01-17 stsp */
2542 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2543 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2544 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2545 9c6338c4 2019-06-02 stsp done:
2546 9c6338c4 2019-06-02 stsp if (new_index)
2547 9c6338c4 2019-06-02 stsp fclose(new_index);
2548 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2549 9c6338c4 2019-06-02 stsp return err;
2550 c932eeeb 2019-05-22 stsp }
2551 6ced4176 2019-07-12 stsp
2552 6ced4176 2019-07-12 stsp static const struct got_error *
2553 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2554 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2555 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2556 6ced4176 2019-07-12 stsp {
2557 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2558 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2559 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2560 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2561 6ced4176 2019-07-12 stsp
2562 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2563 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2564 6ced4176 2019-07-12 stsp *tree_id = NULL;
2565 6ced4176 2019-07-12 stsp
2566 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2567 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2568 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2569 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2570 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2571 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2572 6ced4176 2019-07-12 stsp goto done;
2573 6ced4176 2019-07-12 stsp }
2574 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2575 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2576 6ced4176 2019-07-12 stsp if (err)
2577 6ced4176 2019-07-12 stsp goto done;
2578 6ced4176 2019-07-12 stsp return NULL;
2579 6ced4176 2019-07-12 stsp }
2580 6ced4176 2019-07-12 stsp
2581 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2582 6ced4176 2019-07-12 stsp
2583 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2584 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2585 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2586 6ced4176 2019-07-12 stsp goto done;
2587 6ced4176 2019-07-12 stsp }
2588 6ced4176 2019-07-12 stsp
2589 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2590 6ced4176 2019-07-12 stsp in_repo_path);
2591 6ced4176 2019-07-12 stsp if (err)
2592 6ced4176 2019-07-12 stsp goto done;
2593 6ced4176 2019-07-12 stsp
2594 6ced4176 2019-07-12 stsp free(in_repo_path);
2595 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2596 6ced4176 2019-07-12 stsp
2597 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2598 6ced4176 2019-07-12 stsp if (err)
2599 6ced4176 2019-07-12 stsp goto done;
2600 c932eeeb 2019-05-22 stsp
2601 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2602 6ced4176 2019-07-12 stsp /* Check out a single file. */
2603 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2604 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2605 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2606 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2607 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2608 6ced4176 2019-07-12 stsp goto done;
2609 6ced4176 2019-07-12 stsp }
2610 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2611 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2612 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2613 6ced4176 2019-07-12 stsp goto done;
2614 6ced4176 2019-07-12 stsp }
2615 6ced4176 2019-07-12 stsp } else {
2616 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2617 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2618 6ced4176 2019-07-12 stsp if (err)
2619 6ced4176 2019-07-12 stsp return err;
2620 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2621 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2622 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2623 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2624 6ced4176 2019-07-12 stsp goto done;
2625 6ced4176 2019-07-12 stsp }
2626 6ced4176 2019-07-12 stsp }
2627 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2628 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2629 6ced4176 2019-07-12 stsp } else {
2630 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2631 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2632 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2633 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2634 6ced4176 2019-07-12 stsp goto done;
2635 6ced4176 2019-07-12 stsp }
2636 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2637 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2638 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2639 6ced4176 2019-07-12 stsp goto done;
2640 6ced4176 2019-07-12 stsp }
2641 6ced4176 2019-07-12 stsp }
2642 6ced4176 2019-07-12 stsp done:
2643 6ced4176 2019-07-12 stsp free(id);
2644 6ced4176 2019-07-12 stsp free(in_repo_path);
2645 6ced4176 2019-07-12 stsp if (err) {
2646 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2647 6ced4176 2019-07-12 stsp free(*tree_relpath);
2648 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2649 6ced4176 2019-07-12 stsp free(*tree_id);
2650 6ced4176 2019-07-12 stsp *tree_id = NULL;
2651 6ced4176 2019-07-12 stsp }
2652 07ed472d 2019-07-12 stsp return err;
2653 07ed472d 2019-07-12 stsp }
2654 07ed472d 2019-07-12 stsp
2655 07ed472d 2019-07-12 stsp static const struct got_error *
2656 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2657 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2658 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2659 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2660 07ed472d 2019-07-12 stsp {
2661 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2662 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2663 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2664 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2665 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2666 07ed472d 2019-07-12 stsp
2667 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2668 7f47418f 2019-12-20 stsp if (err) {
2669 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2670 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2671 7f47418f 2019-12-20 stsp goto done;
2672 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2673 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2674 7f47418f 2019-12-20 stsp if (err)
2675 7f47418f 2019-12-20 stsp return err;
2676 7f47418f 2019-12-20 stsp }
2677 07ed472d 2019-07-12 stsp
2678 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2679 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2680 07ed472d 2019-07-12 stsp if (err)
2681 07ed472d 2019-07-12 stsp goto done;
2682 07ed472d 2019-07-12 stsp
2683 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2684 07ed472d 2019-07-12 stsp if (err)
2685 07ed472d 2019-07-12 stsp goto done;
2686 07ed472d 2019-07-12 stsp
2687 07ed472d 2019-07-12 stsp if (entry_name &&
2688 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2689 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2690 07ed472d 2019-07-12 stsp goto done;
2691 07ed472d 2019-07-12 stsp }
2692 07ed472d 2019-07-12 stsp
2693 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2694 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2695 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2696 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2697 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2698 07ed472d 2019-07-12 stsp arg.repo = repo;
2699 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2700 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2701 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2702 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2703 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2704 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2705 07ed472d 2019-07-12 stsp done:
2706 07ed472d 2019-07-12 stsp if (tree)
2707 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2708 07ed472d 2019-07-12 stsp if (commit)
2709 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2710 6ced4176 2019-07-12 stsp return err;
2711 6ced4176 2019-07-12 stsp }
2712 6ced4176 2019-07-12 stsp
2713 9d31a1d8 2018-03-11 stsp const struct got_error *
2714 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2715 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2716 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2717 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2718 9d31a1d8 2018-03-11 stsp {
2719 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2720 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2721 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2722 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2723 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2724 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2725 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2726 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2727 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2728 f2ea84fa 2019-07-27 stsp int entry_type;
2729 f2ea84fa 2019-07-27 stsp char *relpath;
2730 f2ea84fa 2019-07-27 stsp char *entry_name;
2731 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2732 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2733 9d31a1d8 2018-03-11 stsp
2734 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2735 f2ea84fa 2019-07-27 stsp
2736 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2737 9d31a1d8 2018-03-11 stsp if (err)
2738 9d31a1d8 2018-03-11 stsp return err;
2739 93a30277 2018-12-24 stsp
2740 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2741 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2742 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2743 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2744 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2745 f2ea84fa 2019-07-27 stsp goto done;
2746 f2ea84fa 2019-07-27 stsp }
2747 6ced4176 2019-07-12 stsp
2748 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2749 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2750 f2ea84fa 2019-07-27 stsp if (err) {
2751 f2ea84fa 2019-07-27 stsp free(tpd);
2752 6ced4176 2019-07-12 stsp goto done;
2753 6ced4176 2019-07-12 stsp }
2754 f2ea84fa 2019-07-27 stsp
2755 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2756 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2757 f2ea84fa 2019-07-27 stsp if (err) {
2758 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2759 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2760 f2ea84fa 2019-07-27 stsp free(tpd);
2761 f2ea84fa 2019-07-27 stsp goto done;
2762 f2ea84fa 2019-07-27 stsp }
2763 f2ea84fa 2019-07-27 stsp } else
2764 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2765 f2ea84fa 2019-07-27 stsp
2766 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2767 6ced4176 2019-07-12 stsp }
2768 6ced4176 2019-07-12 stsp
2769 51514078 2018-12-25 stsp /*
2770 51514078 2018-12-25 stsp * Read the file index.
2771 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2772 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2773 51514078 2018-12-25 stsp */
2774 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2775 8da9e5f4 2019-01-12 stsp if (err)
2776 c4cdcb68 2019-04-03 stsp goto done;
2777 c4cdcb68 2019-04-03 stsp
2778 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2779 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2780 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2781 f2ea84fa 2019-07-27 stsp
2782 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2783 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2784 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2785 f2ea84fa 2019-07-27 stsp if (err)
2786 f2ea84fa 2019-07-27 stsp break;
2787 f2ea84fa 2019-07-27 stsp
2788 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2789 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2790 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2791 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2792 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2793 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2794 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2795 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2796 f2ea84fa 2019-07-27 stsp if (err)
2797 f2ea84fa 2019-07-27 stsp break;
2798 f2ea84fa 2019-07-27 stsp
2799 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2800 711d9cd9 2019-07-12 stsp }
2801 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2802 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2803 af12c6b9 2019-06-04 stsp err = sync_err;
2804 9d31a1d8 2018-03-11 stsp done:
2805 9c6338c4 2019-06-02 stsp free(fileindex_path);
2806 edfa7d7f 2018-09-11 stsp if (tree)
2807 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2808 9d31a1d8 2018-03-11 stsp if (commit)
2809 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2810 5ade8233 2019-07-12 stsp if (fileindex)
2811 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2812 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2813 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2814 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2815 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2816 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2817 f2ea84fa 2019-07-27 stsp free(tpd);
2818 f2ea84fa 2019-07-27 stsp }
2819 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2820 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2821 9d31a1d8 2018-03-11 stsp err = unlockerr;
2822 f8d1f275 2019-02-04 stsp return err;
2823 f8d1f275 2019-02-04 stsp }
2824 f8d1f275 2019-02-04 stsp
2825 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2826 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2827 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2828 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2829 234035bc 2019-06-01 stsp void *progress_arg;
2830 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2831 234035bc 2019-06-01 stsp void *cancel_arg;
2832 f69721c3 2019-10-21 stsp const char *label_orig;
2833 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2834 234035bc 2019-06-01 stsp };
2835 234035bc 2019-06-01 stsp
2836 234035bc 2019-06-01 stsp static const struct got_error *
2837 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2838 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2839 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2840 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2841 234035bc 2019-06-01 stsp {
2842 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2843 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2844 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2845 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2846 234035bc 2019-06-01 stsp struct stat sb;
2847 234035bc 2019-06-01 stsp unsigned char status;
2848 234035bc 2019-06-01 stsp int local_changes_subsumed;
2849 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2850 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2851 234035bc 2019-06-01 stsp
2852 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2853 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2854 d6c87207 2019-08-02 stsp strlen(path2));
2855 1ee397ad 2019-07-12 stsp if (ie == NULL)
2856 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2857 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2858 234035bc 2019-06-01 stsp
2859 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2860 234035bc 2019-06-01 stsp path2) == -1)
2861 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2862 234035bc 2019-06-01 stsp
2863 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2864 7f91a133 2019-12-13 stsp repo);
2865 234035bc 2019-06-01 stsp if (err)
2866 234035bc 2019-06-01 stsp goto done;
2867 234035bc 2019-06-01 stsp
2868 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2869 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2870 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2871 234035bc 2019-06-01 stsp goto done;
2872 234035bc 2019-06-01 stsp }
2873 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2874 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2875 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2876 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2877 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2878 234035bc 2019-06-01 stsp goto done;
2879 234035bc 2019-06-01 stsp }
2880 234035bc 2019-06-01 stsp
2881 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2882 36bf999c 2020-07-23 stsp char *link_target2;
2883 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2884 36bf999c 2020-07-23 stsp if (err)
2885 36bf999c 2020-07-23 stsp goto done;
2886 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2887 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2888 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2889 36bf999c 2020-07-23 stsp free(link_target2);
2890 af57b12a 2020-07-23 stsp } else {
2891 1af628f4 2021-06-03 stsp int fd;
2892 1af628f4 2021-06-03 stsp
2893 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2894 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2895 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2896 1af628f4 2021-06-03 stsp goto done;
2897 1af628f4 2021-06-03 stsp }
2898 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2899 1af628f4 2021-06-03 stsp f_orig, blob1);
2900 1af628f4 2021-06-03 stsp if (err)
2901 1af628f4 2021-06-03 stsp goto done;
2902 1af628f4 2021-06-03 stsp
2903 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2904 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2905 1af628f4 2021-06-03 stsp goto done;
2906 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2907 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2908 1af628f4 2021-06-03 stsp if (err)
2909 1af628f4 2021-06-03 stsp goto done;
2910 1af628f4 2021-06-03 stsp
2911 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2912 1af628f4 2021-06-03 stsp if (fd == -1) {
2913 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2914 1af628f4 2021-06-03 stsp ondisk_path);
2915 1af628f4 2021-06-03 stsp goto done;
2916 1af628f4 2021-06-03 stsp }
2917 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2918 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2919 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2920 1af628f4 2021-06-03 stsp ondisk_path);
2921 1af628f4 2021-06-03 stsp close(fd);
2922 1af628f4 2021-06-03 stsp goto done;
2923 1af628f4 2021-06-03 stsp }
2924 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2925 1af628f4 2021-06-03 stsp if (err)
2926 1af628f4 2021-06-03 stsp goto done;
2927 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2928 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2929 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2930 1af628f4 2021-06-03 stsp goto done;
2931 1af628f4 2021-06-03 stsp }
2932 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2933 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2934 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2935 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2936 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2937 af57b12a 2020-07-23 stsp }
2938 234035bc 2019-06-01 stsp } else if (blob1) {
2939 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2940 d6c87207 2019-08-02 stsp strlen(path1));
2941 1ee397ad 2019-07-12 stsp if (ie == NULL)
2942 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2943 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2944 2b92fad7 2019-06-02 stsp
2945 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2946 2b92fad7 2019-06-02 stsp path1) == -1)
2947 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2948 2b92fad7 2019-06-02 stsp
2949 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2950 7f91a133 2019-12-13 stsp repo);
2951 2b92fad7 2019-06-02 stsp if (err)
2952 2b92fad7 2019-06-02 stsp goto done;
2953 2b92fad7 2019-06-02 stsp
2954 2b92fad7 2019-06-02 stsp switch (status) {
2955 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2956 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2957 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2958 1ee397ad 2019-07-12 stsp if (err)
2959 1ee397ad 2019-07-12 stsp goto done;
2960 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2961 2b92fad7 2019-06-02 stsp if (err)
2962 2b92fad7 2019-06-02 stsp goto done;
2963 2b92fad7 2019-06-02 stsp if (ie)
2964 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2965 2b92fad7 2019-06-02 stsp break;
2966 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2967 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2968 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2969 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2970 1ee397ad 2019-07-12 stsp if (err)
2971 1ee397ad 2019-07-12 stsp goto done;
2972 2b92fad7 2019-06-02 stsp if (ie)
2973 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2974 2b92fad7 2019-06-02 stsp break;
2975 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2976 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2977 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2978 0a22ca1a 2020-09-23 stsp /*
2979 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2980 0a22ca1a 2020-09-23 stsp * exists in the repository.
2981 0a22ca1a 2020-09-23 stsp */
2982 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2983 0a22ca1a 2020-09-23 stsp if (err)
2984 0a22ca1a 2020-09-23 stsp goto done;
2985 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2986 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2987 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2988 0a22ca1a 2020-09-23 stsp if (err)
2989 0a22ca1a 2020-09-23 stsp goto done;
2990 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2991 0a22ca1a 2020-09-23 stsp path1);
2992 0a22ca1a 2020-09-23 stsp if (err)
2993 0a22ca1a 2020-09-23 stsp goto done;
2994 0a22ca1a 2020-09-23 stsp if (ie)
2995 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2996 0a22ca1a 2020-09-23 stsp ie);
2997 0a22ca1a 2020-09-23 stsp } else {
2998 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2999 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3000 0a22ca1a 2020-09-23 stsp }
3001 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3002 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3003 0a22ca1a 2020-09-23 stsp free(id);
3004 0a22ca1a 2020-09-23 stsp if (err)
3005 0a22ca1a 2020-09-23 stsp goto done;
3006 0a22ca1a 2020-09-23 stsp break;
3007 0a22ca1a 2020-09-23 stsp }
3008 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3009 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3010 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3011 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3012 1ee397ad 2019-07-12 stsp if (err)
3013 1ee397ad 2019-07-12 stsp goto done;
3014 2b92fad7 2019-06-02 stsp break;
3015 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3016 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3017 1ee397ad 2019-07-12 stsp if (err)
3018 1ee397ad 2019-07-12 stsp goto done;
3019 2b92fad7 2019-06-02 stsp break;
3020 2b92fad7 2019-06-02 stsp default:
3021 2b92fad7 2019-06-02 stsp break;
3022 2b92fad7 2019-06-02 stsp }
3023 234035bc 2019-06-01 stsp } else if (blob2) {
3024 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3025 234035bc 2019-06-01 stsp path2) == -1)
3026 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3027 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3028 d6c87207 2019-08-02 stsp strlen(path2));
3029 234035bc 2019-06-01 stsp if (ie) {
3030 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3031 7f91a133 2019-12-13 stsp -1, NULL, repo);
3032 234035bc 2019-06-01 stsp if (err)
3033 234035bc 2019-06-01 stsp goto done;
3034 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3035 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3036 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3037 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3039 1ee397ad 2019-07-12 stsp status, path2);
3040 234035bc 2019-06-01 stsp goto done;
3041 234035bc 2019-06-01 stsp }
3042 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3043 36bf999c 2020-07-23 stsp char *link_target2;
3044 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3045 36bf999c 2020-07-23 stsp blob2);
3046 36bf999c 2020-07-23 stsp if (err)
3047 36bf999c 2020-07-23 stsp goto done;
3048 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3049 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3050 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3051 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3052 36bf999c 2020-07-23 stsp free(link_target2);
3053 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3054 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3055 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3056 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3057 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3058 526a746f 2020-07-23 stsp a->progress_arg);
3059 dfe9fba0 2020-07-23 stsp } else {
3060 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3061 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3062 526a746f 2020-07-23 stsp }
3063 dfe9fba0 2020-07-23 stsp if (err)
3064 dfe9fba0 2020-07-23 stsp goto done;
3065 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3066 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3067 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3068 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3069 234035bc 2019-06-01 stsp if (err)
3070 234035bc 2019-06-01 stsp goto done;
3071 234035bc 2019-06-01 stsp }
3072 234035bc 2019-06-01 stsp } else {
3073 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3074 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3075 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3076 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3077 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3078 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3079 2e1fa222 2020-07-23 stsp } else {
3080 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3081 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3082 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3083 2e1fa222 2020-07-23 stsp }
3084 234035bc 2019-06-01 stsp if (err)
3085 234035bc 2019-06-01 stsp goto done;
3086 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3087 234035bc 2019-06-01 stsp if (err)
3088 234035bc 2019-06-01 stsp goto done;
3089 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3090 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3091 3969253a 2020-03-07 stsp if (err) {
3092 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3093 3969253a 2020-03-07 stsp goto done;
3094 3969253a 2020-03-07 stsp }
3095 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3096 2b92fad7 2019-06-02 stsp if (err) {
3097 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3098 2b92fad7 2019-06-02 stsp goto done;
3099 2b92fad7 2019-06-02 stsp }
3100 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3101 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3102 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3103 2e1fa222 2020-07-23 stsp }
3104 234035bc 2019-06-01 stsp }
3105 234035bc 2019-06-01 stsp }
3106 234035bc 2019-06-01 stsp done:
3107 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3108 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3109 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3110 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3111 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3112 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3113 1af628f4 2021-06-03 stsp free(id_str);
3114 54d5be07 2021-06-03 stsp free(label_deriv2);
3115 234035bc 2019-06-01 stsp free(ondisk_path);
3116 234035bc 2019-06-01 stsp return err;
3117 234035bc 2019-06-01 stsp }
3118 234035bc 2019-06-01 stsp
3119 69de9dd4 2021-09-03 stsp static const struct got_error *
3120 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3121 69de9dd4 2021-09-03 stsp {
3122 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3123 69de9dd4 2021-09-03 stsp
3124 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3125 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3126 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3127 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3128 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3129 69de9dd4 2021-09-03 stsp
3130 69de9dd4 2021-09-03 stsp return NULL;
3131 69de9dd4 2021-09-03 stsp }
3132 69de9dd4 2021-09-03 stsp
3133 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3134 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3135 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3136 234035bc 2019-06-01 stsp struct got_repository *repo;
3137 234035bc 2019-06-01 stsp };
3138 234035bc 2019-06-01 stsp
3139 234035bc 2019-06-01 stsp static const struct got_error *
3140 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3141 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3142 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3143 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3144 234035bc 2019-06-01 stsp {
3145 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3146 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3147 234035bc 2019-06-01 stsp unsigned char status;
3148 234035bc 2019-06-01 stsp struct stat sb;
3149 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3150 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3151 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3152 234035bc 2019-06-01 stsp char *ondisk_path;
3153 234035bc 2019-06-01 stsp
3154 69de9dd4 2021-09-03 stsp if (id == NULL)
3155 69de9dd4 2021-09-03 stsp return NULL;
3156 234035bc 2019-06-01 stsp
3157 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3158 69de9dd4 2021-09-03 stsp if (ie == NULL)
3159 69de9dd4 2021-09-03 stsp return NULL;
3160 69de9dd4 2021-09-03 stsp
3161 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3162 234035bc 2019-06-01 stsp == -1)
3163 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3164 234035bc 2019-06-01 stsp
3165 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3166 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3167 5546d466 2021-09-02 stsp free(ondisk_path);
3168 234035bc 2019-06-01 stsp if (err)
3169 234035bc 2019-06-01 stsp return err;
3170 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3171 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3172 234035bc 2019-06-01 stsp
3173 234035bc 2019-06-01 stsp return NULL;
3174 234035bc 2019-06-01 stsp }
3175 234035bc 2019-06-01 stsp
3176 818c7501 2019-07-11 stsp static const struct got_error *
3177 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3178 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3179 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3180 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3181 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3182 234035bc 2019-06-01 stsp {
3183 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3184 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3187 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3188 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3189 234035bc 2019-06-01 stsp
3190 03415a1a 2019-06-02 stsp if (commit_id1) {
3191 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3192 03415a1a 2019-06-02 stsp worktree->path_prefix);
3193 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3194 03415a1a 2019-06-02 stsp goto done;
3195 69d57f3d 2020-07-31 stsp }
3196 69d57f3d 2020-07-31 stsp if (tree_id1) {
3197 69d57f3d 2020-07-31 stsp char *id_str;
3198 03415a1a 2019-06-02 stsp
3199 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3200 03415a1a 2019-06-02 stsp if (err)
3201 03415a1a 2019-06-02 stsp goto done;
3202 f69721c3 2019-10-21 stsp
3203 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3204 f69721c3 2019-10-21 stsp if (err)
3205 f69721c3 2019-10-21 stsp goto done;
3206 f69721c3 2019-10-21 stsp
3207 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3208 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3209 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3210 f69721c3 2019-10-21 stsp free(id_str);
3211 f69721c3 2019-10-21 stsp goto done;
3212 f69721c3 2019-10-21 stsp }
3213 f69721c3 2019-10-21 stsp free(id_str);
3214 03415a1a 2019-06-02 stsp }
3215 234035bc 2019-06-01 stsp
3216 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3217 234035bc 2019-06-01 stsp worktree->path_prefix);
3218 234035bc 2019-06-01 stsp if (err)
3219 234035bc 2019-06-01 stsp goto done;
3220 234035bc 2019-06-01 stsp
3221 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3222 234035bc 2019-06-01 stsp if (err)
3223 234035bc 2019-06-01 stsp goto done;
3224 234035bc 2019-06-01 stsp
3225 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3226 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3227 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3228 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3229 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3230 69de9dd4 2021-09-03 stsp if (err)
3231 69de9dd4 2021-09-03 stsp goto done;
3232 69de9dd4 2021-09-03 stsp
3233 234035bc 2019-06-01 stsp arg.worktree = worktree;
3234 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3235 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3236 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3237 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3238 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3239 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3240 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3241 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3242 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3243 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3244 af12c6b9 2019-06-04 stsp err = sync_err;
3245 234035bc 2019-06-01 stsp done:
3246 234035bc 2019-06-01 stsp if (tree1)
3247 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3248 234035bc 2019-06-01 stsp if (tree2)
3249 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3250 f69721c3 2019-10-21 stsp free(label_orig);
3251 818c7501 2019-07-11 stsp return err;
3252 818c7501 2019-07-11 stsp }
3253 234035bc 2019-06-01 stsp
3254 818c7501 2019-07-11 stsp const struct got_error *
3255 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3256 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3257 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3258 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3259 818c7501 2019-07-11 stsp {
3260 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3261 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3262 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3263 818c7501 2019-07-11 stsp
3264 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3265 818c7501 2019-07-11 stsp if (err)
3266 818c7501 2019-07-11 stsp return err;
3267 818c7501 2019-07-11 stsp
3268 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3269 818c7501 2019-07-11 stsp if (err)
3270 818c7501 2019-07-11 stsp goto done;
3271 818c7501 2019-07-11 stsp
3272 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3273 69de9dd4 2021-09-03 stsp worktree);
3274 818c7501 2019-07-11 stsp if (err)
3275 818c7501 2019-07-11 stsp goto done;
3276 818c7501 2019-07-11 stsp
3277 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3278 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3279 818c7501 2019-07-11 stsp done:
3280 818c7501 2019-07-11 stsp if (fileindex)
3281 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3282 818c7501 2019-07-11 stsp free(fileindex_path);
3283 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3284 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3285 234035bc 2019-06-01 stsp err = unlockerr;
3286 234035bc 2019-06-01 stsp return err;
3287 234035bc 2019-06-01 stsp }
3288 234035bc 2019-06-01 stsp
3289 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3290 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3291 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3292 927df6b7 2019-02-10 stsp const char *status_path;
3293 927df6b7 2019-02-10 stsp size_t status_path_len;
3294 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3295 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3296 f8d1f275 2019-02-04 stsp void *status_arg;
3297 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3298 f8d1f275 2019-02-04 stsp void *cancel_arg;
3299 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3300 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3301 f2a9dc41 2019-12-13 tracey int report_unchanged;
3302 3143d852 2020-06-25 stsp int no_ignores;
3303 f8d1f275 2019-02-04 stsp };
3304 88d0e355 2019-08-03 stsp
3305 f8d1f275 2019-02-04 stsp static const struct got_error *
3306 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3307 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3308 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3309 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3310 927df6b7 2019-02-10 stsp {
3311 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3312 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3313 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3314 927df6b7 2019-02-10 stsp struct stat sb;
3315 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3316 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3317 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3318 927df6b7 2019-02-10 stsp
3319 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3320 98eaaa12 2019-08-03 stsp if (err)
3321 98eaaa12 2019-08-03 stsp return err;
3322 98eaaa12 2019-08-03 stsp
3323 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3324 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3325 98eaaa12 2019-08-03 stsp return NULL;
3326 98eaaa12 2019-08-03 stsp
3327 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3328 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3329 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3330 98eaaa12 2019-08-03 stsp }
3331 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3332 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3333 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3334 927df6b7 2019-02-10 stsp }
3335 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3336 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3337 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3338 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3339 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3340 98eaaa12 2019-08-03 stsp }
3341 98eaaa12 2019-08-03 stsp
3342 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3343 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3344 927df6b7 2019-02-10 stsp }
3345 927df6b7 2019-02-10 stsp
3346 927df6b7 2019-02-10 stsp static const struct got_error *
3347 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3348 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3349 f8d1f275 2019-02-04 stsp {
3350 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3351 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3352 f8d1f275 2019-02-04 stsp char *abspath;
3353 f8d1f275 2019-02-04 stsp
3354 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3355 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3356 0584f854 2019-04-06 stsp
3357 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3358 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3359 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3360 927df6b7 2019-02-10 stsp return NULL;
3361 927df6b7 2019-02-10 stsp
3362 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3363 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3364 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3365 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3366 f8d1f275 2019-02-04 stsp } else {
3367 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3368 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3369 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3370 f8d1f275 2019-02-04 stsp }
3371 f8d1f275 2019-02-04 stsp
3372 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3373 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3374 f8d1f275 2019-02-04 stsp free(abspath);
3375 9d31a1d8 2018-03-11 stsp return err;
3376 9d31a1d8 2018-03-11 stsp }
3377 f8d1f275 2019-02-04 stsp
3378 f8d1f275 2019-02-04 stsp static const struct got_error *
3379 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3380 f8d1f275 2019-02-04 stsp {
3381 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3382 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3383 2ec1f75b 2019-03-26 stsp unsigned char status;
3384 927df6b7 2019-02-10 stsp
3385 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3386 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3387 0584f854 2019-04-06 stsp
3388 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3389 927df6b7 2019-02-10 stsp return NULL;
3390 927df6b7 2019-02-10 stsp
3391 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3392 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3393 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3394 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3395 2ec1f75b 2019-03-26 stsp else
3396 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3397 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3398 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3399 6841da00 2019-08-08 stsp }
3400 6841da00 2019-08-08 stsp
3401 6841da00 2019-08-08 stsp void
3402 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3403 6841da00 2019-08-08 stsp {
3404 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3405 6841da00 2019-08-08 stsp
3406 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3407 6841da00 2019-08-08 stsp free((char *)pe->path);
3408 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3409 6841da00 2019-08-08 stsp }
3410 6841da00 2019-08-08 stsp
3411 6841da00 2019-08-08 stsp void
3412 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3413 6841da00 2019-08-08 stsp {
3414 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3415 6841da00 2019-08-08 stsp
3416 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3417 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3418 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3419 6841da00 2019-08-08 stsp free((char *)pe->path);
3420 6841da00 2019-08-08 stsp }
3421 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3422 6841da00 2019-08-08 stsp }
3423 6841da00 2019-08-08 stsp
3424 6841da00 2019-08-08 stsp static const struct got_error *
3425 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3426 6841da00 2019-08-08 stsp {
3427 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3428 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3429 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3430 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3431 6841da00 2019-08-08 stsp size_t linesize = 0;
3432 6841da00 2019-08-08 stsp ssize_t linelen;
3433 6841da00 2019-08-08 stsp
3434 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3435 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3436 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3437 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3438 6841da00 2019-08-08 stsp
3439 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3440 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3441 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3442 bd8de430 2019-10-04 stsp
3443 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3444 bd8de430 2019-10-04 stsp if (line[0] == '#')
3445 bd8de430 2019-10-04 stsp continue;
3446 bd8de430 2019-10-04 stsp
3447 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3448 bd8de430 2019-10-04 stsp if (line[0] == '!')
3449 bd8de430 2019-10-04 stsp continue;
3450 bd8de430 2019-10-04 stsp
3451 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3452 6841da00 2019-08-08 stsp line) == -1) {
3453 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3454 6841da00 2019-08-08 stsp goto done;
3455 6841da00 2019-08-08 stsp }
3456 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3457 6841da00 2019-08-08 stsp if (err)
3458 6841da00 2019-08-08 stsp goto done;
3459 6841da00 2019-08-08 stsp }
3460 6841da00 2019-08-08 stsp if (ferror(f)) {
3461 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3462 6841da00 2019-08-08 stsp goto done;
3463 6841da00 2019-08-08 stsp }
3464 6841da00 2019-08-08 stsp
3465 6841da00 2019-08-08 stsp dirpath = strdup(path);
3466 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3467 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3468 6841da00 2019-08-08 stsp goto done;
3469 6841da00 2019-08-08 stsp }
3470 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3471 6841da00 2019-08-08 stsp done:
3472 6841da00 2019-08-08 stsp free(line);
3473 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3474 6841da00 2019-08-08 stsp free(dirpath);
3475 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3476 6841da00 2019-08-08 stsp }
3477 6841da00 2019-08-08 stsp return err;
3478 6841da00 2019-08-08 stsp }
3479 6841da00 2019-08-08 stsp
3480 6841da00 2019-08-08 stsp int
3481 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3482 6841da00 2019-08-08 stsp {
3483 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3484 bd8de430 2019-10-04 stsp
3485 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3486 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3487 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3488 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3489 bd8de430 2019-10-04 stsp
3490 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3491 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3492 6841da00 2019-08-08 stsp
3493 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3494 bd8de430 2019-10-04 stsp continue;
3495 bd8de430 2019-10-04 stsp pattern += 3;
3496 bd8de430 2019-10-04 stsp p = path;
3497 bd8de430 2019-10-04 stsp while (*p) {
3498 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3499 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3500 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3501 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3502 bd8de430 2019-10-04 stsp p++;
3503 bd8de430 2019-10-04 stsp while (*p == '/')
3504 bd8de430 2019-10-04 stsp p++;
3505 bd8de430 2019-10-04 stsp continue;
3506 bd8de430 2019-10-04 stsp }
3507 bd8de430 2019-10-04 stsp return 1;
3508 bd8de430 2019-10-04 stsp }
3509 bd8de430 2019-10-04 stsp }
3510 bd8de430 2019-10-04 stsp }
3511 bd8de430 2019-10-04 stsp
3512 6841da00 2019-08-08 stsp /*
3513 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3514 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3515 6841da00 2019-08-08 stsp * ignores backwards.
3516 6841da00 2019-08-08 stsp */
3517 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3518 6841da00 2019-08-08 stsp while (pe) {
3519 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3520 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3521 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3522 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3523 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3524 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3525 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3526 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3527 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3528 6841da00 2019-08-08 stsp continue;
3529 6841da00 2019-08-08 stsp return 1;
3530 6841da00 2019-08-08 stsp }
3531 6841da00 2019-08-08 stsp }
3532 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3533 6841da00 2019-08-08 stsp }
3534 6841da00 2019-08-08 stsp
3535 6841da00 2019-08-08 stsp return 0;
3536 6841da00 2019-08-08 stsp }
3537 6841da00 2019-08-08 stsp
3538 6841da00 2019-08-08 stsp static const struct got_error *
3539 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3540 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3541 6841da00 2019-08-08 stsp {
3542 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3543 6841da00 2019-08-08 stsp char *ignorespath;
3544 886cec17 2019-12-15 stsp int fd = -1;
3545 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3546 6841da00 2019-08-08 stsp
3547 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3548 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3549 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3550 6841da00 2019-08-08 stsp
3551 886cec17 2019-12-15 stsp if (dirfd != -1) {
3552 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3553 886cec17 2019-12-15 stsp if (fd == -1) {
3554 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3555 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3556 886cec17 2019-12-15 stsp ignorespath);
3557 886cec17 2019-12-15 stsp } else {
3558 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3559 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3560 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3561 886cec17 2019-12-15 stsp ignorespath);
3562 886cec17 2019-12-15 stsp else {
3563 886cec17 2019-12-15 stsp fd = -1;
3564 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3565 886cec17 2019-12-15 stsp }
3566 886cec17 2019-12-15 stsp }
3567 886cec17 2019-12-15 stsp } else {
3568 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3569 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3570 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3571 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3572 886cec17 2019-12-15 stsp ignorespath);
3573 886cec17 2019-12-15 stsp } else
3574 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3575 886cec17 2019-12-15 stsp }
3576 6841da00 2019-08-08 stsp
3577 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3578 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3579 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3580 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3581 6841da00 2019-08-08 stsp free(ignorespath);
3582 6841da00 2019-08-08 stsp return err;
3583 f8d1f275 2019-02-04 stsp }
3584 f8d1f275 2019-02-04 stsp
3585 f8d1f275 2019-02-04 stsp static const struct got_error *
3586 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3587 f8d1f275 2019-02-04 stsp {
3588 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3589 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3590 f8d1f275 2019-02-04 stsp char *path = NULL;
3591 f8d1f275 2019-02-04 stsp
3592 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3593 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3594 0584f854 2019-04-06 stsp
3595 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3596 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3597 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3598 f8d1f275 2019-02-04 stsp } else {
3599 f8d1f275 2019-02-04 stsp path = de->d_name;
3600 f8d1f275 2019-02-04 stsp }
3601 f8d1f275 2019-02-04 stsp
3602 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3603 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3604 ff56836b 2021-07-08 stsp && !match_ignores(a->ignores, path))
3605 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3606 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3607 f8d1f275 2019-02-04 stsp if (parent_path[0])
3608 f8d1f275 2019-02-04 stsp free(path);
3609 b72f483a 2019-02-05 stsp return err;
3610 f8d1f275 2019-02-04 stsp }
3611 f8d1f275 2019-02-04 stsp
3612 347d1d3e 2019-07-12 stsp static const struct got_error *
3613 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3614 3143d852 2020-06-25 stsp {
3615 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3616 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3617 3143d852 2020-06-25 stsp
3618 3143d852 2020-06-25 stsp if (a->no_ignores)
3619 3143d852 2020-06-25 stsp return NULL;
3620 3143d852 2020-06-25 stsp
3621 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3622 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3623 3143d852 2020-06-25 stsp if (err)
3624 3143d852 2020-06-25 stsp return err;
3625 3143d852 2020-06-25 stsp
3626 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3627 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3628 3143d852 2020-06-25 stsp
3629 3143d852 2020-06-25 stsp return err;
3630 3143d852 2020-06-25 stsp }
3631 3143d852 2020-06-25 stsp
3632 3143d852 2020-06-25 stsp static const struct got_error *
3633 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3634 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3635 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3636 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3637 abb4604f 2019-07-27 stsp {
3638 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3639 abb4604f 2019-07-27 stsp struct stat sb;
3640 abb4604f 2019-07-27 stsp
3641 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3642 ff56836b 2021-07-08 stsp return NULL;
3643 ff56836b 2021-07-08 stsp
3644 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3645 abb4604f 2019-07-27 stsp if (ie)
3646 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3647 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3648 abb4604f 2019-07-27 stsp
3649 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3650 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3651 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3652 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3653 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3654 abb4604f 2019-07-27 stsp return NULL;
3655 abb4604f 2019-07-27 stsp }
3656 abb4604f 2019-07-27 stsp
3657 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3658 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3659 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3660 abb4604f 2019-07-27 stsp
3661 abb4604f 2019-07-27 stsp return NULL;
3662 3143d852 2020-06-25 stsp }
3663 3143d852 2020-06-25 stsp
3664 3143d852 2020-06-25 stsp static const struct got_error *
3665 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3666 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3667 3143d852 2020-06-25 stsp {
3668 3143d852 2020-06-25 stsp const struct got_error *err;
3669 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3670 3143d852 2020-06-25 stsp
3671 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3672 3143d852 2020-06-25 stsp ".cvsignore");
3673 3143d852 2020-06-25 stsp if (err)
3674 3143d852 2020-06-25 stsp return err;
3675 3143d852 2020-06-25 stsp
3676 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3677 3143d852 2020-06-25 stsp ".gitignore");
3678 3143d852 2020-06-25 stsp if (err)
3679 3143d852 2020-06-25 stsp return err;
3680 3143d852 2020-06-25 stsp
3681 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3682 3143d852 2020-06-25 stsp if (err) {
3683 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3684 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3685 3143d852 2020-06-25 stsp return err;
3686 3143d852 2020-06-25 stsp }
3687 3143d852 2020-06-25 stsp for (;;) {
3688 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3689 3143d852 2020-06-25 stsp ".cvsignore");
3690 3143d852 2020-06-25 stsp if (err)
3691 3143d852 2020-06-25 stsp break;
3692 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3693 3143d852 2020-06-25 stsp ".gitignore");
3694 3143d852 2020-06-25 stsp if (err)
3695 3143d852 2020-06-25 stsp break;
3696 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3697 3143d852 2020-06-25 stsp if (err) {
3698 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3699 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3700 3143d852 2020-06-25 stsp break;
3701 3143d852 2020-06-25 stsp }
3702 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3703 ff56836b 2021-07-08 stsp break;
3704 b737c85a 2020-06-26 stsp free(parent_path);
3705 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3706 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3707 3143d852 2020-06-25 stsp }
3708 3143d852 2020-06-25 stsp
3709 b737c85a 2020-06-26 stsp free(parent_path);
3710 b737c85a 2020-06-26 stsp free(next_parent_path);
3711 3143d852 2020-06-25 stsp return err;
3712 abb4604f 2019-07-27 stsp }
3713 abb4604f 2019-07-27 stsp
3714 abb4604f 2019-07-27 stsp static const struct got_error *
3715 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3716 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3717 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3718 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3719 f2a9dc41 2019-12-13 tracey int report_unchanged)
3720 f8d1f275 2019-02-04 stsp {
3721 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3722 6fc93f37 2019-12-13 stsp int fd = -1;
3723 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3724 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3725 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3726 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3727 3143d852 2020-06-25 stsp
3728 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3729 f8d1f275 2019-02-04 stsp
3730 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3731 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3732 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3733 8dc303cc 2019-07-27 stsp
3734 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3735 6fc93f37 2019-12-13 stsp if (fd == -1) {
3736 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3737 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3738 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3739 ff56836b 2021-07-08 stsp else {
3740 ff56836b 2021-07-08 stsp if (!no_ignores) {
3741 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3742 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3743 ff56836b 2021-07-08 stsp if (err)
3744 ff56836b 2021-07-08 stsp goto done;
3745 ff56836b 2021-07-08 stsp }
3746 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3747 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3748 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3749 ff56836b 2021-07-08 stsp }
3750 abb4604f 2019-07-27 stsp } else {
3751 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3752 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3753 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3754 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3755 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3756 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3757 abb4604f 2019-07-27 stsp arg.status_path = path;
3758 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3759 abb4604f 2019-07-27 stsp arg.repo = repo;
3760 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3761 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3762 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3763 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3764 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3765 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3766 022fae89 2019-12-06 tracey if (!no_ignores) {
3767 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3768 3143d852 2020-06-25 stsp worktree->root_path, path);
3769 3143d852 2020-06-25 stsp if (err)
3770 3143d852 2020-06-25 stsp goto done;
3771 022fae89 2019-12-06 tracey }
3772 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3773 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3774 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3775 927df6b7 2019-02-10 stsp }
3776 3143d852 2020-06-25 stsp done:
3777 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3778 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3779 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3780 927df6b7 2019-02-10 stsp free(ondisk_path);
3781 347d1d3e 2019-07-12 stsp return err;
3782 347d1d3e 2019-07-12 stsp }
3783 347d1d3e 2019-07-12 stsp
3784 347d1d3e 2019-07-12 stsp const struct got_error *
3785 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3786 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3787 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3788 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3789 347d1d3e 2019-07-12 stsp {
3790 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3791 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3792 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3793 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3794 347d1d3e 2019-07-12 stsp
3795 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3796 347d1d3e 2019-07-12 stsp if (err)
3797 347d1d3e 2019-07-12 stsp return err;
3798 347d1d3e 2019-07-12 stsp
3799 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3800 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3801 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3802 f6343036 2021-06-22 stsp no_ignores, 0);
3803 72ea6654 2019-07-27 stsp if (err)
3804 72ea6654 2019-07-27 stsp break;
3805 72ea6654 2019-07-27 stsp }
3806 f8d1f275 2019-02-04 stsp free(fileindex_path);
3807 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3808 f8d1f275 2019-02-04 stsp return err;
3809 f8d1f275 2019-02-04 stsp }
3810 6c7ab921 2019-03-18 stsp
3811 6c7ab921 2019-03-18 stsp const struct got_error *
3812 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3813 6c7ab921 2019-03-18 stsp const char *arg)
3814 6c7ab921 2019-03-18 stsp {
3815 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3816 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3817 6c7ab921 2019-03-18 stsp size_t len;
3818 00bb5ea0 2020-07-23 stsp struct stat sb;
3819 01740607 2020-11-04 stsp char *abspath = NULL;
3820 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3821 6c7ab921 2019-03-18 stsp
3822 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3823 6c7ab921 2019-03-18 stsp
3824 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3825 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3826 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3827 00bb5ea0 2020-07-23 stsp
3828 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3829 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3830 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3831 00bb5ea0 2020-07-23 stsp goto done;
3832 00bb5ea0 2020-07-23 stsp }
3833 727173c3 2020-11-06 stsp sb.st_mode = 0;
3834 00bb5ea0 2020-07-23 stsp }
3835 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3836 00bb5ea0 2020-07-23 stsp /*
3837 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3838 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3839 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3840 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3841 00bb5ea0 2020-07-23 stsp */
3842 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3843 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3844 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3845 00bb5ea0 2020-07-23 stsp goto done;
3846 00bb5ea0 2020-07-23 stsp }
3847 00bb5ea0 2020-07-23 stsp
3848 00bb5ea0 2020-07-23 stsp }
3849 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3850 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3851 00bb5ea0 2020-07-23 stsp if (err)
3852 d0710d08 2019-07-22 stsp goto done;
3853 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3854 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3855 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3856 00bb5ea0 2020-07-23 stsp goto done;
3857 00bb5ea0 2020-07-23 stsp }
3858 00bb5ea0 2020-07-23 stsp } else {
3859 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3860 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3861 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3862 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3863 00bb5ea0 2020-07-23 stsp goto done;
3864 00bb5ea0 2020-07-23 stsp }
3865 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3866 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3867 01740607 2020-11-04 stsp goto done;
3868 01740607 2020-11-04 stsp }
3869 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3870 01740607 2020-11-04 stsp sizeof(canonpath));
3871 01740607 2020-11-04 stsp if (err)
3872 00bb5ea0 2020-07-23 stsp goto done;
3873 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3874 01740607 2020-11-04 stsp if (resolved == NULL) {
3875 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3876 01740607 2020-11-04 stsp goto done;
3877 00bb5ea0 2020-07-23 stsp }
3878 d0710d08 2019-07-22 stsp }
3879 d0710d08 2019-07-22 stsp }
3880 6c7ab921 2019-03-18 stsp
3881 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3882 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3883 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3884 6c7ab921 2019-03-18 stsp goto done;
3885 6c7ab921 2019-03-18 stsp }
3886 6c7ab921 2019-03-18 stsp
3887 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3888 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3889 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3890 f6d88e1a 2019-05-29 stsp if (err)
3891 f6d88e1a 2019-05-29 stsp goto done;
3892 f6d88e1a 2019-05-29 stsp } else {
3893 f6d88e1a 2019-05-29 stsp path = strdup("");
3894 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3895 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3896 f6d88e1a 2019-05-29 stsp goto done;
3897 f6d88e1a 2019-05-29 stsp }
3898 6c7ab921 2019-03-18 stsp }
3899 6c7ab921 2019-03-18 stsp
3900 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3901 6c7ab921 2019-03-18 stsp len = strlen(path);
3902 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3903 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3904 6c7ab921 2019-03-18 stsp len--;
3905 6c7ab921 2019-03-18 stsp }
3906 6c7ab921 2019-03-18 stsp done:
3907 01740607 2020-11-04 stsp free(abspath);
3908 6c7ab921 2019-03-18 stsp free(resolved);
3909 d0710d08 2019-07-22 stsp free(cwd);
3910 6c7ab921 2019-03-18 stsp if (err == NULL)
3911 6c7ab921 2019-03-18 stsp *wt_path = path;
3912 6c7ab921 2019-03-18 stsp else
3913 6c7ab921 2019-03-18 stsp free(path);
3914 d00136be 2019-03-26 stsp return err;
3915 d00136be 2019-03-26 stsp }
3916 d00136be 2019-03-26 stsp
3917 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3918 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3919 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3920 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3921 4e68cba3 2019-11-23 stsp void *progress_arg;
3922 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3923 4e68cba3 2019-11-23 stsp };
3924 4e68cba3 2019-11-23 stsp
3925 4e68cba3 2019-11-23 stsp static const struct got_error *
3926 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3927 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3928 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3929 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3930 07f5b47a 2019-06-02 stsp {
3931 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3932 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3933 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3934 6d022e97 2019-08-04 stsp struct stat sb;
3935 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3936 07f5b47a 2019-06-02 stsp
3937 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3938 dbb83fbd 2019-12-12 stsp relpath) == -1)
3939 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3940 dbb83fbd 2019-12-12 stsp
3941 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3942 6d022e97 2019-08-04 stsp if (ie) {
3943 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3944 12463d8b 2019-12-13 stsp de_name, a->repo);
3945 6d022e97 2019-08-04 stsp if (err)
3946 dbb83fbd 2019-12-12 stsp goto done;
3947 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3948 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3949 dbb83fbd 2019-12-12 stsp goto done;
3950 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3951 dbb83fbd 2019-12-12 stsp if (err)
3952 dbb83fbd 2019-12-12 stsp goto done;
3953 6d022e97 2019-08-04 stsp }
3954 07f5b47a 2019-06-02 stsp
3955 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3956 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3957 dbb83fbd 2019-12-12 stsp goto done;
3958 4e68cba3 2019-11-23 stsp }
3959 07f5b47a 2019-06-02 stsp
3960 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3961 4e68cba3 2019-11-23 stsp if (err)
3962 4e68cba3 2019-11-23 stsp goto done;
3963 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3964 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3965 3969253a 2020-03-07 stsp if (err) {
3966 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3967 3969253a 2020-03-07 stsp goto done;
3968 3969253a 2020-03-07 stsp }
3969 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3970 07f5b47a 2019-06-02 stsp if (err) {
3971 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3972 4e68cba3 2019-11-23 stsp goto done;
3973 07f5b47a 2019-06-02 stsp }
3974 4e68cba3 2019-11-23 stsp done:
3975 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3976 dbb83fbd 2019-12-12 stsp if (err)
3977 dbb83fbd 2019-12-12 stsp return err;
3978 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3979 dbb83fbd 2019-12-12 stsp return NULL;
3980 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3981 07f5b47a 2019-06-02 stsp }
3982 07f5b47a 2019-06-02 stsp
3983 d00136be 2019-03-26 stsp const struct got_error *
3984 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3985 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3986 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3987 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3988 d00136be 2019-03-26 stsp {
3989 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3990 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3991 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3992 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3993 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3994 d00136be 2019-03-26 stsp
3995 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3996 d00136be 2019-03-26 stsp if (err)
3997 d00136be 2019-03-26 stsp return err;
3998 d00136be 2019-03-26 stsp
3999 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4000 d00136be 2019-03-26 stsp if (err)
4001 d00136be 2019-03-26 stsp goto done;
4002 d00136be 2019-03-26 stsp
4003 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4004 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4005 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4006 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4007 4e68cba3 2019-11-23 stsp saa.repo = repo;
4008 4e68cba3 2019-11-23 stsp
4009 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4010 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4011 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4012 1dd54920 2019-05-11 stsp if (err)
4013 af12c6b9 2019-06-04 stsp break;
4014 1dd54920 2019-05-11 stsp }
4015 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4016 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4017 af12c6b9 2019-06-04 stsp err = sync_err;
4018 d00136be 2019-03-26 stsp done:
4019 fb399478 2019-07-12 stsp free(fileindex_path);
4020 d00136be 2019-03-26 stsp if (fileindex)
4021 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4022 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4023 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4024 d00136be 2019-03-26 stsp err = unlockerr;
4025 6c7ab921 2019-03-18 stsp return err;
4026 6c7ab921 2019-03-18 stsp }
4027 17ed4618 2019-06-02 stsp
4028 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4029 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4030 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4031 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4032 f2a9dc41 2019-12-13 tracey void *progress_arg;
4033 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4034 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4035 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4036 766841c2 2020-08-13 stsp const char *status_codes;
4037 f2a9dc41 2019-12-13 tracey };
4038 f2a9dc41 2019-12-13 tracey
4039 f2a9dc41 2019-12-13 tracey static const struct got_error *
4040 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4041 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4042 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4043 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4044 17ed4618 2019-06-02 stsp {
4045 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4046 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4047 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4048 17ed4618 2019-06-02 stsp struct stat sb;
4049 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4050 17ed4618 2019-06-02 stsp
4051 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4052 17ed4618 2019-06-02 stsp if (ie == NULL)
4053 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4054 2ec1f75b 2019-03-26 stsp
4055 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4056 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4057 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4058 9acbc4fa 2019-08-03 stsp return NULL;
4059 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4060 9acbc4fa 2019-08-03 stsp }
4061 9acbc4fa 2019-08-03 stsp
4062 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4063 f2a9dc41 2019-12-13 tracey relpath) == -1)
4064 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4065 f2a9dc41 2019-12-13 tracey
4066 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4067 12463d8b 2019-12-13 stsp a->repo);
4068 17ed4618 2019-06-02 stsp if (err)
4069 f2a9dc41 2019-12-13 tracey goto done;
4070 17ed4618 2019-06-02 stsp
4071 766841c2 2020-08-13 stsp if (a->status_codes) {
4072 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4073 766841c2 2020-08-13 stsp int i;
4074 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4075 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4076 766841c2 2020-08-13 stsp break;
4077 766841c2 2020-08-13 stsp }
4078 766841c2 2020-08-13 stsp if (i == ncodes) {
4079 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4080 766841c2 2020-08-13 stsp free(ondisk_path);
4081 766841c2 2020-08-13 stsp return NULL;
4082 766841c2 2020-08-13 stsp }
4083 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4084 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4085 766841c2 2020-08-13 stsp static char msg[64];
4086 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4087 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4088 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4089 766841c2 2020-08-13 stsp goto done;
4090 766841c2 2020-08-13 stsp }
4091 766841c2 2020-08-13 stsp }
4092 766841c2 2020-08-13 stsp
4093 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4094 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4095 f2a9dc41 2019-12-13 tracey goto done;
4096 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4097 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4098 f2a9dc41 2019-12-13 tracey goto done;
4099 f2a9dc41 2019-12-13 tracey }
4100 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4101 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4102 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4103 f2a9dc41 2019-12-13 tracey goto done;
4104 f2a9dc41 2019-12-13 tracey }
4105 17ed4618 2019-06-02 stsp }
4106 17ed4618 2019-06-02 stsp
4107 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4108 20a2ad1c 2020-10-20 stsp size_t root_len;
4109 20a2ad1c 2020-10-20 stsp
4110 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4111 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4112 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4113 12463d8b 2019-12-13 stsp ondisk_path);
4114 12463d8b 2019-12-13 stsp goto done;
4115 12463d8b 2019-12-13 stsp }
4116 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4117 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4118 12463d8b 2019-12-13 stsp goto done;
4119 15341bfd 2020-03-05 tracey }
4120 15341bfd 2020-03-05 tracey
4121 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4122 20a2ad1c 2020-10-20 stsp do {
4123 20a2ad1c 2020-10-20 stsp char *parent;
4124 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4125 20a2ad1c 2020-10-20 stsp if (err)
4126 2513f20a 2020-10-20 stsp goto done;
4127 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4128 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4129 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4130 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4131 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4132 20a2ad1c 2020-10-20 stsp ondisk_path);
4133 15341bfd 2020-03-05 tracey break;
4134 15341bfd 2020-03-05 tracey }
4135 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4136 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4137 f2a9dc41 2019-12-13 tracey }
4138 17ed4618 2019-06-02 stsp
4139 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4140 f2a9dc41 2019-12-13 tracey done:
4141 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4142 f2a9dc41 2019-12-13 tracey if (err)
4143 f2a9dc41 2019-12-13 tracey return err;
4144 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4145 f2a9dc41 2019-12-13 tracey return NULL;
4146 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4147 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4148 17ed4618 2019-06-02 stsp }
4149 17ed4618 2019-06-02 stsp
4150 2ec1f75b 2019-03-26 stsp const struct got_error *
4151 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4152 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4153 766841c2 2020-08-13 stsp const char *status_codes,
4154 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4155 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4156 2ec1f75b 2019-03-26 stsp {
4157 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4158 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4159 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4160 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4161 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4162 2ec1f75b 2019-03-26 stsp
4163 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4164 2ec1f75b 2019-03-26 stsp if (err)
4165 2ec1f75b 2019-03-26 stsp return err;
4166 2ec1f75b 2019-03-26 stsp
4167 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4168 2ec1f75b 2019-03-26 stsp if (err)
4169 2ec1f75b 2019-03-26 stsp goto done;
4170 f2a9dc41 2019-12-13 tracey
4171 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4172 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4173 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4174 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4175 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4176 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4177 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4178 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4179 2ec1f75b 2019-03-26 stsp
4180 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4181 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4182 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4183 17ed4618 2019-06-02 stsp if (err)
4184 af12c6b9 2019-06-04 stsp break;
4185 2ec1f75b 2019-03-26 stsp }
4186 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4187 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4188 af12c6b9 2019-06-04 stsp err = sync_err;
4189 2ec1f75b 2019-03-26 stsp done:
4190 fb399478 2019-07-12 stsp free(fileindex_path);
4191 2ec1f75b 2019-03-26 stsp if (fileindex)
4192 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4193 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4194 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4195 2ec1f75b 2019-03-26 stsp err = unlockerr;
4196 33aa809d 2019-08-08 stsp return err;
4197 33aa809d 2019-08-08 stsp }
4198 33aa809d 2019-08-08 stsp
4199 33aa809d 2019-08-08 stsp static const struct got_error *
4200 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4201 33aa809d 2019-08-08 stsp {
4202 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4203 33aa809d 2019-08-08 stsp char *line = NULL;
4204 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4205 33aa809d 2019-08-08 stsp ssize_t linelen;
4206 33aa809d 2019-08-08 stsp
4207 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4208 33aa809d 2019-08-08 stsp if (linelen == -1) {
4209 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4210 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4211 33aa809d 2019-08-08 stsp goto done;
4212 33aa809d 2019-08-08 stsp }
4213 33aa809d 2019-08-08 stsp return NULL;
4214 33aa809d 2019-08-08 stsp }
4215 33aa809d 2019-08-08 stsp if (outfile) {
4216 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4217 33aa809d 2019-08-08 stsp if (n != linelen) {
4218 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4219 33aa809d 2019-08-08 stsp goto done;
4220 33aa809d 2019-08-08 stsp }
4221 33aa809d 2019-08-08 stsp }
4222 33aa809d 2019-08-08 stsp if (rejectfile) {
4223 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4224 33aa809d 2019-08-08 stsp if (n != linelen)
4225 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4226 33aa809d 2019-08-08 stsp }
4227 33aa809d 2019-08-08 stsp done:
4228 33aa809d 2019-08-08 stsp free(line);
4229 2ec1f75b 2019-03-26 stsp return err;
4230 2ec1f75b 2019-03-26 stsp }
4231 1f1abb7e 2019-08-08 stsp
4232 33aa809d 2019-08-08 stsp static const struct got_error *
4233 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4234 33aa809d 2019-08-08 stsp {
4235 33aa809d 2019-08-08 stsp char *line = NULL;
4236 33aa809d 2019-08-08 stsp size_t linesize = 0;
4237 33aa809d 2019-08-08 stsp ssize_t linelen;
4238 33aa809d 2019-08-08 stsp
4239 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4240 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4241 500467ff 2019-09-25 hiltjo if (ferror(f))
4242 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4243 500467ff 2019-09-25 hiltjo return NULL;
4244 500467ff 2019-09-25 hiltjo }
4245 33aa809d 2019-08-08 stsp free(line);
4246 33aa809d 2019-08-08 stsp return NULL;
4247 33aa809d 2019-08-08 stsp }
4248 33aa809d 2019-08-08 stsp
4249 33aa809d 2019-08-08 stsp static const struct got_error *
4250 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4251 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4252 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4253 abc59930 2021-09-05 naddy {
4254 33aa809d 2019-08-08 stsp const struct got_error *err;
4255 33aa809d 2019-08-08 stsp
4256 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4257 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4258 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4259 33aa809d 2019-08-08 stsp if (err)
4260 33aa809d 2019-08-08 stsp return err;
4261 33aa809d 2019-08-08 stsp (*line_cur1)++;
4262 33aa809d 2019-08-08 stsp }
4263 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4264 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4265 33aa809d 2019-08-08 stsp if (rejectfile)
4266 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4267 33aa809d 2019-08-08 stsp else
4268 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4269 33aa809d 2019-08-08 stsp if (err)
4270 33aa809d 2019-08-08 stsp return err;
4271 33aa809d 2019-08-08 stsp (*line_cur2)++;
4272 33aa809d 2019-08-08 stsp }
4273 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4274 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4275 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4276 33aa809d 2019-08-08 stsp if (err)
4277 33aa809d 2019-08-08 stsp return err;
4278 33aa809d 2019-08-08 stsp (*line_cur2)++;
4279 33aa809d 2019-08-08 stsp }
4280 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4281 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4282 33aa809d 2019-08-08 stsp if (rejectfile)
4283 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4284 33aa809d 2019-08-08 stsp else
4285 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4286 33aa809d 2019-08-08 stsp if (err)
4287 33aa809d 2019-08-08 stsp return err;
4288 33aa809d 2019-08-08 stsp (*line_cur1)++;
4289 33aa809d 2019-08-08 stsp }
4290 f1e81a05 2019-08-10 stsp
4291 f1e81a05 2019-08-10 stsp return NULL;
4292 f1e81a05 2019-08-10 stsp }
4293 f1e81a05 2019-08-10 stsp
4294 f1e81a05 2019-08-10 stsp static const struct got_error *
4295 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4296 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4297 f1e81a05 2019-08-10 stsp {
4298 f1e81a05 2019-08-10 stsp const struct got_error *err;
4299 f1e81a05 2019-08-10 stsp
4300 f1e81a05 2019-08-10 stsp if (outfile) {
4301 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4302 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4303 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4304 f1e81a05 2019-08-10 stsp if (err)
4305 f1e81a05 2019-08-10 stsp return err;
4306 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4307 f1e81a05 2019-08-10 stsp }
4308 f1e81a05 2019-08-10 stsp }
4309 f1e81a05 2019-08-10 stsp if (rejectfile) {
4310 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4311 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4312 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4313 f1e81a05 2019-08-10 stsp if (err)
4314 f1e81a05 2019-08-10 stsp return err;
4315 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4316 f1e81a05 2019-08-10 stsp }
4317 33aa809d 2019-08-08 stsp }
4318 33aa809d 2019-08-08 stsp
4319 33aa809d 2019-08-08 stsp return NULL;
4320 33aa809d 2019-08-08 stsp }
4321 33aa809d 2019-08-08 stsp
4322 33aa809d 2019-08-08 stsp static const struct got_error *
4323 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4324 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4325 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4326 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4327 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4328 33aa809d 2019-08-08 stsp {
4329 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4330 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4331 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4332 33aa809d 2019-08-08 stsp FILE *hunkfile;
4333 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4334 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4335 fe621944 2020-11-10 stsp int rc;
4336 33aa809d 2019-08-08 stsp
4337 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4338 33aa809d 2019-08-08 stsp
4339 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4340 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4341 fe621944 2020-11-10 stsp start_old = cc.left.start;
4342 fe621944 2020-11-10 stsp end_old = cc.left.end;
4343 fe621944 2020-11-10 stsp start_new = cc.right.start;
4344 fe621944 2020-11-10 stsp end_new = cc.right.end;
4345 33aa809d 2019-08-08 stsp
4346 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4347 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4348 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4349 33aa809d 2019-08-08 stsp
4350 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4351 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4352 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4353 33aa809d 2019-08-08 stsp
4354 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4355 fe621944 2020-11-10 stsp if (diff_state == NULL)
4356 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4357 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4358 fe621944 2020-11-10 stsp
4359 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4360 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4361 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4362 33aa809d 2019-08-08 stsp goto done;
4363 33aa809d 2019-08-08 stsp }
4364 fe621944 2020-11-10 stsp
4365 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4366 fe621944 2020-11-10 stsp diff_result, &cc);
4367 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4368 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4369 33aa809d 2019-08-08 stsp goto done;
4370 33aa809d 2019-08-08 stsp }
4371 fe621944 2020-11-10 stsp
4372 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4373 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4374 33aa809d 2019-08-08 stsp goto done;
4375 33aa809d 2019-08-08 stsp }
4376 33aa809d 2019-08-08 stsp
4377 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4378 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4379 33aa809d 2019-08-08 stsp if (err)
4380 33aa809d 2019-08-08 stsp goto done;
4381 33aa809d 2019-08-08 stsp
4382 33aa809d 2019-08-08 stsp switch (*choice) {
4383 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4384 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4385 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4386 33aa809d 2019-08-08 stsp break;
4387 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4388 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4389 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4390 33aa809d 2019-08-08 stsp break;
4391 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4392 33aa809d 2019-08-08 stsp break;
4393 33aa809d 2019-08-08 stsp default:
4394 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4395 33aa809d 2019-08-08 stsp break;
4396 33aa809d 2019-08-08 stsp }
4397 33aa809d 2019-08-08 stsp done:
4398 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4399 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4400 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4401 33aa809d 2019-08-08 stsp return err;
4402 33aa809d 2019-08-08 stsp }
4403 33aa809d 2019-08-08 stsp
4404 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4405 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4406 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4407 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4408 1f1abb7e 2019-08-08 stsp void *progress_arg;
4409 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4410 33aa809d 2019-08-08 stsp void *patch_arg;
4411 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4412 1f1abb7e 2019-08-08 stsp };
4413 a129376b 2019-03-28 stsp
4414 e20a8b6f 2019-06-04 stsp static const struct got_error *
4415 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4416 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4417 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4418 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4419 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4420 33aa809d 2019-08-08 stsp {
4421 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4422 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4423 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4424 1ebedb77 2019-10-19 stsp int fd2 = -1;
4425 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4426 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4427 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4428 fe621944 2020-11-10 stsp struct stat sb2;
4429 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4430 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4431 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4432 33aa809d 2019-08-08 stsp
4433 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4434 33aa809d 2019-08-08 stsp
4435 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4436 33aa809d 2019-08-08 stsp if (err)
4437 33aa809d 2019-08-08 stsp return err;
4438 33aa809d 2019-08-08 stsp
4439 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4440 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4441 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4442 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4443 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4444 fa3cef63 2020-07-23 stsp goto done;
4445 fa3cef63 2020-07-23 stsp }
4446 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4447 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4448 fa3cef63 2020-07-23 stsp if (link_len == -1)
4449 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4450 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4451 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4452 12463d8b 2019-12-13 stsp }
4453 12463d8b 2019-12-13 stsp } else {
4454 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4455 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4456 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4457 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4458 fa3cef63 2020-07-23 stsp goto done;
4459 fa3cef63 2020-07-23 stsp }
4460 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4461 fa3cef63 2020-07-23 stsp sizeof(link_target));
4462 fa3cef63 2020-07-23 stsp if (link_len == -1)
4463 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4464 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4465 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4466 12463d8b 2019-12-13 stsp }
4467 1ebedb77 2019-10-19 stsp }
4468 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4469 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4470 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4471 fa3cef63 2020-07-23 stsp goto done;
4472 fa3cef63 2020-07-23 stsp }
4473 1ebedb77 2019-10-19 stsp
4474 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4475 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4476 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4477 fa3cef63 2020-07-23 stsp goto done;
4478 fa3cef63 2020-07-23 stsp }
4479 fa3cef63 2020-07-23 stsp fd2 = -1;
4480 fa3cef63 2020-07-23 stsp } else {
4481 fa3cef63 2020-07-23 stsp size_t n;
4482 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4483 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4484 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4485 fa3cef63 2020-07-23 stsp goto done;
4486 fa3cef63 2020-07-23 stsp }
4487 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4488 fa3cef63 2020-07-23 stsp if (n != link_len) {
4489 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4490 fa3cef63 2020-07-23 stsp goto done;
4491 fa3cef63 2020-07-23 stsp }
4492 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4493 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4494 fa3cef63 2020-07-23 stsp goto done;
4495 fa3cef63 2020-07-23 stsp }
4496 fa3cef63 2020-07-23 stsp rewind(f2);
4497 33aa809d 2019-08-08 stsp }
4498 33aa809d 2019-08-08 stsp
4499 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4500 33aa809d 2019-08-08 stsp if (err)
4501 33aa809d 2019-08-08 stsp goto done;
4502 33aa809d 2019-08-08 stsp
4503 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4504 33aa809d 2019-08-08 stsp if (err)
4505 33aa809d 2019-08-08 stsp goto done;
4506 33aa809d 2019-08-08 stsp
4507 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4508 33aa809d 2019-08-08 stsp if (err)
4509 33aa809d 2019-08-08 stsp goto done;
4510 33aa809d 2019-08-08 stsp
4511 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4512 fe621944 2020-11-10 stsp NULL);
4513 33aa809d 2019-08-08 stsp if (err)
4514 33aa809d 2019-08-08 stsp goto done;
4515 33aa809d 2019-08-08 stsp
4516 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4517 33aa809d 2019-08-08 stsp if (err)
4518 33aa809d 2019-08-08 stsp goto done;
4519 33aa809d 2019-08-08 stsp
4520 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4521 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4522 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4523 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4524 fe621944 2020-11-10 stsp
4525 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4526 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4527 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4528 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4529 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4530 fe621944 2020-11-10 stsp nchanges++;
4531 fe621944 2020-11-10 stsp }
4532 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4533 33aa809d 2019-08-08 stsp int choice;
4534 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4535 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4536 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4537 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4538 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4539 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4540 33aa809d 2019-08-08 stsp if (err)
4541 33aa809d 2019-08-08 stsp goto done;
4542 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4543 33aa809d 2019-08-08 stsp have_content = 1;
4544 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4545 33aa809d 2019-08-08 stsp break;
4546 33aa809d 2019-08-08 stsp }
4547 1ebedb77 2019-10-19 stsp if (have_content) {
4548 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4549 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4550 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4551 1ebedb77 2019-10-19 stsp if (err)
4552 1ebedb77 2019-10-19 stsp goto done;
4553 1ebedb77 2019-10-19 stsp
4554 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4555 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4556 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4557 fa3cef63 2020-07-23 stsp goto done;
4558 fa3cef63 2020-07-23 stsp }
4559 1ebedb77 2019-10-19 stsp }
4560 1ebedb77 2019-10-19 stsp }
4561 33aa809d 2019-08-08 stsp done:
4562 33aa809d 2019-08-08 stsp free(id_str);
4563 33aa809d 2019-08-08 stsp if (blob)
4564 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4565 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4566 fe621944 2020-11-10 stsp if (err == NULL)
4567 fe621944 2020-11-10 stsp err = free_err;
4568 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4569 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4570 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4571 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4572 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4573 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4574 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4575 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4576 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4577 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4578 33aa809d 2019-08-08 stsp if (err || !have_content) {
4579 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4580 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4581 33aa809d 2019-08-08 stsp free(*path_outfile);
4582 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4583 33aa809d 2019-08-08 stsp }
4584 33aa809d 2019-08-08 stsp free(path1);
4585 33aa809d 2019-08-08 stsp return err;
4586 33aa809d 2019-08-08 stsp }
4587 33aa809d 2019-08-08 stsp
4588 33aa809d 2019-08-08 stsp static const struct got_error *
4589 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4590 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4591 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4592 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4593 a129376b 2019-03-28 stsp {
4594 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4595 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4596 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4597 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4598 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4599 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4600 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4601 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4602 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4603 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4604 a129376b 2019-03-28 stsp
4605 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4606 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4607 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4608 d3bcc3d1 2019-08-08 stsp return NULL;
4609 3d69ad8d 2019-08-17 semarie
4610 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4611 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4612 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4613 d3bcc3d1 2019-08-08 stsp
4614 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4615 65084dad 2019-08-08 stsp if (ie == NULL)
4616 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4617 a129376b 2019-03-28 stsp
4618 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4619 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4620 a129376b 2019-03-28 stsp if (err) {
4621 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4622 a129376b 2019-03-28 stsp goto done;
4623 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4624 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4625 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4626 e20a8b6f 2019-06-04 stsp goto done;
4627 e20a8b6f 2019-06-04 stsp }
4628 a129376b 2019-03-28 stsp }
4629 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4630 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4631 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4632 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4633 a129376b 2019-03-28 stsp goto done;
4634 a129376b 2019-03-28 stsp }
4635 a129376b 2019-03-28 stsp } else {
4636 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4637 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4638 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4639 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4640 a129376b 2019-03-28 stsp goto done;
4641 a129376b 2019-03-28 stsp }
4642 a129376b 2019-03-28 stsp } else {
4643 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4644 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4645 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4646 a129376b 2019-03-28 stsp goto done;
4647 a129376b 2019-03-28 stsp }
4648 a129376b 2019-03-28 stsp }
4649 a129376b 2019-03-28 stsp }
4650 a129376b 2019-03-28 stsp
4651 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4652 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4653 a9fa2909 2019-07-27 stsp if (err) {
4654 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4655 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4656 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4657 a9fa2909 2019-07-27 stsp goto done;
4658 a9fa2909 2019-07-27 stsp } else {
4659 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4660 a9fa2909 2019-07-27 stsp if (err)
4661 a9fa2909 2019-07-27 stsp goto done;
4662 a9fa2909 2019-07-27 stsp
4663 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4664 1233e6b6 2020-10-19 stsp if (err)
4665 a9fa2909 2019-07-27 stsp goto done;
4666 a9fa2909 2019-07-27 stsp
4667 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4668 1233e6b6 2020-10-19 stsp free(te_name);
4669 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4670 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4671 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4672 a9fa2909 2019-07-27 stsp goto done;
4673 a9fa2909 2019-07-27 stsp }
4674 a129376b 2019-03-28 stsp }
4675 a129376b 2019-03-28 stsp
4676 a129376b 2019-03-28 stsp switch (status) {
4677 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4678 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4679 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4680 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4681 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4682 33aa809d 2019-08-08 stsp if (err)
4683 33aa809d 2019-08-08 stsp goto done;
4684 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4685 33aa809d 2019-08-08 stsp break;
4686 33aa809d 2019-08-08 stsp }
4687 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4688 1f1abb7e 2019-08-08 stsp ie->path);
4689 1ee397ad 2019-07-12 stsp if (err)
4690 1ee397ad 2019-07-12 stsp goto done;
4691 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4692 a129376b 2019-03-28 stsp break;
4693 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4694 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4695 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4696 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4697 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4698 33aa809d 2019-08-08 stsp if (err)
4699 33aa809d 2019-08-08 stsp goto done;
4700 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4701 33aa809d 2019-08-08 stsp break;
4702 33aa809d 2019-08-08 stsp }
4703 33aa809d 2019-08-08 stsp /* fall through */
4704 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4705 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4706 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4707 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4708 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4709 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4710 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4711 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4712 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4713 24278f30 2019-08-03 stsp } else
4714 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4715 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4716 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4717 a129376b 2019-03-28 stsp if (err)
4718 65084dad 2019-08-08 stsp goto done;
4719 65084dad 2019-08-08 stsp
4720 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4721 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4722 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4723 a129376b 2019-03-28 stsp goto done;
4724 65084dad 2019-08-08 stsp }
4725 65084dad 2019-08-08 stsp
4726 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4727 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4728 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4729 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4730 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4731 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4732 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4733 33aa809d 2019-08-08 stsp break;
4734 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4735 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4736 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4737 369fd7e5 2020-07-23 stsp path_content);
4738 369fd7e5 2020-07-23 stsp break;
4739 369fd7e5 2020-07-23 stsp }
4740 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4741 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4742 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4743 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4744 369fd7e5 2020-07-23 stsp } else {
4745 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4746 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4747 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4748 369fd7e5 2020-07-23 stsp goto done;
4749 369fd7e5 2020-07-23 stsp }
4750 33aa809d 2019-08-08 stsp }
4751 33aa809d 2019-08-08 stsp } else {
4752 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4753 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4754 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4755 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4756 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4757 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4758 2e1fa222 2020-07-23 stsp } else {
4759 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4760 2e1fa222 2020-07-23 stsp ie->path,
4761 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4762 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4763 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4764 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4765 2e1fa222 2020-07-23 stsp }
4766 a129376b 2019-03-28 stsp if (err)
4767 a129376b 2019-03-28 stsp goto done;
4768 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4769 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4770 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4771 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4772 437adc9d 2020-12-10 yzhong blob->id.sha1,
4773 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4774 2e1fa222 2020-07-23 stsp if (err)
4775 2e1fa222 2020-07-23 stsp goto done;
4776 2e1fa222 2020-07-23 stsp }
4777 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4778 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4779 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4780 33aa809d 2019-08-08 stsp }
4781 a129376b 2019-03-28 stsp }
4782 a129376b 2019-03-28 stsp break;
4783 e20a8b6f 2019-06-04 stsp }
4784 a129376b 2019-03-28 stsp default:
4785 1f1abb7e 2019-08-08 stsp break;
4786 a129376b 2019-03-28 stsp }
4787 a129376b 2019-03-28 stsp done:
4788 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4789 33aa809d 2019-08-08 stsp free(path_content);
4790 e20a8b6f 2019-06-04 stsp free(parent_path);
4791 a129376b 2019-03-28 stsp free(tree_path);
4792 a129376b 2019-03-28 stsp if (blob)
4793 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4794 a129376b 2019-03-28 stsp if (tree)
4795 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4796 a129376b 2019-03-28 stsp free(tree_id);
4797 e20a8b6f 2019-06-04 stsp return err;
4798 e20a8b6f 2019-06-04 stsp }
4799 e20a8b6f 2019-06-04 stsp
4800 e20a8b6f 2019-06-04 stsp const struct got_error *
4801 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4802 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4803 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4804 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4805 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4806 e20a8b6f 2019-06-04 stsp {
4807 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4808 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4809 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4810 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4811 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4812 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4813 e20a8b6f 2019-06-04 stsp
4814 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4815 e20a8b6f 2019-06-04 stsp if (err)
4816 e20a8b6f 2019-06-04 stsp return err;
4817 e20a8b6f 2019-06-04 stsp
4818 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4819 e20a8b6f 2019-06-04 stsp if (err)
4820 e20a8b6f 2019-06-04 stsp goto done;
4821 e20a8b6f 2019-06-04 stsp
4822 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4823 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4824 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4825 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4826 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4827 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4828 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4829 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4830 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4831 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4832 e20a8b6f 2019-06-04 stsp if (err)
4833 af12c6b9 2019-06-04 stsp break;
4834 e20a8b6f 2019-06-04 stsp }
4835 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4836 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4837 e20a8b6f 2019-06-04 stsp err = sync_err;
4838 af12c6b9 2019-06-04 stsp done:
4839 fb399478 2019-07-12 stsp free(fileindex_path);
4840 a129376b 2019-03-28 stsp if (fileindex)
4841 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4842 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4843 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4844 a129376b 2019-03-28 stsp err = unlockerr;
4845 c4296144 2019-05-09 stsp return err;
4846 c4296144 2019-05-09 stsp }
4847 c4296144 2019-05-09 stsp
4848 cf066bf8 2019-05-09 stsp static void
4849 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4850 cf066bf8 2019-05-09 stsp {
4851 24519714 2019-05-09 stsp free(ct->path);
4852 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4853 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4854 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4855 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4856 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4857 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4858 cf066bf8 2019-05-09 stsp free(ct);
4859 cf066bf8 2019-05-09 stsp }
4860 24519714 2019-05-09 stsp
4861 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4862 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4863 24519714 2019-05-09 stsp struct got_repository *repo;
4864 24519714 2019-05-09 stsp struct got_worktree *worktree;
4865 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4866 5f8a88c6 2019-08-03 stsp int have_staged_files;
4867 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4868 24519714 2019-05-09 stsp };
4869 cf066bf8 2019-05-09 stsp
4870 c4296144 2019-05-09 stsp static const struct got_error *
4871 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4872 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4873 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4874 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4875 c4296144 2019-05-09 stsp {
4876 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4877 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4878 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4879 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4880 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4881 768aea60 2019-05-09 stsp struct stat sb;
4882 c4296144 2019-05-09 stsp
4883 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4884 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4885 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4886 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4887 5f8a88c6 2019-08-03 stsp return NULL;
4888 5f8a88c6 2019-08-03 stsp } else {
4889 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4890 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4891 c4296144 2019-05-09 stsp
4892 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4893 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4894 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4895 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4896 5f8a88c6 2019-08-03 stsp return NULL;
4897 5f8a88c6 2019-08-03 stsp }
4898 0b5cc0d6 2019-05-09 stsp
4899 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4900 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4901 036813ee 2019-05-09 stsp goto done;
4902 036813ee 2019-05-09 stsp }
4903 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4904 036813ee 2019-05-09 stsp parent_path = strdup("");
4905 69960a46 2019-05-09 stsp if (parent_path == NULL)
4906 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4907 69960a46 2019-05-09 stsp } else {
4908 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4909 69960a46 2019-05-09 stsp if (err)
4910 69960a46 2019-05-09 stsp return err;
4911 69960a46 2019-05-09 stsp }
4912 c4296144 2019-05-09 stsp
4913 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4914 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4915 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4916 c4296144 2019-05-09 stsp goto done;
4917 768aea60 2019-05-09 stsp }
4918 768aea60 2019-05-09 stsp
4919 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4920 768aea60 2019-05-09 stsp relpath) == -1) {
4921 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4922 768aea60 2019-05-09 stsp goto done;
4923 768aea60 2019-05-09 stsp }
4924 0aeb8099 2020-07-23 stsp
4925 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4926 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4927 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4928 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4929 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4930 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4931 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4932 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4933 0aeb8099 2020-07-23 stsp break;
4934 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4935 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4936 0aeb8099 2020-07-23 stsp break;
4937 0aeb8099 2020-07-23 stsp default:
4938 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4939 0aeb8099 2020-07-23 stsp goto done;
4940 0aeb8099 2020-07-23 stsp }
4941 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4942 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4943 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4944 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4945 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4946 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4947 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4948 12463d8b 2019-12-13 stsp ct->ondisk_path);
4949 12463d8b 2019-12-13 stsp goto done;
4950 12463d8b 2019-12-13 stsp }
4951 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4952 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4953 768aea60 2019-05-09 stsp goto done;
4954 768aea60 2019-05-09 stsp }
4955 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4956 c4296144 2019-05-09 stsp }
4957 c4296144 2019-05-09 stsp
4958 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4959 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4960 44d03001 2019-05-09 stsp relpath) == -1) {
4961 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4962 44d03001 2019-05-09 stsp goto done;
4963 44d03001 2019-05-09 stsp }
4964 44d03001 2019-05-09 stsp
4965 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4966 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4967 35213c7c 2020-07-23 stsp int is_bad_symlink;
4968 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4969 35213c7c 2020-07-23 stsp ssize_t target_len;
4970 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4971 35213c7c 2020-07-23 stsp sizeof(target_path));
4972 35213c7c 2020-07-23 stsp if (target_len == -1) {
4973 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4974 35213c7c 2020-07-23 stsp ct->ondisk_path);
4975 35213c7c 2020-07-23 stsp goto done;
4976 35213c7c 2020-07-23 stsp }
4977 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4978 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4979 35213c7c 2020-07-23 stsp if (err)
4980 35213c7c 2020-07-23 stsp goto done;
4981 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4982 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4983 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4984 35213c7c 2020-07-23 stsp goto done;
4985 35213c7c 2020-07-23 stsp }
4986 35213c7c 2020-07-23 stsp }
4987 35213c7c 2020-07-23 stsp
4988 35213c7c 2020-07-23 stsp
4989 cf066bf8 2019-05-09 stsp ct->status = status;
4990 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4991 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4992 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4993 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4994 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4995 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4996 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4997 b416585c 2019-05-13 stsp goto done;
4998 b416585c 2019-05-13 stsp }
4999 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5000 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5001 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5002 f0b75401 2019-08-03 stsp goto done;
5003 f0b75401 2019-08-03 stsp }
5004 f0b75401 2019-08-03 stsp }
5005 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5006 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5007 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5008 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5009 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5010 036813ee 2019-05-09 stsp goto done;
5011 036813ee 2019-05-09 stsp }
5012 ca2503ea 2019-05-09 stsp }
5013 24519714 2019-05-09 stsp ct->path = strdup(path);
5014 24519714 2019-05-09 stsp if (ct->path == NULL) {
5015 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5016 24519714 2019-05-09 stsp goto done;
5017 24519714 2019-05-09 stsp }
5018 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5019 c4296144 2019-05-09 stsp done:
5020 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5021 ed175427 2019-05-09 stsp free_commitable(ct);
5022 24519714 2019-05-09 stsp free(parent_path);
5023 036813ee 2019-05-09 stsp free(path);
5024 a129376b 2019-03-28 stsp return err;
5025 a129376b 2019-03-28 stsp }
5026 c4296144 2019-05-09 stsp
5027 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5028 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5029 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5030 036813ee 2019-05-09 stsp struct got_repository *);
5031 ed175427 2019-05-09 stsp
5032 ed175427 2019-05-09 stsp static const struct got_error *
5033 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5034 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5035 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5036 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5037 afa376bf 2019-05-09 stsp struct got_repository *repo)
5038 ed175427 2019-05-09 stsp {
5039 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5040 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5041 036813ee 2019-05-09 stsp char *subpath;
5042 ed175427 2019-05-09 stsp
5043 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5044 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5045 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5046 ed175427 2019-05-09 stsp
5047 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5048 ed175427 2019-05-09 stsp if (err)
5049 ed175427 2019-05-09 stsp return err;
5050 ed175427 2019-05-09 stsp
5051 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5052 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5053 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5054 036813ee 2019-05-09 stsp free(subpath);
5055 036813ee 2019-05-09 stsp return err;
5056 036813ee 2019-05-09 stsp }
5057 ed175427 2019-05-09 stsp
5058 036813ee 2019-05-09 stsp static const struct got_error *
5059 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5060 036813ee 2019-05-09 stsp {
5061 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5062 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5063 ed175427 2019-05-09 stsp
5064 036813ee 2019-05-09 stsp *match = 0;
5065 ed175427 2019-05-09 stsp
5066 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5067 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5068 0f63689d 2019-05-10 stsp return NULL;
5069 036813ee 2019-05-09 stsp }
5070 0b5cc0d6 2019-05-09 stsp
5071 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5072 0f63689d 2019-05-10 stsp if (err)
5073 0f63689d 2019-05-10 stsp return err;
5074 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5075 036813ee 2019-05-09 stsp free(ct_parent_path);
5076 036813ee 2019-05-09 stsp return err;
5077 036813ee 2019-05-09 stsp }
5078 0b5cc0d6 2019-05-09 stsp
5079 768aea60 2019-05-09 stsp static mode_t
5080 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5081 768aea60 2019-05-09 stsp {
5082 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5083 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5084 3d9a4ec4 2020-07-23 stsp
5085 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5086 768aea60 2019-05-09 stsp }
5087 768aea60 2019-05-09 stsp
5088 036813ee 2019-05-09 stsp static const struct got_error *
5089 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5090 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5091 036813ee 2019-05-09 stsp {
5092 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5093 ca2503ea 2019-05-09 stsp
5094 036813ee 2019-05-09 stsp *new_te = NULL;
5095 0b5cc0d6 2019-05-09 stsp
5096 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5097 036813ee 2019-05-09 stsp if (err)
5098 036813ee 2019-05-09 stsp goto done;
5099 0b5cc0d6 2019-05-09 stsp
5100 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5101 036813ee 2019-05-09 stsp
5102 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5103 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5104 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5105 5f8a88c6 2019-08-03 stsp else
5106 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5107 036813ee 2019-05-09 stsp done:
5108 036813ee 2019-05-09 stsp if (err && *new_te) {
5109 56e0773d 2019-11-28 stsp free(*new_te);
5110 036813ee 2019-05-09 stsp *new_te = NULL;
5111 036813ee 2019-05-09 stsp }
5112 036813ee 2019-05-09 stsp return err;
5113 036813ee 2019-05-09 stsp }
5114 036813ee 2019-05-09 stsp
5115 036813ee 2019-05-09 stsp static const struct got_error *
5116 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5117 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5118 036813ee 2019-05-09 stsp {
5119 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5120 102b254e 2020-10-19 stsp char *ct_name = NULL;
5121 036813ee 2019-05-09 stsp
5122 036813ee 2019-05-09 stsp *new_te = NULL;
5123 036813ee 2019-05-09 stsp
5124 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5125 036813ee 2019-05-09 stsp if (*new_te == NULL)
5126 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5127 036813ee 2019-05-09 stsp
5128 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5129 102b254e 2020-10-19 stsp if (err)
5130 036813ee 2019-05-09 stsp goto done;
5131 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5132 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5133 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5134 036813ee 2019-05-09 stsp goto done;
5135 036813ee 2019-05-09 stsp }
5136 036813ee 2019-05-09 stsp
5137 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5138 036813ee 2019-05-09 stsp
5139 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5140 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5141 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5142 5f8a88c6 2019-08-03 stsp else
5143 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5144 036813ee 2019-05-09 stsp done:
5145 102b254e 2020-10-19 stsp free(ct_name);
5146 036813ee 2019-05-09 stsp if (err && *new_te) {
5147 56e0773d 2019-11-28 stsp free(*new_te);
5148 036813ee 2019-05-09 stsp *new_te = NULL;
5149 036813ee 2019-05-09 stsp }
5150 036813ee 2019-05-09 stsp return err;
5151 036813ee 2019-05-09 stsp }
5152 036813ee 2019-05-09 stsp
5153 036813ee 2019-05-09 stsp static const struct got_error *
5154 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5155 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5156 036813ee 2019-05-09 stsp {
5157 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5158 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5159 036813ee 2019-05-09 stsp
5160 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5161 036813ee 2019-05-09 stsp if (err)
5162 036813ee 2019-05-09 stsp return err;
5163 036813ee 2019-05-09 stsp if (new_pe == NULL)
5164 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5165 036813ee 2019-05-09 stsp return NULL;
5166 afa376bf 2019-05-09 stsp }
5167 afa376bf 2019-05-09 stsp
5168 afa376bf 2019-05-09 stsp static const struct got_error *
5169 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5170 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5171 afa376bf 2019-05-09 stsp {
5172 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5173 5f8a88c6 2019-08-03 stsp unsigned char status;
5174 5f8a88c6 2019-08-03 stsp
5175 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5176 afa376bf 2019-05-09 stsp ct_path++;
5177 5f8a88c6 2019-08-03 stsp
5178 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5179 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5180 5f8a88c6 2019-08-03 stsp else
5181 5f8a88c6 2019-08-03 stsp status = ct->status;
5182 5f8a88c6 2019-08-03 stsp
5183 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5184 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5185 036813ee 2019-05-09 stsp }
5186 036813ee 2019-05-09 stsp
5187 036813ee 2019-05-09 stsp static const struct got_error *
5188 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5189 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5190 44d03001 2019-05-09 stsp {
5191 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5192 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5193 44d03001 2019-05-09 stsp char *te_path;
5194 44d03001 2019-05-09 stsp
5195 44d03001 2019-05-09 stsp *modified = 0;
5196 44d03001 2019-05-09 stsp
5197 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5198 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5199 44d03001 2019-05-09 stsp te->name) == -1)
5200 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5201 44d03001 2019-05-09 stsp
5202 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5203 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5204 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5205 44d03001 2019-05-09 stsp strlen(te_path));
5206 62d463ca 2020-10-20 naddy if (*modified)
5207 44d03001 2019-05-09 stsp break;
5208 44d03001 2019-05-09 stsp }
5209 44d03001 2019-05-09 stsp
5210 44d03001 2019-05-09 stsp free(te_path);
5211 44d03001 2019-05-09 stsp return err;
5212 44d03001 2019-05-09 stsp }
5213 44d03001 2019-05-09 stsp
5214 44d03001 2019-05-09 stsp static const struct got_error *
5215 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5216 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5217 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5218 036813ee 2019-05-09 stsp {
5219 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5220 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5221 036813ee 2019-05-09 stsp
5222 036813ee 2019-05-09 stsp *ctp = NULL;
5223 036813ee 2019-05-09 stsp
5224 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5225 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5226 036813ee 2019-05-09 stsp char *ct_name = NULL;
5227 036813ee 2019-05-09 stsp int path_matches;
5228 036813ee 2019-05-09 stsp
5229 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5230 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5231 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5232 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5233 5f8a88c6 2019-08-03 stsp continue;
5234 5f8a88c6 2019-08-03 stsp } else {
5235 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5236 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5237 5f8a88c6 2019-08-03 stsp continue;
5238 5f8a88c6 2019-08-03 stsp }
5239 036813ee 2019-05-09 stsp
5240 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5241 036813ee 2019-05-09 stsp continue;
5242 036813ee 2019-05-09 stsp
5243 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5244 62d463ca 2020-10-20 naddy if (err)
5245 036813ee 2019-05-09 stsp return err;
5246 036813ee 2019-05-09 stsp if (!path_matches)
5247 036813ee 2019-05-09 stsp continue;
5248 036813ee 2019-05-09 stsp
5249 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5250 d34b633e 2020-10-19 stsp if (err)
5251 d34b633e 2020-10-19 stsp return err;
5252 d34b633e 2020-10-19 stsp
5253 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5254 d34b633e 2020-10-19 stsp free(ct_name);
5255 036813ee 2019-05-09 stsp continue;
5256 d34b633e 2020-10-19 stsp }
5257 d34b633e 2020-10-19 stsp free(ct_name);
5258 036813ee 2019-05-09 stsp
5259 036813ee 2019-05-09 stsp *ctp = ct;
5260 036813ee 2019-05-09 stsp break;
5261 036813ee 2019-05-09 stsp }
5262 2b496619 2019-07-10 stsp
5263 2b496619 2019-07-10 stsp return err;
5264 2b496619 2019-07-10 stsp }
5265 2b496619 2019-07-10 stsp
5266 2b496619 2019-07-10 stsp static const struct got_error *
5267 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5268 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5269 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5270 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5271 2b496619 2019-07-10 stsp struct got_repository *repo)
5272 2b496619 2019-07-10 stsp {
5273 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5274 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5275 2b496619 2019-07-10 stsp char *subtree_path;
5276 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5277 ba580f68 2020-03-22 stsp int nentries;
5278 2b496619 2019-07-10 stsp
5279 2b496619 2019-07-10 stsp *new_tep = NULL;
5280 2b496619 2019-07-10 stsp
5281 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5282 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5283 2b496619 2019-07-10 stsp child_path) == -1)
5284 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5285 036813ee 2019-05-09 stsp
5286 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5287 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5288 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5289 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5290 56e0773d 2019-11-28 stsp
5291 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5292 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5293 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5294 2b496619 2019-07-10 stsp goto done;
5295 2b496619 2019-07-10 stsp }
5296 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5297 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5298 2b496619 2019-07-10 stsp if (err) {
5299 56e0773d 2019-11-28 stsp free(new_te);
5300 2b496619 2019-07-10 stsp goto done;
5301 2b496619 2019-07-10 stsp }
5302 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5303 2b496619 2019-07-10 stsp done:
5304 56e0773d 2019-11-28 stsp free(id);
5305 2b496619 2019-07-10 stsp free(subtree_path);
5306 2b496619 2019-07-10 stsp if (err == NULL)
5307 2b496619 2019-07-10 stsp *new_tep = new_te;
5308 036813ee 2019-05-09 stsp return err;
5309 036813ee 2019-05-09 stsp }
5310 036813ee 2019-05-09 stsp
5311 036813ee 2019-05-09 stsp static const struct got_error *
5312 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5313 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5314 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5315 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5316 036813ee 2019-05-09 stsp struct got_repository *repo)
5317 036813ee 2019-05-09 stsp {
5318 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5319 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5320 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5321 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5322 036813ee 2019-05-09 stsp
5323 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5324 ba580f68 2020-03-22 stsp *nentries = 0;
5325 036813ee 2019-05-09 stsp
5326 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5327 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5328 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5329 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5330 036813ee 2019-05-09 stsp
5331 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5332 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5333 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5334 036813ee 2019-05-09 stsp continue;
5335 036813ee 2019-05-09 stsp
5336 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5337 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5338 036813ee 2019-05-09 stsp continue;
5339 036813ee 2019-05-09 stsp
5340 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5341 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5342 036813ee 2019-05-09 stsp if (err)
5343 036813ee 2019-05-09 stsp goto done;
5344 036813ee 2019-05-09 stsp
5345 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5346 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5347 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5348 036813ee 2019-05-09 stsp if (err)
5349 036813ee 2019-05-09 stsp goto done;
5350 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5351 afa376bf 2019-05-09 stsp if (err)
5352 afa376bf 2019-05-09 stsp goto done;
5353 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5354 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5355 2b496619 2019-07-10 stsp if (err)
5356 2f51b5b3 2019-05-09 stsp goto done;
5357 ba580f68 2020-03-22 stsp (*nentries)++;
5358 2b496619 2019-07-10 stsp } else {
5359 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5360 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5361 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5362 2b496619 2019-07-10 stsp == NULL) {
5363 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5364 2b496619 2019-07-10 stsp child_path, path_base_tree,
5365 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5366 2b496619 2019-07-10 stsp repo);
5367 2b496619 2019-07-10 stsp if (err)
5368 2b496619 2019-07-10 stsp goto done;
5369 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5370 2b496619 2019-07-10 stsp if (err)
5371 2b496619 2019-07-10 stsp goto done;
5372 ba580f68 2020-03-22 stsp (*nentries)++;
5373 9ba0479c 2019-05-10 stsp }
5374 ed175427 2019-05-09 stsp }
5375 2f51b5b3 2019-05-09 stsp }
5376 2f51b5b3 2019-05-09 stsp
5377 2f51b5b3 2019-05-09 stsp if (base_tree) {
5378 56e0773d 2019-11-28 stsp int i, nbase_entries;
5379 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5380 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5381 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5382 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5383 63c5ca5d 2019-08-24 stsp
5384 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5385 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5386 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5387 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5388 63c5ca5d 2019-08-24 stsp if (err)
5389 63c5ca5d 2019-08-24 stsp goto done;
5390 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5391 63c5ca5d 2019-08-24 stsp if (err)
5392 63c5ca5d 2019-08-24 stsp goto done;
5393 ba580f68 2020-03-22 stsp (*nentries)++;
5394 63c5ca5d 2019-08-24 stsp continue;
5395 63c5ca5d 2019-08-24 stsp }
5396 2f51b5b3 2019-05-09 stsp
5397 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5398 44d03001 2019-05-09 stsp int modified;
5399 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5400 036813ee 2019-05-09 stsp if (err)
5401 036813ee 2019-05-09 stsp goto done;
5402 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5403 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5404 2f51b5b3 2019-05-09 stsp if (err)
5405 2f51b5b3 2019-05-09 stsp goto done;
5406 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5407 44d03001 2019-05-09 stsp if (modified) {
5408 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5409 ba580f68 2020-03-22 stsp int nsubentries;
5410 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5411 ba580f68 2020-03-22 stsp &nsubentries, te,
5412 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5413 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5414 44d03001 2019-05-09 stsp if (err)
5415 44d03001 2019-05-09 stsp goto done;
5416 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5417 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5418 ba580f68 2020-03-22 stsp free(new_id);
5419 ba580f68 2020-03-22 stsp continue;
5420 ba580f68 2020-03-22 stsp }
5421 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5422 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5423 56e0773d 2019-11-28 stsp free(new_id);
5424 44d03001 2019-05-09 stsp }
5425 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5426 036813ee 2019-05-09 stsp if (err)
5427 036813ee 2019-05-09 stsp goto done;
5428 ba580f68 2020-03-22 stsp (*nentries)++;
5429 2f51b5b3 2019-05-09 stsp continue;
5430 036813ee 2019-05-09 stsp }
5431 2f51b5b3 2019-05-09 stsp
5432 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5433 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5434 f66c734c 2019-09-22 stsp if (err)
5435 f66c734c 2019-09-22 stsp goto done;
5436 2f51b5b3 2019-05-09 stsp if (ct) {
5437 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5438 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5439 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5440 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5441 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5442 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5443 2f51b5b3 2019-05-09 stsp if (err)
5444 2f51b5b3 2019-05-09 stsp goto done;
5445 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5446 2f51b5b3 2019-05-09 stsp if (err)
5447 2f51b5b3 2019-05-09 stsp goto done;
5448 ba580f68 2020-03-22 stsp (*nentries)++;
5449 2f51b5b3 2019-05-09 stsp }
5450 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5451 b416585c 2019-05-13 stsp status_arg);
5452 afa376bf 2019-05-09 stsp if (err)
5453 afa376bf 2019-05-09 stsp goto done;
5454 2f51b5b3 2019-05-09 stsp } else {
5455 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5456 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5457 2f51b5b3 2019-05-09 stsp if (err)
5458 2f51b5b3 2019-05-09 stsp goto done;
5459 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5460 2f51b5b3 2019-05-09 stsp if (err)
5461 2f51b5b3 2019-05-09 stsp goto done;
5462 ba580f68 2020-03-22 stsp (*nentries)++;
5463 2f51b5b3 2019-05-09 stsp }
5464 ca2503ea 2019-05-09 stsp }
5465 ed175427 2019-05-09 stsp }
5466 0b5cc0d6 2019-05-09 stsp
5467 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5468 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5469 ed175427 2019-05-09 stsp done:
5470 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5471 2e1fa222 2020-07-23 stsp return err;
5472 2e1fa222 2020-07-23 stsp }
5473 2e1fa222 2020-07-23 stsp
5474 2e1fa222 2020-07-23 stsp static const struct got_error *
5475 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5476 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5477 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5478 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5479 ebf99748 2019-05-09 stsp {
5480 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5481 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5482 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5483 ebf99748 2019-05-09 stsp
5484 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5485 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5486 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5487 ebf99748 2019-05-09 stsp
5488 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5489 437adc9d 2020-12-10 yzhong
5490 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5491 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5492 437adc9d 2020-12-10 yzhong if (err)
5493 437adc9d 2020-12-10 yzhong goto done;
5494 437adc9d 2020-12-10 yzhong
5495 ebf99748 2019-05-09 stsp if (ie) {
5496 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5497 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5498 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5499 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5500 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5501 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5502 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5503 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5504 437adc9d 2020-12-10 yzhong
5505 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5506 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5507 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5508 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5509 72fd46fa 2019-09-06 stsp !have_staged_files);
5510 ebf99748 2019-05-09 stsp } else
5511 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5512 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5513 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5514 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5515 72fd46fa 2019-09-06 stsp !have_staged_files);
5516 ebf99748 2019-05-09 stsp } else {
5517 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5518 ebf99748 2019-05-09 stsp if (err)
5519 437adc9d 2020-12-10 yzhong goto done;
5520 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5521 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5522 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5523 3969253a 2020-03-07 stsp if (err) {
5524 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5525 437adc9d 2020-12-10 yzhong goto done;
5526 3969253a 2020-03-07 stsp }
5527 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5528 3969253a 2020-03-07 stsp if (err) {
5529 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5530 437adc9d 2020-12-10 yzhong goto done;
5531 3969253a 2020-03-07 stsp }
5532 ebf99748 2019-05-09 stsp }
5533 437adc9d 2020-12-10 yzhong free(relpath);
5534 437adc9d 2020-12-10 yzhong relpath = NULL;
5535 ebf99748 2019-05-09 stsp }
5536 437adc9d 2020-12-10 yzhong done:
5537 437adc9d 2020-12-10 yzhong free(relpath);
5538 d56d26ce 2019-05-10 stsp return err;
5539 d56d26ce 2019-05-10 stsp }
5540 735ef5ac 2019-08-03 stsp
5541 d56d26ce 2019-05-10 stsp
5542 d56d26ce 2019-05-10 stsp static const struct got_error *
5543 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5544 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5545 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5546 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5547 735ef5ac 2019-08-03 stsp int ood_errcode)
5548 d56d26ce 2019-05-10 stsp {
5549 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5550 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5551 1a36436d 2019-06-10 stsp
5552 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5553 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5554 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5555 1a36436d 2019-06-10 stsp return NULL;
5556 9bead371 2019-07-28 stsp /*
5557 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5558 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5559 9bead371 2019-07-28 stsp */
5560 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5561 735ef5ac 2019-08-03 stsp in_repo_path);
5562 9bead371 2019-07-28 stsp if (err) {
5563 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5564 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5565 1a36436d 2019-06-10 stsp goto done;
5566 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5567 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5568 1a36436d 2019-06-10 stsp } else {
5569 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5570 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5571 735ef5ac 2019-08-03 stsp in_repo_path);
5572 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5573 1a36436d 2019-06-10 stsp goto done;
5574 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5575 1a36436d 2019-06-10 stsp }
5576 1a36436d 2019-06-10 stsp done:
5577 1a36436d 2019-06-10 stsp free(id);
5578 1a36436d 2019-06-10 stsp return err;
5579 ebf99748 2019-05-09 stsp }
5580 ebf99748 2019-05-09 stsp
5581 c4296144 2019-05-09 stsp const struct got_error *
5582 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5583 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5584 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5585 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5586 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5587 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5588 afa376bf 2019-05-09 stsp struct got_repository *repo)
5589 c4296144 2019-05-09 stsp {
5590 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5591 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5592 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5593 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5594 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5595 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5596 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5597 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5598 ba580f68 2020-03-22 stsp int nentries;
5599 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5600 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5601 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5602 c4296144 2019-05-09 stsp
5603 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5604 c4296144 2019-05-09 stsp
5605 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5606 675c7539 2019-05-09 stsp
5607 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5608 588edf97 2019-05-10 stsp if (err)
5609 588edf97 2019-05-10 stsp goto done;
5610 de18fc63 2019-05-09 stsp
5611 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5612 588edf97 2019-05-10 stsp if (err)
5613 588edf97 2019-05-10 stsp goto done;
5614 588edf97 2019-05-10 stsp
5615 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5616 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5617 33ad4cbe 2019-05-12 jcs if (err)
5618 33ad4cbe 2019-05-12 jcs goto done;
5619 33ad4cbe 2019-05-12 jcs }
5620 c4296144 2019-05-09 stsp
5621 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5622 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5623 33ad4cbe 2019-05-12 jcs goto done;
5624 33ad4cbe 2019-05-12 jcs }
5625 33ad4cbe 2019-05-12 jcs
5626 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5627 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5628 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5629 cf066bf8 2019-05-09 stsp char *ondisk_path;
5630 cf066bf8 2019-05-09 stsp
5631 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5632 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5633 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5634 5f8a88c6 2019-08-03 stsp continue;
5635 5f8a88c6 2019-08-03 stsp
5636 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5637 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5638 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5639 cf066bf8 2019-05-09 stsp continue;
5640 cf066bf8 2019-05-09 stsp
5641 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5642 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5643 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5644 cf066bf8 2019-05-09 stsp goto done;
5645 cf066bf8 2019-05-09 stsp }
5646 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5647 2e1fa222 2020-07-23 stsp free(ondisk_path);
5648 2e1fa222 2020-07-23 stsp if (err)
5649 cf066bf8 2019-05-09 stsp goto done;
5650 cf066bf8 2019-05-09 stsp }
5651 036813ee 2019-05-09 stsp
5652 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5653 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5654 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5655 036813ee 2019-05-09 stsp if (err)
5656 036813ee 2019-05-09 stsp goto done;
5657 036813ee 2019-05-09 stsp
5658 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5659 de18fc63 2019-05-09 stsp if (err)
5660 de18fc63 2019-05-09 stsp goto done;
5661 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5662 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5663 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5664 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5665 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5666 33ad4cbe 2019-05-12 jcs free(logmsg);
5667 9d40349a 2019-05-09 stsp if (err)
5668 9d40349a 2019-05-09 stsp goto done;
5669 9d40349a 2019-05-09 stsp
5670 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5671 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5672 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5673 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5674 09f5bd90 2019-05-09 stsp goto done;
5675 09f5bd90 2019-05-09 stsp }
5676 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5677 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5678 09f5bd90 2019-05-09 stsp if (err)
5679 09f5bd90 2019-05-09 stsp goto done;
5680 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5681 ebf99748 2019-05-09 stsp if (err)
5682 ebf99748 2019-05-09 stsp goto done;
5683 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5684 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5685 09f5bd90 2019-05-09 stsp goto done;
5686 09f5bd90 2019-05-09 stsp }
5687 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5688 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5689 f2c16586 2019-05-09 stsp if (err)
5690 f2c16586 2019-05-09 stsp goto done;
5691 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5692 f2c16586 2019-05-09 stsp if (err)
5693 f2c16586 2019-05-09 stsp goto done;
5694 f2c16586 2019-05-09 stsp
5695 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5696 09f5bd90 2019-05-09 stsp if (err)
5697 09f5bd90 2019-05-09 stsp goto done;
5698 09f5bd90 2019-05-09 stsp
5699 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5700 ebf99748 2019-05-09 stsp if (err)
5701 ebf99748 2019-05-09 stsp goto done;
5702 c4296144 2019-05-09 stsp done:
5703 588edf97 2019-05-10 stsp if (head_tree)
5704 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5705 588edf97 2019-05-10 stsp if (head_commit)
5706 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5707 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5708 2f17228e 2019-05-12 stsp if (head_ref2) {
5709 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5710 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5711 2f17228e 2019-05-12 stsp err = unlockerr;
5712 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5713 39cd0ff6 2019-07-12 stsp }
5714 39cd0ff6 2019-07-12 stsp return err;
5715 39cd0ff6 2019-07-12 stsp }
5716 5c1e53bc 2019-07-28 stsp
5717 5c1e53bc 2019-07-28 stsp static const struct got_error *
5718 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5719 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5720 5c1e53bc 2019-07-28 stsp {
5721 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5722 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5723 39cd0ff6 2019-07-12 stsp
5724 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5725 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5726 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5727 5c1e53bc 2019-07-28 stsp
5728 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5729 5c1e53bc 2019-07-28 stsp ct_path++;
5730 5c1e53bc 2019-07-28 stsp
5731 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5732 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5733 5c1e53bc 2019-07-28 stsp break;
5734 5c1e53bc 2019-07-28 stsp }
5735 5c1e53bc 2019-07-28 stsp
5736 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5737 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5738 5c1e53bc 2019-07-28 stsp
5739 5c1e53bc 2019-07-28 stsp return NULL;
5740 5c1e53bc 2019-07-28 stsp }
5741 5c1e53bc 2019-07-28 stsp
5742 f0b75401 2019-08-03 stsp static const struct got_error *
5743 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5744 f0b75401 2019-08-03 stsp {
5745 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5746 f0b75401 2019-08-03 stsp
5747 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5748 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5749 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5750 f0b75401 2019-08-03 stsp }
5751 f0b75401 2019-08-03 stsp
5752 f0b75401 2019-08-03 stsp return NULL;
5753 f0b75401 2019-08-03 stsp }
5754 f0b75401 2019-08-03 stsp
5755 5f8a88c6 2019-08-03 stsp static const struct got_error *
5756 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5757 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5758 5f8a88c6 2019-08-03 stsp {
5759 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5760 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5761 5f8a88c6 2019-08-03 stsp
5762 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5763 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5764 5f8a88c6 2019-08-03 stsp continue;
5765 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5766 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5767 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5768 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5769 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5770 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5771 5f8a88c6 2019-08-03 stsp }
5772 5f8a88c6 2019-08-03 stsp
5773 5f8a88c6 2019-08-03 stsp return NULL;
5774 5f8a88c6 2019-08-03 stsp }
5775 5f8a88c6 2019-08-03 stsp
5776 39cd0ff6 2019-07-12 stsp const struct got_error *
5777 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5778 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5779 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5780 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5781 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5782 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5783 39cd0ff6 2019-07-12 stsp {
5784 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5785 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5786 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5787 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5788 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5789 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5790 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5791 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5792 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5793 39cd0ff6 2019-07-12 stsp
5794 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5795 39cd0ff6 2019-07-12 stsp
5796 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5797 39cd0ff6 2019-07-12 stsp
5798 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5799 39cd0ff6 2019-07-12 stsp if (err)
5800 39cd0ff6 2019-07-12 stsp goto done;
5801 39cd0ff6 2019-07-12 stsp
5802 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5803 39cd0ff6 2019-07-12 stsp if (err)
5804 39cd0ff6 2019-07-12 stsp goto done;
5805 39cd0ff6 2019-07-12 stsp
5806 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5807 39cd0ff6 2019-07-12 stsp if (err)
5808 39cd0ff6 2019-07-12 stsp goto done;
5809 39cd0ff6 2019-07-12 stsp
5810 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5811 39cd0ff6 2019-07-12 stsp if (err)
5812 39cd0ff6 2019-07-12 stsp goto done;
5813 39cd0ff6 2019-07-12 stsp
5814 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5815 5f8a88c6 2019-08-03 stsp &have_staged_files);
5816 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5817 5f8a88c6 2019-08-03 stsp goto done;
5818 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5819 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5820 5f8a88c6 2019-08-03 stsp if (err)
5821 5f8a88c6 2019-08-03 stsp goto done;
5822 5f8a88c6 2019-08-03 stsp }
5823 5f8a88c6 2019-08-03 stsp
5824 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5825 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5826 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5827 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5828 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5829 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5830 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5831 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5832 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5833 5c1e53bc 2019-07-28 stsp if (err)
5834 5c1e53bc 2019-07-28 stsp goto done;
5835 5c1e53bc 2019-07-28 stsp }
5836 39cd0ff6 2019-07-12 stsp
5837 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5838 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5839 39cd0ff6 2019-07-12 stsp goto done;
5840 5c1e53bc 2019-07-28 stsp }
5841 5c1e53bc 2019-07-28 stsp
5842 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5843 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5844 5c1e53bc 2019-07-28 stsp if (err)
5845 5c1e53bc 2019-07-28 stsp goto done;
5846 39cd0ff6 2019-07-12 stsp }
5847 f0b75401 2019-08-03 stsp
5848 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5849 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5850 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5851 f0b75401 2019-08-03 stsp
5852 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5853 f0b75401 2019-08-03 stsp ct_path++;
5854 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5855 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5856 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5857 b50cabdf 2019-07-12 stsp if (err)
5858 b50cabdf 2019-07-12 stsp goto done;
5859 f0b75401 2019-08-03 stsp
5860 b50cabdf 2019-07-12 stsp }
5861 b50cabdf 2019-07-12 stsp
5862 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5863 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5864 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5865 39cd0ff6 2019-07-12 stsp if (err)
5866 39cd0ff6 2019-07-12 stsp goto done;
5867 39cd0ff6 2019-07-12 stsp
5868 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5869 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5870 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5871 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5872 39cd0ff6 2019-07-12 stsp err = sync_err;
5873 39cd0ff6 2019-07-12 stsp done:
5874 39cd0ff6 2019-07-12 stsp if (fileindex)
5875 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5876 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5877 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5878 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5879 39cd0ff6 2019-07-12 stsp err = unlockerr;
5880 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5881 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5882 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5883 39cd0ff6 2019-07-12 stsp }
5884 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5885 c4296144 2019-05-09 stsp return err;
5886 8656d6c4 2019-05-20 stsp }
5887 8656d6c4 2019-05-20 stsp
5888 8656d6c4 2019-05-20 stsp const char *
5889 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5890 8656d6c4 2019-05-20 stsp {
5891 8656d6c4 2019-05-20 stsp return ct->path;
5892 8656d6c4 2019-05-20 stsp }
5893 8656d6c4 2019-05-20 stsp
5894 8656d6c4 2019-05-20 stsp unsigned int
5895 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5896 8656d6c4 2019-05-20 stsp {
5897 8656d6c4 2019-05-20 stsp return ct->status;
5898 818c7501 2019-07-11 stsp }
5899 818c7501 2019-07-11 stsp
5900 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5901 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5902 818c7501 2019-07-11 stsp struct got_repository *repo;
5903 818c7501 2019-07-11 stsp };
5904 818c7501 2019-07-11 stsp
5905 818c7501 2019-07-11 stsp static const struct got_error *
5906 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5907 818c7501 2019-07-11 stsp {
5908 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5909 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5910 818c7501 2019-07-11 stsp unsigned char status;
5911 818c7501 2019-07-11 stsp struct stat sb;
5912 818c7501 2019-07-11 stsp char *ondisk_path;
5913 818c7501 2019-07-11 stsp
5914 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5915 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5916 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5917 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5918 818c7501 2019-07-11 stsp
5919 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5920 818c7501 2019-07-11 stsp == -1)
5921 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5922 818c7501 2019-07-11 stsp
5923 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5924 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5925 818c7501 2019-07-11 stsp free(ondisk_path);
5926 818c7501 2019-07-11 stsp if (err)
5927 818c7501 2019-07-11 stsp return err;
5928 818c7501 2019-07-11 stsp
5929 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5930 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5931 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5932 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5933 818c7501 2019-07-11 stsp
5934 818c7501 2019-07-11 stsp return NULL;
5935 818c7501 2019-07-11 stsp }
5936 818c7501 2019-07-11 stsp
5937 818c7501 2019-07-11 stsp const struct got_error *
5938 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5939 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5940 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5941 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5942 818c7501 2019-07-11 stsp {
5943 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5944 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5945 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5946 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5947 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5948 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5949 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5950 818c7501 2019-07-11 stsp
5951 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5952 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5953 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5954 818c7501 2019-07-11 stsp
5955 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5956 818c7501 2019-07-11 stsp if (err)
5957 818c7501 2019-07-11 stsp return err;
5958 818c7501 2019-07-11 stsp
5959 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5960 818c7501 2019-07-11 stsp if (err)
5961 818c7501 2019-07-11 stsp goto done;
5962 818c7501 2019-07-11 stsp
5963 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5964 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5965 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5966 818c7501 2019-07-11 stsp &ok_arg);
5967 818c7501 2019-07-11 stsp if (err)
5968 818c7501 2019-07-11 stsp goto done;
5969 818c7501 2019-07-11 stsp
5970 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5971 818c7501 2019-07-11 stsp if (err)
5972 818c7501 2019-07-11 stsp goto done;
5973 818c7501 2019-07-11 stsp
5974 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5975 818c7501 2019-07-11 stsp if (err)
5976 818c7501 2019-07-11 stsp goto done;
5977 818c7501 2019-07-11 stsp
5978 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5979 818c7501 2019-07-11 stsp if (err)
5980 818c7501 2019-07-11 stsp goto done;
5981 818c7501 2019-07-11 stsp
5982 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5983 818c7501 2019-07-11 stsp 0);
5984 e51d7b55 2020-01-04 stsp if (err)
5985 e51d7b55 2020-01-04 stsp goto done;
5986 e51d7b55 2020-01-04 stsp
5987 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5988 818c7501 2019-07-11 stsp if (err)
5989 818c7501 2019-07-11 stsp goto done;
5990 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5991 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5992 e51d7b55 2020-01-04 stsp goto done;
5993 e51d7b55 2020-01-04 stsp }
5994 818c7501 2019-07-11 stsp
5995 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5996 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5997 818c7501 2019-07-11 stsp if (err)
5998 818c7501 2019-07-11 stsp goto done;
5999 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6000 818c7501 2019-07-11 stsp if (err)
6001 818c7501 2019-07-11 stsp goto done;
6002 818c7501 2019-07-11 stsp
6003 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6004 818c7501 2019-07-11 stsp
6005 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6006 818c7501 2019-07-11 stsp if (err)
6007 818c7501 2019-07-11 stsp goto done;
6008 818c7501 2019-07-11 stsp
6009 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6010 818c7501 2019-07-11 stsp if (err)
6011 818c7501 2019-07-11 stsp goto done;
6012 818c7501 2019-07-11 stsp
6013 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6014 818c7501 2019-07-11 stsp worktree->base_commit_id);
6015 818c7501 2019-07-11 stsp if (err)
6016 818c7501 2019-07-11 stsp goto done;
6017 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6018 818c7501 2019-07-11 stsp if (err)
6019 818c7501 2019-07-11 stsp goto done;
6020 818c7501 2019-07-11 stsp
6021 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6022 818c7501 2019-07-11 stsp if (err)
6023 818c7501 2019-07-11 stsp goto done;
6024 818c7501 2019-07-11 stsp done:
6025 818c7501 2019-07-11 stsp free(fileindex_path);
6026 818c7501 2019-07-11 stsp free(tmp_branch_name);
6027 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6028 818c7501 2019-07-11 stsp free(branch_ref_name);
6029 818c7501 2019-07-11 stsp if (branch_ref)
6030 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6031 818c7501 2019-07-11 stsp if (wt_branch)
6032 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6033 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6034 818c7501 2019-07-11 stsp if (err) {
6035 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6036 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6037 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6038 818c7501 2019-07-11 stsp }
6039 818c7501 2019-07-11 stsp if (*tmp_branch) {
6040 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6041 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6042 818c7501 2019-07-11 stsp }
6043 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6044 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6045 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6046 3e3a69f1 2019-07-25 stsp }
6047 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6048 818c7501 2019-07-11 stsp }
6049 818c7501 2019-07-11 stsp return err;
6050 818c7501 2019-07-11 stsp }
6051 818c7501 2019-07-11 stsp
6052 818c7501 2019-07-11 stsp const struct got_error *
6053 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6054 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6055 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6056 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6057 818c7501 2019-07-11 stsp {
6058 818c7501 2019-07-11 stsp const struct got_error *err;
6059 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6060 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6061 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6062 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6063 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6064 818c7501 2019-07-11 stsp
6065 818c7501 2019-07-11 stsp *commit_id = NULL;
6066 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6067 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6068 3e3a69f1 2019-07-25 stsp *branch = NULL;
6069 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6070 3e3a69f1 2019-07-25 stsp
6071 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6072 3e3a69f1 2019-07-25 stsp if (err)
6073 3e3a69f1 2019-07-25 stsp return err;
6074 818c7501 2019-07-11 stsp
6075 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6076 3e3a69f1 2019-07-25 stsp if (err)
6077 f032f1f7 2019-08-04 stsp goto done;
6078 f032f1f7 2019-08-04 stsp
6079 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6080 f032f1f7 2019-08-04 stsp &have_staged_files);
6081 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6082 f032f1f7 2019-08-04 stsp goto done;
6083 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6084 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6085 3e3a69f1 2019-07-25 stsp goto done;
6086 f032f1f7 2019-08-04 stsp }
6087 3e3a69f1 2019-07-25 stsp
6088 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6089 818c7501 2019-07-11 stsp if (err)
6090 f032f1f7 2019-08-04 stsp goto done;
6091 818c7501 2019-07-11 stsp
6092 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6093 818c7501 2019-07-11 stsp if (err)
6094 818c7501 2019-07-11 stsp goto done;
6095 818c7501 2019-07-11 stsp
6096 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6097 818c7501 2019-07-11 stsp if (err)
6098 818c7501 2019-07-11 stsp goto done;
6099 818c7501 2019-07-11 stsp
6100 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6101 818c7501 2019-07-11 stsp if (err)
6102 818c7501 2019-07-11 stsp goto done;
6103 818c7501 2019-07-11 stsp
6104 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6105 818c7501 2019-07-11 stsp if (err)
6106 818c7501 2019-07-11 stsp goto done;
6107 818c7501 2019-07-11 stsp
6108 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6109 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6110 818c7501 2019-07-11 stsp if (err)
6111 818c7501 2019-07-11 stsp goto done;
6112 818c7501 2019-07-11 stsp
6113 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6114 818c7501 2019-07-11 stsp if (err)
6115 818c7501 2019-07-11 stsp goto done;
6116 818c7501 2019-07-11 stsp
6117 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6118 818c7501 2019-07-11 stsp if (err)
6119 818c7501 2019-07-11 stsp goto done;
6120 818c7501 2019-07-11 stsp
6121 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6122 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6123 818c7501 2019-07-11 stsp if (err)
6124 818c7501 2019-07-11 stsp goto done;
6125 818c7501 2019-07-11 stsp
6126 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6127 818c7501 2019-07-11 stsp if (err)
6128 818c7501 2019-07-11 stsp goto done;
6129 818c7501 2019-07-11 stsp done:
6130 818c7501 2019-07-11 stsp free(commit_ref_name);
6131 818c7501 2019-07-11 stsp free(branch_ref_name);
6132 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6133 818c7501 2019-07-11 stsp if (commit_ref)
6134 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6135 818c7501 2019-07-11 stsp if (branch_ref)
6136 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6137 818c7501 2019-07-11 stsp if (err) {
6138 818c7501 2019-07-11 stsp free(*commit_id);
6139 818c7501 2019-07-11 stsp *commit_id = NULL;
6140 818c7501 2019-07-11 stsp if (*tmp_branch) {
6141 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6142 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6143 818c7501 2019-07-11 stsp }
6144 818c7501 2019-07-11 stsp if (*new_base_branch) {
6145 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6146 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6147 818c7501 2019-07-11 stsp }
6148 818c7501 2019-07-11 stsp if (*branch) {
6149 818c7501 2019-07-11 stsp got_ref_close(*branch);
6150 818c7501 2019-07-11 stsp *branch = NULL;
6151 818c7501 2019-07-11 stsp }
6152 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6153 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6154 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6155 3e3a69f1 2019-07-25 stsp }
6156 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6157 818c7501 2019-07-11 stsp }
6158 818c7501 2019-07-11 stsp return err;
6159 c4296144 2019-05-09 stsp }
6160 818c7501 2019-07-11 stsp
6161 818c7501 2019-07-11 stsp const struct got_error *
6162 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6163 818c7501 2019-07-11 stsp {
6164 818c7501 2019-07-11 stsp const struct got_error *err;
6165 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6166 818c7501 2019-07-11 stsp
6167 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6168 818c7501 2019-07-11 stsp if (err)
6169 818c7501 2019-07-11 stsp return err;
6170 818c7501 2019-07-11 stsp
6171 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6172 818c7501 2019-07-11 stsp free(tmp_branch_name);
6173 818c7501 2019-07-11 stsp return NULL;
6174 818c7501 2019-07-11 stsp }
6175 818c7501 2019-07-11 stsp
6176 818c7501 2019-07-11 stsp static const struct got_error *
6177 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6178 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6179 818c7501 2019-07-11 stsp {
6180 0ebf8283 2019-07-24 stsp *logmsg = arg;
6181 818c7501 2019-07-11 stsp return NULL;
6182 818c7501 2019-07-11 stsp }
6183 818c7501 2019-07-11 stsp
6184 818c7501 2019-07-11 stsp static const struct got_error *
6185 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6186 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6187 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6188 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6189 818c7501 2019-07-11 stsp {
6190 818c7501 2019-07-11 stsp return NULL;
6191 01757395 2019-07-12 stsp }
6192 01757395 2019-07-12 stsp
6193 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6194 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6195 01757395 2019-07-12 stsp void *progress_arg;
6196 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6197 01757395 2019-07-12 stsp };
6198 01757395 2019-07-12 stsp
6199 01757395 2019-07-12 stsp static const struct got_error *
6200 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6201 01757395 2019-07-12 stsp {
6202 01757395 2019-07-12 stsp const struct got_error *err;
6203 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6204 01757395 2019-07-12 stsp char *p;
6205 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6206 01757395 2019-07-12 stsp
6207 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6208 01757395 2019-07-12 stsp if (err)
6209 01757395 2019-07-12 stsp return err;
6210 01757395 2019-07-12 stsp
6211 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6212 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6213 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6214 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6215 01757395 2019-07-12 stsp return NULL;
6216 01757395 2019-07-12 stsp
6217 01757395 2019-07-12 stsp p = strdup(path);
6218 01757395 2019-07-12 stsp if (p == NULL)
6219 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6220 01757395 2019-07-12 stsp
6221 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6222 01757395 2019-07-12 stsp if (err || new == NULL)
6223 01757395 2019-07-12 stsp free(p);
6224 01757395 2019-07-12 stsp return err;
6225 01757395 2019-07-12 stsp }
6226 01757395 2019-07-12 stsp
6227 01757395 2019-07-12 stsp void
6228 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6229 01757395 2019-07-12 stsp {
6230 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6231 01757395 2019-07-12 stsp
6232 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6233 01757395 2019-07-12 stsp free((char *)pe->path);
6234 01757395 2019-07-12 stsp
6235 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6236 818c7501 2019-07-11 stsp }
6237 818c7501 2019-07-11 stsp
6238 0ebf8283 2019-07-24 stsp static const struct got_error *
6239 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6240 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6241 818c7501 2019-07-11 stsp {
6242 818c7501 2019-07-11 stsp const struct got_error *err;
6243 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6244 818c7501 2019-07-11 stsp
6245 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6246 818c7501 2019-07-11 stsp if (err) {
6247 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6248 818c7501 2019-07-11 stsp goto done;
6249 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6250 818c7501 2019-07-11 stsp if (err)
6251 818c7501 2019-07-11 stsp goto done;
6252 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6253 818c7501 2019-07-11 stsp if (err)
6254 818c7501 2019-07-11 stsp goto done;
6255 de05890f 2020-03-05 stsp } else if (is_rebase) {
6256 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6257 818c7501 2019-07-11 stsp int cmp;
6258 818c7501 2019-07-11 stsp
6259 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6260 818c7501 2019-07-11 stsp if (err)
6261 818c7501 2019-07-11 stsp goto done;
6262 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6263 818c7501 2019-07-11 stsp free(stored_id);
6264 818c7501 2019-07-11 stsp if (cmp != 0) {
6265 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6266 818c7501 2019-07-11 stsp goto done;
6267 818c7501 2019-07-11 stsp }
6268 818c7501 2019-07-11 stsp }
6269 0ebf8283 2019-07-24 stsp done:
6270 0ebf8283 2019-07-24 stsp if (commit_ref)
6271 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6272 0ebf8283 2019-07-24 stsp return err;
6273 0ebf8283 2019-07-24 stsp }
6274 0ebf8283 2019-07-24 stsp
6275 0ebf8283 2019-07-24 stsp static const struct got_error *
6276 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6277 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6278 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6279 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6280 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6281 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6282 0ebf8283 2019-07-24 stsp {
6283 0ebf8283 2019-07-24 stsp const struct got_error *err;
6284 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6285 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6286 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6287 818c7501 2019-07-11 stsp
6288 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6289 0ebf8283 2019-07-24 stsp
6290 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6291 0ebf8283 2019-07-24 stsp if (err)
6292 0ebf8283 2019-07-24 stsp return err;
6293 0ebf8283 2019-07-24 stsp
6294 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6295 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6296 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6297 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6298 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6299 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6300 818c7501 2019-07-11 stsp if (commit_ref)
6301 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6302 818c7501 2019-07-11 stsp return err;
6303 818c7501 2019-07-11 stsp }
6304 818c7501 2019-07-11 stsp
6305 818c7501 2019-07-11 stsp const struct got_error *
6306 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6307 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6308 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6309 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6310 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6311 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6312 818c7501 2019-07-11 stsp {
6313 0ebf8283 2019-07-24 stsp const struct got_error *err;
6314 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6315 0ebf8283 2019-07-24 stsp
6316 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6317 0ebf8283 2019-07-24 stsp if (err)
6318 0ebf8283 2019-07-24 stsp return err;
6319 0ebf8283 2019-07-24 stsp
6320 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6321 0ebf8283 2019-07-24 stsp if (err)
6322 0ebf8283 2019-07-24 stsp goto done;
6323 0ebf8283 2019-07-24 stsp
6324 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6325 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6326 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6327 0ebf8283 2019-07-24 stsp done:
6328 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6329 0ebf8283 2019-07-24 stsp return err;
6330 0ebf8283 2019-07-24 stsp }
6331 0ebf8283 2019-07-24 stsp
6332 0ebf8283 2019-07-24 stsp const struct got_error *
6333 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6334 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6335 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6336 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6337 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6338 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6339 0ebf8283 2019-07-24 stsp {
6340 0ebf8283 2019-07-24 stsp const struct got_error *err;
6341 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6342 0ebf8283 2019-07-24 stsp
6343 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6344 0ebf8283 2019-07-24 stsp if (err)
6345 0ebf8283 2019-07-24 stsp return err;
6346 0ebf8283 2019-07-24 stsp
6347 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6348 0ebf8283 2019-07-24 stsp if (err)
6349 0ebf8283 2019-07-24 stsp goto done;
6350 0ebf8283 2019-07-24 stsp
6351 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6352 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6353 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6354 0ebf8283 2019-07-24 stsp done:
6355 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6356 0ebf8283 2019-07-24 stsp return err;
6357 0ebf8283 2019-07-24 stsp }
6358 0ebf8283 2019-07-24 stsp
6359 0ebf8283 2019-07-24 stsp static const struct got_error *
6360 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6361 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6362 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6363 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6364 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6365 0ebf8283 2019-07-24 stsp {
6366 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6367 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6368 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6369 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6370 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6371 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6372 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6373 818c7501 2019-07-11 stsp
6374 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6375 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6376 a0e95631 2019-07-12 stsp
6377 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6378 818c7501 2019-07-11 stsp
6379 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6380 a0e95631 2019-07-12 stsp if (err)
6381 3e3a69f1 2019-07-25 stsp return err;
6382 a0e95631 2019-07-12 stsp
6383 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6384 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6385 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6386 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6387 01757395 2019-07-12 stsp /*
6388 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6389 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6390 6c13b005 2021-09-02 stsp *
6391 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6392 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6393 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6394 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6395 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6396 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6397 01757395 2019-07-12 stsp */
6398 01757395 2019-07-12 stsp if (merged_paths) {
6399 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6400 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6401 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6402 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6403 f2a9dc41 2019-12-13 tracey 0);
6404 01757395 2019-07-12 stsp if (err)
6405 01757395 2019-07-12 stsp goto done;
6406 01757395 2019-07-12 stsp }
6407 01757395 2019-07-12 stsp } else {
6408 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6409 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6410 01757395 2019-07-12 stsp if (err)
6411 01757395 2019-07-12 stsp goto done;
6412 01757395 2019-07-12 stsp }
6413 a0e95631 2019-07-12 stsp
6414 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6415 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6416 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6417 a0e95631 2019-07-12 stsp if (err)
6418 a0e95631 2019-07-12 stsp goto done;
6419 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6420 a0e95631 2019-07-12 stsp goto done;
6421 ff0d2220 2019-07-11 stsp }
6422 818c7501 2019-07-11 stsp
6423 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6424 edd02c5e 2019-07-11 stsp if (err)
6425 edd02c5e 2019-07-11 stsp goto done;
6426 edd02c5e 2019-07-11 stsp
6427 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6428 818c7501 2019-07-11 stsp if (err)
6429 818c7501 2019-07-11 stsp goto done;
6430 818c7501 2019-07-11 stsp
6431 5943eee2 2019-08-13 stsp if (new_logmsg) {
6432 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6433 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6434 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6435 5943eee2 2019-08-13 stsp goto done;
6436 5943eee2 2019-08-13 stsp }
6437 5943eee2 2019-08-13 stsp } else {
6438 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6439 5943eee2 2019-08-13 stsp if (err)
6440 5943eee2 2019-08-13 stsp goto done;
6441 5943eee2 2019-08-13 stsp }
6442 0ebf8283 2019-07-24 stsp
6443 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6444 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6445 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6446 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6447 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6448 a0e95631 2019-07-12 stsp if (err)
6449 a0e95631 2019-07-12 stsp goto done;
6450 a0e95631 2019-07-12 stsp
6451 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6452 a0e95631 2019-07-12 stsp if (err)
6453 a0e95631 2019-07-12 stsp goto done;
6454 a0e95631 2019-07-12 stsp
6455 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6456 a0e95631 2019-07-12 stsp if (err)
6457 a0e95631 2019-07-12 stsp goto done;
6458 a0e95631 2019-07-12 stsp
6459 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6460 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6461 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6462 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6463 a0e95631 2019-07-12 stsp err = sync_err;
6464 818c7501 2019-07-11 stsp done:
6465 a0e95631 2019-07-12 stsp free(fileindex_path);
6466 a0e95631 2019-07-12 stsp free(head_commit_id);
6467 a0e95631 2019-07-12 stsp if (head_ref)
6468 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6469 818c7501 2019-07-11 stsp if (err) {
6470 818c7501 2019-07-11 stsp free(*new_commit_id);
6471 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6472 818c7501 2019-07-11 stsp }
6473 818c7501 2019-07-11 stsp return err;
6474 818c7501 2019-07-11 stsp }
6475 818c7501 2019-07-11 stsp
6476 818c7501 2019-07-11 stsp const struct got_error *
6477 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6478 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6479 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6480 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6481 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6482 0ebf8283 2019-07-24 stsp {
6483 0ebf8283 2019-07-24 stsp const struct got_error *err;
6484 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6485 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6486 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6487 0ebf8283 2019-07-24 stsp
6488 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6489 0ebf8283 2019-07-24 stsp if (err)
6490 0ebf8283 2019-07-24 stsp return err;
6491 0ebf8283 2019-07-24 stsp
6492 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6493 0ebf8283 2019-07-24 stsp if (err)
6494 0ebf8283 2019-07-24 stsp goto done;
6495 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6496 0ebf8283 2019-07-24 stsp if (err)
6497 0ebf8283 2019-07-24 stsp goto done;
6498 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6499 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6500 0ebf8283 2019-07-24 stsp goto done;
6501 0ebf8283 2019-07-24 stsp }
6502 0ebf8283 2019-07-24 stsp
6503 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6504 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6505 0ebf8283 2019-07-24 stsp done:
6506 0ebf8283 2019-07-24 stsp if (commit_ref)
6507 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6508 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6509 0ebf8283 2019-07-24 stsp free(commit_id);
6510 0ebf8283 2019-07-24 stsp return err;
6511 0ebf8283 2019-07-24 stsp }
6512 0ebf8283 2019-07-24 stsp
6513 0ebf8283 2019-07-24 stsp const struct got_error *
6514 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6515 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6516 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6517 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6518 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6519 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6520 0ebf8283 2019-07-24 stsp {
6521 0ebf8283 2019-07-24 stsp const struct got_error *err;
6522 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6523 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6524 0ebf8283 2019-07-24 stsp
6525 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6526 0ebf8283 2019-07-24 stsp if (err)
6527 0ebf8283 2019-07-24 stsp return err;
6528 0ebf8283 2019-07-24 stsp
6529 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6530 0ebf8283 2019-07-24 stsp if (err)
6531 0ebf8283 2019-07-24 stsp goto done;
6532 0ebf8283 2019-07-24 stsp
6533 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6534 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6535 0ebf8283 2019-07-24 stsp done:
6536 0ebf8283 2019-07-24 stsp if (commit_ref)
6537 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6538 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6539 0ebf8283 2019-07-24 stsp return err;
6540 0ebf8283 2019-07-24 stsp }
6541 0ebf8283 2019-07-24 stsp
6542 0ebf8283 2019-07-24 stsp const struct got_error *
6543 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6544 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6545 818c7501 2019-07-11 stsp {
6546 3e3a69f1 2019-07-25 stsp if (fileindex)
6547 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6548 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6549 69844fba 2019-07-11 stsp }
6550 69844fba 2019-07-11 stsp
6551 69844fba 2019-07-11 stsp static const struct got_error *
6552 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6553 69844fba 2019-07-11 stsp {
6554 69844fba 2019-07-11 stsp const struct got_error *err;
6555 69844fba 2019-07-11 stsp struct got_reference *ref;
6556 69844fba 2019-07-11 stsp
6557 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6558 69844fba 2019-07-11 stsp if (err) {
6559 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6560 69844fba 2019-07-11 stsp return NULL;
6561 69844fba 2019-07-11 stsp return err;
6562 69844fba 2019-07-11 stsp }
6563 69844fba 2019-07-11 stsp
6564 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6565 69844fba 2019-07-11 stsp got_ref_close(ref);
6566 69844fba 2019-07-11 stsp return err;
6567 818c7501 2019-07-11 stsp }
6568 818c7501 2019-07-11 stsp
6569 69844fba 2019-07-11 stsp static const struct got_error *
6570 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6571 69844fba 2019-07-11 stsp {
6572 69844fba 2019-07-11 stsp const struct got_error *err;
6573 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6574 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6575 69844fba 2019-07-11 stsp
6576 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6577 69844fba 2019-07-11 stsp if (err)
6578 69844fba 2019-07-11 stsp goto done;
6579 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6580 69844fba 2019-07-11 stsp if (err)
6581 69844fba 2019-07-11 stsp goto done;
6582 69844fba 2019-07-11 stsp
6583 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6584 69844fba 2019-07-11 stsp if (err)
6585 69844fba 2019-07-11 stsp goto done;
6586 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6587 69844fba 2019-07-11 stsp if (err)
6588 69844fba 2019-07-11 stsp goto done;
6589 69844fba 2019-07-11 stsp
6590 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6591 69844fba 2019-07-11 stsp if (err)
6592 69844fba 2019-07-11 stsp goto done;
6593 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6594 69844fba 2019-07-11 stsp if (err)
6595 69844fba 2019-07-11 stsp goto done;
6596 69844fba 2019-07-11 stsp
6597 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6598 69844fba 2019-07-11 stsp if (err)
6599 69844fba 2019-07-11 stsp goto done;
6600 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6601 69844fba 2019-07-11 stsp if (err)
6602 69844fba 2019-07-11 stsp goto done;
6603 69844fba 2019-07-11 stsp
6604 69844fba 2019-07-11 stsp done:
6605 69844fba 2019-07-11 stsp free(tmp_branch_name);
6606 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6607 69844fba 2019-07-11 stsp free(branch_ref_name);
6608 69844fba 2019-07-11 stsp free(commit_ref_name);
6609 e600f124 2021-03-21 stsp return err;
6610 e600f124 2021-03-21 stsp }
6611 e600f124 2021-03-21 stsp
6612 e600f124 2021-03-21 stsp const struct got_error *
6613 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6614 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6615 e600f124 2021-03-21 stsp {
6616 e600f124 2021-03-21 stsp const struct got_error *err;
6617 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6618 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6619 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6620 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6621 e600f124 2021-03-21 stsp char *refname = NULL;
6622 e600f124 2021-03-21 stsp
6623 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6624 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6625 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6626 e600f124 2021-03-21 stsp branch_name += 11;
6627 e600f124 2021-03-21 stsp
6628 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6629 e600f124 2021-03-21 stsp if (err)
6630 e600f124 2021-03-21 stsp return err;
6631 e600f124 2021-03-21 stsp
6632 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6633 e600f124 2021-03-21 stsp new_id_str) == -1) {
6634 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6635 e600f124 2021-03-21 stsp goto done;
6636 e600f124 2021-03-21 stsp }
6637 e600f124 2021-03-21 stsp
6638 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6639 e600f124 2021-03-21 stsp if (err)
6640 e600f124 2021-03-21 stsp goto done;
6641 e600f124 2021-03-21 stsp
6642 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6643 e600f124 2021-03-21 stsp if (err)
6644 e600f124 2021-03-21 stsp goto done;
6645 e600f124 2021-03-21 stsp
6646 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6647 e600f124 2021-03-21 stsp done:
6648 e600f124 2021-03-21 stsp free(new_id_str);
6649 e600f124 2021-03-21 stsp free(refname);
6650 e600f124 2021-03-21 stsp free(old_commit_id);
6651 e600f124 2021-03-21 stsp if (ref)
6652 e600f124 2021-03-21 stsp got_ref_close(ref);
6653 69844fba 2019-07-11 stsp return err;
6654 69844fba 2019-07-11 stsp }
6655 69844fba 2019-07-11 stsp
6656 818c7501 2019-07-11 stsp const struct got_error *
6657 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6658 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6659 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6660 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6661 818c7501 2019-07-11 stsp {
6662 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6663 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6664 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6665 818c7501 2019-07-11 stsp
6666 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6667 818c7501 2019-07-11 stsp if (err)
6668 818c7501 2019-07-11 stsp return err;
6669 e600f124 2021-03-21 stsp
6670 e600f124 2021-03-21 stsp if (create_backup) {
6671 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6672 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6673 e600f124 2021-03-21 stsp if (err)
6674 e600f124 2021-03-21 stsp goto done;
6675 e600f124 2021-03-21 stsp }
6676 818c7501 2019-07-11 stsp
6677 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6678 818c7501 2019-07-11 stsp if (err)
6679 818c7501 2019-07-11 stsp goto done;
6680 818c7501 2019-07-11 stsp
6681 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6682 818c7501 2019-07-11 stsp if (err)
6683 818c7501 2019-07-11 stsp goto done;
6684 818c7501 2019-07-11 stsp
6685 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6686 818c7501 2019-07-11 stsp if (err)
6687 818c7501 2019-07-11 stsp goto done;
6688 818c7501 2019-07-11 stsp
6689 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6690 a615e0e7 2020-12-16 stsp if (err)
6691 a615e0e7 2020-12-16 stsp goto done;
6692 a615e0e7 2020-12-16 stsp
6693 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6694 a615e0e7 2020-12-16 stsp if (err)
6695 a615e0e7 2020-12-16 stsp goto done;
6696 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6697 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6698 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6699 a615e0e7 2020-12-16 stsp err = sync_err;
6700 818c7501 2019-07-11 stsp done:
6701 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6702 a615e0e7 2020-12-16 stsp free(fileindex_path);
6703 818c7501 2019-07-11 stsp free(new_head_commit_id);
6704 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6705 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6706 818c7501 2019-07-11 stsp err = unlockerr;
6707 818c7501 2019-07-11 stsp return err;
6708 818c7501 2019-07-11 stsp }
6709 818c7501 2019-07-11 stsp
6710 818c7501 2019-07-11 stsp const struct got_error *
6711 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6712 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6713 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6714 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6715 818c7501 2019-07-11 stsp {
6716 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6717 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6718 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6719 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6720 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6721 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6722 818c7501 2019-07-11 stsp
6723 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6724 818c7501 2019-07-11 stsp if (err)
6725 818c7501 2019-07-11 stsp return err;
6726 818c7501 2019-07-11 stsp
6727 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6728 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6729 818c7501 2019-07-11 stsp if (err)
6730 818c7501 2019-07-11 stsp goto done;
6731 818c7501 2019-07-11 stsp
6732 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6733 818c7501 2019-07-11 stsp if (err)
6734 818c7501 2019-07-11 stsp goto done;
6735 818c7501 2019-07-11 stsp
6736 818c7501 2019-07-11 stsp /*
6737 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6738 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6739 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6740 818c7501 2019-07-11 stsp */
6741 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6742 818c7501 2019-07-11 stsp if (err)
6743 818c7501 2019-07-11 stsp goto done;
6744 818c7501 2019-07-11 stsp
6745 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6746 818c7501 2019-07-11 stsp if (err)
6747 818c7501 2019-07-11 stsp goto done;
6748 818c7501 2019-07-11 stsp
6749 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6750 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6751 ca355955 2019-07-12 stsp if (err)
6752 ca355955 2019-07-12 stsp goto done;
6753 ca355955 2019-07-12 stsp
6754 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6755 ca355955 2019-07-12 stsp if (err)
6756 ca355955 2019-07-12 stsp goto done;
6757 ca355955 2019-07-12 stsp
6758 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6759 818c7501 2019-07-11 stsp if (err)
6760 818c7501 2019-07-11 stsp goto done;
6761 818c7501 2019-07-11 stsp
6762 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6763 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6764 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6765 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6766 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6767 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6768 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6769 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6770 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6771 55bd499d 2019-07-12 stsp if (err)
6772 1f1abb7e 2019-08-08 stsp goto sync;
6773 55bd499d 2019-07-12 stsp
6774 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6775 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6776 ca355955 2019-07-12 stsp sync:
6777 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6778 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6779 ca355955 2019-07-12 stsp err = sync_err;
6780 818c7501 2019-07-11 stsp done:
6781 818c7501 2019-07-11 stsp got_ref_close(resolved);
6782 a3a2faf2 2019-07-12 stsp free(tree_id);
6783 818c7501 2019-07-11 stsp free(commit_id);
6784 0ebf8283 2019-07-24 stsp if (fileindex)
6785 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6786 0ebf8283 2019-07-24 stsp free(fileindex_path);
6787 0ebf8283 2019-07-24 stsp
6788 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6789 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6790 0ebf8283 2019-07-24 stsp err = unlockerr;
6791 0ebf8283 2019-07-24 stsp return err;
6792 0ebf8283 2019-07-24 stsp }
6793 0ebf8283 2019-07-24 stsp
6794 0ebf8283 2019-07-24 stsp const struct got_error *
6795 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6796 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6797 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6798 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6799 0ebf8283 2019-07-24 stsp {
6800 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6801 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6802 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6803 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6804 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6805 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6806 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6807 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6808 0ebf8283 2019-07-24 stsp
6809 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6810 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6811 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6812 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6813 0ebf8283 2019-07-24 stsp
6814 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6815 0ebf8283 2019-07-24 stsp if (err)
6816 0ebf8283 2019-07-24 stsp return err;
6817 0ebf8283 2019-07-24 stsp
6818 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6819 0ebf8283 2019-07-24 stsp if (err)
6820 0ebf8283 2019-07-24 stsp goto done;
6821 0ebf8283 2019-07-24 stsp
6822 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6823 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6824 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6825 0ebf8283 2019-07-24 stsp &ok_arg);
6826 0ebf8283 2019-07-24 stsp if (err)
6827 0ebf8283 2019-07-24 stsp goto done;
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6830 0ebf8283 2019-07-24 stsp if (err)
6831 0ebf8283 2019-07-24 stsp goto done;
6832 0ebf8283 2019-07-24 stsp
6833 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6834 0ebf8283 2019-07-24 stsp if (err)
6835 0ebf8283 2019-07-24 stsp goto done;
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6838 0ebf8283 2019-07-24 stsp worktree);
6839 0ebf8283 2019-07-24 stsp if (err)
6840 0ebf8283 2019-07-24 stsp goto done;
6841 0ebf8283 2019-07-24 stsp
6842 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6843 0ebf8283 2019-07-24 stsp 0);
6844 0ebf8283 2019-07-24 stsp if (err)
6845 0ebf8283 2019-07-24 stsp goto done;
6846 0ebf8283 2019-07-24 stsp
6847 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6848 0ebf8283 2019-07-24 stsp if (err)
6849 0ebf8283 2019-07-24 stsp goto done;
6850 0ebf8283 2019-07-24 stsp
6851 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6852 0ebf8283 2019-07-24 stsp if (err)
6853 0ebf8283 2019-07-24 stsp goto done;
6854 0ebf8283 2019-07-24 stsp
6855 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6856 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6857 0ebf8283 2019-07-24 stsp if (err)
6858 0ebf8283 2019-07-24 stsp goto done;
6859 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6860 0ebf8283 2019-07-24 stsp if (err)
6861 0ebf8283 2019-07-24 stsp goto done;
6862 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6863 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6864 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6865 0ebf8283 2019-07-24 stsp goto done;
6866 0ebf8283 2019-07-24 stsp }
6867 0ebf8283 2019-07-24 stsp
6868 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6869 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6870 0ebf8283 2019-07-24 stsp if (err)
6871 0ebf8283 2019-07-24 stsp goto done;
6872 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6873 0ebf8283 2019-07-24 stsp if (err)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp
6876 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6877 0ebf8283 2019-07-24 stsp if (err)
6878 0ebf8283 2019-07-24 stsp goto done;
6879 0ebf8283 2019-07-24 stsp done:
6880 0ebf8283 2019-07-24 stsp free(fileindex_path);
6881 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6882 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6883 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6884 0ebf8283 2019-07-24 stsp if (wt_branch)
6885 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6886 0ebf8283 2019-07-24 stsp if (err) {
6887 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6888 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6889 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6890 0ebf8283 2019-07-24 stsp }
6891 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6892 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6893 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6894 0ebf8283 2019-07-24 stsp }
6895 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6896 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6897 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6898 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6899 3e3a69f1 2019-07-25 stsp }
6900 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6901 0ebf8283 2019-07-24 stsp }
6902 0ebf8283 2019-07-24 stsp return err;
6903 0ebf8283 2019-07-24 stsp }
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp const struct got_error *
6906 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6907 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6908 0ebf8283 2019-07-24 stsp {
6909 3e3a69f1 2019-07-25 stsp if (fileindex)
6910 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6911 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6912 0ebf8283 2019-07-24 stsp }
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp const struct got_error *
6915 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6916 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6917 0ebf8283 2019-07-24 stsp {
6918 0ebf8283 2019-07-24 stsp const struct got_error *err;
6919 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp return err;
6924 0ebf8283 2019-07-24 stsp
6925 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6926 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6927 0ebf8283 2019-07-24 stsp return NULL;
6928 0ebf8283 2019-07-24 stsp }
6929 0ebf8283 2019-07-24 stsp
6930 0ebf8283 2019-07-24 stsp const struct got_error *
6931 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6932 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6933 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6934 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6935 0ebf8283 2019-07-24 stsp {
6936 0ebf8283 2019-07-24 stsp const struct got_error *err;
6937 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6938 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6939 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6940 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6941 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6942 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6943 0ebf8283 2019-07-24 stsp
6944 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6945 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6946 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6947 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6948 0ebf8283 2019-07-24 stsp
6949 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6950 3e3a69f1 2019-07-25 stsp if (err)
6951 3e3a69f1 2019-07-25 stsp return err;
6952 3e3a69f1 2019-07-25 stsp
6953 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6954 3e3a69f1 2019-07-25 stsp if (err)
6955 f032f1f7 2019-08-04 stsp goto done;
6956 f032f1f7 2019-08-04 stsp
6957 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6958 f032f1f7 2019-08-04 stsp &have_staged_files);
6959 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6960 f032f1f7 2019-08-04 stsp goto done;
6961 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6962 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6963 3e3a69f1 2019-07-25 stsp goto done;
6964 f032f1f7 2019-08-04 stsp }
6965 3e3a69f1 2019-07-25 stsp
6966 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6967 0ebf8283 2019-07-24 stsp if (err)
6968 f032f1f7 2019-08-04 stsp goto done;
6969 0ebf8283 2019-07-24 stsp
6970 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6971 0ebf8283 2019-07-24 stsp if (err)
6972 0ebf8283 2019-07-24 stsp goto done;
6973 0ebf8283 2019-07-24 stsp
6974 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6975 0ebf8283 2019-07-24 stsp if (err)
6976 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp
6978 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6979 0ebf8283 2019-07-24 stsp worktree);
6980 0ebf8283 2019-07-24 stsp if (err)
6981 0ebf8283 2019-07-24 stsp goto done;
6982 0ebf8283 2019-07-24 stsp
6983 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6984 0ebf8283 2019-07-24 stsp if (err)
6985 0ebf8283 2019-07-24 stsp goto done;
6986 0ebf8283 2019-07-24 stsp
6987 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6988 0ebf8283 2019-07-24 stsp if (err)
6989 0ebf8283 2019-07-24 stsp goto done;
6990 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6991 0ebf8283 2019-07-24 stsp if (err)
6992 0ebf8283 2019-07-24 stsp goto done;
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6995 0ebf8283 2019-07-24 stsp if (err)
6996 0ebf8283 2019-07-24 stsp goto done;
6997 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6998 0ebf8283 2019-07-24 stsp if (err)
6999 0ebf8283 2019-07-24 stsp goto done;
7000 0ebf8283 2019-07-24 stsp
7001 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7002 0ebf8283 2019-07-24 stsp if (err)
7003 0ebf8283 2019-07-24 stsp goto done;
7004 0ebf8283 2019-07-24 stsp done:
7005 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7006 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7007 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7008 0ebf8283 2019-07-24 stsp if (commit_ref)
7009 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7010 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7011 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7012 0ebf8283 2019-07-24 stsp if (err) {
7013 0ebf8283 2019-07-24 stsp free(*commit_id);
7014 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7015 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7016 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7017 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7018 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7019 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7020 0ebf8283 2019-07-24 stsp }
7021 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7022 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7023 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7024 3e3a69f1 2019-07-25 stsp }
7025 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7026 0ebf8283 2019-07-24 stsp }
7027 0ebf8283 2019-07-24 stsp return err;
7028 0ebf8283 2019-07-24 stsp }
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp static const struct got_error *
7031 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7032 0ebf8283 2019-07-24 stsp {
7033 0ebf8283 2019-07-24 stsp const struct got_error *err;
7034 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7035 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7036 0ebf8283 2019-07-24 stsp
7037 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7038 0ebf8283 2019-07-24 stsp if (err)
7039 0ebf8283 2019-07-24 stsp goto done;
7040 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
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_base_commit_ref_name(&base_commit_ref_name,
7045 0ebf8283 2019-07-24 stsp worktree);
7046 0ebf8283 2019-07-24 stsp if (err)
7047 0ebf8283 2019-07-24 stsp goto done;
7048 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7049 0ebf8283 2019-07-24 stsp if (err)
7050 0ebf8283 2019-07-24 stsp goto done;
7051 0ebf8283 2019-07-24 stsp
7052 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7053 0ebf8283 2019-07-24 stsp if (err)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7056 0ebf8283 2019-07-24 stsp if (err)
7057 0ebf8283 2019-07-24 stsp goto done;
7058 0ebf8283 2019-07-24 stsp
7059 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7060 0ebf8283 2019-07-24 stsp if (err)
7061 0ebf8283 2019-07-24 stsp goto done;
7062 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7063 0ebf8283 2019-07-24 stsp if (err)
7064 0ebf8283 2019-07-24 stsp goto done;
7065 0ebf8283 2019-07-24 stsp done:
7066 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7067 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7068 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7069 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7070 0ebf8283 2019-07-24 stsp return err;
7071 0ebf8283 2019-07-24 stsp }
7072 0ebf8283 2019-07-24 stsp
7073 0ebf8283 2019-07-24 stsp const struct got_error *
7074 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7075 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7076 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7077 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7078 0ebf8283 2019-07-24 stsp {
7079 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7080 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7081 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7082 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7083 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7084 0ebf8283 2019-07-24 stsp
7085 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7086 0ebf8283 2019-07-24 stsp if (err)
7087 0ebf8283 2019-07-24 stsp return err;
7088 0ebf8283 2019-07-24 stsp
7089 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7090 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7091 0ebf8283 2019-07-24 stsp if (err)
7092 0ebf8283 2019-07-24 stsp goto done;
7093 0ebf8283 2019-07-24 stsp
7094 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7095 0ebf8283 2019-07-24 stsp if (err)
7096 0ebf8283 2019-07-24 stsp goto done;
7097 0ebf8283 2019-07-24 stsp
7098 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7099 0ebf8283 2019-07-24 stsp if (err)
7100 0ebf8283 2019-07-24 stsp goto done;
7101 0ebf8283 2019-07-24 stsp
7102 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7103 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7104 0ebf8283 2019-07-24 stsp if (err)
7105 0ebf8283 2019-07-24 stsp goto done;
7106 0ebf8283 2019-07-24 stsp
7107 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7108 0ebf8283 2019-07-24 stsp if (err)
7109 0ebf8283 2019-07-24 stsp goto done;
7110 0ebf8283 2019-07-24 stsp
7111 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7112 0ebf8283 2019-07-24 stsp if (err)
7113 0ebf8283 2019-07-24 stsp goto done;
7114 0ebf8283 2019-07-24 stsp
7115 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7116 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7117 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7118 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7119 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7120 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7121 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7122 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7123 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7124 0ebf8283 2019-07-24 stsp if (err)
7125 1f1abb7e 2019-08-08 stsp goto sync;
7126 0ebf8283 2019-07-24 stsp
7127 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7128 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7129 0ebf8283 2019-07-24 stsp sync:
7130 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7131 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7132 0ebf8283 2019-07-24 stsp err = sync_err;
7133 0ebf8283 2019-07-24 stsp done:
7134 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7135 0ebf8283 2019-07-24 stsp free(tree_id);
7136 818c7501 2019-07-11 stsp free(fileindex_path);
7137 818c7501 2019-07-11 stsp
7138 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7139 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7140 818c7501 2019-07-11 stsp err = unlockerr;
7141 818c7501 2019-07-11 stsp return err;
7142 818c7501 2019-07-11 stsp }
7143 0ebf8283 2019-07-24 stsp
7144 0ebf8283 2019-07-24 stsp const struct got_error *
7145 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7146 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7147 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7148 0ebf8283 2019-07-24 stsp {
7149 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7150 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7151 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7152 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7153 0ebf8283 2019-07-24 stsp
7154 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7155 0ebf8283 2019-07-24 stsp if (err)
7156 0ebf8283 2019-07-24 stsp return err;
7157 0ebf8283 2019-07-24 stsp
7158 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7159 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7160 e600f124 2021-03-21 stsp if (err)
7161 e600f124 2021-03-21 stsp goto done;
7162 e600f124 2021-03-21 stsp
7163 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7164 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
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_ref_change_ref(resolved, new_head_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_ref_write(resolved, repo);
7173 0ebf8283 2019-07-24 stsp if (err)
7174 0ebf8283 2019-07-24 stsp goto done;
7175 0ebf8283 2019-07-24 stsp
7176 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7177 0ebf8283 2019-07-24 stsp if (err)
7178 0ebf8283 2019-07-24 stsp goto done;
7179 0ebf8283 2019-07-24 stsp
7180 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7181 a615e0e7 2020-12-16 stsp if (err)
7182 a615e0e7 2020-12-16 stsp goto done;
7183 a615e0e7 2020-12-16 stsp
7184 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7185 a615e0e7 2020-12-16 stsp if (err)
7186 a615e0e7 2020-12-16 stsp goto done;
7187 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7188 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7189 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7190 a615e0e7 2020-12-16 stsp err = sync_err;
7191 0ebf8283 2019-07-24 stsp done:
7192 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7193 a615e0e7 2020-12-16 stsp free(fileindex_path);
7194 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7195 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7196 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7197 0ebf8283 2019-07-24 stsp err = unlockerr;
7198 0ebf8283 2019-07-24 stsp return err;
7199 0ebf8283 2019-07-24 stsp }
7200 0ebf8283 2019-07-24 stsp
7201 0ebf8283 2019-07-24 stsp const struct got_error *
7202 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7203 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7204 0ebf8283 2019-07-24 stsp {
7205 0ebf8283 2019-07-24 stsp const struct got_error *err;
7206 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7207 0ebf8283 2019-07-24 stsp
7208 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp return err;
7211 0ebf8283 2019-07-24 stsp
7212 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7213 0ebf8283 2019-07-24 stsp if (err)
7214 0ebf8283 2019-07-24 stsp goto done;
7215 0ebf8283 2019-07-24 stsp
7216 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7217 0ebf8283 2019-07-24 stsp done:
7218 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7219 2822a352 2019-10-15 stsp return err;
7220 2822a352 2019-10-15 stsp }
7221 2822a352 2019-10-15 stsp
7222 2822a352 2019-10-15 stsp const struct got_error *
7223 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7224 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7225 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7226 2822a352 2019-10-15 stsp struct got_repository *repo)
7227 2822a352 2019-10-15 stsp {
7228 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7229 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7230 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7231 2822a352 2019-10-15 stsp
7232 2822a352 2019-10-15 stsp *fileindex = NULL;
7233 2822a352 2019-10-15 stsp *branch_ref = NULL;
7234 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7235 2822a352 2019-10-15 stsp
7236 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7237 2822a352 2019-10-15 stsp if (err)
7238 2822a352 2019-10-15 stsp return err;
7239 2822a352 2019-10-15 stsp
7240 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7241 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7242 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7243 2822a352 2019-10-15 stsp "update -b or different branch name required");
7244 2822a352 2019-10-15 stsp goto done;
7245 2822a352 2019-10-15 stsp }
7246 2822a352 2019-10-15 stsp
7247 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7248 2822a352 2019-10-15 stsp if (err)
7249 2822a352 2019-10-15 stsp goto done;
7250 2822a352 2019-10-15 stsp
7251 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7252 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7253 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7254 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7255 2822a352 2019-10-15 stsp &ok_arg);
7256 2822a352 2019-10-15 stsp if (err)
7257 2822a352 2019-10-15 stsp goto done;
7258 2822a352 2019-10-15 stsp
7259 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7260 2822a352 2019-10-15 stsp if (err)
7261 2822a352 2019-10-15 stsp goto done;
7262 2822a352 2019-10-15 stsp
7263 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7264 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7265 2822a352 2019-10-15 stsp done:
7266 2822a352 2019-10-15 stsp if (err) {
7267 2822a352 2019-10-15 stsp if (*branch_ref) {
7268 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7269 2822a352 2019-10-15 stsp *branch_ref = NULL;
7270 2822a352 2019-10-15 stsp }
7271 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7272 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7273 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7274 2822a352 2019-10-15 stsp }
7275 2822a352 2019-10-15 stsp if (*fileindex) {
7276 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7277 2822a352 2019-10-15 stsp *fileindex = NULL;
7278 2822a352 2019-10-15 stsp }
7279 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7280 2822a352 2019-10-15 stsp }
7281 0cb83759 2019-08-03 stsp return err;
7282 0cb83759 2019-08-03 stsp }
7283 0cb83759 2019-08-03 stsp
7284 2822a352 2019-10-15 stsp const struct got_error *
7285 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7286 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7287 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7288 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7289 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7290 2822a352 2019-10-15 stsp {
7291 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7292 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7293 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7294 2822a352 2019-10-15 stsp
7295 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7296 2822a352 2019-10-15 stsp if (err)
7297 2822a352 2019-10-15 stsp goto done;
7298 2822a352 2019-10-15 stsp
7299 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7300 2822a352 2019-10-15 stsp if (err)
7301 2822a352 2019-10-15 stsp goto done;
7302 2822a352 2019-10-15 stsp
7303 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7304 2822a352 2019-10-15 stsp worktree->path_prefix);
7305 2822a352 2019-10-15 stsp if (err)
7306 2822a352 2019-10-15 stsp goto done;
7307 2822a352 2019-10-15 stsp
7308 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7309 2822a352 2019-10-15 stsp if (err)
7310 2822a352 2019-10-15 stsp goto done;
7311 2822a352 2019-10-15 stsp
7312 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7313 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7314 2822a352 2019-10-15 stsp if (err)
7315 2822a352 2019-10-15 stsp goto sync;
7316 2822a352 2019-10-15 stsp
7317 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7318 2822a352 2019-10-15 stsp if (err)
7319 2822a352 2019-10-15 stsp goto sync;
7320 2822a352 2019-10-15 stsp
7321 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7322 6e210706 2021-01-22 stsp if (err)
7323 6e210706 2021-01-22 stsp goto sync;
7324 6e210706 2021-01-22 stsp
7325 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7326 2822a352 2019-10-15 stsp sync:
7327 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7328 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7329 2822a352 2019-10-15 stsp err = sync_err;
7330 2822a352 2019-10-15 stsp
7331 2822a352 2019-10-15 stsp done:
7332 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7333 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7334 2822a352 2019-10-15 stsp err = unlockerr;
7335 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7336 2822a352 2019-10-15 stsp
7337 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7338 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7339 2822a352 2019-10-15 stsp err = unlockerr;
7340 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7341 2822a352 2019-10-15 stsp
7342 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7343 2822a352 2019-10-15 stsp free(fileindex_path);
7344 2822a352 2019-10-15 stsp free(tree_id);
7345 2822a352 2019-10-15 stsp
7346 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7347 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7348 2822a352 2019-10-15 stsp err = unlockerr;
7349 2822a352 2019-10-15 stsp return err;
7350 2822a352 2019-10-15 stsp }
7351 2822a352 2019-10-15 stsp
7352 2822a352 2019-10-15 stsp const struct got_error *
7353 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7354 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7355 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7356 2822a352 2019-10-15 stsp {
7357 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7358 8b692cd0 2019-10-21 stsp
7359 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7360 8b692cd0 2019-10-21 stsp
7361 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7362 8b692cd0 2019-10-21 stsp
7363 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7364 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7365 8b692cd0 2019-10-21 stsp err = unlockerr;
7366 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7367 8b692cd0 2019-10-21 stsp
7368 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7369 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7370 8b692cd0 2019-10-21 stsp err = unlockerr;
7371 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7372 8b692cd0 2019-10-21 stsp
7373 8b692cd0 2019-10-21 stsp return err;
7374 2822a352 2019-10-15 stsp }
7375 2822a352 2019-10-15 stsp
7376 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7377 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7378 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7379 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7380 2db2652d 2019-08-07 stsp struct got_repository *repo;
7381 2db2652d 2019-08-07 stsp int have_changes;
7382 2db2652d 2019-08-07 stsp };
7383 2db2652d 2019-08-07 stsp
7384 2db2652d 2019-08-07 stsp const struct got_error *
7385 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7386 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7387 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7388 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7389 735ef5ac 2019-08-03 stsp {
7390 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7391 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7392 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7393 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7394 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7395 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7396 8b13ce36 2019-08-08 stsp
7397 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7398 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7399 8b13ce36 2019-08-08 stsp return NULL;
7400 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7401 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7402 735ef5ac 2019-08-03 stsp
7403 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7404 735ef5ac 2019-08-03 stsp if (ie == NULL)
7405 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7406 735ef5ac 2019-08-03 stsp
7407 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7408 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7409 735ef5ac 2019-08-03 stsp relpath) == -1)
7410 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7411 735ef5ac 2019-08-03 stsp
7412 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7413 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7414 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7415 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7416 735ef5ac 2019-08-03 stsp }
7417 735ef5ac 2019-08-03 stsp
7418 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7419 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7420 3aa5969e 2019-08-06 stsp goto done;
7421 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7422 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7423 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7424 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7425 735ef5ac 2019-08-03 stsp goto done;
7426 3aa5969e 2019-08-06 stsp }
7427 735ef5ac 2019-08-03 stsp
7428 2db2652d 2019-08-07 stsp a->have_changes = 1;
7429 2db2652d 2019-08-07 stsp
7430 735ef5ac 2019-08-03 stsp p = in_repo_path;
7431 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7432 735ef5ac 2019-08-03 stsp p++;
7433 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7434 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7435 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7436 735ef5ac 2019-08-03 stsp done:
7437 735ef5ac 2019-08-03 stsp free(in_repo_path);
7438 dc424a06 2019-08-07 stsp return err;
7439 dc424a06 2019-08-07 stsp }
7440 dc424a06 2019-08-07 stsp
7441 2db2652d 2019-08-07 stsp struct stage_path_arg {
7442 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7443 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7444 2db2652d 2019-08-07 stsp struct got_repository *repo;
7445 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7446 2db2652d 2019-08-07 stsp void *status_arg;
7447 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7448 2db2652d 2019-08-07 stsp void *patch_arg;
7449 7b5dc508 2019-10-28 stsp int staged_something;
7450 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7451 2db2652d 2019-08-07 stsp };
7452 2db2652d 2019-08-07 stsp
7453 2db2652d 2019-08-07 stsp static const struct got_error *
7454 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7455 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7456 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7457 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7458 0cb83759 2019-08-03 stsp {
7459 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7460 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7461 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7462 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7463 0cb83759 2019-08-03 stsp uint32_t stage;
7464 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7465 0aeb8099 2020-07-23 stsp struct stat sb;
7466 8b13ce36 2019-08-08 stsp
7467 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7468 8b13ce36 2019-08-08 stsp return NULL;
7469 0cb83759 2019-08-03 stsp
7470 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7471 d3e7c587 2019-08-03 stsp if (ie == NULL)
7472 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7473 0cb83759 2019-08-03 stsp
7474 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7475 2db2652d 2019-08-07 stsp relpath)== -1)
7476 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7477 0cb83759 2019-08-03 stsp
7478 0cb83759 2019-08-03 stsp switch (status) {
7479 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7480 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7481 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7482 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7483 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7484 0aeb8099 2020-07-23 stsp break;
7485 0aeb8099 2020-07-23 stsp }
7486 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7487 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7488 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7489 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7490 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7491 dc424a06 2019-08-07 stsp if (err)
7492 dc424a06 2019-08-07 stsp break;
7493 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7494 dc424a06 2019-08-07 stsp break;
7495 dc424a06 2019-08-07 stsp } else {
7496 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7497 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7498 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7499 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7500 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7501 dc424a06 2019-08-07 stsp break;
7502 dc424a06 2019-08-07 stsp }
7503 dc424a06 2019-08-07 stsp }
7504 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7505 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7506 0cb83759 2019-08-03 stsp if (err)
7507 dc424a06 2019-08-07 stsp break;
7508 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7509 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7510 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7511 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7512 0cb83759 2019-08-03 stsp else
7513 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7514 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7515 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7516 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7517 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7518 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7519 35213c7c 2020-07-23 stsp ssize_t target_len;
7520 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7521 35213c7c 2020-07-23 stsp sizeof(target_path));
7522 35213c7c 2020-07-23 stsp if (target_len == -1) {
7523 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7524 35213c7c 2020-07-23 stsp ondisk_path);
7525 35213c7c 2020-07-23 stsp break;
7526 35213c7c 2020-07-23 stsp }
7527 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7528 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7529 35213c7c 2020-07-23 stsp a->worktree->root_path);
7530 35213c7c 2020-07-23 stsp if (err)
7531 35213c7c 2020-07-23 stsp break;
7532 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7533 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7534 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7535 35213c7c 2020-07-23 stsp break;
7536 35213c7c 2020-07-23 stsp }
7537 35213c7c 2020-07-23 stsp }
7538 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7539 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7540 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7541 35213c7c 2020-07-23 stsp else
7542 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7543 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7544 0aeb8099 2020-07-23 stsp } else {
7545 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7546 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7547 0aeb8099 2020-07-23 stsp }
7548 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7549 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7550 dc424a06 2019-08-07 stsp break;
7551 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7552 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7553 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7554 0cb83759 2019-08-03 stsp break;
7555 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7556 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7557 d3e7c587 2019-08-03 stsp break;
7558 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7559 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7560 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7561 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7562 dc424a06 2019-08-07 stsp if (err)
7563 dc424a06 2019-08-07 stsp break;
7564 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7565 88f33a19 2019-08-08 stsp break;
7566 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7567 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7568 dc424a06 2019-08-07 stsp break;
7569 88f33a19 2019-08-08 stsp }
7570 dc424a06 2019-08-07 stsp }
7571 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7572 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7573 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7574 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7575 dc424a06 2019-08-07 stsp break;
7576 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7577 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7578 12463d8b 2019-12-13 stsp de_name);
7579 0cb83759 2019-08-03 stsp break;
7580 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7581 d3e7c587 2019-08-03 stsp break;
7582 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7583 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7584 ebf48fd5 2019-08-03 stsp break;
7585 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7586 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7587 2a06fe5f 2019-08-24 stsp break;
7588 0cb83759 2019-08-03 stsp default:
7589 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7590 537ac44b 2019-08-03 stsp break;
7591 0cb83759 2019-08-03 stsp }
7592 dc424a06 2019-08-07 stsp
7593 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7594 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7595 dc424a06 2019-08-07 stsp free(path_content);
7596 2db2652d 2019-08-07 stsp free(ondisk_path);
7597 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7598 0ebf8283 2019-07-24 stsp return err;
7599 0ebf8283 2019-07-24 stsp }
7600 0cb83759 2019-08-03 stsp
7601 0cb83759 2019-08-03 stsp const struct got_error *
7602 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7603 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7604 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7605 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7606 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7607 0cb83759 2019-08-03 stsp {
7608 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7609 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7610 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7611 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7612 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7613 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7614 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7615 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7616 0cb83759 2019-08-03 stsp
7617 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7618 0cb83759 2019-08-03 stsp if (err)
7619 0cb83759 2019-08-03 stsp return err;
7620 0cb83759 2019-08-03 stsp
7621 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7622 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7623 735ef5ac 2019-08-03 stsp if (err)
7624 735ef5ac 2019-08-03 stsp goto done;
7625 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7626 735ef5ac 2019-08-03 stsp if (err)
7627 735ef5ac 2019-08-03 stsp goto done;
7628 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7629 0cb83759 2019-08-03 stsp if (err)
7630 0cb83759 2019-08-03 stsp goto done;
7631 0cb83759 2019-08-03 stsp
7632 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7633 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7634 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7635 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7636 2db2652d 2019-08-07 stsp oka.repo = repo;
7637 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7638 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7639 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7640 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7641 735ef5ac 2019-08-03 stsp if (err)
7642 735ef5ac 2019-08-03 stsp goto done;
7643 735ef5ac 2019-08-03 stsp }
7644 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7645 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7646 2db2652d 2019-08-07 stsp goto done;
7647 2db2652d 2019-08-07 stsp }
7648 735ef5ac 2019-08-03 stsp
7649 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7650 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7651 2db2652d 2019-08-07 stsp spa.repo = repo;
7652 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7653 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7654 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7655 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7656 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7657 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7658 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7659 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7660 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7661 42005733 2019-08-03 stsp if (err)
7662 2db2652d 2019-08-07 stsp goto done;
7663 7b5dc508 2019-10-28 stsp }
7664 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7665 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7666 7b5dc508 2019-10-28 stsp goto done;
7667 0cb83759 2019-08-03 stsp }
7668 0cb83759 2019-08-03 stsp
7669 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7670 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7671 0cb83759 2019-08-03 stsp err = sync_err;
7672 0cb83759 2019-08-03 stsp done:
7673 735ef5ac 2019-08-03 stsp if (head_ref)
7674 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7675 735ef5ac 2019-08-03 stsp free(head_commit_id);
7676 0cb83759 2019-08-03 stsp free(fileindex_path);
7677 0cb83759 2019-08-03 stsp if (fileindex)
7678 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7679 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7680 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7681 0cb83759 2019-08-03 stsp err = unlockerr;
7682 0cb83759 2019-08-03 stsp return err;
7683 0cb83759 2019-08-03 stsp }
7684 ad493afc 2019-08-03 stsp
7685 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7686 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7687 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7688 ad493afc 2019-08-03 stsp struct got_repository *repo;
7689 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7690 ad493afc 2019-08-03 stsp void *progress_arg;
7691 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7692 2e1f37b0 2019-08-08 stsp void *patch_arg;
7693 ad493afc 2019-08-03 stsp };
7694 2e1f37b0 2019-08-08 stsp
7695 2e1f37b0 2019-08-08 stsp static const struct got_error *
7696 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7697 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7698 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7699 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7700 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7701 2e1f37b0 2019-08-08 stsp {
7702 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7703 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7704 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7705 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7706 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7707 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7708 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7709 2e1f37b0 2019-08-08 stsp
7710 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7711 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7712 2e1f37b0 2019-08-08 stsp
7713 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7714 2e1f37b0 2019-08-08 stsp if (err)
7715 2e1f37b0 2019-08-08 stsp return err;
7716 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7717 2e1f37b0 2019-08-08 stsp if (err)
7718 2e1f37b0 2019-08-08 stsp goto done;
7719 2e1f37b0 2019-08-08 stsp
7720 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7721 2e1f37b0 2019-08-08 stsp if (err)
7722 2e1f37b0 2019-08-08 stsp goto done;
7723 2e1f37b0 2019-08-08 stsp
7724 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7725 2e1f37b0 2019-08-08 stsp if (err)
7726 2e1f37b0 2019-08-08 stsp goto done;
7727 2e1f37b0 2019-08-08 stsp
7728 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7729 2e1f37b0 2019-08-08 stsp if (err)
7730 2e1f37b0 2019-08-08 stsp goto done;
7731 2e1f37b0 2019-08-08 stsp
7732 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7733 2e1f37b0 2019-08-08 stsp if (err)
7734 2e1f37b0 2019-08-08 stsp goto done;
7735 ad493afc 2019-08-03 stsp
7736 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7737 2e1f37b0 2019-08-08 stsp if (err)
7738 2e1f37b0 2019-08-08 stsp goto done;
7739 2e1f37b0 2019-08-08 stsp
7740 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7741 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7742 2e1f37b0 2019-08-08 stsp if (err)
7743 2e1f37b0 2019-08-08 stsp goto done;
7744 2e1f37b0 2019-08-08 stsp
7745 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7746 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7747 2e1f37b0 2019-08-08 stsp if (err)
7748 2e1f37b0 2019-08-08 stsp goto done;
7749 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7750 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7751 2e1f37b0 2019-08-08 stsp if (err)
7752 2e1f37b0 2019-08-08 stsp goto done;
7753 2e1f37b0 2019-08-08 stsp
7754 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7755 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7756 2e1f37b0 2019-08-08 stsp goto done;
7757 2e1f37b0 2019-08-08 stsp }
7758 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7759 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7760 2e1f37b0 2019-08-08 stsp goto done;
7761 2e1f37b0 2019-08-08 stsp }
7762 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7763 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7764 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7765 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7766 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7767 fe621944 2020-11-10 stsp nchanges++;
7768 fe621944 2020-11-10 stsp }
7769 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7770 2e1f37b0 2019-08-08 stsp int choice;
7771 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7772 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7773 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7774 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7775 2e1f37b0 2019-08-08 stsp if (err)
7776 2e1f37b0 2019-08-08 stsp goto done;
7777 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7778 2e1f37b0 2019-08-08 stsp have_content = 1;
7779 19e4b907 2019-08-08 stsp else
7780 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7781 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7782 2e1f37b0 2019-08-08 stsp break;
7783 2e1f37b0 2019-08-08 stsp }
7784 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7785 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7786 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7787 2e1f37b0 2019-08-08 stsp done:
7788 2e1f37b0 2019-08-08 stsp free(label1);
7789 2e1f37b0 2019-08-08 stsp if (blob)
7790 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7791 2e1f37b0 2019-08-08 stsp if (staged_blob)
7792 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7793 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7794 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7795 fe621944 2020-11-10 stsp err = free_err;
7796 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7797 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7798 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7799 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7800 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7801 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7802 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7803 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7804 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7805 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7806 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7807 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7808 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7809 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7810 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7811 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7812 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7813 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7814 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7815 2e1f37b0 2019-08-08 stsp }
7816 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7817 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7818 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7819 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7820 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7821 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7822 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7823 2e1f37b0 2019-08-08 stsp }
7824 2e1f37b0 2019-08-08 stsp free(path1);
7825 2e1f37b0 2019-08-08 stsp free(path2);
7826 fda8017d 2020-07-23 stsp return err;
7827 fda8017d 2020-07-23 stsp }
7828 fda8017d 2020-07-23 stsp
7829 fda8017d 2020-07-23 stsp static const struct got_error *
7830 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7831 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7832 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7833 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7834 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7835 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7836 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7837 fda8017d 2020-07-23 stsp {
7838 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7839 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7840 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7841 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
7842 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
7843 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7844 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7845 fda8017d 2020-07-23 stsp struct stat sb;
7846 fda8017d 2020-07-23 stsp
7847 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7848 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7849 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7850 fda8017d 2020-07-23 stsp if (err)
7851 fda8017d 2020-07-23 stsp return err;
7852 fda8017d 2020-07-23 stsp
7853 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7854 fda8017d 2020-07-23 stsp return NULL;
7855 fda8017d 2020-07-23 stsp
7856 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7857 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7858 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7859 fda8017d 2020-07-23 stsp if (err)
7860 fda8017d 2020-07-23 stsp goto done;
7861 fda8017d 2020-07-23 stsp }
7862 fda8017d 2020-07-23 stsp
7863 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7864 fda8017d 2020-07-23 stsp if (f == NULL) {
7865 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7866 fda8017d 2020-07-23 stsp path_unstaged_content);
7867 fda8017d 2020-07-23 stsp goto done;
7868 fda8017d 2020-07-23 stsp }
7869 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7870 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7871 fda8017d 2020-07-23 stsp goto done;
7872 fda8017d 2020-07-23 stsp }
7873 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7874 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7875 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7876 fda8017d 2020-07-23 stsp size_t r;
7877 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7878 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7879 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7880 fda8017d 2020-07-23 stsp goto done;
7881 fda8017d 2020-07-23 stsp }
7882 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7883 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7884 fda8017d 2020-07-23 stsp goto done;
7885 fda8017d 2020-07-23 stsp }
7886 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7887 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7888 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7889 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7890 fda8017d 2020-07-23 stsp progress_arg);
7891 fda8017d 2020-07-23 stsp } else {
7892 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7893 67a66647 2021-05-31 stsp
7894 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
7895 67a66647 2021-05-31 stsp if (err)
7896 67a66647 2021-05-31 stsp return err;
7897 67a66647 2021-05-31 stsp
7898 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7899 67a66647 2021-05-31 stsp parent) == -1) {
7900 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
7901 67a66647 2021-05-31 stsp base_path = NULL;
7902 67a66647 2021-05-31 stsp goto done;
7903 67a66647 2021-05-31 stsp }
7904 67a66647 2021-05-31 stsp
7905 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7906 67a66647 2021-05-31 stsp if (err)
7907 67a66647 2021-05-31 stsp goto done;
7908 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7909 67a66647 2021-05-31 stsp blob_base);
7910 67a66647 2021-05-31 stsp if (err)
7911 67a66647 2021-05-31 stsp goto done;
7912 67a66647 2021-05-31 stsp
7913 eec2f5a9 2021-06-03 stsp /*
7914 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
7915 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
7916 eec2f5a9 2021-06-03 stsp */
7917 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7918 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
7919 eec2f5a9 2021-06-03 stsp ondisk_path);
7920 eec2f5a9 2021-06-03 stsp if (err)
7921 eec2f5a9 2021-06-03 stsp goto done;
7922 eec2f5a9 2021-06-03 stsp } else {
7923 eec2f5a9 2021-06-03 stsp int fd;
7924 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7925 eec2f5a9 2021-06-03 stsp if (fd == -1) {
7926 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
7927 eec2f5a9 2021-06-03 stsp goto done;
7928 eec2f5a9 2021-06-03 stsp }
7929 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
7930 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
7931 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
7932 eec2f5a9 2021-06-03 stsp close(fd);
7933 eec2f5a9 2021-06-03 stsp goto done;
7934 eec2f5a9 2021-06-03 stsp }
7935 eec2f5a9 2021-06-03 stsp }
7936 eec2f5a9 2021-06-03 stsp
7937 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7938 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
7939 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7940 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7941 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7942 fda8017d 2020-07-23 stsp }
7943 fda8017d 2020-07-23 stsp if (err)
7944 fda8017d 2020-07-23 stsp goto done;
7945 fda8017d 2020-07-23 stsp
7946 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7947 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7948 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7949 2ac8aa02 2020-11-09 stsp } else {
7950 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7951 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7952 2ac8aa02 2020-11-09 stsp }
7953 fda8017d 2020-07-23 stsp done:
7954 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7955 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7956 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7957 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7958 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7959 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7960 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7961 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7962 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
7963 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
7964 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7965 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7966 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7967 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7968 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
7969 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7970 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7971 67a66647 2021-05-31 stsp free(blob_base_path);
7972 67a66647 2021-05-31 stsp free(parent);
7973 67a66647 2021-05-31 stsp free(base_path);
7974 2e1f37b0 2019-08-08 stsp return err;
7975 2e1f37b0 2019-08-08 stsp }
7976 2e1f37b0 2019-08-08 stsp
7977 ad493afc 2019-08-03 stsp static const struct got_error *
7978 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7979 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7980 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7981 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7982 ad493afc 2019-08-03 stsp {
7983 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7984 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7985 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7986 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7987 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7988 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7989 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7990 9bc94a15 2019-08-03 stsp struct stat sb;
7991 ad493afc 2019-08-03 stsp
7992 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7993 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7994 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7995 2e1f37b0 2019-08-08 stsp return NULL;
7996 2e1f37b0 2019-08-08 stsp
7997 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7998 ad493afc 2019-08-03 stsp if (ie == NULL)
7999 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8000 9bc94a15 2019-08-03 stsp
8001 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8002 9bc94a15 2019-08-03 stsp == -1)
8003 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8004 ad493afc 2019-08-03 stsp
8005 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8006 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8007 f69721c3 2019-10-21 stsp if (err)
8008 f69721c3 2019-10-21 stsp goto done;
8009 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8010 f69721c3 2019-10-21 stsp id_str) == -1) {
8011 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8012 f69721c3 2019-10-21 stsp goto done;
8013 f69721c3 2019-10-21 stsp }
8014 f69721c3 2019-10-21 stsp
8015 ad493afc 2019-08-03 stsp switch (staged_status) {
8016 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8017 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8018 ad493afc 2019-08-03 stsp blob_id, 8192);
8019 ad493afc 2019-08-03 stsp if (err)
8020 ad493afc 2019-08-03 stsp break;
8021 ad493afc 2019-08-03 stsp /* fall through */
8022 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8023 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8024 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8025 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8026 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8027 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8028 2e1f37b0 2019-08-08 stsp if (err)
8029 2e1f37b0 2019-08-08 stsp break;
8030 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8031 2e1f37b0 2019-08-08 stsp break;
8032 2e1f37b0 2019-08-08 stsp } else {
8033 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8034 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8035 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8036 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8037 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8038 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8039 2e1f37b0 2019-08-08 stsp }
8040 2e1f37b0 2019-08-08 stsp }
8041 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8042 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8043 ad493afc 2019-08-03 stsp if (err)
8044 ad493afc 2019-08-03 stsp break;
8045 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8046 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8047 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8048 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8049 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8050 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8051 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8052 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8053 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8054 ea7786be 2020-07-23 stsp break;
8055 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8056 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8057 36bf999c 2020-07-23 stsp char *staged_target;
8058 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8059 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8060 36bf999c 2020-07-23 stsp if (err)
8061 36bf999c 2020-07-23 stsp goto done;
8062 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8063 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8064 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8065 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8066 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8067 36bf999c 2020-07-23 stsp free(staged_target);
8068 dfe9fba0 2020-07-23 stsp } else {
8069 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8070 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8071 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8072 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8073 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8074 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8075 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8076 dfe9fba0 2020-07-23 stsp }
8077 ea7786be 2020-07-23 stsp break;
8078 ea7786be 2020-07-23 stsp default:
8079 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8080 ea7786be 2020-07-23 stsp break;
8081 ea7786be 2020-07-23 stsp }
8082 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8083 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8084 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8085 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8086 2ac8aa02 2020-11-09 stsp }
8087 ad493afc 2019-08-03 stsp break;
8088 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8089 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8090 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8091 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8092 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8093 2e1f37b0 2019-08-08 stsp if (err)
8094 2e1f37b0 2019-08-08 stsp break;
8095 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8096 2e1f37b0 2019-08-08 stsp break;
8097 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8098 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8099 2e1f37b0 2019-08-08 stsp break;
8100 2e1f37b0 2019-08-08 stsp }
8101 2e1f37b0 2019-08-08 stsp }
8102 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8103 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8104 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8105 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8106 9bc94a15 2019-08-03 stsp if (err)
8107 9bc94a15 2019-08-03 stsp break;
8108 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8109 ad493afc 2019-08-03 stsp break;
8110 ad493afc 2019-08-03 stsp }
8111 f69721c3 2019-10-21 stsp done:
8112 ad493afc 2019-08-03 stsp free(ondisk_path);
8113 ad493afc 2019-08-03 stsp if (blob_base)
8114 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8115 ad493afc 2019-08-03 stsp if (blob_staged)
8116 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8117 f69721c3 2019-10-21 stsp free(id_str);
8118 f69721c3 2019-10-21 stsp free(label_orig);
8119 ad493afc 2019-08-03 stsp return err;
8120 ad493afc 2019-08-03 stsp }
8121 ad493afc 2019-08-03 stsp
8122 ad493afc 2019-08-03 stsp const struct got_error *
8123 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8124 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8125 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8126 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8127 ad493afc 2019-08-03 stsp struct got_repository *repo)
8128 ad493afc 2019-08-03 stsp {
8129 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8130 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8131 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8132 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8133 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8134 ad493afc 2019-08-03 stsp
8135 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8136 ad493afc 2019-08-03 stsp if (err)
8137 ad493afc 2019-08-03 stsp return err;
8138 ad493afc 2019-08-03 stsp
8139 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8140 ad493afc 2019-08-03 stsp if (err)
8141 ad493afc 2019-08-03 stsp goto done;
8142 ad493afc 2019-08-03 stsp
8143 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8144 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8145 ad493afc 2019-08-03 stsp upa.repo = repo;
8146 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8147 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8148 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8149 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8150 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8151 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8152 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8153 ad493afc 2019-08-03 stsp if (err)
8154 ad493afc 2019-08-03 stsp goto done;
8155 ad493afc 2019-08-03 stsp }
8156 ad493afc 2019-08-03 stsp
8157 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8158 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8159 ad493afc 2019-08-03 stsp err = sync_err;
8160 ad493afc 2019-08-03 stsp done:
8161 ad493afc 2019-08-03 stsp free(fileindex_path);
8162 ad493afc 2019-08-03 stsp if (fileindex)
8163 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8164 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8165 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8166 ad493afc 2019-08-03 stsp err = unlockerr;
8167 ad493afc 2019-08-03 stsp return err;
8168 ad493afc 2019-08-03 stsp }
8169 b2118c49 2020-07-28 stsp
8170 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8171 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8172 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8173 b2118c49 2020-07-28 stsp void *info_arg;
8174 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8175 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8176 b2118c49 2020-07-28 stsp void *cancel_arg;
8177 b2118c49 2020-07-28 stsp };
8178 b2118c49 2020-07-28 stsp
8179 b2118c49 2020-07-28 stsp static const struct got_error *
8180 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8181 b2118c49 2020-07-28 stsp {
8182 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8183 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8184 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8185 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8186 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8187 b2118c49 2020-07-28 stsp int stage;
8188 b2118c49 2020-07-28 stsp
8189 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8190 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8191 b2118c49 2020-07-28 stsp
8192 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8193 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8194 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8195 b2118c49 2020-07-28 stsp break;
8196 b2118c49 2020-07-28 stsp }
8197 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8198 b2118c49 2020-07-28 stsp return NULL;
8199 b2118c49 2020-07-28 stsp
8200 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8201 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8202 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8203 b2118c49 2020-07-28 stsp }
8204 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8205 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8206 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8207 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8208 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8209 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8210 b2118c49 2020-07-28 stsp }
8211 b2118c49 2020-07-28 stsp
8212 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8213 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8214 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8215 b2118c49 2020-07-28 stsp }
8216 b2118c49 2020-07-28 stsp
8217 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8218 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8219 b2118c49 2020-07-28 stsp }
8220 b2118c49 2020-07-28 stsp
8221 b2118c49 2020-07-28 stsp const struct got_error *
8222 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8223 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8224 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8225 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8226 b2118c49 2020-07-28 stsp
8227 b2118c49 2020-07-28 stsp {
8228 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8229 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8230 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8231 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8232 b2118c49 2020-07-28 stsp
8233 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8234 b2118c49 2020-07-28 stsp if (err)
8235 b2118c49 2020-07-28 stsp return err;
8236 b2118c49 2020-07-28 stsp
8237 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8238 b2118c49 2020-07-28 stsp if (err)
8239 b2118c49 2020-07-28 stsp goto done;
8240 b2118c49 2020-07-28 stsp
8241 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8242 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8243 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8244 b2118c49 2020-07-28 stsp arg.paths = paths;
8245 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8246 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8247 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8248 b2118c49 2020-07-28 stsp &arg);
8249 b2118c49 2020-07-28 stsp done:
8250 b2118c49 2020-07-28 stsp free(fileindex_path);
8251 b2118c49 2020-07-28 stsp if (fileindex)
8252 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8253 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8254 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8255 b2118c49 2020-07-28 stsp err = unlockerr;
8256 b2118c49 2020-07-28 stsp return err;
8257 b2118c49 2020-07-28 stsp }