Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 eaccb85f 2018-12-25 stsp if (repo)
440 eaccb85f 2018-12-25 stsp got_repo_close(repo);
441 7ac97322 2018-03-11 stsp free(path_got);
442 056e7441 2018-03-11 stsp free(path_lock);
443 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
444 c442a90d 2019-03-10 stsp free(uuidstr);
445 bd165944 2019-03-10 stsp free(formatstr);
446 6d9d28c3 2018-03-11 stsp if (err) {
447 6d9d28c3 2018-03-11 stsp if (fd != -1)
448 6d9d28c3 2018-03-11 stsp close(fd);
449 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
450 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
451 6d9d28c3 2018-03-11 stsp *worktree = NULL;
452 6d9d28c3 2018-03-11 stsp } else
453 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
454 6d9d28c3 2018-03-11 stsp
455 6d9d28c3 2018-03-11 stsp return err;
456 86c3caaf 2018-03-09 stsp }
457 247140b2 2019-02-05 stsp
458 247140b2 2019-02-05 stsp const struct got_error *
459 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
460 247140b2 2019-02-05 stsp {
461 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
462 aedea96d 2020-10-20 stsp char *worktree_path;
463 86c3caaf 2018-03-09 stsp
464 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
465 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
466 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
467 aedea96d 2020-10-20 stsp
468 aedea96d 2020-10-20 stsp for (;;) {
469 aedea96d 2020-10-20 stsp char *parent_path;
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
472 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 aedea96d 2020-10-20 stsp free(worktree_path);
474 247140b2 2019-02-05 stsp return err;
475 aedea96d 2020-10-20 stsp }
476 aedea96d 2020-10-20 stsp if (*worktree) {
477 aedea96d 2020-10-20 stsp free(worktree_path);
478 aedea96d 2020-10-20 stsp return NULL;
479 aedea96d 2020-10-20 stsp }
480 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 aedea96d 2020-10-20 stsp break;
482 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
483 aedea96d 2020-10-20 stsp if (err) {
484 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
485 aedea96d 2020-10-20 stsp free(worktree_path);
486 aedea96d 2020-10-20 stsp return err;
487 aedea96d 2020-10-20 stsp }
488 aedea96d 2020-10-20 stsp break;
489 aedea96d 2020-10-20 stsp }
490 aedea96d 2020-10-20 stsp free(worktree_path);
491 aedea96d 2020-10-20 stsp worktree_path = parent_path;
492 aedea96d 2020-10-20 stsp }
493 247140b2 2019-02-05 stsp
494 aedea96d 2020-10-20 stsp free(worktree_path);
495 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
496 247140b2 2019-02-05 stsp }
497 247140b2 2019-02-05 stsp
498 3a6ce05a 2019-02-11 stsp const struct got_error *
499 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
500 86c3caaf 2018-03-09 stsp {
501 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
502 60e40e95 2021-01-22 stsp
503 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
504 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
505 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
506 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
507 a6b21eef 2021-01-21 stsp }
508 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
509 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
510 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
511 60e40e95 2021-01-22 stsp free(worktree->repo_path);
512 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
513 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
514 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
515 60e40e95 2021-01-22 stsp free(worktree->root_path);
516 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
517 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
518 a06ff56f 2021-01-21 naddy free(worktree);
519 3a6ce05a 2019-02-11 stsp return err;
520 86c3caaf 2018-03-09 stsp }
521 86c3caaf 2018-03-09 stsp
522 2fbdb5ae 2018-12-29 stsp const char *
523 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
524 c7f4312f 2019-02-05 stsp {
525 c7f4312f 2019-02-05 stsp return worktree->root_path;
526 c7f4312f 2019-02-05 stsp }
527 c7f4312f 2019-02-05 stsp
528 c7f4312f 2019-02-05 stsp const char *
529 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
530 86c3caaf 2018-03-09 stsp {
531 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
532 49520a32 2018-12-29 stsp }
533 49520a32 2018-12-29 stsp const char *
534 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
535 49520a32 2018-12-29 stsp {
536 f609be2e 2018-12-29 stsp return worktree->path_prefix;
537 e5dc7198 2018-12-29 stsp }
538 e5dc7198 2018-12-29 stsp
539 e5dc7198 2018-12-29 stsp const struct got_error *
540 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 e5dc7198 2018-12-29 stsp const char *path_prefix)
542 e5dc7198 2018-12-29 stsp {
543 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
544 e5dc7198 2018-12-29 stsp
545 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
546 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
548 e5dc7198 2018-12-29 stsp }
549 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
550 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
551 e5dc7198 2018-12-29 stsp free(absprefix);
552 e5dc7198 2018-12-29 stsp return NULL;
553 86c3caaf 2018-03-09 stsp }
554 86c3caaf 2018-03-09 stsp
555 bc70eb79 2019-05-09 stsp const char *
556 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
557 86c3caaf 2018-03-09 stsp {
558 36a38700 2019-05-10 stsp return worktree->head_ref_name;
559 024e9686 2019-05-14 stsp }
560 024e9686 2019-05-14 stsp
561 024e9686 2019-05-14 stsp const struct got_error *
562 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
563 024e9686 2019-05-14 stsp struct got_reference *head_ref)
564 024e9686 2019-05-14 stsp {
565 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
566 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
567 024e9686 2019-05-14 stsp
568 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
570 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
571 024e9686 2019-05-14 stsp path_got = NULL;
572 024e9686 2019-05-14 stsp goto done;
573 024e9686 2019-05-14 stsp }
574 024e9686 2019-05-14 stsp
575 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
576 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
577 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
578 024e9686 2019-05-14 stsp goto done;
579 024e9686 2019-05-14 stsp }
580 024e9686 2019-05-14 stsp
581 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
582 024e9686 2019-05-14 stsp if (err)
583 024e9686 2019-05-14 stsp goto done;
584 024e9686 2019-05-14 stsp
585 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
586 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
587 024e9686 2019-05-14 stsp done:
588 024e9686 2019-05-14 stsp free(path_got);
589 024e9686 2019-05-14 stsp if (err)
590 024e9686 2019-05-14 stsp free(head_ref_name);
591 024e9686 2019-05-14 stsp return err;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp
594 b72f483a 2019-02-05 stsp struct got_object_id *
595 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
596 507dc3bb 2018-12-29 stsp {
597 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
598 86c3caaf 2018-03-09 stsp }
599 86c3caaf 2018-03-09 stsp
600 507dc3bb 2018-12-29 stsp const struct got_error *
601 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
603 507dc3bb 2018-12-29 stsp {
604 507dc3bb 2018-12-29 stsp const struct got_error *err;
605 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
606 507dc3bb 2018-12-29 stsp char *id_str = NULL;
607 507dc3bb 2018-12-29 stsp char *path_got = NULL;
608 507dc3bb 2018-12-29 stsp
609 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
611 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
612 507dc3bb 2018-12-29 stsp path_got = NULL;
613 507dc3bb 2018-12-29 stsp goto done;
614 507dc3bb 2018-12-29 stsp }
615 507dc3bb 2018-12-29 stsp
616 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
617 507dc3bb 2018-12-29 stsp if (err)
618 507dc3bb 2018-12-29 stsp return err;
619 507dc3bb 2018-12-29 stsp
620 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
622 507dc3bb 2018-12-29 stsp goto done;
623 507dc3bb 2018-12-29 stsp }
624 507dc3bb 2018-12-29 stsp
625 507dc3bb 2018-12-29 stsp /* Record our base commit. */
626 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
627 507dc3bb 2018-12-29 stsp if (err)
628 507dc3bb 2018-12-29 stsp goto done;
629 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 507dc3bb 2018-12-29 stsp if (err)
631 507dc3bb 2018-12-29 stsp goto done;
632 507dc3bb 2018-12-29 stsp
633 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
634 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
635 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
637 507dc3bb 2018-12-29 stsp goto done;
638 507dc3bb 2018-12-29 stsp }
639 507dc3bb 2018-12-29 stsp done:
640 507dc3bb 2018-12-29 stsp if (obj)
641 507dc3bb 2018-12-29 stsp got_object_close(obj);
642 507dc3bb 2018-12-29 stsp free(id_str);
643 507dc3bb 2018-12-29 stsp free(path_got);
644 507dc3bb 2018-12-29 stsp return err;
645 50b0790e 2020-09-11 stsp }
646 50b0790e 2020-09-11 stsp
647 50b0790e 2020-09-11 stsp const struct got_gotconfig *
648 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
649 50b0790e 2020-09-11 stsp {
650 50b0790e 2020-09-11 stsp return worktree->gotconfig;
651 507dc3bb 2018-12-29 stsp }
652 507dc3bb 2018-12-29 stsp
653 9d31a1d8 2018-03-11 stsp static const struct got_error *
654 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
655 86c3caaf 2018-03-09 stsp {
656 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
659 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
660 86c3caaf 2018-03-09 stsp return NULL;
661 21908da4 2019-01-13 stsp }
662 21908da4 2019-01-13 stsp
663 21908da4 2019-01-13 stsp static const struct got_error *
664 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
665 4a1ddfc2 2019-01-12 stsp {
666 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
667 4a1ddfc2 2019-01-12 stsp char *abspath;
668 4a1ddfc2 2019-01-12 stsp
669 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
671 4a1ddfc2 2019-01-12 stsp
672 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
673 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 ddcd8544 2019-03-11 stsp struct stat sb;
675 ddcd8544 2019-03-11 stsp err = NULL;
676 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
677 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
678 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
679 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
680 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
681 ddcd8544 2019-03-11 stsp }
682 ddcd8544 2019-03-11 stsp }
683 4a1ddfc2 2019-01-12 stsp free(abspath);
684 68c76935 2019-02-19 stsp return err;
685 68c76935 2019-02-19 stsp }
686 68c76935 2019-02-19 stsp
687 68c76935 2019-02-19 stsp static const struct got_error *
688 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
689 68c76935 2019-02-19 stsp {
690 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
691 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
692 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
693 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp *same = 1;
696 68c76935 2019-02-19 stsp
697 230a42bd 2019-05-11 jcs for (;;) {
698 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
700 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
701 68c76935 2019-02-19 stsp break;
702 68c76935 2019-02-19 stsp }
703 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
706 68c76935 2019-02-19 stsp break;
707 68c76935 2019-02-19 stsp }
708 68c76935 2019-02-19 stsp if (flen1 == 0) {
709 68c76935 2019-02-19 stsp if (flen2 != 0)
710 68c76935 2019-02-19 stsp *same = 0;
711 68c76935 2019-02-19 stsp break;
712 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
713 68c76935 2019-02-19 stsp if (flen1 != 0)
714 68c76935 2019-02-19 stsp *same = 0;
715 68c76935 2019-02-19 stsp break;
716 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
717 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
718 68c76935 2019-02-19 stsp *same = 0;
719 68c76935 2019-02-19 stsp break;
720 68c76935 2019-02-19 stsp }
721 68c76935 2019-02-19 stsp } else {
722 68c76935 2019-02-19 stsp *same = 0;
723 68c76935 2019-02-19 stsp break;
724 68c76935 2019-02-19 stsp }
725 68c76935 2019-02-19 stsp }
726 68c76935 2019-02-19 stsp
727 68c76935 2019-02-19 stsp return err;
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp static const struct got_error *
731 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
732 68c76935 2019-02-19 stsp {
733 68c76935 2019-02-19 stsp struct stat sb;
734 68c76935 2019-02-19 stsp size_t size1, size2;
735 68c76935 2019-02-19 stsp
736 68c76935 2019-02-19 stsp *same = 1;
737 68c76935 2019-02-19 stsp
738 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
739 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
740 68c76935 2019-02-19 stsp size1 = sb.st_size;
741 68c76935 2019-02-19 stsp
742 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
743 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
744 68c76935 2019-02-19 stsp size2 = sb.st_size;
745 68c76935 2019-02-19 stsp
746 68c76935 2019-02-19 stsp if (size1 != size2) {
747 68c76935 2019-02-19 stsp *same = 0;
748 68c76935 2019-02-19 stsp return NULL;
749 68c76935 2019-02-19 stsp }
750 68c76935 2019-02-19 stsp
751 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
752 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
753 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
754 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
755 68c76935 2019-02-19 stsp
756 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
757 6353ad76 2019-02-08 stsp }
758 6353ad76 2019-02-08 stsp
759 6353ad76 2019-02-08 stsp /*
760 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
761 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
762 db590691 2021-06-02 stsp * and the file at ondisk_path acts as both the target and the
763 db590691 2021-06-02 stsp * second derived version
764 6353ad76 2019-02-08 stsp */
765 6353ad76 2019-02-08 stsp static const struct got_error *
766 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
767 db590691 2021-06-02 stsp FILE *f_orig, const char *ondisk_path,
768 db590691 2021-06-02 stsp const char *path, uint16_t st_mode, FILE *f_deriv,
769 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
770 f69721c3 2019-10-21 stsp struct got_repository *repo,
771 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
772 6353ad76 2019-02-08 stsp {
773 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
774 6353ad76 2019-02-08 stsp int merged_fd = -1;
775 db590691 2021-06-02 stsp FILE *f_merged = NULL;
776 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
777 234035bc 2019-06-01 stsp int overlapcnt = 0;
778 3524bbf9 2020-10-20 stsp char *parent = NULL;
779 db590691 2021-06-02 stsp FILE *f = NULL;
780 6353ad76 2019-02-08 stsp
781 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
782 234035bc 2019-06-01 stsp
783 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
784 3524bbf9 2020-10-20 stsp if (err)
785 3524bbf9 2020-10-20 stsp return err;
786 af54ae4a 2019-02-19 stsp
787 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
788 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
789 3524bbf9 2020-10-20 stsp goto done;
790 3524bbf9 2020-10-20 stsp }
791 af54ae4a 2019-02-19 stsp
792 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
793 6353ad76 2019-02-08 stsp if (err)
794 6353ad76 2019-02-08 stsp goto done;
795 6353ad76 2019-02-08 stsp
796 3b9f0f87 2020-07-23 stsp /*
797 db590691 2021-06-02 stsp * Open the merge target file.
798 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
799 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
800 3b9f0f87 2020-07-23 stsp */
801 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
802 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
803 3b9f0f87 2020-07-23 stsp ssize_t target_len;
804 3b9f0f87 2020-07-23 stsp size_t n;
805 3b9f0f87 2020-07-23 stsp
806 3b9f0f87 2020-07-23 stsp free(base_path);
807 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
808 3b9f0f87 2020-07-23 stsp parent) == -1) {
809 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
810 3b9f0f87 2020-07-23 stsp base_path = NULL;
811 3b9f0f87 2020-07-23 stsp goto done;
812 3b9f0f87 2020-07-23 stsp }
813 db590691 2021-06-02 stsp f = got_opentemp();
814 db590691 2021-06-02 stsp if (f == NULL) {
815 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
816 3b9f0f87 2020-07-23 stsp goto done;
817 db590691 2021-06-02 stsp }
818 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
819 3b9f0f87 2020-07-23 stsp sizeof(target_path));
820 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
821 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
822 3b9f0f87 2020-07-23 stsp goto done;
823 3b9f0f87 2020-07-23 stsp }
824 db590691 2021-06-02 stsp n = fwrite(target_path, 1, target_len, f);
825 3b9f0f87 2020-07-23 stsp if (n != target_len) {
826 db590691 2021-06-02 stsp err = got_ferror(f, GOT_ERR_IO);
827 3b9f0f87 2020-07-23 stsp goto done;
828 3b9f0f87 2020-07-23 stsp }
829 db590691 2021-06-02 stsp if (fflush(f) == EOF) {
830 db590691 2021-06-02 stsp err = got_error_from_errno("fflush");
831 3b9f0f87 2020-07-23 stsp goto done;
832 3b9f0f87 2020-07-23 stsp }
833 db590691 2021-06-02 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
834 db590691 2021-06-02 stsp err = got_ferror(f, GOT_ERR_IO);
835 db590691 2021-06-02 stsp goto done;
836 db590691 2021-06-02 stsp }
837 db590691 2021-06-02 stsp } else {
838 db590691 2021-06-02 stsp int fd;
839 db590691 2021-06-02 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
840 db590691 2021-06-02 stsp if (fd == -1) {
841 db590691 2021-06-02 stsp err = got_error_from_errno2("open", ondisk_path);
842 db590691 2021-06-02 stsp goto done;
843 db590691 2021-06-02 stsp }
844 db590691 2021-06-02 stsp f = fdopen(fd, "r");
845 db590691 2021-06-02 stsp if (f == NULL) {
846 db590691 2021-06-02 stsp err = got_error_from_errno2("fdopen", ondisk_path);
847 db590691 2021-06-02 stsp close(fd);
848 db590691 2021-06-02 stsp goto done;
849 db590691 2021-06-02 stsp }
850 3b9f0f87 2020-07-23 stsp }
851 3b9f0f87 2020-07-23 stsp
852 db590691 2021-06-02 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig, f,
853 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
854 6353ad76 2019-02-08 stsp if (err)
855 6353ad76 2019-02-08 stsp goto done;
856 6353ad76 2019-02-08 stsp
857 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
858 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
859 1ee397ad 2019-07-12 stsp if (err)
860 1ee397ad 2019-07-12 stsp goto done;
861 6353ad76 2019-02-08 stsp
862 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
863 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
864 816dc654 2019-02-16 stsp goto done;
865 68c76935 2019-02-19 stsp }
866 68c76935 2019-02-19 stsp
867 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
868 db590691 2021-06-02 stsp if (f_merged == NULL) {
869 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
870 db590691 2021-06-02 stsp goto done;
871 db590691 2021-06-02 stsp }
872 db590691 2021-06-02 stsp merged_fd = -1;
873 db590691 2021-06-02 stsp
874 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
875 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
876 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
877 db590691 2021-06-02 stsp f_merged);
878 68c76935 2019-02-19 stsp if (err)
879 68c76935 2019-02-19 stsp goto done;
880 816dc654 2019-02-16 stsp }
881 6353ad76 2019-02-08 stsp
882 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
883 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
884 70a0c8ec 2019-02-20 stsp goto done;
885 70a0c8ec 2019-02-20 stsp }
886 70a0c8ec 2019-02-20 stsp
887 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
888 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
889 230a42bd 2019-05-11 jcs ondisk_path);
890 6353ad76 2019-02-08 stsp goto done;
891 6353ad76 2019-02-08 stsp }
892 6353ad76 2019-02-08 stsp done:
893 fdcb7daf 2019-12-15 stsp if (err) {
894 fdcb7daf 2019-12-15 stsp if (merged_path)
895 fdcb7daf 2019-12-15 stsp unlink(merged_path);
896 3b9f0f87 2020-07-23 stsp }
897 db590691 2021-06-02 stsp if (f && fclose(f) == EOF && err == NULL)
898 db590691 2021-06-02 stsp err = got_error_from_errno("fclose");
899 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
900 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
901 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
902 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
903 6353ad76 2019-02-08 stsp free(merged_path);
904 af54ae4a 2019-02-19 stsp free(base_path);
905 3524bbf9 2020-10-20 stsp free(parent);
906 af57b12a 2020-07-23 stsp return err;
907 af57b12a 2020-07-23 stsp }
908 af57b12a 2020-07-23 stsp
909 af57b12a 2020-07-23 stsp static const struct got_error *
910 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
911 af57b12a 2020-07-23 stsp size_t target_len)
912 af57b12a 2020-07-23 stsp {
913 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
914 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
915 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
916 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
917 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
918 af57b12a 2020-07-23 stsp ondisk_path);
919 af57b12a 2020-07-23 stsp return NULL;
920 af57b12a 2020-07-23 stsp }
921 af57b12a 2020-07-23 stsp
922 af57b12a 2020-07-23 stsp /*
923 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
924 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
925 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
926 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
927 993e2a1b 2020-07-23 stsp *
928 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
929 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
930 993e2a1b 2020-07-23 stsp *
931 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
932 993e2a1b 2020-07-23 stsp * has deleted this symlink.
933 11cc08c1 2020-07-23 stsp */
934 11cc08c1 2020-07-23 stsp static const struct got_error *
935 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
936 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
937 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
938 11cc08c1 2020-07-23 stsp {
939 11cc08c1 2020-07-23 stsp const struct got_error *err;
940 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
941 11cc08c1 2020-07-23 stsp FILE *f = NULL;
942 11cc08c1 2020-07-23 stsp
943 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
944 11cc08c1 2020-07-23 stsp if (err)
945 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
946 11cc08c1 2020-07-23 stsp
947 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
948 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
949 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
950 11cc08c1 2020-07-23 stsp goto done;
951 11cc08c1 2020-07-23 stsp }
952 11cc08c1 2020-07-23 stsp
953 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
954 11cc08c1 2020-07-23 stsp if (err)
955 3818e3c4 2020-11-01 naddy goto done;
956 3818e3c4 2020-11-01 naddy
957 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
958 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
959 11cc08c1 2020-07-23 stsp goto done;
960 3818e3c4 2020-11-01 naddy }
961 11cc08c1 2020-07-23 stsp
962 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
963 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
964 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
965 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
966 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
967 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
968 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
969 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
970 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
971 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
972 11cc08c1 2020-07-23 stsp goto done;
973 11cc08c1 2020-07-23 stsp }
974 11cc08c1 2020-07-23 stsp
975 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
976 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
977 11cc08c1 2020-07-23 stsp goto done;
978 11cc08c1 2020-07-23 stsp }
979 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
980 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
981 11cc08c1 2020-07-23 stsp goto done;
982 11cc08c1 2020-07-23 stsp }
983 11cc08c1 2020-07-23 stsp done:
984 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
985 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
986 11cc08c1 2020-07-23 stsp free(path);
987 11cc08c1 2020-07-23 stsp free(id_str);
988 11cc08c1 2020-07-23 stsp free(label_deriv);
989 11cc08c1 2020-07-23 stsp return err;
990 11cc08c1 2020-07-23 stsp }
991 d219f183 2020-07-23 stsp
992 d219f183 2020-07-23 stsp /* forward declaration */
993 d219f183 2020-07-23 stsp static const struct got_error *
994 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
995 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
996 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
997 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
998 11cc08c1 2020-07-23 stsp
999 11cc08c1 2020-07-23 stsp /*
1000 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1001 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1002 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1003 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1004 af57b12a 2020-07-23 stsp */
1005 af57b12a 2020-07-23 stsp static const struct got_error *
1006 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1007 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1008 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1009 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1010 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1011 af57b12a 2020-07-23 stsp {
1012 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1013 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1014 af57b12a 2020-07-23 stsp struct stat sb;
1015 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1016 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1017 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1018 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1019 af57b12a 2020-07-23 stsp
1020 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1021 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1022 af57b12a 2020-07-23 stsp
1023 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1024 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1025 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1026 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1027 af57b12a 2020-07-23 stsp ondisk_path);
1028 af57b12a 2020-07-23 stsp goto done;
1029 af57b12a 2020-07-23 stsp }
1030 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1031 af57b12a 2020-07-23 stsp
1032 526a746f 2020-07-23 stsp if (blob_orig) {
1033 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1034 526a746f 2020-07-23 stsp if (err)
1035 526a746f 2020-07-23 stsp goto done;
1036 526a746f 2020-07-23 stsp }
1037 af57b12a 2020-07-23 stsp
1038 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1039 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1040 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1041 11cc08c1 2020-07-23 stsp have_local_change = 1;
1042 11cc08c1 2020-07-23 stsp
1043 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1044 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1045 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1046 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1047 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1048 11cc08c1 2020-07-23 stsp
1049 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1050 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1051 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1052 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1053 11cc08c1 2020-07-23 stsp path);
1054 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1055 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1056 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1057 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1058 11cc08c1 2020-07-23 stsp path);
1059 11cc08c1 2020-07-23 stsp } else {
1060 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1061 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1062 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1063 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1064 11cc08c1 2020-07-23 stsp if (err)
1065 11cc08c1 2020-07-23 stsp goto done;
1066 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1067 11cc08c1 2020-07-23 stsp path);
1068 11cc08c1 2020-07-23 stsp }
1069 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1070 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1071 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1072 af57b12a 2020-07-23 stsp strlen(deriv_target));
1073 af57b12a 2020-07-23 stsp if (err)
1074 af57b12a 2020-07-23 stsp goto done;
1075 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1076 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1077 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1078 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1079 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1080 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1081 ea7786be 2020-07-23 stsp path);
1082 ea7786be 2020-07-23 stsp } else {
1083 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1084 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1085 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1086 ea7786be 2020-07-23 stsp if (err)
1087 ea7786be 2020-07-23 stsp goto done;
1088 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1089 ea7786be 2020-07-23 stsp path);
1090 ea7786be 2020-07-23 stsp }
1091 6353ad76 2019-02-08 stsp }
1092 11cc08c1 2020-07-23 stsp
1093 af57b12a 2020-07-23 stsp done:
1094 af57b12a 2020-07-23 stsp free(ancestor_target);
1095 14c901f1 2019-08-08 stsp return err;
1096 14c901f1 2019-08-08 stsp }
1097 14c901f1 2019-08-08 stsp
1098 14c901f1 2019-08-08 stsp /*
1099 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1100 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1101 14c901f1 2019-08-08 stsp * acts as the second derived version.
1102 14c901f1 2019-08-08 stsp */
1103 14c901f1 2019-08-08 stsp static const struct got_error *
1104 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1105 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1106 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1107 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1108 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1109 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1110 14c901f1 2019-08-08 stsp {
1111 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1112 67a66647 2021-05-31 stsp FILE *f_orig = NULL, *f_deriv = NULL;
1113 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1114 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1115 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1116 14c901f1 2019-08-08 stsp
1117 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1118 14c901f1 2019-08-08 stsp
1119 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1120 ed6b5030 2020-10-20 stsp if (err)
1121 ed6b5030 2020-10-20 stsp return err;
1122 14c901f1 2019-08-08 stsp
1123 67a66647 2021-05-31 stsp if (blob_orig) {
1124 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1125 67a66647 2021-05-31 stsp parent) == -1) {
1126 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1127 67a66647 2021-05-31 stsp base_path = NULL;
1128 67a66647 2021-05-31 stsp goto done;
1129 67a66647 2021-05-31 stsp }
1130 67a66647 2021-05-31 stsp
1131 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1132 67a66647 2021-05-31 stsp if (err)
1133 67a66647 2021-05-31 stsp goto done;
1134 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1135 67a66647 2021-05-31 stsp blob_orig);
1136 67a66647 2021-05-31 stsp if (err)
1137 67a66647 2021-05-31 stsp goto done;
1138 67a66647 2021-05-31 stsp free(base_path);
1139 db590691 2021-06-02 stsp } else {
1140 db590691 2021-06-02 stsp /*
1141 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1142 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1143 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1144 db590691 2021-06-02 stsp */
1145 db590691 2021-06-02 stsp f_orig = got_opentemp();
1146 db590691 2021-06-02 stsp if (f_orig == NULL) {
1147 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1148 db590691 2021-06-02 stsp goto done;
1149 db590691 2021-06-02 stsp }
1150 67a66647 2021-05-31 stsp }
1151 67a66647 2021-05-31 stsp
1152 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1153 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1154 14c901f1 2019-08-08 stsp base_path = NULL;
1155 14c901f1 2019-08-08 stsp goto done;
1156 14c901f1 2019-08-08 stsp }
1157 14c901f1 2019-08-08 stsp
1158 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1159 14c901f1 2019-08-08 stsp if (err)
1160 14c901f1 2019-08-08 stsp goto done;
1161 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1162 14c901f1 2019-08-08 stsp blob_deriv);
1163 14c901f1 2019-08-08 stsp if (err)
1164 14c901f1 2019-08-08 stsp goto done;
1165 14c901f1 2019-08-08 stsp
1166 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1167 14c901f1 2019-08-08 stsp if (err)
1168 14c901f1 2019-08-08 stsp goto done;
1169 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1170 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1171 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1172 14c901f1 2019-08-08 stsp goto done;
1173 14c901f1 2019-08-08 stsp }
1174 14c901f1 2019-08-08 stsp
1175 db590691 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig,
1176 db590691 2021-06-02 stsp ondisk_path, path, st_mode, f_deriv, label_orig, label_deriv,
1177 db590691 2021-06-02 stsp repo, progress_cb, progress_arg);
1178 14c901f1 2019-08-08 stsp done:
1179 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1180 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1181 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1182 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1183 14c901f1 2019-08-08 stsp free(base_path);
1184 67a66647 2021-05-31 stsp if (blob_orig_path) {
1185 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1186 67a66647 2021-05-31 stsp free(blob_orig_path);
1187 67a66647 2021-05-31 stsp }
1188 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1189 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1190 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1191 14c901f1 2019-08-08 stsp }
1192 6353ad76 2019-02-08 stsp free(id_str);
1193 818c7501 2019-07-11 stsp free(label_deriv);
1194 ed6b5030 2020-10-20 stsp free(parent);
1195 4a1ddfc2 2019-01-12 stsp return err;
1196 4a1ddfc2 2019-01-12 stsp }
1197 4a1ddfc2 2019-01-12 stsp
1198 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1199 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1200 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1201 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1202 13d9040b 2019-03-27 stsp {
1203 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1204 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1205 13d9040b 2019-03-27 stsp
1206 65b05cec 2020-07-23 stsp *new_iep = NULL;
1207 65b05cec 2020-07-23 stsp
1208 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1209 054041d0 2020-06-24 stsp if (err)
1210 054041d0 2020-06-24 stsp return err;
1211 054041d0 2020-06-24 stsp
1212 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1213 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1214 054041d0 2020-06-24 stsp if (err)
1215 054041d0 2020-06-24 stsp goto done;
1216 054041d0 2020-06-24 stsp
1217 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1218 054041d0 2020-06-24 stsp done:
1219 054041d0 2020-06-24 stsp if (err)
1220 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1221 65b05cec 2020-07-23 stsp else
1222 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1223 13d9040b 2019-03-27 stsp return err;
1224 1ebedb77 2019-10-19 stsp }
1225 1ebedb77 2019-10-19 stsp
1226 1ebedb77 2019-10-19 stsp static mode_t
1227 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1228 1ebedb77 2019-10-19 stsp {
1229 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1230 1ebedb77 2019-10-19 stsp
1231 1ebedb77 2019-10-19 stsp if (executable) {
1232 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1233 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1234 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1235 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1236 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1237 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1238 1ebedb77 2019-10-19 stsp }
1239 1ebedb77 2019-10-19 stsp
1240 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1241 13d9040b 2019-03-27 stsp }
1242 13d9040b 2019-03-27 stsp
1243 8ba819a3 2020-07-23 stsp /* forward declaration */
1244 13d9040b 2019-03-27 stsp static const struct got_error *
1245 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1246 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1247 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1248 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1249 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1250 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1251 8ba819a3 2020-07-23 stsp
1252 5a1fbc73 2020-07-23 stsp /*
1253 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1254 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1255 5a1fbc73 2020-07-23 stsp */
1256 8ba819a3 2020-07-23 stsp static const struct got_error *
1257 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1258 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1259 5a1fbc73 2020-07-23 stsp {
1260 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1261 5a1fbc73 2020-07-23 stsp ssize_t elen;
1262 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1263 5a1fbc73 2020-07-23 stsp int fd;
1264 5a1fbc73 2020-07-23 stsp
1265 c6e8a826 2021-04-05 stsp *did_something = 0;
1266 c6e8a826 2021-04-05 stsp
1267 5a1fbc73 2020-07-23 stsp /*
1268 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1269 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1270 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1271 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1272 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1273 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1274 5a1fbc73 2020-07-23 stsp */
1275 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1276 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1277 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1278 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1279 5a1fbc73 2020-07-23 stsp
1280 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1281 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1282 5a1fbc73 2020-07-23 stsp if (elen == -1)
1283 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1284 5a1fbc73 2020-07-23 stsp
1285 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1286 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1287 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1288 5a1fbc73 2020-07-23 stsp }
1289 5a1fbc73 2020-07-23 stsp
1290 c6e8a826 2021-04-05 stsp *did_something = 1;
1291 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1292 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1293 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1294 5a1fbc73 2020-07-23 stsp return err;
1295 3c1ec782 2020-07-23 stsp }
1296 3c1ec782 2020-07-23 stsp
1297 3c1ec782 2020-07-23 stsp static const struct got_error *
1298 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1299 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1300 3c1ec782 2020-07-23 stsp {
1301 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1302 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1303 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1304 3c1ec782 2020-07-23 stsp
1305 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1306 3c1ec782 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1308 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1309 3c1ec782 2020-07-23 stsp return NULL;
1310 3c1ec782 2020-07-23 stsp }
1311 3c1ec782 2020-07-23 stsp
1312 3c1ec782 2020-07-23 stsp /*
1313 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1314 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1315 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1316 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1317 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1318 3c1ec782 2020-07-23 stsp */
1319 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1320 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1321 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1322 ce031e9e 2020-10-20 stsp if (err)
1323 ce031e9e 2020-10-20 stsp return err;
1324 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1325 ce031e9e 2020-10-20 stsp free(parent);
1326 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1327 ce031e9e 2020-10-20 stsp }
1328 ce031e9e 2020-10-20 stsp free(parent);
1329 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1330 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1331 3c1ec782 2020-07-23 stsp free(abspath);
1332 3c1ec782 2020-07-23 stsp return err;
1333 3c1ec782 2020-07-23 stsp }
1334 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1335 3c1ec782 2020-07-23 stsp free(abspath);
1336 3c1ec782 2020-07-23 stsp if (err)
1337 3c1ec782 2020-07-23 stsp return err;
1338 3c1ec782 2020-07-23 stsp } else {
1339 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1340 3c1ec782 2020-07-23 stsp if (err)
1341 3c1ec782 2020-07-23 stsp return err;
1342 3c1ec782 2020-07-23 stsp }
1343 3c1ec782 2020-07-23 stsp
1344 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1345 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1346 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1347 3c1ec782 2020-07-23 stsp return NULL;
1348 3c1ec782 2020-07-23 stsp }
1349 3c1ec782 2020-07-23 stsp
1350 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1351 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1352 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1353 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1354 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1355 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1356 3c1ec782 2020-07-23 stsp
1357 3c1ec782 2020-07-23 stsp free(path_got);
1358 3c1ec782 2020-07-23 stsp return NULL;
1359 5a1fbc73 2020-07-23 stsp }
1360 5a1fbc73 2020-07-23 stsp
1361 5a1fbc73 2020-07-23 stsp static const struct got_error *
1362 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1363 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1364 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1365 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1366 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1367 9d31a1d8 2018-03-11 stsp {
1368 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1369 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1370 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1371 906c123b 2020-07-23 stsp char *path_got = NULL;
1372 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1373 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1374 8ba819a3 2020-07-23 stsp
1375 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1376 2e1fa222 2020-07-23 stsp
1377 8ba819a3 2020-07-23 stsp /*
1378 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1379 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1380 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1381 8ba819a3 2020-07-23 stsp * in the blob object.
1382 8ba819a3 2020-07-23 stsp */
1383 8ba819a3 2020-07-23 stsp do {
1384 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1385 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1386 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1387 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1388 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1389 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1390 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1392 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1393 3b9f0f87 2020-07-23 stsp progress_arg);
1394 8ba819a3 2020-07-23 stsp }
1395 8ba819a3 2020-07-23 stsp if (len > 0) {
1396 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1397 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1398 8ba819a3 2020-07-23 stsp len - hdrlen);
1399 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1400 8ba819a3 2020-07-23 stsp hdrlen = 0;
1401 8ba819a3 2020-07-23 stsp }
1402 8ba819a3 2020-07-23 stsp } while (len != 0);
1403 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1404 8ba819a3 2020-07-23 stsp
1405 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1406 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1407 3c1ec782 2020-07-23 stsp if (err)
1408 3c1ec782 2020-07-23 stsp return err;
1409 8ba819a3 2020-07-23 stsp
1410 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1411 906c123b 2020-07-23 stsp /* install as a regular file */
1412 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1413 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1414 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1416 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1417 906c123b 2020-07-23 stsp goto done;
1418 906c123b 2020-07-23 stsp }
1419 906c123b 2020-07-23 stsp
1420 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1421 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1422 c6e8a826 2021-04-05 stsp int symlink_replaced;
1423 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1424 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1425 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1426 c90c8ce3 2020-07-23 stsp goto done;
1427 c90c8ce3 2020-07-23 stsp }
1428 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1429 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1430 5a1fbc73 2020-07-23 stsp if (err)
1431 f35fa46a 2020-07-23 stsp goto done;
1432 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1433 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1434 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1435 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1436 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1437 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1438 c6e8a826 2021-04-05 stsp } else {
1439 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1440 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1441 c6e8a826 2021-04-05 stsp }
1442 f35fa46a 2020-07-23 stsp }
1443 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1444 f35fa46a 2020-07-23 stsp }
1445 f35fa46a 2020-07-23 stsp
1446 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1447 f4994adc 2020-10-20 stsp char *parent;
1448 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1449 f4994adc 2020-10-20 stsp if (err)
1450 f4994adc 2020-10-20 stsp goto done;
1451 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1452 f4994adc 2020-10-20 stsp free(parent);
1453 f35fa46a 2020-07-23 stsp if (err)
1454 f35fa46a 2020-07-23 stsp goto done;
1455 f35fa46a 2020-07-23 stsp /*
1456 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1457 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1458 f35fa46a 2020-07-23 stsp */
1459 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1460 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1461 f35fa46a 2020-07-23 stsp goto done;
1462 f35fa46a 2020-07-23 stsp }
1463 f35fa46a 2020-07-23 stsp }
1464 f35fa46a 2020-07-23 stsp
1465 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1466 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1467 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1468 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1469 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1470 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1471 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1472 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1473 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1474 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1475 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1476 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1477 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1478 8ba819a3 2020-07-23 stsp } else {
1479 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1480 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1481 8ba819a3 2020-07-23 stsp }
1482 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1483 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1484 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1485 8ba819a3 2020-07-23 stsp done:
1486 906c123b 2020-07-23 stsp free(path_got);
1487 8ba819a3 2020-07-23 stsp return err;
1488 8ba819a3 2020-07-23 stsp }
1489 8ba819a3 2020-07-23 stsp
1490 8ba819a3 2020-07-23 stsp static const struct got_error *
1491 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1492 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1493 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1494 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1495 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1496 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1497 8ba819a3 2020-07-23 stsp {
1498 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1499 507dc3bb 2018-12-29 stsp int fd = -1;
1500 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1501 507dc3bb 2018-12-29 stsp int update = 0;
1502 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1503 8ba819a3 2020-07-23 stsp
1504 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1505 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1506 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1507 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1508 f5375317 2020-10-20 stsp char *parent;
1509 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1510 f5375317 2020-10-20 stsp if (err)
1511 f5375317 2020-10-20 stsp return err;
1512 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1513 f5375317 2020-10-20 stsp free(parent);
1514 21908da4 2019-01-13 stsp if (err)
1515 21908da4 2019-01-13 stsp return err;
1516 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1517 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1518 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1519 21908da4 2019-01-13 stsp if (fd == -1)
1520 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1521 230a42bd 2019-05-11 jcs ondisk_path);
1522 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1523 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1524 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1525 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1526 3b9f0f87 2020-07-23 stsp goto done;
1527 3b9f0f87 2020-07-23 stsp }
1528 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1529 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1530 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1531 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1532 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1533 507dc3bb 2018-12-29 stsp goto done;
1534 d70b8e30 2018-12-27 stsp } else {
1535 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1536 507dc3bb 2018-12-29 stsp ondisk_path);
1537 507dc3bb 2018-12-29 stsp if (err)
1538 507dc3bb 2018-12-29 stsp goto done;
1539 507dc3bb 2018-12-29 stsp update = 1;
1540 9d31a1d8 2018-03-11 stsp }
1541 507dc3bb 2018-12-29 stsp } else
1542 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1543 9d31a1d8 2018-03-11 stsp }
1544 9d31a1d8 2018-03-11 stsp
1545 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1546 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1547 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1548 3818e3c4 2020-11-01 naddy goto done;
1549 3818e3c4 2020-11-01 naddy }
1550 3818e3c4 2020-11-01 naddy
1551 bd6aa359 2020-07-23 stsp if (progress_cb) {
1552 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1553 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1554 bd6aa359 2020-07-23 stsp path);
1555 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1556 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1557 bd6aa359 2020-07-23 stsp path);
1558 bd6aa359 2020-07-23 stsp else
1559 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1560 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1561 bd6aa359 2020-07-23 stsp if (err)
1562 bd6aa359 2020-07-23 stsp goto done;
1563 bd6aa359 2020-07-23 stsp }
1564 d7b62c98 2018-12-27 stsp
1565 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1566 9d31a1d8 2018-03-11 stsp do {
1567 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1568 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1569 9d31a1d8 2018-03-11 stsp if (err)
1570 9d31a1d8 2018-03-11 stsp break;
1571 9d31a1d8 2018-03-11 stsp if (len > 0) {
1572 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1573 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1574 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1575 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1576 b87c6f83 2018-12-24 stsp goto done;
1577 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1578 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1579 b87c6f83 2018-12-24 stsp goto done;
1580 9d31a1d8 2018-03-11 stsp }
1581 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1582 9d31a1d8 2018-03-11 stsp }
1583 9d31a1d8 2018-03-11 stsp } while (len != 0);
1584 9d31a1d8 2018-03-11 stsp
1585 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1586 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1587 816dc654 2019-02-16 stsp goto done;
1588 816dc654 2019-02-16 stsp }
1589 9d31a1d8 2018-03-11 stsp
1590 507dc3bb 2018-12-29 stsp if (update) {
1591 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1592 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1593 f6d8c0ac 2020-11-09 stsp goto done;
1594 f6d8c0ac 2020-11-09 stsp }
1595 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1597 230a42bd 2019-05-11 jcs ondisk_path);
1598 68ed9ba5 2019-02-10 stsp goto done;
1599 68ed9ba5 2019-02-10 stsp }
1600 63df146d 2020-11-09 stsp free(tmppath);
1601 63df146d 2020-11-09 stsp tmppath = NULL;
1602 507dc3bb 2018-12-29 stsp }
1603 507dc3bb 2018-12-29 stsp
1604 9d31a1d8 2018-03-11 stsp done:
1605 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1606 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1607 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1608 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1609 507dc3bb 2018-12-29 stsp free(tmppath);
1610 6353ad76 2019-02-08 stsp return err;
1611 6353ad76 2019-02-08 stsp }
1612 6353ad76 2019-02-08 stsp
1613 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1614 6353ad76 2019-02-08 stsp static const struct got_error *
1615 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1616 7154f6ce 2019-03-27 stsp {
1617 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1618 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1619 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1620 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1621 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1622 7154f6ce 2019-03-27 stsp };
1623 7154f6ce 2019-03-27 stsp int i = 0;
1624 9bdd68dd 2021-01-02 naddy char *line = NULL;
1625 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1626 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1627 7154f6ce 2019-03-27 stsp
1628 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1629 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1630 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1631 7154f6ce 2019-03-27 stsp if (feof(f))
1632 7154f6ce 2019-03-27 stsp break;
1633 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1634 7154f6ce 2019-03-27 stsp break;
1635 7154f6ce 2019-03-27 stsp }
1636 7154f6ce 2019-03-27 stsp
1637 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1638 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1639 19332e6d 2019-05-13 stsp == 0)
1640 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1641 7154f6ce 2019-03-27 stsp else
1642 7154f6ce 2019-03-27 stsp i++;
1643 7154f6ce 2019-03-27 stsp }
1644 7154f6ce 2019-03-27 stsp }
1645 9bdd68dd 2021-01-02 naddy free(line);
1646 7154f6ce 2019-03-27 stsp
1647 7154f6ce 2019-03-27 stsp return err;
1648 e2b1e152 2019-07-13 stsp }
1649 e2b1e152 2019-07-13 stsp
1650 e2b1e152 2019-07-13 stsp static int
1651 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1652 1ebedb77 2019-10-19 stsp {
1653 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1654 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1655 1ebedb77 2019-10-19 stsp }
1656 1ebedb77 2019-10-19 stsp
1657 1ebedb77 2019-10-19 stsp static int
1658 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1659 e2b1e152 2019-07-13 stsp {
1660 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1661 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1662 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1663 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1664 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1665 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1666 c363b2c1 2019-08-03 stsp }
1667 c363b2c1 2019-08-03 stsp
1668 c363b2c1 2019-08-03 stsp static unsigned char
1669 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1670 c363b2c1 2019-08-03 stsp {
1671 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1672 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1673 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1674 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1675 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1676 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1677 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1678 c363b2c1 2019-08-03 stsp default:
1679 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1680 a919d5c4 2020-07-23 stsp }
1681 a919d5c4 2020-07-23 stsp }
1682 a919d5c4 2020-07-23 stsp
1683 a919d5c4 2020-07-23 stsp static const struct got_error *
1684 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1685 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1686 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1687 a919d5c4 2020-07-23 stsp {
1688 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1689 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1690 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1691 a919d5c4 2020-07-23 stsp ssize_t elen;
1692 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1693 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1694 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1695 a919d5c4 2020-07-23 stsp
1696 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1697 a919d5c4 2020-07-23 stsp
1698 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1699 a919d5c4 2020-07-23 stsp do {
1700 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1701 a919d5c4 2020-07-23 stsp if (err)
1702 a919d5c4 2020-07-23 stsp return err;
1703 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1704 a919d5c4 2020-07-23 stsp /*
1705 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1706 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1707 a919d5c4 2020-07-23 stsp */
1708 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1709 a919d5c4 2020-07-23 stsp }
1710 a919d5c4 2020-07-23 stsp if (len > 0) {
1711 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1712 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1713 a919d5c4 2020-07-23 stsp len - hdrlen);
1714 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1715 a919d5c4 2020-07-23 stsp hdrlen = 0;
1716 a919d5c4 2020-07-23 stsp }
1717 a919d5c4 2020-07-23 stsp } while (len != 0);
1718 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1719 a919d5c4 2020-07-23 stsp
1720 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1721 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1722 a919d5c4 2020-07-23 stsp if (elen == -1)
1723 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1724 a919d5c4 2020-07-23 stsp } else {
1725 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1726 a919d5c4 2020-07-23 stsp if (elen == -1)
1727 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1728 c363b2c1 2019-08-03 stsp }
1729 a919d5c4 2020-07-23 stsp
1730 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1731 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1732 a919d5c4 2020-07-23 stsp
1733 a919d5c4 2020-07-23 stsp return NULL;
1734 7154f6ce 2019-03-27 stsp }
1735 7154f6ce 2019-03-27 stsp
1736 7154f6ce 2019-03-27 stsp static const struct got_error *
1737 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1738 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1739 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1740 6353ad76 2019-02-08 stsp {
1741 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1742 6353ad76 2019-02-08 stsp struct got_object_id id;
1743 6353ad76 2019-02-08 stsp size_t hdrlen;
1744 1338848f 2019-12-13 stsp int fd = -1;
1745 6353ad76 2019-02-08 stsp FILE *f = NULL;
1746 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1747 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1748 6353ad76 2019-02-08 stsp size_t flen, blen;
1749 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1750 6353ad76 2019-02-08 stsp
1751 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1752 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1753 6353ad76 2019-02-08 stsp
1754 7f91a133 2019-12-13 stsp /*
1755 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1756 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1757 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1758 7f91a133 2019-12-13 stsp */
1759 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1760 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1761 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1762 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1763 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1764 882ef1b9 2019-12-13 stsp else
1765 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1766 882ef1b9 2019-12-13 stsp goto done;
1767 882ef1b9 2019-12-13 stsp }
1768 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1769 3d35a492 2019-12-13 stsp goto done;
1770 3d35a492 2019-12-13 stsp }
1771 7f91a133 2019-12-13 stsp } else {
1772 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1773 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1774 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1775 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1776 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1777 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1778 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1779 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1780 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1781 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1782 3d35a492 2019-12-13 stsp else
1783 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1784 3d35a492 2019-12-13 stsp goto done;
1785 3d35a492 2019-12-13 stsp }
1786 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1787 1338848f 2019-12-13 stsp goto done;
1788 a378724f 2019-02-10 stsp }
1789 a378724f 2019-02-10 stsp }
1790 6353ad76 2019-02-08 stsp
1791 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1792 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1793 1338848f 2019-12-13 stsp goto done;
1794 3f148bc6 2019-07-27 stsp }
1795 efdd40df 2019-07-27 stsp
1796 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1797 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1798 1338848f 2019-12-13 stsp goto done;
1799 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1800 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1801 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1802 1338848f 2019-12-13 stsp goto done;
1803 d00136be 2019-03-26 stsp }
1804 b8f41171 2019-02-10 stsp
1805 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1806 f179e66d 2020-07-23 stsp goto done;
1807 f179e66d 2020-07-23 stsp
1808 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1809 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1810 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1811 1338848f 2019-12-13 stsp goto done;
1812 f179e66d 2020-07-23 stsp }
1813 6353ad76 2019-02-08 stsp
1814 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1815 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1816 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1817 c363b2c1 2019-08-03 stsp else
1818 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1819 c363b2c1 2019-08-03 stsp
1820 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1821 6353ad76 2019-02-08 stsp if (err)
1822 1338848f 2019-12-13 stsp goto done;
1823 6353ad76 2019-02-08 stsp
1824 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1825 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1826 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1827 a919d5c4 2020-07-23 stsp goto done;
1828 a919d5c4 2020-07-23 stsp }
1829 a919d5c4 2020-07-23 stsp
1830 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1831 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1832 ab0d4361 2019-12-13 stsp if (fd == -1) {
1833 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1834 ab0d4361 2019-12-13 stsp goto done;
1835 ab0d4361 2019-12-13 stsp }
1836 3d35a492 2019-12-13 stsp }
1837 3d35a492 2019-12-13 stsp
1838 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1839 6353ad76 2019-02-08 stsp if (f == NULL) {
1840 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1841 6353ad76 2019-02-08 stsp goto done;
1842 6353ad76 2019-02-08 stsp }
1843 1338848f 2019-12-13 stsp fd = -1;
1844 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1845 656b1f76 2019-05-11 jcs for (;;) {
1846 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1847 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1848 6353ad76 2019-02-08 stsp if (err)
1849 7154f6ce 2019-03-27 stsp goto done;
1850 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1851 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1852 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1853 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1854 7154f6ce 2019-03-27 stsp goto done;
1855 80c5c120 2019-02-19 stsp }
1856 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1857 6353ad76 2019-02-08 stsp if (flen != 0)
1858 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1859 6353ad76 2019-02-08 stsp break;
1860 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1861 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1862 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1863 6353ad76 2019-02-08 stsp break;
1864 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1865 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1866 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1867 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1868 6353ad76 2019-02-08 stsp break;
1869 6353ad76 2019-02-08 stsp }
1870 6353ad76 2019-02-08 stsp } else {
1871 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1872 6353ad76 2019-02-08 stsp break;
1873 6353ad76 2019-02-08 stsp }
1874 6353ad76 2019-02-08 stsp hdrlen = 0;
1875 6353ad76 2019-02-08 stsp }
1876 7154f6ce 2019-03-27 stsp
1877 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1878 7154f6ce 2019-03-27 stsp rewind(f);
1879 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1880 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1881 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1882 6353ad76 2019-02-08 stsp done:
1883 6353ad76 2019-02-08 stsp if (blob)
1884 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1885 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1886 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1887 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1888 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1889 9d31a1d8 2018-03-11 stsp return err;
1890 e2b1e152 2019-07-13 stsp }
1891 e2b1e152 2019-07-13 stsp
1892 e2b1e152 2019-07-13 stsp /*
1893 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1894 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1895 e2b1e152 2019-07-13 stsp */
1896 e2b1e152 2019-07-13 stsp static const struct got_error *
1897 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1898 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1899 e2b1e152 2019-07-13 stsp {
1900 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1901 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1902 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1903 e2b1e152 2019-07-13 stsp
1904 e2b1e152 2019-07-13 stsp return NULL;
1905 9d31a1d8 2018-03-11 stsp }
1906 9d31a1d8 2018-03-11 stsp
1907 9d31a1d8 2018-03-11 stsp static const struct got_error *
1908 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1909 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1910 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1911 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1912 0584f854 2019-04-06 stsp void *progress_arg)
1913 9d31a1d8 2018-03-11 stsp {
1914 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1915 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1916 6353ad76 2019-02-08 stsp char *ondisk_path;
1917 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1918 b8f41171 2019-02-10 stsp struct stat sb;
1919 9d31a1d8 2018-03-11 stsp
1920 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1921 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1922 6353ad76 2019-02-08 stsp
1923 abb4604f 2019-07-27 stsp if (ie) {
1924 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1925 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1926 a76c42e6 2019-08-03 stsp goto done;
1927 a76c42e6 2019-08-03 stsp }
1928 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1929 7f91a133 2019-12-13 stsp repo);
1930 abb4604f 2019-07-27 stsp if (err)
1931 abb4604f 2019-07-27 stsp goto done;
1932 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1933 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1934 c90c8ce3 2020-07-23 stsp } else {
1935 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1936 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1937 c90c8ce3 2020-07-23 stsp }
1938 6353ad76 2019-02-08 stsp
1939 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1940 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1941 b8f41171 2019-02-10 stsp goto done;
1942 b8f41171 2019-02-10 stsp }
1943 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1944 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1945 5036ab18 2020-04-18 stsp path);
1946 5036ab18 2020-04-18 stsp goto done;
1947 5036ab18 2020-04-18 stsp }
1948 b8f41171 2019-02-10 stsp
1949 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1950 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1951 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1952 c6e8a826 2021-04-05 stsp /*
1953 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1954 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1955 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1956 c6e8a826 2021-04-05 stsp * updating contents of this file.
1957 c6e8a826 2021-04-05 stsp */
1958 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1959 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1960 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1961 c6e8a826 2021-04-05 stsp /* Same commit. */
1962 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1963 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1964 e2b1e152 2019-07-13 stsp if (err)
1965 e2b1e152 2019-07-13 stsp goto done;
1966 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1967 b8f41171 2019-02-10 stsp path);
1968 6353ad76 2019-02-08 stsp goto done;
1969 a378724f 2019-02-10 stsp }
1970 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1971 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1972 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1973 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1974 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1975 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1976 0f58026f 2021-04-05 stsp if (err)
1977 0f58026f 2021-04-05 stsp goto done;
1978 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1979 0f58026f 2021-04-05 stsp path);
1980 b8f41171 2019-02-10 stsp goto done;
1981 e2b1e152 2019-07-13 stsp }
1982 9d31a1d8 2018-03-11 stsp }
1983 9d31a1d8 2018-03-11 stsp
1984 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1985 8da9e5f4 2019-01-12 stsp if (err)
1986 6353ad76 2019-02-08 stsp goto done;
1987 512f0d0e 2019-01-02 stsp
1988 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1989 234035bc 2019-06-01 stsp int update_timestamps;
1990 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1991 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1992 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1993 234035bc 2019-06-01 stsp struct got_object_id id2;
1994 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1995 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1996 234035bc 2019-06-01 stsp if (err)
1997 f69721c3 2019-10-21 stsp goto done;
1998 f69721c3 2019-10-21 stsp }
1999 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2000 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2001 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2002 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2003 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2004 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2005 f69721c3 2019-10-21 stsp goto done;
2006 f69721c3 2019-10-21 stsp }
2007 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2008 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2009 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2010 234035bc 2019-06-01 stsp goto done;
2011 f69721c3 2019-10-21 stsp }
2012 234035bc 2019-06-01 stsp }
2013 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2014 36bf999c 2020-07-23 stsp char *link_target;
2015 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2016 36bf999c 2020-07-23 stsp if (err)
2017 36bf999c 2020-07-23 stsp goto done;
2018 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2019 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2020 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2021 36bf999c 2020-07-23 stsp free(link_target);
2022 993e2a1b 2020-07-23 stsp } else {
2023 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2024 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2025 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2026 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2027 993e2a1b 2020-07-23 stsp }
2028 f69721c3 2019-10-21 stsp free(label_orig);
2029 234035bc 2019-06-01 stsp if (blob2)
2030 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2031 909d120e 2019-09-22 stsp if (err)
2032 909d120e 2019-09-22 stsp goto done;
2033 234035bc 2019-06-01 stsp /*
2034 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2035 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2036 234035bc 2019-06-01 stsp * unmodified files again.
2037 234035bc 2019-06-01 stsp */
2038 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2040 234035bc 2019-06-01 stsp update_timestamps);
2041 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2042 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2043 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2044 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2045 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2046 1ee397ad 2019-07-12 stsp if (err)
2047 1ee397ad 2019-07-12 stsp goto done;
2048 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2050 13d9040b 2019-03-27 stsp if (err)
2051 13d9040b 2019-03-27 stsp goto done;
2052 13d9040b 2019-03-27 stsp } else {
2053 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2054 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2055 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2056 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2057 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2058 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2059 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2060 2e1fa222 2020-07-23 stsp } else {
2061 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2062 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2063 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2064 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2065 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2066 2e1fa222 2020-07-23 stsp }
2067 13d9040b 2019-03-27 stsp if (err)
2068 13d9040b 2019-03-27 stsp goto done;
2069 65b05cec 2020-07-23 stsp
2070 054041d0 2020-06-24 stsp if (ie) {
2071 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2072 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2073 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2074 054041d0 2020-06-24 stsp } else {
2075 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2076 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2077 054041d0 2020-06-24 stsp &blob->id);
2078 054041d0 2020-06-24 stsp }
2079 13d9040b 2019-03-27 stsp if (err)
2080 13d9040b 2019-03-27 stsp goto done;
2081 65b05cec 2020-07-23 stsp
2082 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2083 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2084 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2085 2e1fa222 2020-07-23 stsp }
2086 13d9040b 2019-03-27 stsp }
2087 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2088 6353ad76 2019-02-08 stsp done:
2089 6353ad76 2019-02-08 stsp free(ondisk_path);
2090 8da9e5f4 2019-01-12 stsp return err;
2091 90285c3b 2019-01-08 stsp }
2092 90285c3b 2019-01-08 stsp
2093 90285c3b 2019-01-08 stsp static const struct got_error *
2094 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2095 90285c3b 2019-01-08 stsp {
2096 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2097 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2098 90285c3b 2019-01-08 stsp
2099 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2100 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2101 90285c3b 2019-01-08 stsp
2102 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2103 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2104 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2105 c97665ae 2019-01-13 stsp } else {
2106 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2107 fddefe3b 2020-10-20 stsp do {
2108 fddefe3b 2020-10-20 stsp char *parent;
2109 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2110 fddefe3b 2020-10-20 stsp if (err)
2111 2513f20a 2020-10-20 stsp break;
2112 fddefe3b 2020-10-20 stsp free(ondisk_path);
2113 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2114 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2115 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2116 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2117 fddefe3b 2020-10-20 stsp ondisk_path);
2118 90285c3b 2019-01-08 stsp break;
2119 90285c3b 2019-01-08 stsp }
2120 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2121 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2122 90285c3b 2019-01-08 stsp }
2123 90285c3b 2019-01-08 stsp free(ondisk_path);
2124 90285c3b 2019-01-08 stsp return err;
2125 512f0d0e 2019-01-02 stsp }
2126 512f0d0e 2019-01-02 stsp
2127 708d8e67 2019-03-27 stsp static const struct got_error *
2128 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2129 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2130 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2131 708d8e67 2019-03-27 stsp {
2132 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2133 708d8e67 2019-03-27 stsp unsigned char status;
2134 708d8e67 2019-03-27 stsp struct stat sb;
2135 708d8e67 2019-03-27 stsp char *ondisk_path;
2136 708d8e67 2019-03-27 stsp
2137 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2138 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2139 a76c42e6 2019-08-03 stsp
2140 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2141 708d8e67 2019-03-27 stsp == -1)
2142 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2143 708d8e67 2019-03-27 stsp
2144 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2145 708d8e67 2019-03-27 stsp if (err)
2146 5a58a424 2020-06-23 stsp goto done;
2147 708d8e67 2019-03-27 stsp
2148 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2149 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2150 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2151 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2152 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2153 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2154 993e2a1b 2020-07-23 stsp goto done;
2155 993e2a1b 2020-07-23 stsp }
2156 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2157 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2158 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2159 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2160 993e2a1b 2020-07-23 stsp if (err)
2161 993e2a1b 2020-07-23 stsp goto done;
2162 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2163 993e2a1b 2020-07-23 stsp ie->path);
2164 993e2a1b 2020-07-23 stsp goto done;
2165 993e2a1b 2020-07-23 stsp }
2166 993e2a1b 2020-07-23 stsp
2167 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2168 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2169 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2170 1ee397ad 2019-07-12 stsp if (err)
2171 5a58a424 2020-06-23 stsp goto done;
2172 fc6346c4 2019-03-27 stsp /*
2173 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2174 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2175 fc6346c4 2019-03-27 stsp */
2176 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2177 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2178 fc6346c4 2019-03-27 stsp } else {
2179 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2180 1ee397ad 2019-07-12 stsp if (err)
2181 5a58a424 2020-06-23 stsp goto done;
2182 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2183 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2184 fc6346c4 2019-03-27 stsp if (err)
2185 5a58a424 2020-06-23 stsp goto done;
2186 fc6346c4 2019-03-27 stsp }
2187 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2188 708d8e67 2019-03-27 stsp }
2189 5a58a424 2020-06-23 stsp done:
2190 5a58a424 2020-06-23 stsp free(ondisk_path);
2191 fc6346c4 2019-03-27 stsp return err;
2192 708d8e67 2019-03-27 stsp }
2193 708d8e67 2019-03-27 stsp
2194 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2195 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2196 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2197 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2198 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2199 8da9e5f4 2019-01-12 stsp void *progress_arg;
2200 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2201 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2202 8da9e5f4 2019-01-12 stsp };
2203 8da9e5f4 2019-01-12 stsp
2204 512f0d0e 2019-01-02 stsp static const struct got_error *
2205 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2206 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2207 512f0d0e 2019-01-02 stsp {
2208 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2209 0584f854 2019-04-06 stsp
2210 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2212 512f0d0e 2019-01-02 stsp
2213 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2214 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2215 8da9e5f4 2019-01-12 stsp }
2216 512f0d0e 2019-01-02 stsp
2217 8da9e5f4 2019-01-12 stsp static const struct got_error *
2218 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2219 8da9e5f4 2019-01-12 stsp {
2220 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2221 7a9df742 2019-01-08 stsp
2222 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2224 0584f854 2019-04-06 stsp
2225 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2226 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2227 9d31a1d8 2018-03-11 stsp }
2228 9d31a1d8 2018-03-11 stsp
2229 9d31a1d8 2018-03-11 stsp static const struct got_error *
2230 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2231 9d31a1d8 2018-03-11 stsp {
2232 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2233 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2234 8da9e5f4 2019-01-12 stsp char *path;
2235 9d31a1d8 2018-03-11 stsp
2236 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2237 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2238 0584f854 2019-04-06 stsp
2239 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2240 63c5ca5d 2019-08-24 stsp return NULL;
2241 63c5ca5d 2019-08-24 stsp
2242 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2243 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2244 8da9e5f4 2019-01-12 stsp == -1)
2245 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2246 9d31a1d8 2018-03-11 stsp
2247 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2248 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2249 8da9e5f4 2019-01-12 stsp else
2250 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2251 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2252 9d31a1d8 2018-03-11 stsp
2253 8da9e5f4 2019-01-12 stsp free(path);
2254 0cd1c46a 2019-03-11 stsp return err;
2255 0cd1c46a 2019-03-11 stsp }
2256 0cd1c46a 2019-03-11 stsp
2257 b2118c49 2020-07-28 stsp const struct got_error *
2258 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2259 b2118c49 2020-07-28 stsp {
2260 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2261 b2118c49 2020-07-28 stsp
2262 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2263 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2264 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2265 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2266 b2118c49 2020-07-28 stsp }
2267 b2118c49 2020-07-28 stsp
2268 b2118c49 2020-07-28 stsp return NULL;
2269 b2118c49 2020-07-28 stsp }
2270 b2118c49 2020-07-28 stsp
2271 818c7501 2019-07-11 stsp static const struct got_error *
2272 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2273 0cd1c46a 2019-03-11 stsp {
2274 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2275 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2276 0cd1c46a 2019-03-11 stsp
2277 517bab73 2019-03-11 stsp *refname = NULL;
2278 517bab73 2019-03-11 stsp
2279 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2280 b2118c49 2020-07-28 stsp if (err)
2281 b2118c49 2020-07-28 stsp return err;
2282 0cd1c46a 2019-03-11 stsp
2283 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2284 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2285 0647c563 2019-03-11 stsp *refname = NULL;
2286 0cd1c46a 2019-03-11 stsp }
2287 517bab73 2019-03-11 stsp free(uuidstr);
2288 517bab73 2019-03-11 stsp return err;
2289 517bab73 2019-03-11 stsp }
2290 517bab73 2019-03-11 stsp
2291 818c7501 2019-07-11 stsp const struct got_error *
2292 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2293 818c7501 2019-07-11 stsp {
2294 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2295 818c7501 2019-07-11 stsp }
2296 818c7501 2019-07-11 stsp
2297 818c7501 2019-07-11 stsp static const struct got_error *
2298 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2299 818c7501 2019-07-11 stsp {
2300 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2301 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2302 818c7501 2019-07-11 stsp }
2303 818c7501 2019-07-11 stsp
2304 818c7501 2019-07-11 stsp static const struct got_error *
2305 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2306 818c7501 2019-07-11 stsp {
2307 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2308 818c7501 2019-07-11 stsp }
2309 818c7501 2019-07-11 stsp
2310 818c7501 2019-07-11 stsp static const struct got_error *
2311 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2312 818c7501 2019-07-11 stsp {
2313 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2314 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2315 818c7501 2019-07-11 stsp }
2316 818c7501 2019-07-11 stsp
2317 818c7501 2019-07-11 stsp static const struct got_error *
2318 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2319 818c7501 2019-07-11 stsp {
2320 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2321 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2322 0ebf8283 2019-07-24 stsp }
2323 0ebf8283 2019-07-24 stsp
2324 0ebf8283 2019-07-24 stsp static const struct got_error *
2325 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2326 0ebf8283 2019-07-24 stsp {
2327 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2328 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2329 0ebf8283 2019-07-24 stsp }
2330 0ebf8283 2019-07-24 stsp
2331 0ebf8283 2019-07-24 stsp static const struct got_error *
2332 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2333 0ebf8283 2019-07-24 stsp {
2334 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2335 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2336 0ebf8283 2019-07-24 stsp }
2337 0ebf8283 2019-07-24 stsp
2338 0ebf8283 2019-07-24 stsp static const struct got_error *
2339 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2340 0ebf8283 2019-07-24 stsp {
2341 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2342 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2343 818c7501 2019-07-11 stsp }
2344 818c7501 2019-07-11 stsp
2345 0ebf8283 2019-07-24 stsp static const struct got_error *
2346 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2347 0ebf8283 2019-07-24 stsp {
2348 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2349 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2350 0ebf8283 2019-07-24 stsp }
2351 818c7501 2019-07-11 stsp
2352 0ebf8283 2019-07-24 stsp const struct got_error *
2353 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2354 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2355 0ebf8283 2019-07-24 stsp {
2356 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2357 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2358 0ebf8283 2019-07-24 stsp *path = NULL;
2359 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2360 0ebf8283 2019-07-24 stsp }
2361 0ebf8283 2019-07-24 stsp return NULL;
2362 0ebf8283 2019-07-24 stsp }
2363 0ebf8283 2019-07-24 stsp
2364 517bab73 2019-03-11 stsp /*
2365 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2366 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2367 517bab73 2019-03-11 stsp */
2368 517bab73 2019-03-11 stsp static const struct got_error *
2369 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2370 517bab73 2019-03-11 stsp {
2371 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2372 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2373 517bab73 2019-03-11 stsp char *refname;
2374 517bab73 2019-03-11 stsp
2375 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2376 517bab73 2019-03-11 stsp if (err)
2377 517bab73 2019-03-11 stsp return err;
2378 0cd1c46a 2019-03-11 stsp
2379 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2380 0cd1c46a 2019-03-11 stsp if (err)
2381 0cd1c46a 2019-03-11 stsp goto done;
2382 0cd1c46a 2019-03-11 stsp
2383 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2384 0cd1c46a 2019-03-11 stsp done:
2385 0cd1c46a 2019-03-11 stsp free(refname);
2386 0cd1c46a 2019-03-11 stsp if (ref)
2387 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2388 3e3a69f1 2019-07-25 stsp return err;
2389 3e3a69f1 2019-07-25 stsp }
2390 3e3a69f1 2019-07-25 stsp
2391 3e3a69f1 2019-07-25 stsp static const struct got_error *
2392 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2393 3e3a69f1 2019-07-25 stsp {
2394 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2395 3e3a69f1 2019-07-25 stsp
2396 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2397 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2398 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2399 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2400 3e3a69f1 2019-07-25 stsp }
2401 9d31a1d8 2018-03-11 stsp return err;
2402 9d31a1d8 2018-03-11 stsp }
2403 9d31a1d8 2018-03-11 stsp
2404 3e3a69f1 2019-07-25 stsp
2405 ebf99748 2019-05-09 stsp static const struct got_error *
2406 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2407 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2408 ebf99748 2019-05-09 stsp {
2409 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2410 ebf99748 2019-05-09 stsp FILE *index = NULL;
2411 0cd1c46a 2019-03-11 stsp
2412 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2413 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2414 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2415 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2416 ebf99748 2019-05-09 stsp
2417 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2418 3e3a69f1 2019-07-25 stsp if (err)
2419 ebf99748 2019-05-09 stsp goto done;
2420 ebf99748 2019-05-09 stsp
2421 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2422 ebf99748 2019-05-09 stsp if (index == NULL) {
2423 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2424 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2425 ebf99748 2019-05-09 stsp } else {
2426 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2427 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2428 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2429 ebf99748 2019-05-09 stsp }
2430 ebf99748 2019-05-09 stsp done:
2431 ebf99748 2019-05-09 stsp if (err) {
2432 ebf99748 2019-05-09 stsp free(*fileindex_path);
2433 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2434 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2435 ebf99748 2019-05-09 stsp *fileindex = NULL;
2436 ebf99748 2019-05-09 stsp }
2437 ebf99748 2019-05-09 stsp return err;
2438 ebf99748 2019-05-09 stsp }
2439 c932eeeb 2019-05-22 stsp
2440 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2441 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2442 c932eeeb 2019-05-22 stsp const char *path;
2443 c932eeeb 2019-05-22 stsp size_t path_len;
2444 c932eeeb 2019-05-22 stsp const char *entry_name;
2445 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2446 a484d721 2019-06-10 stsp void *progress_arg;
2447 c932eeeb 2019-05-22 stsp };
2448 ebf99748 2019-05-09 stsp
2449 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2450 c932eeeb 2019-05-22 stsp static const struct got_error *
2451 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2452 c932eeeb 2019-05-22 stsp {
2453 1ee397ad 2019-07-12 stsp const struct got_error *err;
2454 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2455 c932eeeb 2019-05-22 stsp
2456 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2457 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2458 c932eeeb 2019-05-22 stsp return NULL;
2459 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2460 102ff934 2019-06-11 stsp return NULL;
2461 102ff934 2019-06-11 stsp
2462 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2463 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2464 c932eeeb 2019-05-22 stsp return NULL;
2465 c932eeeb 2019-05-22 stsp
2466 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2467 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2468 edd02c5e 2019-07-11 stsp ie->path);
2469 1ee397ad 2019-07-12 stsp if (err)
2470 1ee397ad 2019-07-12 stsp return err;
2471 1ee397ad 2019-07-12 stsp }
2472 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2473 c932eeeb 2019-05-22 stsp return NULL;
2474 a615e0e7 2020-12-16 stsp }
2475 a615e0e7 2020-12-16 stsp
2476 a615e0e7 2020-12-16 stsp static const struct got_error *
2477 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2478 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2479 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2480 a615e0e7 2020-12-16 stsp {
2481 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2482 a615e0e7 2020-12-16 stsp
2483 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2484 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2485 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2486 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2487 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2488 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2489 a615e0e7 2020-12-16 stsp
2490 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2491 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2492 9c6338c4 2019-06-02 stsp }
2493 9c6338c4 2019-06-02 stsp
2494 9c6338c4 2019-06-02 stsp static const struct got_error *
2495 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2496 9c6338c4 2019-06-02 stsp {
2497 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2498 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2499 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2500 867630bb 2020-01-17 stsp struct timespec timeout;
2501 9c6338c4 2019-06-02 stsp
2502 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2503 9c6338c4 2019-06-02 stsp fileindex_path);
2504 9c6338c4 2019-06-02 stsp if (err)
2505 9c6338c4 2019-06-02 stsp goto done;
2506 9c6338c4 2019-06-02 stsp
2507 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2508 9c6338c4 2019-06-02 stsp if (err)
2509 9c6338c4 2019-06-02 stsp goto done;
2510 9c6338c4 2019-06-02 stsp
2511 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2512 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2513 9c6338c4 2019-06-02 stsp fileindex_path);
2514 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2515 9c6338c4 2019-06-02 stsp }
2516 867630bb 2020-01-17 stsp
2517 867630bb 2020-01-17 stsp /*
2518 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2519 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2520 867630bb 2020-01-17 stsp * was recorded in the file index.
2521 867630bb 2020-01-17 stsp */
2522 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2523 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2524 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2525 9c6338c4 2019-06-02 stsp done:
2526 9c6338c4 2019-06-02 stsp if (new_index)
2527 9c6338c4 2019-06-02 stsp fclose(new_index);
2528 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2529 9c6338c4 2019-06-02 stsp return err;
2530 c932eeeb 2019-05-22 stsp }
2531 6ced4176 2019-07-12 stsp
2532 6ced4176 2019-07-12 stsp static const struct got_error *
2533 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2534 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2535 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2536 6ced4176 2019-07-12 stsp {
2537 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2538 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2539 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2540 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2541 6ced4176 2019-07-12 stsp
2542 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2543 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2544 6ced4176 2019-07-12 stsp *tree_id = NULL;
2545 6ced4176 2019-07-12 stsp
2546 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2547 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2548 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2549 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2550 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2551 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2552 6ced4176 2019-07-12 stsp goto done;
2553 6ced4176 2019-07-12 stsp }
2554 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2555 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2556 6ced4176 2019-07-12 stsp if (err)
2557 6ced4176 2019-07-12 stsp goto done;
2558 6ced4176 2019-07-12 stsp return NULL;
2559 6ced4176 2019-07-12 stsp }
2560 6ced4176 2019-07-12 stsp
2561 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2562 6ced4176 2019-07-12 stsp
2563 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2564 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2565 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2566 6ced4176 2019-07-12 stsp goto done;
2567 6ced4176 2019-07-12 stsp }
2568 6ced4176 2019-07-12 stsp
2569 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2570 6ced4176 2019-07-12 stsp in_repo_path);
2571 6ced4176 2019-07-12 stsp if (err)
2572 6ced4176 2019-07-12 stsp goto done;
2573 6ced4176 2019-07-12 stsp
2574 6ced4176 2019-07-12 stsp free(in_repo_path);
2575 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2576 6ced4176 2019-07-12 stsp
2577 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2578 6ced4176 2019-07-12 stsp if (err)
2579 6ced4176 2019-07-12 stsp goto done;
2580 c932eeeb 2019-05-22 stsp
2581 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2582 6ced4176 2019-07-12 stsp /* Check out a single file. */
2583 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2584 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2585 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2586 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2587 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2588 6ced4176 2019-07-12 stsp goto done;
2589 6ced4176 2019-07-12 stsp }
2590 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2591 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2592 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2593 6ced4176 2019-07-12 stsp goto done;
2594 6ced4176 2019-07-12 stsp }
2595 6ced4176 2019-07-12 stsp } else {
2596 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2597 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2598 6ced4176 2019-07-12 stsp if (err)
2599 6ced4176 2019-07-12 stsp return err;
2600 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2601 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2602 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2603 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2604 6ced4176 2019-07-12 stsp goto done;
2605 6ced4176 2019-07-12 stsp }
2606 6ced4176 2019-07-12 stsp }
2607 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2608 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2609 6ced4176 2019-07-12 stsp } else {
2610 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2611 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2612 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2613 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2614 6ced4176 2019-07-12 stsp goto done;
2615 6ced4176 2019-07-12 stsp }
2616 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2617 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2618 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2619 6ced4176 2019-07-12 stsp goto done;
2620 6ced4176 2019-07-12 stsp }
2621 6ced4176 2019-07-12 stsp }
2622 6ced4176 2019-07-12 stsp done:
2623 6ced4176 2019-07-12 stsp free(id);
2624 6ced4176 2019-07-12 stsp free(in_repo_path);
2625 6ced4176 2019-07-12 stsp if (err) {
2626 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2627 6ced4176 2019-07-12 stsp free(*tree_relpath);
2628 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2629 6ced4176 2019-07-12 stsp free(*tree_id);
2630 6ced4176 2019-07-12 stsp *tree_id = NULL;
2631 6ced4176 2019-07-12 stsp }
2632 07ed472d 2019-07-12 stsp return err;
2633 07ed472d 2019-07-12 stsp }
2634 07ed472d 2019-07-12 stsp
2635 07ed472d 2019-07-12 stsp static const struct got_error *
2636 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2637 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2638 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2639 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2640 07ed472d 2019-07-12 stsp {
2641 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2642 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2643 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2644 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2645 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2646 07ed472d 2019-07-12 stsp
2647 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2648 7f47418f 2019-12-20 stsp if (err) {
2649 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2650 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2651 7f47418f 2019-12-20 stsp goto done;
2652 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2653 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2654 7f47418f 2019-12-20 stsp if (err)
2655 7f47418f 2019-12-20 stsp return err;
2656 7f47418f 2019-12-20 stsp }
2657 07ed472d 2019-07-12 stsp
2658 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2659 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2660 07ed472d 2019-07-12 stsp if (err)
2661 07ed472d 2019-07-12 stsp goto done;
2662 07ed472d 2019-07-12 stsp
2663 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2664 07ed472d 2019-07-12 stsp if (err)
2665 07ed472d 2019-07-12 stsp goto done;
2666 07ed472d 2019-07-12 stsp
2667 07ed472d 2019-07-12 stsp if (entry_name &&
2668 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2669 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2670 07ed472d 2019-07-12 stsp goto done;
2671 07ed472d 2019-07-12 stsp }
2672 07ed472d 2019-07-12 stsp
2673 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2674 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2675 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2676 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2677 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2678 07ed472d 2019-07-12 stsp arg.repo = repo;
2679 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2680 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2681 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2682 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2683 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2684 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2685 07ed472d 2019-07-12 stsp done:
2686 07ed472d 2019-07-12 stsp if (tree)
2687 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2688 07ed472d 2019-07-12 stsp if (commit)
2689 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2690 6ced4176 2019-07-12 stsp return err;
2691 6ced4176 2019-07-12 stsp }
2692 6ced4176 2019-07-12 stsp
2693 9d31a1d8 2018-03-11 stsp const struct got_error *
2694 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2695 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2696 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2697 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2698 9d31a1d8 2018-03-11 stsp {
2699 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2700 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2701 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2702 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2703 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2704 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2705 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2706 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2707 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2708 f2ea84fa 2019-07-27 stsp int entry_type;
2709 f2ea84fa 2019-07-27 stsp char *relpath;
2710 f2ea84fa 2019-07-27 stsp char *entry_name;
2711 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2712 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2713 9d31a1d8 2018-03-11 stsp
2714 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2715 f2ea84fa 2019-07-27 stsp
2716 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2717 9d31a1d8 2018-03-11 stsp if (err)
2718 9d31a1d8 2018-03-11 stsp return err;
2719 93a30277 2018-12-24 stsp
2720 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2721 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2722 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2723 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2724 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2725 f2ea84fa 2019-07-27 stsp goto done;
2726 f2ea84fa 2019-07-27 stsp }
2727 6ced4176 2019-07-12 stsp
2728 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2729 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2730 f2ea84fa 2019-07-27 stsp if (err) {
2731 f2ea84fa 2019-07-27 stsp free(tpd);
2732 6ced4176 2019-07-12 stsp goto done;
2733 6ced4176 2019-07-12 stsp }
2734 f2ea84fa 2019-07-27 stsp
2735 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2736 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2737 f2ea84fa 2019-07-27 stsp if (err) {
2738 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2739 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2740 f2ea84fa 2019-07-27 stsp free(tpd);
2741 f2ea84fa 2019-07-27 stsp goto done;
2742 f2ea84fa 2019-07-27 stsp }
2743 f2ea84fa 2019-07-27 stsp } else
2744 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2745 f2ea84fa 2019-07-27 stsp
2746 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2747 6ced4176 2019-07-12 stsp }
2748 6ced4176 2019-07-12 stsp
2749 51514078 2018-12-25 stsp /*
2750 51514078 2018-12-25 stsp * Read the file index.
2751 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2752 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2753 51514078 2018-12-25 stsp */
2754 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2755 8da9e5f4 2019-01-12 stsp if (err)
2756 c4cdcb68 2019-04-03 stsp goto done;
2757 c4cdcb68 2019-04-03 stsp
2758 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2759 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2760 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2761 f2ea84fa 2019-07-27 stsp
2762 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2763 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2764 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2765 f2ea84fa 2019-07-27 stsp if (err)
2766 f2ea84fa 2019-07-27 stsp break;
2767 f2ea84fa 2019-07-27 stsp
2768 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2769 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2770 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2771 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2772 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2773 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2774 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2775 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2776 f2ea84fa 2019-07-27 stsp if (err)
2777 f2ea84fa 2019-07-27 stsp break;
2778 f2ea84fa 2019-07-27 stsp
2779 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2780 711d9cd9 2019-07-12 stsp }
2781 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2782 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2783 af12c6b9 2019-06-04 stsp err = sync_err;
2784 9d31a1d8 2018-03-11 stsp done:
2785 9c6338c4 2019-06-02 stsp free(fileindex_path);
2786 edfa7d7f 2018-09-11 stsp if (tree)
2787 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2788 9d31a1d8 2018-03-11 stsp if (commit)
2789 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2790 5ade8233 2019-07-12 stsp if (fileindex)
2791 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2792 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2793 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2794 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2795 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2796 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2797 f2ea84fa 2019-07-27 stsp free(tpd);
2798 f2ea84fa 2019-07-27 stsp }
2799 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2800 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2801 9d31a1d8 2018-03-11 stsp err = unlockerr;
2802 f8d1f275 2019-02-04 stsp return err;
2803 f8d1f275 2019-02-04 stsp }
2804 f8d1f275 2019-02-04 stsp
2805 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2806 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2807 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2808 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2809 234035bc 2019-06-01 stsp void *progress_arg;
2810 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2811 234035bc 2019-06-01 stsp void *cancel_arg;
2812 f69721c3 2019-10-21 stsp const char *label_orig;
2813 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2814 234035bc 2019-06-01 stsp };
2815 234035bc 2019-06-01 stsp
2816 234035bc 2019-06-01 stsp static const struct got_error *
2817 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2818 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2819 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2820 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2821 234035bc 2019-06-01 stsp {
2822 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2823 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2824 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2825 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2826 234035bc 2019-06-01 stsp struct stat sb;
2827 234035bc 2019-06-01 stsp unsigned char status;
2828 234035bc 2019-06-01 stsp int local_changes_subsumed;
2829 234035bc 2019-06-01 stsp
2830 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2831 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2832 d6c87207 2019-08-02 stsp strlen(path2));
2833 1ee397ad 2019-07-12 stsp if (ie == NULL)
2834 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2835 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2836 234035bc 2019-06-01 stsp
2837 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2838 234035bc 2019-06-01 stsp path2) == -1)
2839 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2840 234035bc 2019-06-01 stsp
2841 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2842 7f91a133 2019-12-13 stsp repo);
2843 234035bc 2019-06-01 stsp if (err)
2844 234035bc 2019-06-01 stsp goto done;
2845 234035bc 2019-06-01 stsp
2846 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2847 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2848 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2849 234035bc 2019-06-01 stsp goto done;
2850 234035bc 2019-06-01 stsp }
2851 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2852 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2853 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2854 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2855 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2856 234035bc 2019-06-01 stsp goto done;
2857 234035bc 2019-06-01 stsp }
2858 234035bc 2019-06-01 stsp
2859 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2860 36bf999c 2020-07-23 stsp char *link_target2;
2861 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2862 36bf999c 2020-07-23 stsp if (err)
2863 36bf999c 2020-07-23 stsp goto done;
2864 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2865 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2866 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2867 36bf999c 2020-07-23 stsp free(link_target2);
2868 af57b12a 2020-07-23 stsp } else {
2869 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2870 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2871 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2872 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2873 af57b12a 2020-07-23 stsp }
2874 234035bc 2019-06-01 stsp } else if (blob1) {
2875 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2876 d6c87207 2019-08-02 stsp strlen(path1));
2877 1ee397ad 2019-07-12 stsp if (ie == NULL)
2878 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2879 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2880 2b92fad7 2019-06-02 stsp
2881 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2882 2b92fad7 2019-06-02 stsp path1) == -1)
2883 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2884 2b92fad7 2019-06-02 stsp
2885 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2886 7f91a133 2019-12-13 stsp repo);
2887 2b92fad7 2019-06-02 stsp if (err)
2888 2b92fad7 2019-06-02 stsp goto done;
2889 2b92fad7 2019-06-02 stsp
2890 2b92fad7 2019-06-02 stsp switch (status) {
2891 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2892 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2893 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2894 1ee397ad 2019-07-12 stsp if (err)
2895 1ee397ad 2019-07-12 stsp goto done;
2896 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2897 2b92fad7 2019-06-02 stsp if (err)
2898 2b92fad7 2019-06-02 stsp goto done;
2899 2b92fad7 2019-06-02 stsp if (ie)
2900 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2901 2b92fad7 2019-06-02 stsp break;
2902 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2903 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2904 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2905 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2906 1ee397ad 2019-07-12 stsp if (err)
2907 1ee397ad 2019-07-12 stsp goto done;
2908 2b92fad7 2019-06-02 stsp if (ie)
2909 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2910 2b92fad7 2019-06-02 stsp break;
2911 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2912 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2913 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2914 0a22ca1a 2020-09-23 stsp /*
2915 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2916 0a22ca1a 2020-09-23 stsp * exists in the repository.
2917 0a22ca1a 2020-09-23 stsp */
2918 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2919 0a22ca1a 2020-09-23 stsp if (err)
2920 0a22ca1a 2020-09-23 stsp goto done;
2921 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2922 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2923 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2924 0a22ca1a 2020-09-23 stsp if (err)
2925 0a22ca1a 2020-09-23 stsp goto done;
2926 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2927 0a22ca1a 2020-09-23 stsp path1);
2928 0a22ca1a 2020-09-23 stsp if (err)
2929 0a22ca1a 2020-09-23 stsp goto done;
2930 0a22ca1a 2020-09-23 stsp if (ie)
2931 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2932 0a22ca1a 2020-09-23 stsp ie);
2933 0a22ca1a 2020-09-23 stsp } else {
2934 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2935 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2936 0a22ca1a 2020-09-23 stsp }
2937 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2938 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2939 0a22ca1a 2020-09-23 stsp free(id);
2940 0a22ca1a 2020-09-23 stsp if (err)
2941 0a22ca1a 2020-09-23 stsp goto done;
2942 0a22ca1a 2020-09-23 stsp break;
2943 0a22ca1a 2020-09-23 stsp }
2944 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2945 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2946 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2947 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2948 1ee397ad 2019-07-12 stsp if (err)
2949 1ee397ad 2019-07-12 stsp goto done;
2950 2b92fad7 2019-06-02 stsp break;
2951 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2952 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2953 1ee397ad 2019-07-12 stsp if (err)
2954 1ee397ad 2019-07-12 stsp goto done;
2955 2b92fad7 2019-06-02 stsp break;
2956 2b92fad7 2019-06-02 stsp default:
2957 2b92fad7 2019-06-02 stsp break;
2958 2b92fad7 2019-06-02 stsp }
2959 234035bc 2019-06-01 stsp } else if (blob2) {
2960 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2961 234035bc 2019-06-01 stsp path2) == -1)
2962 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2963 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2964 d6c87207 2019-08-02 stsp strlen(path2));
2965 234035bc 2019-06-01 stsp if (ie) {
2966 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2967 7f91a133 2019-12-13 stsp -1, NULL, repo);
2968 234035bc 2019-06-01 stsp if (err)
2969 234035bc 2019-06-01 stsp goto done;
2970 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2971 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2972 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2973 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2974 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2975 1ee397ad 2019-07-12 stsp status, path2);
2976 234035bc 2019-06-01 stsp goto done;
2977 234035bc 2019-06-01 stsp }
2978 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2979 36bf999c 2020-07-23 stsp char *link_target2;
2980 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2981 36bf999c 2020-07-23 stsp blob2);
2982 36bf999c 2020-07-23 stsp if (err)
2983 36bf999c 2020-07-23 stsp goto done;
2984 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2985 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2986 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2987 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2988 36bf999c 2020-07-23 stsp free(link_target2);
2989 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2990 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2991 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2992 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2993 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2994 526a746f 2020-07-23 stsp a->progress_arg);
2995 dfe9fba0 2020-07-23 stsp } else {
2996 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2997 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2998 526a746f 2020-07-23 stsp }
2999 dfe9fba0 2020-07-23 stsp if (err)
3000 dfe9fba0 2020-07-23 stsp goto done;
3001 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3002 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3003 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3004 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3005 234035bc 2019-06-01 stsp if (err)
3006 234035bc 2019-06-01 stsp goto done;
3007 234035bc 2019-06-01 stsp }
3008 234035bc 2019-06-01 stsp } else {
3009 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3010 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3011 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3012 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3013 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3014 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3015 2e1fa222 2020-07-23 stsp } else {
3016 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3017 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3018 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3019 2e1fa222 2020-07-23 stsp }
3020 234035bc 2019-06-01 stsp if (err)
3021 234035bc 2019-06-01 stsp goto done;
3022 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3023 234035bc 2019-06-01 stsp if (err)
3024 234035bc 2019-06-01 stsp goto done;
3025 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3026 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3027 3969253a 2020-03-07 stsp if (err) {
3028 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3029 3969253a 2020-03-07 stsp goto done;
3030 3969253a 2020-03-07 stsp }
3031 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3032 2b92fad7 2019-06-02 stsp if (err) {
3033 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3034 2b92fad7 2019-06-02 stsp goto done;
3035 2b92fad7 2019-06-02 stsp }
3036 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3037 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3038 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3039 2e1fa222 2020-07-23 stsp }
3040 234035bc 2019-06-01 stsp }
3041 234035bc 2019-06-01 stsp }
3042 234035bc 2019-06-01 stsp done:
3043 234035bc 2019-06-01 stsp free(ondisk_path);
3044 234035bc 2019-06-01 stsp return err;
3045 234035bc 2019-06-01 stsp }
3046 234035bc 2019-06-01 stsp
3047 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3048 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3049 234035bc 2019-06-01 stsp struct got_repository *repo;
3050 234035bc 2019-06-01 stsp };
3051 234035bc 2019-06-01 stsp
3052 234035bc 2019-06-01 stsp static const struct got_error *
3053 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3054 234035bc 2019-06-01 stsp {
3055 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3056 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3057 234035bc 2019-06-01 stsp unsigned char status;
3058 234035bc 2019-06-01 stsp struct stat sb;
3059 234035bc 2019-06-01 stsp char *ondisk_path;
3060 234035bc 2019-06-01 stsp
3061 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3062 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3063 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3064 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3065 234035bc 2019-06-01 stsp
3066 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3067 234035bc 2019-06-01 stsp == -1)
3068 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3069 234035bc 2019-06-01 stsp
3070 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3071 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3072 234035bc 2019-06-01 stsp if (err)
3073 234035bc 2019-06-01 stsp return err;
3074 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3075 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3076 234035bc 2019-06-01 stsp
3077 234035bc 2019-06-01 stsp return NULL;
3078 234035bc 2019-06-01 stsp }
3079 234035bc 2019-06-01 stsp
3080 818c7501 2019-07-11 stsp static const struct got_error *
3081 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3082 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3083 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3084 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3085 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3086 234035bc 2019-06-01 stsp {
3087 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3088 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3089 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3090 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3091 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3092 234035bc 2019-06-01 stsp
3093 03415a1a 2019-06-02 stsp if (commit_id1) {
3094 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3095 03415a1a 2019-06-02 stsp worktree->path_prefix);
3096 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3097 03415a1a 2019-06-02 stsp goto done;
3098 69d57f3d 2020-07-31 stsp }
3099 69d57f3d 2020-07-31 stsp if (tree_id1) {
3100 69d57f3d 2020-07-31 stsp char *id_str;
3101 03415a1a 2019-06-02 stsp
3102 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3103 03415a1a 2019-06-02 stsp if (err)
3104 03415a1a 2019-06-02 stsp goto done;
3105 f69721c3 2019-10-21 stsp
3106 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3107 f69721c3 2019-10-21 stsp if (err)
3108 f69721c3 2019-10-21 stsp goto done;
3109 f69721c3 2019-10-21 stsp
3110 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3111 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3112 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3113 f69721c3 2019-10-21 stsp free(id_str);
3114 f69721c3 2019-10-21 stsp goto done;
3115 f69721c3 2019-10-21 stsp }
3116 f69721c3 2019-10-21 stsp free(id_str);
3117 03415a1a 2019-06-02 stsp }
3118 234035bc 2019-06-01 stsp
3119 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3120 234035bc 2019-06-01 stsp worktree->path_prefix);
3121 234035bc 2019-06-01 stsp if (err)
3122 234035bc 2019-06-01 stsp goto done;
3123 234035bc 2019-06-01 stsp
3124 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3125 234035bc 2019-06-01 stsp if (err)
3126 234035bc 2019-06-01 stsp goto done;
3127 234035bc 2019-06-01 stsp
3128 234035bc 2019-06-01 stsp arg.worktree = worktree;
3129 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3130 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3131 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3132 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3133 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3134 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3135 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3136 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3137 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3138 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3139 af12c6b9 2019-06-04 stsp err = sync_err;
3140 234035bc 2019-06-01 stsp done:
3141 234035bc 2019-06-01 stsp if (tree1)
3142 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3143 234035bc 2019-06-01 stsp if (tree2)
3144 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3145 f69721c3 2019-10-21 stsp free(label_orig);
3146 818c7501 2019-07-11 stsp return err;
3147 818c7501 2019-07-11 stsp }
3148 234035bc 2019-06-01 stsp
3149 818c7501 2019-07-11 stsp const struct got_error *
3150 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3151 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3152 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3153 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3154 818c7501 2019-07-11 stsp {
3155 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3156 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3157 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3158 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3159 818c7501 2019-07-11 stsp
3160 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3161 818c7501 2019-07-11 stsp if (err)
3162 818c7501 2019-07-11 stsp return err;
3163 818c7501 2019-07-11 stsp
3164 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3165 818c7501 2019-07-11 stsp if (err)
3166 818c7501 2019-07-11 stsp goto done;
3167 818c7501 2019-07-11 stsp
3168 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3169 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3170 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3171 818c7501 2019-07-11 stsp &mok_arg);
3172 818c7501 2019-07-11 stsp if (err)
3173 818c7501 2019-07-11 stsp goto done;
3174 818c7501 2019-07-11 stsp
3175 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3176 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3177 818c7501 2019-07-11 stsp done:
3178 818c7501 2019-07-11 stsp if (fileindex)
3179 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3180 818c7501 2019-07-11 stsp free(fileindex_path);
3181 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3182 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3183 234035bc 2019-06-01 stsp err = unlockerr;
3184 234035bc 2019-06-01 stsp return err;
3185 234035bc 2019-06-01 stsp }
3186 234035bc 2019-06-01 stsp
3187 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3188 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3189 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3190 927df6b7 2019-02-10 stsp const char *status_path;
3191 927df6b7 2019-02-10 stsp size_t status_path_len;
3192 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3193 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3194 f8d1f275 2019-02-04 stsp void *status_arg;
3195 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3196 f8d1f275 2019-02-04 stsp void *cancel_arg;
3197 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3198 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3199 f2a9dc41 2019-12-13 tracey int report_unchanged;
3200 3143d852 2020-06-25 stsp int no_ignores;
3201 f8d1f275 2019-02-04 stsp };
3202 88d0e355 2019-08-03 stsp
3203 f8d1f275 2019-02-04 stsp static const struct got_error *
3204 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3205 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3206 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3207 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3208 927df6b7 2019-02-10 stsp {
3209 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3210 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3211 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3212 927df6b7 2019-02-10 stsp struct stat sb;
3213 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3214 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3215 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3216 927df6b7 2019-02-10 stsp
3217 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3218 98eaaa12 2019-08-03 stsp if (err)
3219 98eaaa12 2019-08-03 stsp return err;
3220 98eaaa12 2019-08-03 stsp
3221 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3222 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3223 98eaaa12 2019-08-03 stsp return NULL;
3224 98eaaa12 2019-08-03 stsp
3225 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3226 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3227 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3228 98eaaa12 2019-08-03 stsp }
3229 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3230 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3231 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3232 927df6b7 2019-02-10 stsp }
3233 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3234 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3235 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3236 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3237 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3238 98eaaa12 2019-08-03 stsp }
3239 98eaaa12 2019-08-03 stsp
3240 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3241 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3242 927df6b7 2019-02-10 stsp }
3243 927df6b7 2019-02-10 stsp
3244 927df6b7 2019-02-10 stsp static const struct got_error *
3245 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3246 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3247 f8d1f275 2019-02-04 stsp {
3248 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3249 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3250 f8d1f275 2019-02-04 stsp char *abspath;
3251 f8d1f275 2019-02-04 stsp
3252 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3253 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3254 0584f854 2019-04-06 stsp
3255 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3256 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3257 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3258 927df6b7 2019-02-10 stsp return NULL;
3259 927df6b7 2019-02-10 stsp
3260 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3261 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3262 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3263 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3264 f8d1f275 2019-02-04 stsp } else {
3265 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3266 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3267 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3268 f8d1f275 2019-02-04 stsp }
3269 f8d1f275 2019-02-04 stsp
3270 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3271 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3272 f8d1f275 2019-02-04 stsp free(abspath);
3273 9d31a1d8 2018-03-11 stsp return err;
3274 9d31a1d8 2018-03-11 stsp }
3275 f8d1f275 2019-02-04 stsp
3276 f8d1f275 2019-02-04 stsp static const struct got_error *
3277 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3278 f8d1f275 2019-02-04 stsp {
3279 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3280 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3281 2ec1f75b 2019-03-26 stsp unsigned char status;
3282 927df6b7 2019-02-10 stsp
3283 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3284 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3285 0584f854 2019-04-06 stsp
3286 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3287 927df6b7 2019-02-10 stsp return NULL;
3288 927df6b7 2019-02-10 stsp
3289 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3290 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3291 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3292 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3293 2ec1f75b 2019-03-26 stsp else
3294 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3295 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3296 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3297 6841da00 2019-08-08 stsp }
3298 6841da00 2019-08-08 stsp
3299 6841da00 2019-08-08 stsp void
3300 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3301 6841da00 2019-08-08 stsp {
3302 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3303 6841da00 2019-08-08 stsp
3304 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3305 6841da00 2019-08-08 stsp free((char *)pe->path);
3306 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3307 6841da00 2019-08-08 stsp }
3308 6841da00 2019-08-08 stsp
3309 6841da00 2019-08-08 stsp void
3310 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3311 6841da00 2019-08-08 stsp {
3312 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3313 6841da00 2019-08-08 stsp
3314 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3315 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3316 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3317 6841da00 2019-08-08 stsp free((char *)pe->path);
3318 6841da00 2019-08-08 stsp }
3319 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3320 6841da00 2019-08-08 stsp }
3321 6841da00 2019-08-08 stsp
3322 6841da00 2019-08-08 stsp static const struct got_error *
3323 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3324 6841da00 2019-08-08 stsp {
3325 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3326 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3327 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3328 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3329 6841da00 2019-08-08 stsp size_t linesize = 0;
3330 6841da00 2019-08-08 stsp ssize_t linelen;
3331 6841da00 2019-08-08 stsp
3332 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3333 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3334 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3335 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3336 6841da00 2019-08-08 stsp
3337 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3338 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3339 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3340 bd8de430 2019-10-04 stsp
3341 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3342 bd8de430 2019-10-04 stsp if (line[0] == '#')
3343 bd8de430 2019-10-04 stsp continue;
3344 bd8de430 2019-10-04 stsp
3345 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3346 bd8de430 2019-10-04 stsp if (line[0] == '!')
3347 bd8de430 2019-10-04 stsp continue;
3348 bd8de430 2019-10-04 stsp
3349 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3350 6841da00 2019-08-08 stsp line) == -1) {
3351 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3352 6841da00 2019-08-08 stsp goto done;
3353 6841da00 2019-08-08 stsp }
3354 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3355 6841da00 2019-08-08 stsp if (err)
3356 6841da00 2019-08-08 stsp goto done;
3357 6841da00 2019-08-08 stsp }
3358 6841da00 2019-08-08 stsp if (ferror(f)) {
3359 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3360 6841da00 2019-08-08 stsp goto done;
3361 6841da00 2019-08-08 stsp }
3362 6841da00 2019-08-08 stsp
3363 6841da00 2019-08-08 stsp dirpath = strdup(path);
3364 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3365 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3366 6841da00 2019-08-08 stsp goto done;
3367 6841da00 2019-08-08 stsp }
3368 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3369 6841da00 2019-08-08 stsp done:
3370 6841da00 2019-08-08 stsp free(line);
3371 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3372 6841da00 2019-08-08 stsp free(dirpath);
3373 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3374 6841da00 2019-08-08 stsp }
3375 6841da00 2019-08-08 stsp return err;
3376 6841da00 2019-08-08 stsp }
3377 6841da00 2019-08-08 stsp
3378 6841da00 2019-08-08 stsp int
3379 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3380 6841da00 2019-08-08 stsp {
3381 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3382 bd8de430 2019-10-04 stsp
3383 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3384 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3385 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3386 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3387 bd8de430 2019-10-04 stsp
3388 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3389 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3390 6841da00 2019-08-08 stsp
3391 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3392 bd8de430 2019-10-04 stsp continue;
3393 bd8de430 2019-10-04 stsp pattern += 3;
3394 bd8de430 2019-10-04 stsp p = path;
3395 bd8de430 2019-10-04 stsp while (*p) {
3396 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3397 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3398 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3399 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3400 bd8de430 2019-10-04 stsp p++;
3401 bd8de430 2019-10-04 stsp while (*p == '/')
3402 bd8de430 2019-10-04 stsp p++;
3403 bd8de430 2019-10-04 stsp continue;
3404 bd8de430 2019-10-04 stsp }
3405 bd8de430 2019-10-04 stsp return 1;
3406 bd8de430 2019-10-04 stsp }
3407 bd8de430 2019-10-04 stsp }
3408 bd8de430 2019-10-04 stsp }
3409 bd8de430 2019-10-04 stsp
3410 6841da00 2019-08-08 stsp /*
3411 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3412 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3413 6841da00 2019-08-08 stsp * ignores backwards.
3414 6841da00 2019-08-08 stsp */
3415 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3416 6841da00 2019-08-08 stsp while (pe) {
3417 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3418 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3419 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3420 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3421 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3422 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3423 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3424 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3425 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3426 6841da00 2019-08-08 stsp continue;
3427 6841da00 2019-08-08 stsp return 1;
3428 6841da00 2019-08-08 stsp }
3429 6841da00 2019-08-08 stsp }
3430 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3431 6841da00 2019-08-08 stsp }
3432 6841da00 2019-08-08 stsp
3433 6841da00 2019-08-08 stsp return 0;
3434 6841da00 2019-08-08 stsp }
3435 6841da00 2019-08-08 stsp
3436 6841da00 2019-08-08 stsp static const struct got_error *
3437 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3438 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3439 6841da00 2019-08-08 stsp {
3440 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3441 6841da00 2019-08-08 stsp char *ignorespath;
3442 886cec17 2019-12-15 stsp int fd = -1;
3443 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3444 6841da00 2019-08-08 stsp
3445 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3446 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3447 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3448 6841da00 2019-08-08 stsp
3449 886cec17 2019-12-15 stsp if (dirfd != -1) {
3450 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3451 886cec17 2019-12-15 stsp if (fd == -1) {
3452 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3453 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3454 886cec17 2019-12-15 stsp ignorespath);
3455 886cec17 2019-12-15 stsp } else {
3456 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3457 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3458 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3459 886cec17 2019-12-15 stsp ignorespath);
3460 886cec17 2019-12-15 stsp else {
3461 886cec17 2019-12-15 stsp fd = -1;
3462 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3463 886cec17 2019-12-15 stsp }
3464 886cec17 2019-12-15 stsp }
3465 886cec17 2019-12-15 stsp } else {
3466 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3467 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3468 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3469 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3470 886cec17 2019-12-15 stsp ignorespath);
3471 886cec17 2019-12-15 stsp } else
3472 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3473 886cec17 2019-12-15 stsp }
3474 6841da00 2019-08-08 stsp
3475 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3476 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3477 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3478 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3479 6841da00 2019-08-08 stsp free(ignorespath);
3480 6841da00 2019-08-08 stsp return err;
3481 f8d1f275 2019-02-04 stsp }
3482 f8d1f275 2019-02-04 stsp
3483 f8d1f275 2019-02-04 stsp static const struct got_error *
3484 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3485 f8d1f275 2019-02-04 stsp {
3486 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3487 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3488 f8d1f275 2019-02-04 stsp char *path = NULL;
3489 f8d1f275 2019-02-04 stsp
3490 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3491 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3492 0584f854 2019-04-06 stsp
3493 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3494 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3495 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3496 f8d1f275 2019-02-04 stsp } else {
3497 f8d1f275 2019-02-04 stsp path = de->d_name;
3498 f8d1f275 2019-02-04 stsp }
3499 f8d1f275 2019-02-04 stsp
3500 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3501 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3502 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3503 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3504 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3505 f8d1f275 2019-02-04 stsp if (parent_path[0])
3506 f8d1f275 2019-02-04 stsp free(path);
3507 b72f483a 2019-02-05 stsp return err;
3508 f8d1f275 2019-02-04 stsp }
3509 f8d1f275 2019-02-04 stsp
3510 347d1d3e 2019-07-12 stsp static const struct got_error *
3511 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3512 3143d852 2020-06-25 stsp {
3513 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3514 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3515 3143d852 2020-06-25 stsp
3516 3143d852 2020-06-25 stsp if (a->no_ignores)
3517 3143d852 2020-06-25 stsp return NULL;
3518 3143d852 2020-06-25 stsp
3519 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3520 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3521 3143d852 2020-06-25 stsp if (err)
3522 3143d852 2020-06-25 stsp return err;
3523 3143d852 2020-06-25 stsp
3524 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3525 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3526 3143d852 2020-06-25 stsp
3527 3143d852 2020-06-25 stsp return err;
3528 3143d852 2020-06-25 stsp }
3529 3143d852 2020-06-25 stsp
3530 3143d852 2020-06-25 stsp static const struct got_error *
3531 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3532 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3533 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3534 abb4604f 2019-07-27 stsp {
3535 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3536 abb4604f 2019-07-27 stsp struct stat sb;
3537 abb4604f 2019-07-27 stsp
3538 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3539 abb4604f 2019-07-27 stsp if (ie)
3540 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3541 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3542 abb4604f 2019-07-27 stsp
3543 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3544 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3545 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3546 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3547 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3548 abb4604f 2019-07-27 stsp return NULL;
3549 abb4604f 2019-07-27 stsp }
3550 abb4604f 2019-07-27 stsp
3551 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3552 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3553 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3554 abb4604f 2019-07-27 stsp
3555 abb4604f 2019-07-27 stsp return NULL;
3556 3143d852 2020-06-25 stsp }
3557 3143d852 2020-06-25 stsp
3558 3143d852 2020-06-25 stsp static const struct got_error *
3559 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3560 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3561 3143d852 2020-06-25 stsp {
3562 3143d852 2020-06-25 stsp const struct got_error *err;
3563 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3564 3143d852 2020-06-25 stsp
3565 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3566 3143d852 2020-06-25 stsp ".cvsignore");
3567 3143d852 2020-06-25 stsp if (err)
3568 3143d852 2020-06-25 stsp return err;
3569 3143d852 2020-06-25 stsp
3570 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3571 3143d852 2020-06-25 stsp ".gitignore");
3572 3143d852 2020-06-25 stsp if (err)
3573 3143d852 2020-06-25 stsp return err;
3574 3143d852 2020-06-25 stsp
3575 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3576 3143d852 2020-06-25 stsp if (err) {
3577 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3578 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3579 3143d852 2020-06-25 stsp return err;
3580 3143d852 2020-06-25 stsp }
3581 3143d852 2020-06-25 stsp for (;;) {
3582 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3583 3143d852 2020-06-25 stsp ".cvsignore");
3584 3143d852 2020-06-25 stsp if (err)
3585 3143d852 2020-06-25 stsp break;
3586 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3587 3143d852 2020-06-25 stsp ".gitignore");
3588 3143d852 2020-06-25 stsp if (err)
3589 3143d852 2020-06-25 stsp break;
3590 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3591 3143d852 2020-06-25 stsp if (err) {
3592 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3593 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3594 3143d852 2020-06-25 stsp break;
3595 3143d852 2020-06-25 stsp }
3596 b737c85a 2020-06-26 stsp free(parent_path);
3597 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3598 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3599 3143d852 2020-06-25 stsp }
3600 3143d852 2020-06-25 stsp
3601 b737c85a 2020-06-26 stsp free(parent_path);
3602 b737c85a 2020-06-26 stsp free(next_parent_path);
3603 3143d852 2020-06-25 stsp return err;
3604 abb4604f 2019-07-27 stsp }
3605 abb4604f 2019-07-27 stsp
3606 abb4604f 2019-07-27 stsp static const struct got_error *
3607 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3608 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3609 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3610 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3611 f2a9dc41 2019-12-13 tracey int report_unchanged)
3612 f8d1f275 2019-02-04 stsp {
3613 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3614 6fc93f37 2019-12-13 stsp int fd = -1;
3615 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3616 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3617 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3618 3143d852 2020-06-25 stsp
3619 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3620 f8d1f275 2019-02-04 stsp
3621 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3622 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3623 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3624 8dc303cc 2019-07-27 stsp
3625 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3626 6fc93f37 2019-12-13 stsp if (fd == -1) {
3627 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3628 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3629 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3630 abb4604f 2019-07-27 stsp else
3631 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3632 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3633 f2a9dc41 2019-12-13 tracey report_unchanged);
3634 abb4604f 2019-07-27 stsp } else {
3635 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3636 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3637 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3638 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3639 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3640 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3641 abb4604f 2019-07-27 stsp arg.status_path = path;
3642 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3643 abb4604f 2019-07-27 stsp arg.repo = repo;
3644 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3645 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3646 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3647 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3648 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3649 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3650 022fae89 2019-12-06 tracey if (!no_ignores) {
3651 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3652 3143d852 2020-06-25 stsp worktree->root_path, path);
3653 3143d852 2020-06-25 stsp if (err)
3654 3143d852 2020-06-25 stsp goto done;
3655 022fae89 2019-12-06 tracey }
3656 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3657 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3658 927df6b7 2019-02-10 stsp }
3659 3143d852 2020-06-25 stsp done:
3660 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3661 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3662 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3663 927df6b7 2019-02-10 stsp free(ondisk_path);
3664 347d1d3e 2019-07-12 stsp return err;
3665 347d1d3e 2019-07-12 stsp }
3666 347d1d3e 2019-07-12 stsp
3667 347d1d3e 2019-07-12 stsp const struct got_error *
3668 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3669 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3670 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3671 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3672 347d1d3e 2019-07-12 stsp {
3673 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3674 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3675 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3676 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3677 347d1d3e 2019-07-12 stsp
3678 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3679 347d1d3e 2019-07-12 stsp if (err)
3680 347d1d3e 2019-07-12 stsp return err;
3681 347d1d3e 2019-07-12 stsp
3682 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3683 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3684 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3685 72ea6654 2019-07-27 stsp if (err)
3686 72ea6654 2019-07-27 stsp break;
3687 72ea6654 2019-07-27 stsp }
3688 f8d1f275 2019-02-04 stsp free(fileindex_path);
3689 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3690 f8d1f275 2019-02-04 stsp return err;
3691 f8d1f275 2019-02-04 stsp }
3692 6c7ab921 2019-03-18 stsp
3693 6c7ab921 2019-03-18 stsp const struct got_error *
3694 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3695 6c7ab921 2019-03-18 stsp const char *arg)
3696 6c7ab921 2019-03-18 stsp {
3697 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3698 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3699 6c7ab921 2019-03-18 stsp size_t len;
3700 00bb5ea0 2020-07-23 stsp struct stat sb;
3701 01740607 2020-11-04 stsp char *abspath = NULL;
3702 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3703 6c7ab921 2019-03-18 stsp
3704 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3705 6c7ab921 2019-03-18 stsp
3706 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3707 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3708 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3709 00bb5ea0 2020-07-23 stsp
3710 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3711 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3712 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3713 00bb5ea0 2020-07-23 stsp goto done;
3714 00bb5ea0 2020-07-23 stsp }
3715 727173c3 2020-11-06 stsp sb.st_mode = 0;
3716 00bb5ea0 2020-07-23 stsp }
3717 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3718 00bb5ea0 2020-07-23 stsp /*
3719 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3720 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3721 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3722 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3723 00bb5ea0 2020-07-23 stsp */
3724 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3725 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3726 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3727 00bb5ea0 2020-07-23 stsp goto done;
3728 00bb5ea0 2020-07-23 stsp }
3729 00bb5ea0 2020-07-23 stsp
3730 00bb5ea0 2020-07-23 stsp }
3731 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3732 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3733 00bb5ea0 2020-07-23 stsp if (err)
3734 d0710d08 2019-07-22 stsp goto done;
3735 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3736 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3737 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3738 00bb5ea0 2020-07-23 stsp goto done;
3739 00bb5ea0 2020-07-23 stsp }
3740 00bb5ea0 2020-07-23 stsp } else {
3741 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3742 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3743 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3744 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3745 00bb5ea0 2020-07-23 stsp goto done;
3746 00bb5ea0 2020-07-23 stsp }
3747 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3748 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3749 01740607 2020-11-04 stsp goto done;
3750 01740607 2020-11-04 stsp }
3751 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3752 01740607 2020-11-04 stsp sizeof(canonpath));
3753 01740607 2020-11-04 stsp if (err)
3754 00bb5ea0 2020-07-23 stsp goto done;
3755 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3756 01740607 2020-11-04 stsp if (resolved == NULL) {
3757 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3758 01740607 2020-11-04 stsp goto done;
3759 00bb5ea0 2020-07-23 stsp }
3760 d0710d08 2019-07-22 stsp }
3761 d0710d08 2019-07-22 stsp }
3762 6c7ab921 2019-03-18 stsp
3763 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3764 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3765 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3766 6c7ab921 2019-03-18 stsp goto done;
3767 6c7ab921 2019-03-18 stsp }
3768 6c7ab921 2019-03-18 stsp
3769 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3770 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3771 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3772 f6d88e1a 2019-05-29 stsp if (err)
3773 f6d88e1a 2019-05-29 stsp goto done;
3774 f6d88e1a 2019-05-29 stsp } else {
3775 f6d88e1a 2019-05-29 stsp path = strdup("");
3776 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3777 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3778 f6d88e1a 2019-05-29 stsp goto done;
3779 f6d88e1a 2019-05-29 stsp }
3780 6c7ab921 2019-03-18 stsp }
3781 6c7ab921 2019-03-18 stsp
3782 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3783 6c7ab921 2019-03-18 stsp len = strlen(path);
3784 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3785 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3786 6c7ab921 2019-03-18 stsp len--;
3787 6c7ab921 2019-03-18 stsp }
3788 6c7ab921 2019-03-18 stsp done:
3789 01740607 2020-11-04 stsp free(abspath);
3790 6c7ab921 2019-03-18 stsp free(resolved);
3791 d0710d08 2019-07-22 stsp free(cwd);
3792 6c7ab921 2019-03-18 stsp if (err == NULL)
3793 6c7ab921 2019-03-18 stsp *wt_path = path;
3794 6c7ab921 2019-03-18 stsp else
3795 6c7ab921 2019-03-18 stsp free(path);
3796 d00136be 2019-03-26 stsp return err;
3797 d00136be 2019-03-26 stsp }
3798 d00136be 2019-03-26 stsp
3799 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3800 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3801 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3802 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3803 4e68cba3 2019-11-23 stsp void *progress_arg;
3804 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3805 4e68cba3 2019-11-23 stsp };
3806 4e68cba3 2019-11-23 stsp
3807 4e68cba3 2019-11-23 stsp static const struct got_error *
3808 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3809 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3810 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3811 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3812 07f5b47a 2019-06-02 stsp {
3813 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3814 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3815 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3816 6d022e97 2019-08-04 stsp struct stat sb;
3817 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3818 07f5b47a 2019-06-02 stsp
3819 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3820 dbb83fbd 2019-12-12 stsp relpath) == -1)
3821 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3822 dbb83fbd 2019-12-12 stsp
3823 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3824 6d022e97 2019-08-04 stsp if (ie) {
3825 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3826 12463d8b 2019-12-13 stsp de_name, a->repo);
3827 6d022e97 2019-08-04 stsp if (err)
3828 dbb83fbd 2019-12-12 stsp goto done;
3829 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3830 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3831 dbb83fbd 2019-12-12 stsp goto done;
3832 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3833 dbb83fbd 2019-12-12 stsp if (err)
3834 dbb83fbd 2019-12-12 stsp goto done;
3835 6d022e97 2019-08-04 stsp }
3836 07f5b47a 2019-06-02 stsp
3837 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3838 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3839 dbb83fbd 2019-12-12 stsp goto done;
3840 4e68cba3 2019-11-23 stsp }
3841 07f5b47a 2019-06-02 stsp
3842 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3843 4e68cba3 2019-11-23 stsp if (err)
3844 4e68cba3 2019-11-23 stsp goto done;
3845 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3846 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3847 3969253a 2020-03-07 stsp if (err) {
3848 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3849 3969253a 2020-03-07 stsp goto done;
3850 3969253a 2020-03-07 stsp }
3851 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3852 07f5b47a 2019-06-02 stsp if (err) {
3853 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3854 4e68cba3 2019-11-23 stsp goto done;
3855 07f5b47a 2019-06-02 stsp }
3856 4e68cba3 2019-11-23 stsp done:
3857 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3858 dbb83fbd 2019-12-12 stsp if (err)
3859 dbb83fbd 2019-12-12 stsp return err;
3860 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3861 dbb83fbd 2019-12-12 stsp return NULL;
3862 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3863 07f5b47a 2019-06-02 stsp }
3864 07f5b47a 2019-06-02 stsp
3865 d00136be 2019-03-26 stsp const struct got_error *
3866 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3867 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3868 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3869 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3870 d00136be 2019-03-26 stsp {
3871 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3872 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3873 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3874 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3875 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3876 d00136be 2019-03-26 stsp
3877 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3878 d00136be 2019-03-26 stsp if (err)
3879 d00136be 2019-03-26 stsp return err;
3880 d00136be 2019-03-26 stsp
3881 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3882 d00136be 2019-03-26 stsp if (err)
3883 d00136be 2019-03-26 stsp goto done;
3884 d00136be 2019-03-26 stsp
3885 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3886 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3887 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3888 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3889 4e68cba3 2019-11-23 stsp saa.repo = repo;
3890 4e68cba3 2019-11-23 stsp
3891 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3892 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3893 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3894 1dd54920 2019-05-11 stsp if (err)
3895 af12c6b9 2019-06-04 stsp break;
3896 1dd54920 2019-05-11 stsp }
3897 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3898 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3899 af12c6b9 2019-06-04 stsp err = sync_err;
3900 d00136be 2019-03-26 stsp done:
3901 fb399478 2019-07-12 stsp free(fileindex_path);
3902 d00136be 2019-03-26 stsp if (fileindex)
3903 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3904 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3905 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3906 d00136be 2019-03-26 stsp err = unlockerr;
3907 6c7ab921 2019-03-18 stsp return err;
3908 6c7ab921 2019-03-18 stsp }
3909 17ed4618 2019-06-02 stsp
3910 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3911 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3912 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3913 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3914 f2a9dc41 2019-12-13 tracey void *progress_arg;
3915 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3916 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3917 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3918 766841c2 2020-08-13 stsp const char *status_codes;
3919 f2a9dc41 2019-12-13 tracey };
3920 f2a9dc41 2019-12-13 tracey
3921 f2a9dc41 2019-12-13 tracey static const struct got_error *
3922 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3923 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3924 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3925 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3926 17ed4618 2019-06-02 stsp {
3927 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3928 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3929 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3930 17ed4618 2019-06-02 stsp struct stat sb;
3931 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3932 17ed4618 2019-06-02 stsp
3933 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3934 17ed4618 2019-06-02 stsp if (ie == NULL)
3935 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3936 2ec1f75b 2019-03-26 stsp
3937 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3938 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3939 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3940 9acbc4fa 2019-08-03 stsp return NULL;
3941 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3942 9acbc4fa 2019-08-03 stsp }
3943 9acbc4fa 2019-08-03 stsp
3944 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3945 f2a9dc41 2019-12-13 tracey relpath) == -1)
3946 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3947 f2a9dc41 2019-12-13 tracey
3948 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3949 12463d8b 2019-12-13 stsp a->repo);
3950 17ed4618 2019-06-02 stsp if (err)
3951 f2a9dc41 2019-12-13 tracey goto done;
3952 17ed4618 2019-06-02 stsp
3953 766841c2 2020-08-13 stsp if (a->status_codes) {
3954 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3955 766841c2 2020-08-13 stsp int i;
3956 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3957 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3958 766841c2 2020-08-13 stsp break;
3959 766841c2 2020-08-13 stsp }
3960 766841c2 2020-08-13 stsp if (i == ncodes) {
3961 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3962 766841c2 2020-08-13 stsp free(ondisk_path);
3963 766841c2 2020-08-13 stsp return NULL;
3964 766841c2 2020-08-13 stsp }
3965 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3966 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3967 766841c2 2020-08-13 stsp static char msg[64];
3968 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3969 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3970 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3971 766841c2 2020-08-13 stsp goto done;
3972 766841c2 2020-08-13 stsp }
3973 766841c2 2020-08-13 stsp }
3974 766841c2 2020-08-13 stsp
3975 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3976 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3977 f2a9dc41 2019-12-13 tracey goto done;
3978 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3979 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3980 f2a9dc41 2019-12-13 tracey goto done;
3981 f2a9dc41 2019-12-13 tracey }
3982 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3983 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3984 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3985 f2a9dc41 2019-12-13 tracey goto done;
3986 f2a9dc41 2019-12-13 tracey }
3987 17ed4618 2019-06-02 stsp }
3988 17ed4618 2019-06-02 stsp
3989 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3990 20a2ad1c 2020-10-20 stsp size_t root_len;
3991 20a2ad1c 2020-10-20 stsp
3992 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3993 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3994 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3995 12463d8b 2019-12-13 stsp ondisk_path);
3996 12463d8b 2019-12-13 stsp goto done;
3997 12463d8b 2019-12-13 stsp }
3998 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3999 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4000 12463d8b 2019-12-13 stsp goto done;
4001 15341bfd 2020-03-05 tracey }
4002 15341bfd 2020-03-05 tracey
4003 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4004 20a2ad1c 2020-10-20 stsp do {
4005 20a2ad1c 2020-10-20 stsp char *parent;
4006 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4007 20a2ad1c 2020-10-20 stsp if (err)
4008 2513f20a 2020-10-20 stsp goto done;
4009 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4010 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4011 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4012 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4013 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4014 20a2ad1c 2020-10-20 stsp ondisk_path);
4015 15341bfd 2020-03-05 tracey break;
4016 15341bfd 2020-03-05 tracey }
4017 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4018 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4019 f2a9dc41 2019-12-13 tracey }
4020 17ed4618 2019-06-02 stsp
4021 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4022 f2a9dc41 2019-12-13 tracey done:
4023 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4024 f2a9dc41 2019-12-13 tracey if (err)
4025 f2a9dc41 2019-12-13 tracey return err;
4026 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4027 f2a9dc41 2019-12-13 tracey return NULL;
4028 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4029 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4030 17ed4618 2019-06-02 stsp }
4031 17ed4618 2019-06-02 stsp
4032 2ec1f75b 2019-03-26 stsp const struct got_error *
4033 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4034 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4035 766841c2 2020-08-13 stsp const char *status_codes,
4036 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4037 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4038 2ec1f75b 2019-03-26 stsp {
4039 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4040 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4041 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4042 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4043 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4044 2ec1f75b 2019-03-26 stsp
4045 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4046 2ec1f75b 2019-03-26 stsp if (err)
4047 2ec1f75b 2019-03-26 stsp return err;
4048 2ec1f75b 2019-03-26 stsp
4049 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4050 2ec1f75b 2019-03-26 stsp if (err)
4051 2ec1f75b 2019-03-26 stsp goto done;
4052 f2a9dc41 2019-12-13 tracey
4053 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4054 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4055 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4056 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4057 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4058 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4059 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4060 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4061 2ec1f75b 2019-03-26 stsp
4062 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4063 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4064 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4065 17ed4618 2019-06-02 stsp if (err)
4066 af12c6b9 2019-06-04 stsp break;
4067 2ec1f75b 2019-03-26 stsp }
4068 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4069 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4070 af12c6b9 2019-06-04 stsp err = sync_err;
4071 2ec1f75b 2019-03-26 stsp done:
4072 fb399478 2019-07-12 stsp free(fileindex_path);
4073 2ec1f75b 2019-03-26 stsp if (fileindex)
4074 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4075 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4076 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4077 2ec1f75b 2019-03-26 stsp err = unlockerr;
4078 33aa809d 2019-08-08 stsp return err;
4079 33aa809d 2019-08-08 stsp }
4080 33aa809d 2019-08-08 stsp
4081 33aa809d 2019-08-08 stsp static const struct got_error *
4082 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4083 33aa809d 2019-08-08 stsp {
4084 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4085 33aa809d 2019-08-08 stsp char *line = NULL;
4086 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4087 33aa809d 2019-08-08 stsp ssize_t linelen;
4088 33aa809d 2019-08-08 stsp
4089 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4090 33aa809d 2019-08-08 stsp if (linelen == -1) {
4091 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4092 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4093 33aa809d 2019-08-08 stsp goto done;
4094 33aa809d 2019-08-08 stsp }
4095 33aa809d 2019-08-08 stsp return NULL;
4096 33aa809d 2019-08-08 stsp }
4097 33aa809d 2019-08-08 stsp if (outfile) {
4098 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4099 33aa809d 2019-08-08 stsp if (n != linelen) {
4100 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4101 33aa809d 2019-08-08 stsp goto done;
4102 33aa809d 2019-08-08 stsp }
4103 33aa809d 2019-08-08 stsp }
4104 33aa809d 2019-08-08 stsp if (rejectfile) {
4105 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4106 33aa809d 2019-08-08 stsp if (n != linelen)
4107 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4108 33aa809d 2019-08-08 stsp }
4109 33aa809d 2019-08-08 stsp done:
4110 33aa809d 2019-08-08 stsp free(line);
4111 2ec1f75b 2019-03-26 stsp return err;
4112 2ec1f75b 2019-03-26 stsp }
4113 1f1abb7e 2019-08-08 stsp
4114 33aa809d 2019-08-08 stsp static const struct got_error *
4115 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4116 33aa809d 2019-08-08 stsp {
4117 33aa809d 2019-08-08 stsp char *line = NULL;
4118 33aa809d 2019-08-08 stsp size_t linesize = 0;
4119 33aa809d 2019-08-08 stsp ssize_t linelen;
4120 33aa809d 2019-08-08 stsp
4121 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4122 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4123 500467ff 2019-09-25 hiltjo if (ferror(f))
4124 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4125 500467ff 2019-09-25 hiltjo return NULL;
4126 500467ff 2019-09-25 hiltjo }
4127 33aa809d 2019-08-08 stsp free(line);
4128 33aa809d 2019-08-08 stsp return NULL;
4129 33aa809d 2019-08-08 stsp }
4130 33aa809d 2019-08-08 stsp
4131 33aa809d 2019-08-08 stsp static const struct got_error *
4132 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4133 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4134 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4135 33aa809d 2019-08-08 stsp {
4136 33aa809d 2019-08-08 stsp const struct got_error *err;
4137 33aa809d 2019-08-08 stsp
4138 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4139 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4140 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4141 33aa809d 2019-08-08 stsp if (err)
4142 33aa809d 2019-08-08 stsp return err;
4143 33aa809d 2019-08-08 stsp (*line_cur1)++;
4144 33aa809d 2019-08-08 stsp }
4145 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4146 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4147 33aa809d 2019-08-08 stsp if (rejectfile)
4148 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4149 33aa809d 2019-08-08 stsp else
4150 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4151 33aa809d 2019-08-08 stsp if (err)
4152 33aa809d 2019-08-08 stsp return err;
4153 33aa809d 2019-08-08 stsp (*line_cur2)++;
4154 33aa809d 2019-08-08 stsp }
4155 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4156 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4157 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4158 33aa809d 2019-08-08 stsp if (err)
4159 33aa809d 2019-08-08 stsp return err;
4160 33aa809d 2019-08-08 stsp (*line_cur2)++;
4161 33aa809d 2019-08-08 stsp }
4162 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4163 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4164 33aa809d 2019-08-08 stsp if (rejectfile)
4165 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4166 33aa809d 2019-08-08 stsp else
4167 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4168 33aa809d 2019-08-08 stsp if (err)
4169 33aa809d 2019-08-08 stsp return err;
4170 33aa809d 2019-08-08 stsp (*line_cur1)++;
4171 33aa809d 2019-08-08 stsp }
4172 f1e81a05 2019-08-10 stsp
4173 f1e81a05 2019-08-10 stsp return NULL;
4174 f1e81a05 2019-08-10 stsp }
4175 f1e81a05 2019-08-10 stsp
4176 f1e81a05 2019-08-10 stsp static const struct got_error *
4177 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4178 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4179 f1e81a05 2019-08-10 stsp {
4180 f1e81a05 2019-08-10 stsp const struct got_error *err;
4181 f1e81a05 2019-08-10 stsp
4182 f1e81a05 2019-08-10 stsp if (outfile) {
4183 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4184 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4185 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4186 f1e81a05 2019-08-10 stsp if (err)
4187 f1e81a05 2019-08-10 stsp return err;
4188 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4189 f1e81a05 2019-08-10 stsp }
4190 f1e81a05 2019-08-10 stsp }
4191 f1e81a05 2019-08-10 stsp if (rejectfile) {
4192 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4193 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4194 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4195 f1e81a05 2019-08-10 stsp if (err)
4196 f1e81a05 2019-08-10 stsp return err;
4197 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4198 f1e81a05 2019-08-10 stsp }
4199 33aa809d 2019-08-08 stsp }
4200 33aa809d 2019-08-08 stsp
4201 33aa809d 2019-08-08 stsp return NULL;
4202 33aa809d 2019-08-08 stsp }
4203 33aa809d 2019-08-08 stsp
4204 33aa809d 2019-08-08 stsp static const struct got_error *
4205 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4206 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4207 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4208 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4209 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4210 33aa809d 2019-08-08 stsp {
4211 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4212 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4213 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4214 33aa809d 2019-08-08 stsp FILE *hunkfile;
4215 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4216 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4217 fe621944 2020-11-10 stsp int rc;
4218 33aa809d 2019-08-08 stsp
4219 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4220 33aa809d 2019-08-08 stsp
4221 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4222 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4223 fe621944 2020-11-10 stsp start_old = cc.left.start;
4224 fe621944 2020-11-10 stsp end_old = cc.left.end;
4225 fe621944 2020-11-10 stsp start_new = cc.right.start;
4226 fe621944 2020-11-10 stsp end_new = cc.right.end;
4227 33aa809d 2019-08-08 stsp
4228 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4229 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4230 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4231 33aa809d 2019-08-08 stsp
4232 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4233 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4234 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4235 33aa809d 2019-08-08 stsp
4236 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4237 fe621944 2020-11-10 stsp if (diff_state == NULL)
4238 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4239 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4240 fe621944 2020-11-10 stsp
4241 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4242 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4243 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4244 33aa809d 2019-08-08 stsp goto done;
4245 33aa809d 2019-08-08 stsp }
4246 fe621944 2020-11-10 stsp
4247 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4248 fe621944 2020-11-10 stsp diff_result, &cc);
4249 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4250 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4251 33aa809d 2019-08-08 stsp goto done;
4252 33aa809d 2019-08-08 stsp }
4253 fe621944 2020-11-10 stsp
4254 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4255 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4256 33aa809d 2019-08-08 stsp goto done;
4257 33aa809d 2019-08-08 stsp }
4258 33aa809d 2019-08-08 stsp
4259 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4260 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4261 33aa809d 2019-08-08 stsp if (err)
4262 33aa809d 2019-08-08 stsp goto done;
4263 33aa809d 2019-08-08 stsp
4264 33aa809d 2019-08-08 stsp switch (*choice) {
4265 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4266 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4267 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4268 33aa809d 2019-08-08 stsp break;
4269 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4270 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4271 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4272 33aa809d 2019-08-08 stsp break;
4273 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4274 33aa809d 2019-08-08 stsp break;
4275 33aa809d 2019-08-08 stsp default:
4276 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4277 33aa809d 2019-08-08 stsp break;
4278 33aa809d 2019-08-08 stsp }
4279 33aa809d 2019-08-08 stsp done:
4280 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4281 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4282 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4283 33aa809d 2019-08-08 stsp return err;
4284 33aa809d 2019-08-08 stsp }
4285 33aa809d 2019-08-08 stsp
4286 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4287 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4288 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4289 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4290 1f1abb7e 2019-08-08 stsp void *progress_arg;
4291 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4292 33aa809d 2019-08-08 stsp void *patch_arg;
4293 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4294 1f1abb7e 2019-08-08 stsp };
4295 a129376b 2019-03-28 stsp
4296 e20a8b6f 2019-06-04 stsp static const struct got_error *
4297 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4298 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4299 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4300 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4301 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4302 33aa809d 2019-08-08 stsp {
4303 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4304 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4305 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4306 1ebedb77 2019-10-19 stsp int fd2 = -1;
4307 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4308 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4309 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4310 fe621944 2020-11-10 stsp struct stat sb2;
4311 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4312 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4313 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4314 33aa809d 2019-08-08 stsp
4315 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4316 33aa809d 2019-08-08 stsp
4317 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4318 33aa809d 2019-08-08 stsp if (err)
4319 33aa809d 2019-08-08 stsp return err;
4320 33aa809d 2019-08-08 stsp
4321 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4322 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4323 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4324 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4325 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4326 fa3cef63 2020-07-23 stsp goto done;
4327 fa3cef63 2020-07-23 stsp }
4328 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4329 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4330 fa3cef63 2020-07-23 stsp if (link_len == -1)
4331 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4332 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4333 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4334 12463d8b 2019-12-13 stsp }
4335 12463d8b 2019-12-13 stsp } else {
4336 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4337 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4338 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4339 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4340 fa3cef63 2020-07-23 stsp goto done;
4341 fa3cef63 2020-07-23 stsp }
4342 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4343 fa3cef63 2020-07-23 stsp sizeof(link_target));
4344 fa3cef63 2020-07-23 stsp if (link_len == -1)
4345 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4346 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4347 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4348 12463d8b 2019-12-13 stsp }
4349 1ebedb77 2019-10-19 stsp }
4350 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4351 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4352 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4353 fa3cef63 2020-07-23 stsp goto done;
4354 fa3cef63 2020-07-23 stsp }
4355 1ebedb77 2019-10-19 stsp
4356 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4357 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4358 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4359 fa3cef63 2020-07-23 stsp goto done;
4360 fa3cef63 2020-07-23 stsp }
4361 fa3cef63 2020-07-23 stsp fd2 = -1;
4362 fa3cef63 2020-07-23 stsp } else {
4363 fa3cef63 2020-07-23 stsp size_t n;
4364 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4365 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4366 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4367 fa3cef63 2020-07-23 stsp goto done;
4368 fa3cef63 2020-07-23 stsp }
4369 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4370 fa3cef63 2020-07-23 stsp if (n != link_len) {
4371 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4372 fa3cef63 2020-07-23 stsp goto done;
4373 fa3cef63 2020-07-23 stsp }
4374 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4375 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4376 fa3cef63 2020-07-23 stsp goto done;
4377 fa3cef63 2020-07-23 stsp }
4378 fa3cef63 2020-07-23 stsp rewind(f2);
4379 33aa809d 2019-08-08 stsp }
4380 33aa809d 2019-08-08 stsp
4381 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4382 33aa809d 2019-08-08 stsp if (err)
4383 33aa809d 2019-08-08 stsp goto done;
4384 33aa809d 2019-08-08 stsp
4385 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4386 33aa809d 2019-08-08 stsp if (err)
4387 33aa809d 2019-08-08 stsp goto done;
4388 33aa809d 2019-08-08 stsp
4389 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4390 33aa809d 2019-08-08 stsp if (err)
4391 33aa809d 2019-08-08 stsp goto done;
4392 33aa809d 2019-08-08 stsp
4393 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4394 fe621944 2020-11-10 stsp NULL);
4395 33aa809d 2019-08-08 stsp if (err)
4396 33aa809d 2019-08-08 stsp goto done;
4397 33aa809d 2019-08-08 stsp
4398 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4399 33aa809d 2019-08-08 stsp if (err)
4400 33aa809d 2019-08-08 stsp goto done;
4401 33aa809d 2019-08-08 stsp
4402 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4403 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4404 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4405 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4406 fe621944 2020-11-10 stsp
4407 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4408 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4409 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4410 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4411 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4412 fe621944 2020-11-10 stsp nchanges++;
4413 fe621944 2020-11-10 stsp }
4414 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4415 33aa809d 2019-08-08 stsp int choice;
4416 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4417 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4418 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4419 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4420 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4421 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4422 33aa809d 2019-08-08 stsp if (err)
4423 33aa809d 2019-08-08 stsp goto done;
4424 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4425 33aa809d 2019-08-08 stsp have_content = 1;
4426 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4427 33aa809d 2019-08-08 stsp break;
4428 33aa809d 2019-08-08 stsp }
4429 1ebedb77 2019-10-19 stsp if (have_content) {
4430 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4431 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4432 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4433 1ebedb77 2019-10-19 stsp if (err)
4434 1ebedb77 2019-10-19 stsp goto done;
4435 1ebedb77 2019-10-19 stsp
4436 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4437 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4438 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4439 fa3cef63 2020-07-23 stsp goto done;
4440 fa3cef63 2020-07-23 stsp }
4441 1ebedb77 2019-10-19 stsp }
4442 1ebedb77 2019-10-19 stsp }
4443 33aa809d 2019-08-08 stsp done:
4444 33aa809d 2019-08-08 stsp free(id_str);
4445 33aa809d 2019-08-08 stsp if (blob)
4446 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4447 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4448 fe621944 2020-11-10 stsp if (err == NULL)
4449 fe621944 2020-11-10 stsp err = free_err;
4450 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4451 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4452 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4453 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4454 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4455 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4456 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4457 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4458 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4459 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4460 33aa809d 2019-08-08 stsp if (err || !have_content) {
4461 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4462 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4463 33aa809d 2019-08-08 stsp free(*path_outfile);
4464 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4465 33aa809d 2019-08-08 stsp }
4466 33aa809d 2019-08-08 stsp free(path1);
4467 33aa809d 2019-08-08 stsp return err;
4468 33aa809d 2019-08-08 stsp }
4469 33aa809d 2019-08-08 stsp
4470 33aa809d 2019-08-08 stsp static const struct got_error *
4471 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4472 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4473 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4474 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4475 a129376b 2019-03-28 stsp {
4476 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4477 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4478 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4479 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4480 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4481 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4482 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4483 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4484 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4485 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4486 a129376b 2019-03-28 stsp
4487 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4488 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4489 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4490 d3bcc3d1 2019-08-08 stsp return NULL;
4491 3d69ad8d 2019-08-17 semarie
4492 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4493 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4494 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4495 d3bcc3d1 2019-08-08 stsp
4496 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4497 65084dad 2019-08-08 stsp if (ie == NULL)
4498 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4499 a129376b 2019-03-28 stsp
4500 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4501 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4502 a129376b 2019-03-28 stsp if (err) {
4503 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4504 a129376b 2019-03-28 stsp goto done;
4505 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4506 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4507 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4508 e20a8b6f 2019-06-04 stsp goto done;
4509 e20a8b6f 2019-06-04 stsp }
4510 a129376b 2019-03-28 stsp }
4511 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4512 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4513 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4514 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4515 a129376b 2019-03-28 stsp goto done;
4516 a129376b 2019-03-28 stsp }
4517 a129376b 2019-03-28 stsp } else {
4518 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4519 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4520 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4521 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4522 a129376b 2019-03-28 stsp goto done;
4523 a129376b 2019-03-28 stsp }
4524 a129376b 2019-03-28 stsp } else {
4525 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4526 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4527 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4528 a129376b 2019-03-28 stsp goto done;
4529 a129376b 2019-03-28 stsp }
4530 a129376b 2019-03-28 stsp }
4531 a129376b 2019-03-28 stsp }
4532 a129376b 2019-03-28 stsp
4533 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4534 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4535 a9fa2909 2019-07-27 stsp if (err) {
4536 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4537 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4538 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4539 a9fa2909 2019-07-27 stsp goto done;
4540 a9fa2909 2019-07-27 stsp } else {
4541 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4542 a9fa2909 2019-07-27 stsp if (err)
4543 a9fa2909 2019-07-27 stsp goto done;
4544 a9fa2909 2019-07-27 stsp
4545 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4546 1233e6b6 2020-10-19 stsp if (err)
4547 a9fa2909 2019-07-27 stsp goto done;
4548 a9fa2909 2019-07-27 stsp
4549 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4550 1233e6b6 2020-10-19 stsp free(te_name);
4551 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4552 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4553 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4554 a9fa2909 2019-07-27 stsp goto done;
4555 a9fa2909 2019-07-27 stsp }
4556 a129376b 2019-03-28 stsp }
4557 a129376b 2019-03-28 stsp
4558 a129376b 2019-03-28 stsp switch (status) {
4559 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4560 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4561 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4562 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4563 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4564 33aa809d 2019-08-08 stsp if (err)
4565 33aa809d 2019-08-08 stsp goto done;
4566 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4567 33aa809d 2019-08-08 stsp break;
4568 33aa809d 2019-08-08 stsp }
4569 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4570 1f1abb7e 2019-08-08 stsp ie->path);
4571 1ee397ad 2019-07-12 stsp if (err)
4572 1ee397ad 2019-07-12 stsp goto done;
4573 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4574 a129376b 2019-03-28 stsp break;
4575 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4576 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4577 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4578 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4579 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4580 33aa809d 2019-08-08 stsp if (err)
4581 33aa809d 2019-08-08 stsp goto done;
4582 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4583 33aa809d 2019-08-08 stsp break;
4584 33aa809d 2019-08-08 stsp }
4585 33aa809d 2019-08-08 stsp /* fall through */
4586 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4587 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4588 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4589 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4590 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4591 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4592 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4593 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4594 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4595 24278f30 2019-08-03 stsp } else
4596 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4597 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4598 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4599 a129376b 2019-03-28 stsp if (err)
4600 65084dad 2019-08-08 stsp goto done;
4601 65084dad 2019-08-08 stsp
4602 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4603 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4604 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4605 a129376b 2019-03-28 stsp goto done;
4606 65084dad 2019-08-08 stsp }
4607 65084dad 2019-08-08 stsp
4608 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4609 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4610 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4611 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4612 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4613 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4614 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4615 33aa809d 2019-08-08 stsp break;
4616 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4617 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4618 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4619 369fd7e5 2020-07-23 stsp path_content);
4620 369fd7e5 2020-07-23 stsp break;
4621 369fd7e5 2020-07-23 stsp }
4622 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4623 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4624 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4625 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4626 369fd7e5 2020-07-23 stsp } else {
4627 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4628 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4629 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4630 369fd7e5 2020-07-23 stsp goto done;
4631 369fd7e5 2020-07-23 stsp }
4632 33aa809d 2019-08-08 stsp }
4633 33aa809d 2019-08-08 stsp } else {
4634 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4635 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4636 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4637 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4638 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4639 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4640 2e1fa222 2020-07-23 stsp } else {
4641 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4642 2e1fa222 2020-07-23 stsp ie->path,
4643 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4644 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4645 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4646 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4647 2e1fa222 2020-07-23 stsp }
4648 a129376b 2019-03-28 stsp if (err)
4649 a129376b 2019-03-28 stsp goto done;
4650 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4651 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4652 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4653 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4654 437adc9d 2020-12-10 yzhong blob->id.sha1,
4655 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4656 2e1fa222 2020-07-23 stsp if (err)
4657 2e1fa222 2020-07-23 stsp goto done;
4658 2e1fa222 2020-07-23 stsp }
4659 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4660 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4661 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4662 33aa809d 2019-08-08 stsp }
4663 a129376b 2019-03-28 stsp }
4664 a129376b 2019-03-28 stsp break;
4665 e20a8b6f 2019-06-04 stsp }
4666 a129376b 2019-03-28 stsp default:
4667 1f1abb7e 2019-08-08 stsp break;
4668 a129376b 2019-03-28 stsp }
4669 a129376b 2019-03-28 stsp done:
4670 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4671 33aa809d 2019-08-08 stsp free(path_content);
4672 e20a8b6f 2019-06-04 stsp free(parent_path);
4673 a129376b 2019-03-28 stsp free(tree_path);
4674 a129376b 2019-03-28 stsp if (blob)
4675 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4676 a129376b 2019-03-28 stsp if (tree)
4677 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4678 a129376b 2019-03-28 stsp free(tree_id);
4679 e20a8b6f 2019-06-04 stsp return err;
4680 e20a8b6f 2019-06-04 stsp }
4681 e20a8b6f 2019-06-04 stsp
4682 e20a8b6f 2019-06-04 stsp const struct got_error *
4683 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4684 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4685 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4686 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4687 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4688 e20a8b6f 2019-06-04 stsp {
4689 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4690 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4691 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4692 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4693 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4694 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4695 e20a8b6f 2019-06-04 stsp
4696 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4697 e20a8b6f 2019-06-04 stsp if (err)
4698 e20a8b6f 2019-06-04 stsp return err;
4699 e20a8b6f 2019-06-04 stsp
4700 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4701 e20a8b6f 2019-06-04 stsp if (err)
4702 e20a8b6f 2019-06-04 stsp goto done;
4703 e20a8b6f 2019-06-04 stsp
4704 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4705 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4706 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4707 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4708 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4709 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4710 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4711 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4712 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4713 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4714 e20a8b6f 2019-06-04 stsp if (err)
4715 af12c6b9 2019-06-04 stsp break;
4716 e20a8b6f 2019-06-04 stsp }
4717 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4718 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4719 e20a8b6f 2019-06-04 stsp err = sync_err;
4720 af12c6b9 2019-06-04 stsp done:
4721 fb399478 2019-07-12 stsp free(fileindex_path);
4722 a129376b 2019-03-28 stsp if (fileindex)
4723 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4724 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4725 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4726 a129376b 2019-03-28 stsp err = unlockerr;
4727 c4296144 2019-05-09 stsp return err;
4728 c4296144 2019-05-09 stsp }
4729 c4296144 2019-05-09 stsp
4730 cf066bf8 2019-05-09 stsp static void
4731 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4732 cf066bf8 2019-05-09 stsp {
4733 24519714 2019-05-09 stsp free(ct->path);
4734 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4735 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4736 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4737 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4738 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4739 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4740 cf066bf8 2019-05-09 stsp free(ct);
4741 cf066bf8 2019-05-09 stsp }
4742 24519714 2019-05-09 stsp
4743 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4744 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4745 24519714 2019-05-09 stsp struct got_repository *repo;
4746 24519714 2019-05-09 stsp struct got_worktree *worktree;
4747 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4748 5f8a88c6 2019-08-03 stsp int have_staged_files;
4749 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4750 24519714 2019-05-09 stsp };
4751 cf066bf8 2019-05-09 stsp
4752 c4296144 2019-05-09 stsp static const struct got_error *
4753 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4754 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4755 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4756 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4757 c4296144 2019-05-09 stsp {
4758 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4759 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4760 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4761 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4762 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4763 768aea60 2019-05-09 stsp struct stat sb;
4764 c4296144 2019-05-09 stsp
4765 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4766 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4767 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4768 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4769 5f8a88c6 2019-08-03 stsp return NULL;
4770 5f8a88c6 2019-08-03 stsp } else {
4771 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4772 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4773 c4296144 2019-05-09 stsp
4774 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4775 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4776 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4777 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4778 5f8a88c6 2019-08-03 stsp return NULL;
4779 5f8a88c6 2019-08-03 stsp }
4780 0b5cc0d6 2019-05-09 stsp
4781 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4782 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4783 036813ee 2019-05-09 stsp goto done;
4784 036813ee 2019-05-09 stsp }
4785 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4786 036813ee 2019-05-09 stsp parent_path = strdup("");
4787 69960a46 2019-05-09 stsp if (parent_path == NULL)
4788 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4789 69960a46 2019-05-09 stsp } else {
4790 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4791 69960a46 2019-05-09 stsp if (err)
4792 69960a46 2019-05-09 stsp return err;
4793 69960a46 2019-05-09 stsp }
4794 c4296144 2019-05-09 stsp
4795 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4796 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4797 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4798 c4296144 2019-05-09 stsp goto done;
4799 768aea60 2019-05-09 stsp }
4800 768aea60 2019-05-09 stsp
4801 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4802 768aea60 2019-05-09 stsp relpath) == -1) {
4803 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4804 768aea60 2019-05-09 stsp goto done;
4805 768aea60 2019-05-09 stsp }
4806 0aeb8099 2020-07-23 stsp
4807 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4808 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4809 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4810 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4811 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4812 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4813 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4814 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4815 0aeb8099 2020-07-23 stsp break;
4816 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4817 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4818 0aeb8099 2020-07-23 stsp break;
4819 0aeb8099 2020-07-23 stsp default:
4820 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4821 0aeb8099 2020-07-23 stsp goto done;
4822 0aeb8099 2020-07-23 stsp }
4823 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4824 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4825 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4826 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4827 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4828 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4829 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4830 12463d8b 2019-12-13 stsp ct->ondisk_path);
4831 12463d8b 2019-12-13 stsp goto done;
4832 12463d8b 2019-12-13 stsp }
4833 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4834 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4835 768aea60 2019-05-09 stsp goto done;
4836 768aea60 2019-05-09 stsp }
4837 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4838 c4296144 2019-05-09 stsp }
4839 c4296144 2019-05-09 stsp
4840 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4841 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4842 44d03001 2019-05-09 stsp relpath) == -1) {
4843 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4844 44d03001 2019-05-09 stsp goto done;
4845 44d03001 2019-05-09 stsp }
4846 44d03001 2019-05-09 stsp
4847 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4848 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4849 35213c7c 2020-07-23 stsp int is_bad_symlink;
4850 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4851 35213c7c 2020-07-23 stsp ssize_t target_len;
4852 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4853 35213c7c 2020-07-23 stsp sizeof(target_path));
4854 35213c7c 2020-07-23 stsp if (target_len == -1) {
4855 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4856 35213c7c 2020-07-23 stsp ct->ondisk_path);
4857 35213c7c 2020-07-23 stsp goto done;
4858 35213c7c 2020-07-23 stsp }
4859 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4860 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4861 35213c7c 2020-07-23 stsp if (err)
4862 35213c7c 2020-07-23 stsp goto done;
4863 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4864 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4865 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4866 35213c7c 2020-07-23 stsp goto done;
4867 35213c7c 2020-07-23 stsp }
4868 35213c7c 2020-07-23 stsp }
4869 35213c7c 2020-07-23 stsp
4870 35213c7c 2020-07-23 stsp
4871 cf066bf8 2019-05-09 stsp ct->status = status;
4872 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4873 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4874 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4875 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4876 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4877 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4878 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4879 b416585c 2019-05-13 stsp goto done;
4880 b416585c 2019-05-13 stsp }
4881 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4882 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4883 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4884 f0b75401 2019-08-03 stsp goto done;
4885 f0b75401 2019-08-03 stsp }
4886 f0b75401 2019-08-03 stsp }
4887 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4888 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4889 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4890 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4891 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4892 036813ee 2019-05-09 stsp goto done;
4893 036813ee 2019-05-09 stsp }
4894 ca2503ea 2019-05-09 stsp }
4895 24519714 2019-05-09 stsp ct->path = strdup(path);
4896 24519714 2019-05-09 stsp if (ct->path == NULL) {
4897 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4898 24519714 2019-05-09 stsp goto done;
4899 24519714 2019-05-09 stsp }
4900 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4901 c4296144 2019-05-09 stsp done:
4902 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4903 ed175427 2019-05-09 stsp free_commitable(ct);
4904 24519714 2019-05-09 stsp free(parent_path);
4905 036813ee 2019-05-09 stsp free(path);
4906 a129376b 2019-03-28 stsp return err;
4907 a129376b 2019-03-28 stsp }
4908 c4296144 2019-05-09 stsp
4909 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4910 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4911 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4912 036813ee 2019-05-09 stsp struct got_repository *);
4913 ed175427 2019-05-09 stsp
4914 ed175427 2019-05-09 stsp static const struct got_error *
4915 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4916 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4917 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4918 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4919 afa376bf 2019-05-09 stsp struct got_repository *repo)
4920 ed175427 2019-05-09 stsp {
4921 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4922 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4923 036813ee 2019-05-09 stsp char *subpath;
4924 ed175427 2019-05-09 stsp
4925 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4926 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4927 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4928 ed175427 2019-05-09 stsp
4929 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4930 ed175427 2019-05-09 stsp if (err)
4931 ed175427 2019-05-09 stsp return err;
4932 ed175427 2019-05-09 stsp
4933 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4934 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4935 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4936 036813ee 2019-05-09 stsp free(subpath);
4937 036813ee 2019-05-09 stsp return err;
4938 036813ee 2019-05-09 stsp }
4939 ed175427 2019-05-09 stsp
4940 036813ee 2019-05-09 stsp static const struct got_error *
4941 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4942 036813ee 2019-05-09 stsp {
4943 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4944 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4945 ed175427 2019-05-09 stsp
4946 036813ee 2019-05-09 stsp *match = 0;
4947 ed175427 2019-05-09 stsp
4948 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4949 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4950 0f63689d 2019-05-10 stsp return NULL;
4951 036813ee 2019-05-09 stsp }
4952 0b5cc0d6 2019-05-09 stsp
4953 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4954 0f63689d 2019-05-10 stsp if (err)
4955 0f63689d 2019-05-10 stsp return err;
4956 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4957 036813ee 2019-05-09 stsp free(ct_parent_path);
4958 036813ee 2019-05-09 stsp return err;
4959 036813ee 2019-05-09 stsp }
4960 0b5cc0d6 2019-05-09 stsp
4961 768aea60 2019-05-09 stsp static mode_t
4962 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4963 768aea60 2019-05-09 stsp {
4964 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4965 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4966 3d9a4ec4 2020-07-23 stsp
4967 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4968 768aea60 2019-05-09 stsp }
4969 768aea60 2019-05-09 stsp
4970 036813ee 2019-05-09 stsp static const struct got_error *
4971 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4972 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4973 036813ee 2019-05-09 stsp {
4974 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4975 ca2503ea 2019-05-09 stsp
4976 036813ee 2019-05-09 stsp *new_te = NULL;
4977 0b5cc0d6 2019-05-09 stsp
4978 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4979 036813ee 2019-05-09 stsp if (err)
4980 036813ee 2019-05-09 stsp goto done;
4981 0b5cc0d6 2019-05-09 stsp
4982 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4983 036813ee 2019-05-09 stsp
4984 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4985 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4986 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4987 5f8a88c6 2019-08-03 stsp else
4988 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4989 036813ee 2019-05-09 stsp done:
4990 036813ee 2019-05-09 stsp if (err && *new_te) {
4991 56e0773d 2019-11-28 stsp free(*new_te);
4992 036813ee 2019-05-09 stsp *new_te = NULL;
4993 036813ee 2019-05-09 stsp }
4994 036813ee 2019-05-09 stsp return err;
4995 036813ee 2019-05-09 stsp }
4996 036813ee 2019-05-09 stsp
4997 036813ee 2019-05-09 stsp static const struct got_error *
4998 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4999 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5000 036813ee 2019-05-09 stsp {
5001 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5002 102b254e 2020-10-19 stsp char *ct_name = NULL;
5003 036813ee 2019-05-09 stsp
5004 036813ee 2019-05-09 stsp *new_te = NULL;
5005 036813ee 2019-05-09 stsp
5006 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5007 036813ee 2019-05-09 stsp if (*new_te == NULL)
5008 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5009 036813ee 2019-05-09 stsp
5010 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5011 102b254e 2020-10-19 stsp if (err)
5012 036813ee 2019-05-09 stsp goto done;
5013 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5014 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5015 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5016 036813ee 2019-05-09 stsp goto done;
5017 036813ee 2019-05-09 stsp }
5018 036813ee 2019-05-09 stsp
5019 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5020 036813ee 2019-05-09 stsp
5021 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5022 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5023 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5024 5f8a88c6 2019-08-03 stsp else
5025 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5026 036813ee 2019-05-09 stsp done:
5027 102b254e 2020-10-19 stsp free(ct_name);
5028 036813ee 2019-05-09 stsp if (err && *new_te) {
5029 56e0773d 2019-11-28 stsp free(*new_te);
5030 036813ee 2019-05-09 stsp *new_te = NULL;
5031 036813ee 2019-05-09 stsp }
5032 036813ee 2019-05-09 stsp return err;
5033 036813ee 2019-05-09 stsp }
5034 036813ee 2019-05-09 stsp
5035 036813ee 2019-05-09 stsp static const struct got_error *
5036 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5037 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5038 036813ee 2019-05-09 stsp {
5039 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5040 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5041 036813ee 2019-05-09 stsp
5042 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5043 036813ee 2019-05-09 stsp if (err)
5044 036813ee 2019-05-09 stsp return err;
5045 036813ee 2019-05-09 stsp if (new_pe == NULL)
5046 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5047 036813ee 2019-05-09 stsp return NULL;
5048 afa376bf 2019-05-09 stsp }
5049 afa376bf 2019-05-09 stsp
5050 afa376bf 2019-05-09 stsp static const struct got_error *
5051 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5052 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5053 afa376bf 2019-05-09 stsp {
5054 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5055 5f8a88c6 2019-08-03 stsp unsigned char status;
5056 5f8a88c6 2019-08-03 stsp
5057 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5058 afa376bf 2019-05-09 stsp ct_path++;
5059 5f8a88c6 2019-08-03 stsp
5060 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5061 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5062 5f8a88c6 2019-08-03 stsp else
5063 5f8a88c6 2019-08-03 stsp status = ct->status;
5064 5f8a88c6 2019-08-03 stsp
5065 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5066 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5067 036813ee 2019-05-09 stsp }
5068 036813ee 2019-05-09 stsp
5069 036813ee 2019-05-09 stsp static const struct got_error *
5070 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5071 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5072 44d03001 2019-05-09 stsp {
5073 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5074 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5075 44d03001 2019-05-09 stsp char *te_path;
5076 44d03001 2019-05-09 stsp
5077 44d03001 2019-05-09 stsp *modified = 0;
5078 44d03001 2019-05-09 stsp
5079 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5080 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5081 44d03001 2019-05-09 stsp te->name) == -1)
5082 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5083 44d03001 2019-05-09 stsp
5084 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5085 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5086 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5087 44d03001 2019-05-09 stsp strlen(te_path));
5088 62d463ca 2020-10-20 naddy if (*modified)
5089 44d03001 2019-05-09 stsp break;
5090 44d03001 2019-05-09 stsp }
5091 44d03001 2019-05-09 stsp
5092 44d03001 2019-05-09 stsp free(te_path);
5093 44d03001 2019-05-09 stsp return err;
5094 44d03001 2019-05-09 stsp }
5095 44d03001 2019-05-09 stsp
5096 44d03001 2019-05-09 stsp static const struct got_error *
5097 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5098 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5099 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5100 036813ee 2019-05-09 stsp {
5101 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5102 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5103 036813ee 2019-05-09 stsp
5104 036813ee 2019-05-09 stsp *ctp = NULL;
5105 036813ee 2019-05-09 stsp
5106 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5107 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5108 036813ee 2019-05-09 stsp char *ct_name = NULL;
5109 036813ee 2019-05-09 stsp int path_matches;
5110 036813ee 2019-05-09 stsp
5111 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5112 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5113 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5114 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5115 5f8a88c6 2019-08-03 stsp continue;
5116 5f8a88c6 2019-08-03 stsp } else {
5117 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5118 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5119 5f8a88c6 2019-08-03 stsp continue;
5120 5f8a88c6 2019-08-03 stsp }
5121 036813ee 2019-05-09 stsp
5122 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5123 036813ee 2019-05-09 stsp continue;
5124 036813ee 2019-05-09 stsp
5125 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5126 62d463ca 2020-10-20 naddy if (err)
5127 036813ee 2019-05-09 stsp return err;
5128 036813ee 2019-05-09 stsp if (!path_matches)
5129 036813ee 2019-05-09 stsp continue;
5130 036813ee 2019-05-09 stsp
5131 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5132 d34b633e 2020-10-19 stsp if (err)
5133 d34b633e 2020-10-19 stsp return err;
5134 d34b633e 2020-10-19 stsp
5135 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5136 d34b633e 2020-10-19 stsp free(ct_name);
5137 036813ee 2019-05-09 stsp continue;
5138 d34b633e 2020-10-19 stsp }
5139 d34b633e 2020-10-19 stsp free(ct_name);
5140 036813ee 2019-05-09 stsp
5141 036813ee 2019-05-09 stsp *ctp = ct;
5142 036813ee 2019-05-09 stsp break;
5143 036813ee 2019-05-09 stsp }
5144 2b496619 2019-07-10 stsp
5145 2b496619 2019-07-10 stsp return err;
5146 2b496619 2019-07-10 stsp }
5147 2b496619 2019-07-10 stsp
5148 2b496619 2019-07-10 stsp static const struct got_error *
5149 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5150 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5151 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5152 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5153 2b496619 2019-07-10 stsp struct got_repository *repo)
5154 2b496619 2019-07-10 stsp {
5155 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5156 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5157 2b496619 2019-07-10 stsp char *subtree_path;
5158 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5159 ba580f68 2020-03-22 stsp int nentries;
5160 2b496619 2019-07-10 stsp
5161 2b496619 2019-07-10 stsp *new_tep = NULL;
5162 2b496619 2019-07-10 stsp
5163 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5164 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5165 2b496619 2019-07-10 stsp child_path) == -1)
5166 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5167 036813ee 2019-05-09 stsp
5168 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5169 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5170 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5171 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5172 56e0773d 2019-11-28 stsp
5173 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5174 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5175 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5176 2b496619 2019-07-10 stsp goto done;
5177 2b496619 2019-07-10 stsp }
5178 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5179 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5180 2b496619 2019-07-10 stsp if (err) {
5181 56e0773d 2019-11-28 stsp free(new_te);
5182 2b496619 2019-07-10 stsp goto done;
5183 2b496619 2019-07-10 stsp }
5184 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5185 2b496619 2019-07-10 stsp done:
5186 56e0773d 2019-11-28 stsp free(id);
5187 2b496619 2019-07-10 stsp free(subtree_path);
5188 2b496619 2019-07-10 stsp if (err == NULL)
5189 2b496619 2019-07-10 stsp *new_tep = new_te;
5190 036813ee 2019-05-09 stsp return err;
5191 036813ee 2019-05-09 stsp }
5192 036813ee 2019-05-09 stsp
5193 036813ee 2019-05-09 stsp static const struct got_error *
5194 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5195 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5196 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5197 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5198 036813ee 2019-05-09 stsp struct got_repository *repo)
5199 036813ee 2019-05-09 stsp {
5200 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5201 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5202 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5203 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5204 036813ee 2019-05-09 stsp
5205 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5206 ba580f68 2020-03-22 stsp *nentries = 0;
5207 036813ee 2019-05-09 stsp
5208 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5209 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5210 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5211 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5212 036813ee 2019-05-09 stsp
5213 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5214 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5215 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5216 036813ee 2019-05-09 stsp continue;
5217 036813ee 2019-05-09 stsp
5218 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5219 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5220 036813ee 2019-05-09 stsp continue;
5221 036813ee 2019-05-09 stsp
5222 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5223 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5224 036813ee 2019-05-09 stsp if (err)
5225 036813ee 2019-05-09 stsp goto done;
5226 036813ee 2019-05-09 stsp
5227 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5228 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5229 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5230 036813ee 2019-05-09 stsp if (err)
5231 036813ee 2019-05-09 stsp goto done;
5232 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5233 afa376bf 2019-05-09 stsp if (err)
5234 afa376bf 2019-05-09 stsp goto done;
5235 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5236 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5237 2b496619 2019-07-10 stsp if (err)
5238 2f51b5b3 2019-05-09 stsp goto done;
5239 ba580f68 2020-03-22 stsp (*nentries)++;
5240 2b496619 2019-07-10 stsp } else {
5241 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5242 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5243 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5244 2b496619 2019-07-10 stsp == NULL) {
5245 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5246 2b496619 2019-07-10 stsp child_path, path_base_tree,
5247 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5248 2b496619 2019-07-10 stsp repo);
5249 2b496619 2019-07-10 stsp if (err)
5250 2b496619 2019-07-10 stsp goto done;
5251 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5252 2b496619 2019-07-10 stsp if (err)
5253 2b496619 2019-07-10 stsp goto done;
5254 ba580f68 2020-03-22 stsp (*nentries)++;
5255 9ba0479c 2019-05-10 stsp }
5256 ed175427 2019-05-09 stsp }
5257 2f51b5b3 2019-05-09 stsp }
5258 2f51b5b3 2019-05-09 stsp
5259 2f51b5b3 2019-05-09 stsp if (base_tree) {
5260 56e0773d 2019-11-28 stsp int i, nbase_entries;
5261 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5262 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5263 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5264 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5265 63c5ca5d 2019-08-24 stsp
5266 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5267 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5268 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5269 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5270 63c5ca5d 2019-08-24 stsp if (err)
5271 63c5ca5d 2019-08-24 stsp goto done;
5272 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5273 63c5ca5d 2019-08-24 stsp if (err)
5274 63c5ca5d 2019-08-24 stsp goto done;
5275 ba580f68 2020-03-22 stsp (*nentries)++;
5276 63c5ca5d 2019-08-24 stsp continue;
5277 63c5ca5d 2019-08-24 stsp }
5278 2f51b5b3 2019-05-09 stsp
5279 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5280 44d03001 2019-05-09 stsp int modified;
5281 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5282 036813ee 2019-05-09 stsp if (err)
5283 036813ee 2019-05-09 stsp goto done;
5284 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5285 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5286 2f51b5b3 2019-05-09 stsp if (err)
5287 2f51b5b3 2019-05-09 stsp goto done;
5288 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5289 44d03001 2019-05-09 stsp if (modified) {
5290 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5291 ba580f68 2020-03-22 stsp int nsubentries;
5292 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5293 ba580f68 2020-03-22 stsp &nsubentries, te,
5294 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5295 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5296 44d03001 2019-05-09 stsp if (err)
5297 44d03001 2019-05-09 stsp goto done;
5298 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5299 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5300 ba580f68 2020-03-22 stsp free(new_id);
5301 ba580f68 2020-03-22 stsp continue;
5302 ba580f68 2020-03-22 stsp }
5303 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5304 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5305 56e0773d 2019-11-28 stsp free(new_id);
5306 44d03001 2019-05-09 stsp }
5307 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5308 036813ee 2019-05-09 stsp if (err)
5309 036813ee 2019-05-09 stsp goto done;
5310 ba580f68 2020-03-22 stsp (*nentries)++;
5311 2f51b5b3 2019-05-09 stsp continue;
5312 036813ee 2019-05-09 stsp }
5313 2f51b5b3 2019-05-09 stsp
5314 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5315 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5316 f66c734c 2019-09-22 stsp if (err)
5317 f66c734c 2019-09-22 stsp goto done;
5318 2f51b5b3 2019-05-09 stsp if (ct) {
5319 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5320 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5321 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5322 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5323 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5324 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5325 2f51b5b3 2019-05-09 stsp if (err)
5326 2f51b5b3 2019-05-09 stsp goto done;
5327 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5328 2f51b5b3 2019-05-09 stsp if (err)
5329 2f51b5b3 2019-05-09 stsp goto done;
5330 ba580f68 2020-03-22 stsp (*nentries)++;
5331 2f51b5b3 2019-05-09 stsp }
5332 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5333 b416585c 2019-05-13 stsp status_arg);
5334 afa376bf 2019-05-09 stsp if (err)
5335 afa376bf 2019-05-09 stsp goto done;
5336 2f51b5b3 2019-05-09 stsp } else {
5337 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5338 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5339 2f51b5b3 2019-05-09 stsp if (err)
5340 2f51b5b3 2019-05-09 stsp goto done;
5341 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5342 2f51b5b3 2019-05-09 stsp if (err)
5343 2f51b5b3 2019-05-09 stsp goto done;
5344 ba580f68 2020-03-22 stsp (*nentries)++;
5345 2f51b5b3 2019-05-09 stsp }
5346 ca2503ea 2019-05-09 stsp }
5347 ed175427 2019-05-09 stsp }
5348 0b5cc0d6 2019-05-09 stsp
5349 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5350 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5351 ed175427 2019-05-09 stsp done:
5352 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5353 2e1fa222 2020-07-23 stsp return err;
5354 2e1fa222 2020-07-23 stsp }
5355 2e1fa222 2020-07-23 stsp
5356 2e1fa222 2020-07-23 stsp static const struct got_error *
5357 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5358 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5359 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5360 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5361 ebf99748 2019-05-09 stsp {
5362 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5363 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5364 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5365 ebf99748 2019-05-09 stsp
5366 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5367 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5368 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5369 ebf99748 2019-05-09 stsp
5370 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5371 437adc9d 2020-12-10 yzhong
5372 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5373 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5374 437adc9d 2020-12-10 yzhong if (err)
5375 437adc9d 2020-12-10 yzhong goto done;
5376 437adc9d 2020-12-10 yzhong
5377 ebf99748 2019-05-09 stsp if (ie) {
5378 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5379 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5380 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5381 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5382 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5383 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5384 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5385 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5386 437adc9d 2020-12-10 yzhong
5387 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5388 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5389 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5390 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5391 72fd46fa 2019-09-06 stsp !have_staged_files);
5392 ebf99748 2019-05-09 stsp } else
5393 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5394 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5395 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5396 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5397 72fd46fa 2019-09-06 stsp !have_staged_files);
5398 ebf99748 2019-05-09 stsp } else {
5399 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5400 ebf99748 2019-05-09 stsp if (err)
5401 437adc9d 2020-12-10 yzhong goto done;
5402 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5403 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5404 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5405 3969253a 2020-03-07 stsp if (err) {
5406 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5407 437adc9d 2020-12-10 yzhong goto done;
5408 3969253a 2020-03-07 stsp }
5409 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5410 3969253a 2020-03-07 stsp if (err) {
5411 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5412 437adc9d 2020-12-10 yzhong goto done;
5413 3969253a 2020-03-07 stsp }
5414 ebf99748 2019-05-09 stsp }
5415 437adc9d 2020-12-10 yzhong free(relpath);
5416 437adc9d 2020-12-10 yzhong relpath = NULL;
5417 ebf99748 2019-05-09 stsp }
5418 437adc9d 2020-12-10 yzhong done:
5419 437adc9d 2020-12-10 yzhong free(relpath);
5420 d56d26ce 2019-05-10 stsp return err;
5421 d56d26ce 2019-05-10 stsp }
5422 735ef5ac 2019-08-03 stsp
5423 d56d26ce 2019-05-10 stsp
5424 d56d26ce 2019-05-10 stsp static const struct got_error *
5425 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5426 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5427 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5428 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5429 735ef5ac 2019-08-03 stsp int ood_errcode)
5430 d56d26ce 2019-05-10 stsp {
5431 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5432 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5433 1a36436d 2019-06-10 stsp
5434 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5435 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5436 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5437 1a36436d 2019-06-10 stsp return NULL;
5438 9bead371 2019-07-28 stsp /*
5439 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5440 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5441 9bead371 2019-07-28 stsp */
5442 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5443 735ef5ac 2019-08-03 stsp in_repo_path);
5444 9bead371 2019-07-28 stsp if (err) {
5445 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5446 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5447 1a36436d 2019-06-10 stsp goto done;
5448 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5449 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5450 1a36436d 2019-06-10 stsp } else {
5451 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5452 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5453 735ef5ac 2019-08-03 stsp in_repo_path);
5454 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5455 1a36436d 2019-06-10 stsp goto done;
5456 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5457 1a36436d 2019-06-10 stsp }
5458 1a36436d 2019-06-10 stsp done:
5459 1a36436d 2019-06-10 stsp free(id);
5460 1a36436d 2019-06-10 stsp return err;
5461 ebf99748 2019-05-09 stsp }
5462 ebf99748 2019-05-09 stsp
5463 c4296144 2019-05-09 stsp const struct got_error *
5464 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5465 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5466 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5467 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5468 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5469 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5470 afa376bf 2019-05-09 stsp struct got_repository *repo)
5471 c4296144 2019-05-09 stsp {
5472 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5473 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5474 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5475 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5476 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5477 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5478 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5479 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5480 ba580f68 2020-03-22 stsp int nentries;
5481 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5482 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5483 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5484 c4296144 2019-05-09 stsp
5485 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5486 c4296144 2019-05-09 stsp
5487 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5488 675c7539 2019-05-09 stsp
5489 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5490 588edf97 2019-05-10 stsp if (err)
5491 588edf97 2019-05-10 stsp goto done;
5492 de18fc63 2019-05-09 stsp
5493 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5494 588edf97 2019-05-10 stsp if (err)
5495 588edf97 2019-05-10 stsp goto done;
5496 588edf97 2019-05-10 stsp
5497 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5498 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5499 33ad4cbe 2019-05-12 jcs if (err)
5500 33ad4cbe 2019-05-12 jcs goto done;
5501 33ad4cbe 2019-05-12 jcs }
5502 c4296144 2019-05-09 stsp
5503 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5504 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5505 33ad4cbe 2019-05-12 jcs goto done;
5506 33ad4cbe 2019-05-12 jcs }
5507 33ad4cbe 2019-05-12 jcs
5508 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5509 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5510 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5511 cf066bf8 2019-05-09 stsp char *ondisk_path;
5512 cf066bf8 2019-05-09 stsp
5513 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5514 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5515 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5516 5f8a88c6 2019-08-03 stsp continue;
5517 5f8a88c6 2019-08-03 stsp
5518 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5519 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5520 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5521 cf066bf8 2019-05-09 stsp continue;
5522 cf066bf8 2019-05-09 stsp
5523 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5524 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5525 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5526 cf066bf8 2019-05-09 stsp goto done;
5527 cf066bf8 2019-05-09 stsp }
5528 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5529 2e1fa222 2020-07-23 stsp free(ondisk_path);
5530 2e1fa222 2020-07-23 stsp if (err)
5531 cf066bf8 2019-05-09 stsp goto done;
5532 cf066bf8 2019-05-09 stsp }
5533 036813ee 2019-05-09 stsp
5534 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5535 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5536 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5537 036813ee 2019-05-09 stsp if (err)
5538 036813ee 2019-05-09 stsp goto done;
5539 036813ee 2019-05-09 stsp
5540 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5541 de18fc63 2019-05-09 stsp if (err)
5542 de18fc63 2019-05-09 stsp goto done;
5543 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5544 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5545 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5546 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5547 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5548 33ad4cbe 2019-05-12 jcs free(logmsg);
5549 9d40349a 2019-05-09 stsp if (err)
5550 9d40349a 2019-05-09 stsp goto done;
5551 9d40349a 2019-05-09 stsp
5552 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5553 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5554 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5555 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5556 09f5bd90 2019-05-09 stsp goto done;
5557 09f5bd90 2019-05-09 stsp }
5558 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5559 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5560 09f5bd90 2019-05-09 stsp if (err)
5561 09f5bd90 2019-05-09 stsp goto done;
5562 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5563 ebf99748 2019-05-09 stsp if (err)
5564 ebf99748 2019-05-09 stsp goto done;
5565 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5566 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5567 09f5bd90 2019-05-09 stsp goto done;
5568 09f5bd90 2019-05-09 stsp }
5569 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5570 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5571 f2c16586 2019-05-09 stsp if (err)
5572 f2c16586 2019-05-09 stsp goto done;
5573 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5574 f2c16586 2019-05-09 stsp if (err)
5575 f2c16586 2019-05-09 stsp goto done;
5576 f2c16586 2019-05-09 stsp
5577 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5578 09f5bd90 2019-05-09 stsp if (err)
5579 09f5bd90 2019-05-09 stsp goto done;
5580 09f5bd90 2019-05-09 stsp
5581 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5582 ebf99748 2019-05-09 stsp if (err)
5583 ebf99748 2019-05-09 stsp goto done;
5584 c4296144 2019-05-09 stsp done:
5585 588edf97 2019-05-10 stsp if (head_tree)
5586 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5587 588edf97 2019-05-10 stsp if (head_commit)
5588 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5589 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5590 2f17228e 2019-05-12 stsp if (head_ref2) {
5591 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5592 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5593 2f17228e 2019-05-12 stsp err = unlockerr;
5594 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5595 39cd0ff6 2019-07-12 stsp }
5596 39cd0ff6 2019-07-12 stsp return err;
5597 39cd0ff6 2019-07-12 stsp }
5598 5c1e53bc 2019-07-28 stsp
5599 5c1e53bc 2019-07-28 stsp static const struct got_error *
5600 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5601 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5602 5c1e53bc 2019-07-28 stsp {
5603 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5604 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5605 39cd0ff6 2019-07-12 stsp
5606 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5607 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5608 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5609 5c1e53bc 2019-07-28 stsp
5610 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5611 5c1e53bc 2019-07-28 stsp ct_path++;
5612 5c1e53bc 2019-07-28 stsp
5613 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5614 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5615 5c1e53bc 2019-07-28 stsp break;
5616 5c1e53bc 2019-07-28 stsp }
5617 5c1e53bc 2019-07-28 stsp
5618 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5619 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5620 5c1e53bc 2019-07-28 stsp
5621 5c1e53bc 2019-07-28 stsp return NULL;
5622 5c1e53bc 2019-07-28 stsp }
5623 5c1e53bc 2019-07-28 stsp
5624 f0b75401 2019-08-03 stsp static const struct got_error *
5625 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5626 f0b75401 2019-08-03 stsp {
5627 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5628 f0b75401 2019-08-03 stsp
5629 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5630 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5631 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5632 f0b75401 2019-08-03 stsp }
5633 f0b75401 2019-08-03 stsp
5634 f0b75401 2019-08-03 stsp return NULL;
5635 f0b75401 2019-08-03 stsp }
5636 f0b75401 2019-08-03 stsp
5637 5f8a88c6 2019-08-03 stsp static const struct got_error *
5638 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5639 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5640 5f8a88c6 2019-08-03 stsp {
5641 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5642 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5643 5f8a88c6 2019-08-03 stsp
5644 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5645 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5646 5f8a88c6 2019-08-03 stsp continue;
5647 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5648 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5649 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5650 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5651 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5652 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5653 5f8a88c6 2019-08-03 stsp }
5654 5f8a88c6 2019-08-03 stsp
5655 5f8a88c6 2019-08-03 stsp return NULL;
5656 5f8a88c6 2019-08-03 stsp }
5657 5f8a88c6 2019-08-03 stsp
5658 39cd0ff6 2019-07-12 stsp const struct got_error *
5659 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5660 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5661 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5662 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5663 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5664 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5665 39cd0ff6 2019-07-12 stsp {
5666 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5667 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5668 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5669 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5670 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5671 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5672 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5673 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5674 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5675 39cd0ff6 2019-07-12 stsp
5676 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5677 39cd0ff6 2019-07-12 stsp
5678 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5679 39cd0ff6 2019-07-12 stsp
5680 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5681 39cd0ff6 2019-07-12 stsp if (err)
5682 39cd0ff6 2019-07-12 stsp goto done;
5683 39cd0ff6 2019-07-12 stsp
5684 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5685 39cd0ff6 2019-07-12 stsp if (err)
5686 39cd0ff6 2019-07-12 stsp goto done;
5687 39cd0ff6 2019-07-12 stsp
5688 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5689 39cd0ff6 2019-07-12 stsp if (err)
5690 39cd0ff6 2019-07-12 stsp goto done;
5691 39cd0ff6 2019-07-12 stsp
5692 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5693 39cd0ff6 2019-07-12 stsp if (err)
5694 39cd0ff6 2019-07-12 stsp goto done;
5695 39cd0ff6 2019-07-12 stsp
5696 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5697 5f8a88c6 2019-08-03 stsp &have_staged_files);
5698 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5699 5f8a88c6 2019-08-03 stsp goto done;
5700 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5701 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5702 5f8a88c6 2019-08-03 stsp if (err)
5703 5f8a88c6 2019-08-03 stsp goto done;
5704 5f8a88c6 2019-08-03 stsp }
5705 5f8a88c6 2019-08-03 stsp
5706 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5707 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5708 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5709 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5710 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5711 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5712 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5713 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5714 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5715 5c1e53bc 2019-07-28 stsp if (err)
5716 5c1e53bc 2019-07-28 stsp goto done;
5717 5c1e53bc 2019-07-28 stsp }
5718 39cd0ff6 2019-07-12 stsp
5719 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5720 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5721 39cd0ff6 2019-07-12 stsp goto done;
5722 5c1e53bc 2019-07-28 stsp }
5723 5c1e53bc 2019-07-28 stsp
5724 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5725 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5726 5c1e53bc 2019-07-28 stsp if (err)
5727 5c1e53bc 2019-07-28 stsp goto done;
5728 39cd0ff6 2019-07-12 stsp }
5729 f0b75401 2019-08-03 stsp
5730 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5731 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5732 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5733 f0b75401 2019-08-03 stsp
5734 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5735 f0b75401 2019-08-03 stsp ct_path++;
5736 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5737 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5738 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5739 b50cabdf 2019-07-12 stsp if (err)
5740 b50cabdf 2019-07-12 stsp goto done;
5741 f0b75401 2019-08-03 stsp
5742 b50cabdf 2019-07-12 stsp }
5743 b50cabdf 2019-07-12 stsp
5744 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5745 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5746 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5747 39cd0ff6 2019-07-12 stsp if (err)
5748 39cd0ff6 2019-07-12 stsp goto done;
5749 39cd0ff6 2019-07-12 stsp
5750 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5751 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5752 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5753 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5754 39cd0ff6 2019-07-12 stsp err = sync_err;
5755 39cd0ff6 2019-07-12 stsp done:
5756 39cd0ff6 2019-07-12 stsp if (fileindex)
5757 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5758 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5759 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5760 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5761 39cd0ff6 2019-07-12 stsp err = unlockerr;
5762 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5763 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5764 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5765 39cd0ff6 2019-07-12 stsp }
5766 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5767 c4296144 2019-05-09 stsp return err;
5768 8656d6c4 2019-05-20 stsp }
5769 8656d6c4 2019-05-20 stsp
5770 8656d6c4 2019-05-20 stsp const char *
5771 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5772 8656d6c4 2019-05-20 stsp {
5773 8656d6c4 2019-05-20 stsp return ct->path;
5774 8656d6c4 2019-05-20 stsp }
5775 8656d6c4 2019-05-20 stsp
5776 8656d6c4 2019-05-20 stsp unsigned int
5777 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5778 8656d6c4 2019-05-20 stsp {
5779 8656d6c4 2019-05-20 stsp return ct->status;
5780 818c7501 2019-07-11 stsp }
5781 818c7501 2019-07-11 stsp
5782 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5783 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5784 818c7501 2019-07-11 stsp struct got_repository *repo;
5785 818c7501 2019-07-11 stsp };
5786 818c7501 2019-07-11 stsp
5787 818c7501 2019-07-11 stsp static const struct got_error *
5788 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5789 818c7501 2019-07-11 stsp {
5790 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5791 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5792 818c7501 2019-07-11 stsp unsigned char status;
5793 818c7501 2019-07-11 stsp struct stat sb;
5794 818c7501 2019-07-11 stsp char *ondisk_path;
5795 818c7501 2019-07-11 stsp
5796 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5797 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5798 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5799 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5800 818c7501 2019-07-11 stsp
5801 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5802 818c7501 2019-07-11 stsp == -1)
5803 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5804 818c7501 2019-07-11 stsp
5805 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5806 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5807 818c7501 2019-07-11 stsp free(ondisk_path);
5808 818c7501 2019-07-11 stsp if (err)
5809 818c7501 2019-07-11 stsp return err;
5810 818c7501 2019-07-11 stsp
5811 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5812 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5813 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5814 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5815 818c7501 2019-07-11 stsp
5816 818c7501 2019-07-11 stsp return NULL;
5817 818c7501 2019-07-11 stsp }
5818 818c7501 2019-07-11 stsp
5819 818c7501 2019-07-11 stsp const struct got_error *
5820 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5821 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5822 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5823 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5824 818c7501 2019-07-11 stsp {
5825 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5826 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5827 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5828 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5829 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5830 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5831 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5832 818c7501 2019-07-11 stsp
5833 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5834 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5835 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5836 818c7501 2019-07-11 stsp
5837 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5838 818c7501 2019-07-11 stsp if (err)
5839 818c7501 2019-07-11 stsp return err;
5840 818c7501 2019-07-11 stsp
5841 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5842 818c7501 2019-07-11 stsp if (err)
5843 818c7501 2019-07-11 stsp goto done;
5844 818c7501 2019-07-11 stsp
5845 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5846 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5847 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5848 818c7501 2019-07-11 stsp &ok_arg);
5849 818c7501 2019-07-11 stsp if (err)
5850 818c7501 2019-07-11 stsp goto done;
5851 818c7501 2019-07-11 stsp
5852 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5853 818c7501 2019-07-11 stsp if (err)
5854 818c7501 2019-07-11 stsp goto done;
5855 818c7501 2019-07-11 stsp
5856 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5857 818c7501 2019-07-11 stsp if (err)
5858 818c7501 2019-07-11 stsp goto done;
5859 818c7501 2019-07-11 stsp
5860 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5861 818c7501 2019-07-11 stsp if (err)
5862 818c7501 2019-07-11 stsp goto done;
5863 818c7501 2019-07-11 stsp
5864 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5865 818c7501 2019-07-11 stsp 0);
5866 e51d7b55 2020-01-04 stsp if (err)
5867 e51d7b55 2020-01-04 stsp goto done;
5868 e51d7b55 2020-01-04 stsp
5869 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5870 818c7501 2019-07-11 stsp if (err)
5871 818c7501 2019-07-11 stsp goto done;
5872 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5873 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5874 e51d7b55 2020-01-04 stsp goto done;
5875 e51d7b55 2020-01-04 stsp }
5876 818c7501 2019-07-11 stsp
5877 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5878 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5879 818c7501 2019-07-11 stsp if (err)
5880 818c7501 2019-07-11 stsp goto done;
5881 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5882 818c7501 2019-07-11 stsp if (err)
5883 818c7501 2019-07-11 stsp goto done;
5884 818c7501 2019-07-11 stsp
5885 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5886 818c7501 2019-07-11 stsp
5887 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5888 818c7501 2019-07-11 stsp if (err)
5889 818c7501 2019-07-11 stsp goto done;
5890 818c7501 2019-07-11 stsp
5891 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5892 818c7501 2019-07-11 stsp if (err)
5893 818c7501 2019-07-11 stsp goto done;
5894 818c7501 2019-07-11 stsp
5895 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5896 818c7501 2019-07-11 stsp worktree->base_commit_id);
5897 818c7501 2019-07-11 stsp if (err)
5898 818c7501 2019-07-11 stsp goto done;
5899 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5900 818c7501 2019-07-11 stsp if (err)
5901 818c7501 2019-07-11 stsp goto done;
5902 818c7501 2019-07-11 stsp
5903 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5904 818c7501 2019-07-11 stsp if (err)
5905 818c7501 2019-07-11 stsp goto done;
5906 818c7501 2019-07-11 stsp done:
5907 818c7501 2019-07-11 stsp free(fileindex_path);
5908 818c7501 2019-07-11 stsp free(tmp_branch_name);
5909 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5910 818c7501 2019-07-11 stsp free(branch_ref_name);
5911 818c7501 2019-07-11 stsp if (branch_ref)
5912 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5913 818c7501 2019-07-11 stsp if (wt_branch)
5914 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5915 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5916 818c7501 2019-07-11 stsp if (err) {
5917 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5918 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5919 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5920 818c7501 2019-07-11 stsp }
5921 818c7501 2019-07-11 stsp if (*tmp_branch) {
5922 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5923 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5924 818c7501 2019-07-11 stsp }
5925 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5926 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5927 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5928 3e3a69f1 2019-07-25 stsp }
5929 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5930 818c7501 2019-07-11 stsp }
5931 818c7501 2019-07-11 stsp return err;
5932 818c7501 2019-07-11 stsp }
5933 818c7501 2019-07-11 stsp
5934 818c7501 2019-07-11 stsp const struct got_error *
5935 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5936 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5937 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5938 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5939 818c7501 2019-07-11 stsp {
5940 818c7501 2019-07-11 stsp const struct got_error *err;
5941 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5942 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5943 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5944 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5945 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5946 818c7501 2019-07-11 stsp
5947 818c7501 2019-07-11 stsp *commit_id = NULL;
5948 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5949 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5950 3e3a69f1 2019-07-25 stsp *branch = NULL;
5951 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5952 3e3a69f1 2019-07-25 stsp
5953 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5954 3e3a69f1 2019-07-25 stsp if (err)
5955 3e3a69f1 2019-07-25 stsp return err;
5956 818c7501 2019-07-11 stsp
5957 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5958 3e3a69f1 2019-07-25 stsp if (err)
5959 f032f1f7 2019-08-04 stsp goto done;
5960 f032f1f7 2019-08-04 stsp
5961 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5962 f032f1f7 2019-08-04 stsp &have_staged_files);
5963 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5964 f032f1f7 2019-08-04 stsp goto done;
5965 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5966 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5967 3e3a69f1 2019-07-25 stsp goto done;
5968 f032f1f7 2019-08-04 stsp }
5969 3e3a69f1 2019-07-25 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 f032f1f7 2019-08-04 stsp goto done;
5973 818c7501 2019-07-11 stsp
5974 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&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_commit_ref_name(&commit_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 = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5983 818c7501 2019-07-11 stsp if (err)
5984 818c7501 2019-07-11 stsp goto done;
5985 818c7501 2019-07-11 stsp
5986 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5987 818c7501 2019-07-11 stsp if (err)
5988 818c7501 2019-07-11 stsp goto done;
5989 818c7501 2019-07-11 stsp
5990 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5991 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5992 818c7501 2019-07-11 stsp if (err)
5993 818c7501 2019-07-11 stsp goto done;
5994 818c7501 2019-07-11 stsp
5995 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5996 818c7501 2019-07-11 stsp if (err)
5997 818c7501 2019-07-11 stsp goto done;
5998 818c7501 2019-07-11 stsp
5999 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
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 err = got_ref_open(new_base_branch, repo,
6004 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6005 818c7501 2019-07-11 stsp if (err)
6006 818c7501 2019-07-11 stsp goto done;
6007 818c7501 2019-07-11 stsp
6008 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6009 818c7501 2019-07-11 stsp if (err)
6010 818c7501 2019-07-11 stsp goto done;
6011 818c7501 2019-07-11 stsp done:
6012 818c7501 2019-07-11 stsp free(commit_ref_name);
6013 818c7501 2019-07-11 stsp free(branch_ref_name);
6014 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6015 818c7501 2019-07-11 stsp if (commit_ref)
6016 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6017 818c7501 2019-07-11 stsp if (branch_ref)
6018 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6019 818c7501 2019-07-11 stsp if (err) {
6020 818c7501 2019-07-11 stsp free(*commit_id);
6021 818c7501 2019-07-11 stsp *commit_id = NULL;
6022 818c7501 2019-07-11 stsp if (*tmp_branch) {
6023 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6024 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6025 818c7501 2019-07-11 stsp }
6026 818c7501 2019-07-11 stsp if (*new_base_branch) {
6027 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6028 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6029 818c7501 2019-07-11 stsp }
6030 818c7501 2019-07-11 stsp if (*branch) {
6031 818c7501 2019-07-11 stsp got_ref_close(*branch);
6032 818c7501 2019-07-11 stsp *branch = NULL;
6033 818c7501 2019-07-11 stsp }
6034 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6035 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6036 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6037 3e3a69f1 2019-07-25 stsp }
6038 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6039 818c7501 2019-07-11 stsp }
6040 818c7501 2019-07-11 stsp return err;
6041 c4296144 2019-05-09 stsp }
6042 818c7501 2019-07-11 stsp
6043 818c7501 2019-07-11 stsp const struct got_error *
6044 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6045 818c7501 2019-07-11 stsp {
6046 818c7501 2019-07-11 stsp const struct got_error *err;
6047 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6048 818c7501 2019-07-11 stsp
6049 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6050 818c7501 2019-07-11 stsp if (err)
6051 818c7501 2019-07-11 stsp return err;
6052 818c7501 2019-07-11 stsp
6053 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6054 818c7501 2019-07-11 stsp free(tmp_branch_name);
6055 818c7501 2019-07-11 stsp return NULL;
6056 818c7501 2019-07-11 stsp }
6057 818c7501 2019-07-11 stsp
6058 818c7501 2019-07-11 stsp static const struct got_error *
6059 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6060 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6061 818c7501 2019-07-11 stsp {
6062 0ebf8283 2019-07-24 stsp *logmsg = arg;
6063 818c7501 2019-07-11 stsp return NULL;
6064 818c7501 2019-07-11 stsp }
6065 818c7501 2019-07-11 stsp
6066 818c7501 2019-07-11 stsp static const struct got_error *
6067 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6068 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6069 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6070 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6071 818c7501 2019-07-11 stsp {
6072 818c7501 2019-07-11 stsp return NULL;
6073 01757395 2019-07-12 stsp }
6074 01757395 2019-07-12 stsp
6075 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6076 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6077 01757395 2019-07-12 stsp void *progress_arg;
6078 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6079 01757395 2019-07-12 stsp };
6080 01757395 2019-07-12 stsp
6081 01757395 2019-07-12 stsp static const struct got_error *
6082 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6083 01757395 2019-07-12 stsp {
6084 01757395 2019-07-12 stsp const struct got_error *err;
6085 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6086 01757395 2019-07-12 stsp char *p;
6087 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6088 01757395 2019-07-12 stsp
6089 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6090 01757395 2019-07-12 stsp if (err)
6091 01757395 2019-07-12 stsp return err;
6092 01757395 2019-07-12 stsp
6093 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6094 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6095 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6096 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6097 01757395 2019-07-12 stsp return NULL;
6098 01757395 2019-07-12 stsp
6099 01757395 2019-07-12 stsp p = strdup(path);
6100 01757395 2019-07-12 stsp if (p == NULL)
6101 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6102 01757395 2019-07-12 stsp
6103 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6104 01757395 2019-07-12 stsp if (err || new == NULL)
6105 01757395 2019-07-12 stsp free(p);
6106 01757395 2019-07-12 stsp return err;
6107 01757395 2019-07-12 stsp }
6108 01757395 2019-07-12 stsp
6109 01757395 2019-07-12 stsp void
6110 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6111 01757395 2019-07-12 stsp {
6112 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6113 01757395 2019-07-12 stsp
6114 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6115 01757395 2019-07-12 stsp free((char *)pe->path);
6116 01757395 2019-07-12 stsp
6117 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6118 818c7501 2019-07-11 stsp }
6119 818c7501 2019-07-11 stsp
6120 0ebf8283 2019-07-24 stsp static const struct got_error *
6121 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6122 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6123 818c7501 2019-07-11 stsp {
6124 818c7501 2019-07-11 stsp const struct got_error *err;
6125 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6126 818c7501 2019-07-11 stsp
6127 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6128 818c7501 2019-07-11 stsp if (err) {
6129 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6130 818c7501 2019-07-11 stsp goto done;
6131 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6132 818c7501 2019-07-11 stsp if (err)
6133 818c7501 2019-07-11 stsp goto done;
6134 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6135 818c7501 2019-07-11 stsp if (err)
6136 818c7501 2019-07-11 stsp goto done;
6137 de05890f 2020-03-05 stsp } else if (is_rebase) {
6138 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6139 818c7501 2019-07-11 stsp int cmp;
6140 818c7501 2019-07-11 stsp
6141 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6142 818c7501 2019-07-11 stsp if (err)
6143 818c7501 2019-07-11 stsp goto done;
6144 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6145 818c7501 2019-07-11 stsp free(stored_id);
6146 818c7501 2019-07-11 stsp if (cmp != 0) {
6147 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6148 818c7501 2019-07-11 stsp goto done;
6149 818c7501 2019-07-11 stsp }
6150 818c7501 2019-07-11 stsp }
6151 0ebf8283 2019-07-24 stsp done:
6152 0ebf8283 2019-07-24 stsp if (commit_ref)
6153 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6154 0ebf8283 2019-07-24 stsp return err;
6155 0ebf8283 2019-07-24 stsp }
6156 0ebf8283 2019-07-24 stsp
6157 0ebf8283 2019-07-24 stsp static const struct got_error *
6158 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6159 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6160 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6161 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6162 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6163 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6164 0ebf8283 2019-07-24 stsp {
6165 0ebf8283 2019-07-24 stsp const struct got_error *err;
6166 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6167 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6168 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6169 818c7501 2019-07-11 stsp
6170 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6171 0ebf8283 2019-07-24 stsp
6172 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6173 0ebf8283 2019-07-24 stsp if (err)
6174 0ebf8283 2019-07-24 stsp return err;
6175 0ebf8283 2019-07-24 stsp
6176 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6177 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6178 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6179 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6180 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6181 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6182 818c7501 2019-07-11 stsp if (commit_ref)
6183 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6184 818c7501 2019-07-11 stsp return err;
6185 818c7501 2019-07-11 stsp }
6186 818c7501 2019-07-11 stsp
6187 818c7501 2019-07-11 stsp const struct got_error *
6188 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6189 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6190 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6191 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6192 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6193 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6194 818c7501 2019-07-11 stsp {
6195 0ebf8283 2019-07-24 stsp const struct got_error *err;
6196 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6197 0ebf8283 2019-07-24 stsp
6198 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6199 0ebf8283 2019-07-24 stsp if (err)
6200 0ebf8283 2019-07-24 stsp return err;
6201 0ebf8283 2019-07-24 stsp
6202 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6203 0ebf8283 2019-07-24 stsp if (err)
6204 0ebf8283 2019-07-24 stsp goto done;
6205 0ebf8283 2019-07-24 stsp
6206 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6207 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6208 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6209 0ebf8283 2019-07-24 stsp done:
6210 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6211 0ebf8283 2019-07-24 stsp return err;
6212 0ebf8283 2019-07-24 stsp }
6213 0ebf8283 2019-07-24 stsp
6214 0ebf8283 2019-07-24 stsp const struct got_error *
6215 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6216 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6217 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6218 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6219 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6220 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6221 0ebf8283 2019-07-24 stsp {
6222 0ebf8283 2019-07-24 stsp const struct got_error *err;
6223 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6224 0ebf8283 2019-07-24 stsp
6225 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6226 0ebf8283 2019-07-24 stsp if (err)
6227 0ebf8283 2019-07-24 stsp return err;
6228 0ebf8283 2019-07-24 stsp
6229 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6230 0ebf8283 2019-07-24 stsp if (err)
6231 0ebf8283 2019-07-24 stsp goto done;
6232 0ebf8283 2019-07-24 stsp
6233 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6234 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6235 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6236 0ebf8283 2019-07-24 stsp done:
6237 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6238 0ebf8283 2019-07-24 stsp return err;
6239 0ebf8283 2019-07-24 stsp }
6240 0ebf8283 2019-07-24 stsp
6241 0ebf8283 2019-07-24 stsp static const struct got_error *
6242 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6243 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6244 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6245 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6246 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6247 0ebf8283 2019-07-24 stsp {
6248 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6249 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6250 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6251 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6252 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6253 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6254 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6255 818c7501 2019-07-11 stsp
6256 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6257 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6258 a0e95631 2019-07-12 stsp
6259 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6260 818c7501 2019-07-11 stsp
6261 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6262 a0e95631 2019-07-12 stsp if (err)
6263 3e3a69f1 2019-07-25 stsp return err;
6264 a0e95631 2019-07-12 stsp
6265 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6266 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6267 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6268 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6269 01757395 2019-07-12 stsp /*
6270 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6271 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6272 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6273 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6274 01757395 2019-07-12 stsp */
6275 01757395 2019-07-12 stsp if (merged_paths) {
6276 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6277 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6278 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6279 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6280 f2a9dc41 2019-12-13 tracey 0);
6281 01757395 2019-07-12 stsp if (err)
6282 01757395 2019-07-12 stsp goto done;
6283 01757395 2019-07-12 stsp }
6284 01757395 2019-07-12 stsp } else {
6285 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6286 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6287 01757395 2019-07-12 stsp if (err)
6288 01757395 2019-07-12 stsp goto done;
6289 01757395 2019-07-12 stsp }
6290 a0e95631 2019-07-12 stsp
6291 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6292 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6293 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6294 a0e95631 2019-07-12 stsp if (err)
6295 a0e95631 2019-07-12 stsp goto done;
6296 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6297 a0e95631 2019-07-12 stsp goto done;
6298 ff0d2220 2019-07-11 stsp }
6299 818c7501 2019-07-11 stsp
6300 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6301 edd02c5e 2019-07-11 stsp if (err)
6302 edd02c5e 2019-07-11 stsp goto done;
6303 edd02c5e 2019-07-11 stsp
6304 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6305 818c7501 2019-07-11 stsp if (err)
6306 818c7501 2019-07-11 stsp goto done;
6307 818c7501 2019-07-11 stsp
6308 5943eee2 2019-08-13 stsp if (new_logmsg) {
6309 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6310 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6311 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6312 5943eee2 2019-08-13 stsp goto done;
6313 5943eee2 2019-08-13 stsp }
6314 5943eee2 2019-08-13 stsp } else {
6315 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6316 5943eee2 2019-08-13 stsp if (err)
6317 5943eee2 2019-08-13 stsp goto done;
6318 5943eee2 2019-08-13 stsp }
6319 0ebf8283 2019-07-24 stsp
6320 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6321 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6322 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6323 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6324 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6325 a0e95631 2019-07-12 stsp if (err)
6326 a0e95631 2019-07-12 stsp goto done;
6327 a0e95631 2019-07-12 stsp
6328 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6329 a0e95631 2019-07-12 stsp if (err)
6330 a0e95631 2019-07-12 stsp goto done;
6331 a0e95631 2019-07-12 stsp
6332 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6333 a0e95631 2019-07-12 stsp if (err)
6334 a0e95631 2019-07-12 stsp goto done;
6335 a0e95631 2019-07-12 stsp
6336 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6337 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6338 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6339 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6340 a0e95631 2019-07-12 stsp err = sync_err;
6341 818c7501 2019-07-11 stsp done:
6342 a0e95631 2019-07-12 stsp free(fileindex_path);
6343 a0e95631 2019-07-12 stsp free(head_commit_id);
6344 a0e95631 2019-07-12 stsp if (head_ref)
6345 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6346 818c7501 2019-07-11 stsp if (err) {
6347 818c7501 2019-07-11 stsp free(*new_commit_id);
6348 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6349 818c7501 2019-07-11 stsp }
6350 818c7501 2019-07-11 stsp return err;
6351 818c7501 2019-07-11 stsp }
6352 818c7501 2019-07-11 stsp
6353 818c7501 2019-07-11 stsp const struct got_error *
6354 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6355 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6356 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6357 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6358 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6359 0ebf8283 2019-07-24 stsp {
6360 0ebf8283 2019-07-24 stsp const struct got_error *err;
6361 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6362 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6363 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6364 0ebf8283 2019-07-24 stsp
6365 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6366 0ebf8283 2019-07-24 stsp if (err)
6367 0ebf8283 2019-07-24 stsp return err;
6368 0ebf8283 2019-07-24 stsp
6369 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6370 0ebf8283 2019-07-24 stsp if (err)
6371 0ebf8283 2019-07-24 stsp goto done;
6372 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6373 0ebf8283 2019-07-24 stsp if (err)
6374 0ebf8283 2019-07-24 stsp goto done;
6375 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6376 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6377 0ebf8283 2019-07-24 stsp goto done;
6378 0ebf8283 2019-07-24 stsp }
6379 0ebf8283 2019-07-24 stsp
6380 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6381 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6382 0ebf8283 2019-07-24 stsp done:
6383 0ebf8283 2019-07-24 stsp if (commit_ref)
6384 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6385 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6386 0ebf8283 2019-07-24 stsp free(commit_id);
6387 0ebf8283 2019-07-24 stsp return err;
6388 0ebf8283 2019-07-24 stsp }
6389 0ebf8283 2019-07-24 stsp
6390 0ebf8283 2019-07-24 stsp const struct got_error *
6391 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6392 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6393 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6394 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6395 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6396 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6397 0ebf8283 2019-07-24 stsp {
6398 0ebf8283 2019-07-24 stsp const struct got_error *err;
6399 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6400 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6401 0ebf8283 2019-07-24 stsp
6402 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6403 0ebf8283 2019-07-24 stsp if (err)
6404 0ebf8283 2019-07-24 stsp return err;
6405 0ebf8283 2019-07-24 stsp
6406 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6407 0ebf8283 2019-07-24 stsp if (err)
6408 0ebf8283 2019-07-24 stsp goto done;
6409 0ebf8283 2019-07-24 stsp
6410 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6411 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6412 0ebf8283 2019-07-24 stsp done:
6413 0ebf8283 2019-07-24 stsp if (commit_ref)
6414 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6415 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6416 0ebf8283 2019-07-24 stsp return err;
6417 0ebf8283 2019-07-24 stsp }
6418 0ebf8283 2019-07-24 stsp
6419 0ebf8283 2019-07-24 stsp const struct got_error *
6420 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6421 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6422 818c7501 2019-07-11 stsp {
6423 3e3a69f1 2019-07-25 stsp if (fileindex)
6424 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6425 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6426 69844fba 2019-07-11 stsp }
6427 69844fba 2019-07-11 stsp
6428 69844fba 2019-07-11 stsp static const struct got_error *
6429 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6430 69844fba 2019-07-11 stsp {
6431 69844fba 2019-07-11 stsp const struct got_error *err;
6432 69844fba 2019-07-11 stsp struct got_reference *ref;
6433 69844fba 2019-07-11 stsp
6434 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6435 69844fba 2019-07-11 stsp if (err) {
6436 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6437 69844fba 2019-07-11 stsp return NULL;
6438 69844fba 2019-07-11 stsp return err;
6439 69844fba 2019-07-11 stsp }
6440 69844fba 2019-07-11 stsp
6441 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6442 69844fba 2019-07-11 stsp got_ref_close(ref);
6443 69844fba 2019-07-11 stsp return err;
6444 818c7501 2019-07-11 stsp }
6445 818c7501 2019-07-11 stsp
6446 69844fba 2019-07-11 stsp static const struct got_error *
6447 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6448 69844fba 2019-07-11 stsp {
6449 69844fba 2019-07-11 stsp const struct got_error *err;
6450 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6451 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6452 69844fba 2019-07-11 stsp
6453 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6454 69844fba 2019-07-11 stsp if (err)
6455 69844fba 2019-07-11 stsp goto done;
6456 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6457 69844fba 2019-07-11 stsp if (err)
6458 69844fba 2019-07-11 stsp goto done;
6459 69844fba 2019-07-11 stsp
6460 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6461 69844fba 2019-07-11 stsp if (err)
6462 69844fba 2019-07-11 stsp goto done;
6463 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6464 69844fba 2019-07-11 stsp if (err)
6465 69844fba 2019-07-11 stsp goto done;
6466 69844fba 2019-07-11 stsp
6467 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6468 69844fba 2019-07-11 stsp if (err)
6469 69844fba 2019-07-11 stsp goto done;
6470 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6471 69844fba 2019-07-11 stsp if (err)
6472 69844fba 2019-07-11 stsp goto done;
6473 69844fba 2019-07-11 stsp
6474 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6475 69844fba 2019-07-11 stsp if (err)
6476 69844fba 2019-07-11 stsp goto done;
6477 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6478 69844fba 2019-07-11 stsp if (err)
6479 69844fba 2019-07-11 stsp goto done;
6480 69844fba 2019-07-11 stsp
6481 69844fba 2019-07-11 stsp done:
6482 69844fba 2019-07-11 stsp free(tmp_branch_name);
6483 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6484 69844fba 2019-07-11 stsp free(branch_ref_name);
6485 69844fba 2019-07-11 stsp free(commit_ref_name);
6486 e600f124 2021-03-21 stsp return err;
6487 e600f124 2021-03-21 stsp }
6488 e600f124 2021-03-21 stsp
6489 e600f124 2021-03-21 stsp const struct got_error *
6490 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6491 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6492 e600f124 2021-03-21 stsp {
6493 e600f124 2021-03-21 stsp const struct got_error *err;
6494 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6495 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6496 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6497 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6498 e600f124 2021-03-21 stsp char *refname = NULL;
6499 e600f124 2021-03-21 stsp
6500 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6501 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6502 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6503 e600f124 2021-03-21 stsp branch_name += 11;
6504 e600f124 2021-03-21 stsp
6505 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6506 e600f124 2021-03-21 stsp if (err)
6507 e600f124 2021-03-21 stsp return err;
6508 e600f124 2021-03-21 stsp
6509 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6510 e600f124 2021-03-21 stsp new_id_str) == -1) {
6511 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6512 e600f124 2021-03-21 stsp goto done;
6513 e600f124 2021-03-21 stsp }
6514 e600f124 2021-03-21 stsp
6515 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6516 e600f124 2021-03-21 stsp if (err)
6517 e600f124 2021-03-21 stsp goto done;
6518 e600f124 2021-03-21 stsp
6519 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6520 e600f124 2021-03-21 stsp if (err)
6521 e600f124 2021-03-21 stsp goto done;
6522 e600f124 2021-03-21 stsp
6523 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6524 e600f124 2021-03-21 stsp done:
6525 e600f124 2021-03-21 stsp free(new_id_str);
6526 e600f124 2021-03-21 stsp free(refname);
6527 e600f124 2021-03-21 stsp free(old_commit_id);
6528 e600f124 2021-03-21 stsp if (ref)
6529 e600f124 2021-03-21 stsp got_ref_close(ref);
6530 69844fba 2019-07-11 stsp return err;
6531 69844fba 2019-07-11 stsp }
6532 69844fba 2019-07-11 stsp
6533 818c7501 2019-07-11 stsp const struct got_error *
6534 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6535 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6536 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6537 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6538 818c7501 2019-07-11 stsp {
6539 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6540 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6541 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6542 818c7501 2019-07-11 stsp
6543 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6544 818c7501 2019-07-11 stsp if (err)
6545 818c7501 2019-07-11 stsp return err;
6546 e600f124 2021-03-21 stsp
6547 e600f124 2021-03-21 stsp if (create_backup) {
6548 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6549 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6550 e600f124 2021-03-21 stsp if (err)
6551 e600f124 2021-03-21 stsp goto done;
6552 e600f124 2021-03-21 stsp }
6553 818c7501 2019-07-11 stsp
6554 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6555 818c7501 2019-07-11 stsp if (err)
6556 818c7501 2019-07-11 stsp goto done;
6557 818c7501 2019-07-11 stsp
6558 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6559 818c7501 2019-07-11 stsp if (err)
6560 818c7501 2019-07-11 stsp goto done;
6561 818c7501 2019-07-11 stsp
6562 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6563 818c7501 2019-07-11 stsp if (err)
6564 818c7501 2019-07-11 stsp goto done;
6565 818c7501 2019-07-11 stsp
6566 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6567 a615e0e7 2020-12-16 stsp if (err)
6568 a615e0e7 2020-12-16 stsp goto done;
6569 a615e0e7 2020-12-16 stsp
6570 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6571 a615e0e7 2020-12-16 stsp if (err)
6572 a615e0e7 2020-12-16 stsp goto done;
6573 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6574 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6575 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6576 a615e0e7 2020-12-16 stsp err = sync_err;
6577 818c7501 2019-07-11 stsp done:
6578 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6579 a615e0e7 2020-12-16 stsp free(fileindex_path);
6580 818c7501 2019-07-11 stsp free(new_head_commit_id);
6581 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6582 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6583 818c7501 2019-07-11 stsp err = unlockerr;
6584 818c7501 2019-07-11 stsp return err;
6585 818c7501 2019-07-11 stsp }
6586 818c7501 2019-07-11 stsp
6587 818c7501 2019-07-11 stsp const struct got_error *
6588 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6589 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6590 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6591 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6592 818c7501 2019-07-11 stsp {
6593 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6594 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6595 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6596 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6597 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6598 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6599 818c7501 2019-07-11 stsp
6600 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6601 818c7501 2019-07-11 stsp if (err)
6602 818c7501 2019-07-11 stsp return err;
6603 818c7501 2019-07-11 stsp
6604 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6605 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6606 818c7501 2019-07-11 stsp if (err)
6607 818c7501 2019-07-11 stsp goto done;
6608 818c7501 2019-07-11 stsp
6609 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6610 818c7501 2019-07-11 stsp if (err)
6611 818c7501 2019-07-11 stsp goto done;
6612 818c7501 2019-07-11 stsp
6613 818c7501 2019-07-11 stsp /*
6614 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6615 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6616 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6617 818c7501 2019-07-11 stsp */
6618 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6619 818c7501 2019-07-11 stsp if (err)
6620 818c7501 2019-07-11 stsp goto done;
6621 818c7501 2019-07-11 stsp
6622 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6623 818c7501 2019-07-11 stsp if (err)
6624 818c7501 2019-07-11 stsp goto done;
6625 818c7501 2019-07-11 stsp
6626 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6627 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6628 ca355955 2019-07-12 stsp if (err)
6629 ca355955 2019-07-12 stsp goto done;
6630 ca355955 2019-07-12 stsp
6631 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6632 ca355955 2019-07-12 stsp if (err)
6633 ca355955 2019-07-12 stsp goto done;
6634 ca355955 2019-07-12 stsp
6635 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6636 818c7501 2019-07-11 stsp if (err)
6637 818c7501 2019-07-11 stsp goto done;
6638 818c7501 2019-07-11 stsp
6639 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6640 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6641 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6642 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6643 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6644 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6645 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6646 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6647 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6648 55bd499d 2019-07-12 stsp if (err)
6649 1f1abb7e 2019-08-08 stsp goto sync;
6650 55bd499d 2019-07-12 stsp
6651 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6652 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6653 ca355955 2019-07-12 stsp sync:
6654 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6655 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6656 ca355955 2019-07-12 stsp err = sync_err;
6657 818c7501 2019-07-11 stsp done:
6658 818c7501 2019-07-11 stsp got_ref_close(resolved);
6659 a3a2faf2 2019-07-12 stsp free(tree_id);
6660 818c7501 2019-07-11 stsp free(commit_id);
6661 0ebf8283 2019-07-24 stsp if (fileindex)
6662 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6663 0ebf8283 2019-07-24 stsp free(fileindex_path);
6664 0ebf8283 2019-07-24 stsp
6665 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6666 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6667 0ebf8283 2019-07-24 stsp err = unlockerr;
6668 0ebf8283 2019-07-24 stsp return err;
6669 0ebf8283 2019-07-24 stsp }
6670 0ebf8283 2019-07-24 stsp
6671 0ebf8283 2019-07-24 stsp const struct got_error *
6672 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6673 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6674 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6675 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6676 0ebf8283 2019-07-24 stsp {
6677 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6678 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6679 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6680 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6681 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6682 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6683 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6684 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6685 0ebf8283 2019-07-24 stsp
6686 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6687 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6688 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6689 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6690 0ebf8283 2019-07-24 stsp
6691 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6692 0ebf8283 2019-07-24 stsp if (err)
6693 0ebf8283 2019-07-24 stsp return err;
6694 0ebf8283 2019-07-24 stsp
6695 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6696 0ebf8283 2019-07-24 stsp if (err)
6697 0ebf8283 2019-07-24 stsp goto done;
6698 0ebf8283 2019-07-24 stsp
6699 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6700 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6701 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6702 0ebf8283 2019-07-24 stsp &ok_arg);
6703 0ebf8283 2019-07-24 stsp if (err)
6704 0ebf8283 2019-07-24 stsp goto done;
6705 0ebf8283 2019-07-24 stsp
6706 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6707 0ebf8283 2019-07-24 stsp if (err)
6708 0ebf8283 2019-07-24 stsp goto done;
6709 0ebf8283 2019-07-24 stsp
6710 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6711 0ebf8283 2019-07-24 stsp if (err)
6712 0ebf8283 2019-07-24 stsp goto done;
6713 0ebf8283 2019-07-24 stsp
6714 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6715 0ebf8283 2019-07-24 stsp worktree);
6716 0ebf8283 2019-07-24 stsp if (err)
6717 0ebf8283 2019-07-24 stsp goto done;
6718 0ebf8283 2019-07-24 stsp
6719 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6720 0ebf8283 2019-07-24 stsp 0);
6721 0ebf8283 2019-07-24 stsp if (err)
6722 0ebf8283 2019-07-24 stsp goto done;
6723 0ebf8283 2019-07-24 stsp
6724 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6725 0ebf8283 2019-07-24 stsp if (err)
6726 0ebf8283 2019-07-24 stsp goto done;
6727 0ebf8283 2019-07-24 stsp
6728 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6729 0ebf8283 2019-07-24 stsp if (err)
6730 0ebf8283 2019-07-24 stsp goto done;
6731 0ebf8283 2019-07-24 stsp
6732 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6733 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6734 0ebf8283 2019-07-24 stsp if (err)
6735 0ebf8283 2019-07-24 stsp goto done;
6736 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6737 0ebf8283 2019-07-24 stsp if (err)
6738 0ebf8283 2019-07-24 stsp goto done;
6739 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6740 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6741 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6742 0ebf8283 2019-07-24 stsp goto done;
6743 0ebf8283 2019-07-24 stsp }
6744 0ebf8283 2019-07-24 stsp
6745 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6746 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6747 0ebf8283 2019-07-24 stsp if (err)
6748 0ebf8283 2019-07-24 stsp goto done;
6749 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6750 0ebf8283 2019-07-24 stsp if (err)
6751 0ebf8283 2019-07-24 stsp goto done;
6752 0ebf8283 2019-07-24 stsp
6753 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6754 0ebf8283 2019-07-24 stsp if (err)
6755 0ebf8283 2019-07-24 stsp goto done;
6756 0ebf8283 2019-07-24 stsp done:
6757 0ebf8283 2019-07-24 stsp free(fileindex_path);
6758 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6759 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6760 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6761 0ebf8283 2019-07-24 stsp if (wt_branch)
6762 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6763 0ebf8283 2019-07-24 stsp if (err) {
6764 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6765 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6766 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6767 0ebf8283 2019-07-24 stsp }
6768 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6769 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6770 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6771 0ebf8283 2019-07-24 stsp }
6772 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6773 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6774 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6775 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6776 3e3a69f1 2019-07-25 stsp }
6777 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6778 0ebf8283 2019-07-24 stsp }
6779 0ebf8283 2019-07-24 stsp return err;
6780 0ebf8283 2019-07-24 stsp }
6781 0ebf8283 2019-07-24 stsp
6782 0ebf8283 2019-07-24 stsp const struct got_error *
6783 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6784 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6785 0ebf8283 2019-07-24 stsp {
6786 3e3a69f1 2019-07-25 stsp if (fileindex)
6787 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6788 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6789 0ebf8283 2019-07-24 stsp }
6790 0ebf8283 2019-07-24 stsp
6791 0ebf8283 2019-07-24 stsp const struct got_error *
6792 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6793 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6794 0ebf8283 2019-07-24 stsp {
6795 0ebf8283 2019-07-24 stsp const struct got_error *err;
6796 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6797 0ebf8283 2019-07-24 stsp
6798 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6799 0ebf8283 2019-07-24 stsp if (err)
6800 0ebf8283 2019-07-24 stsp return err;
6801 0ebf8283 2019-07-24 stsp
6802 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6803 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6804 0ebf8283 2019-07-24 stsp return NULL;
6805 0ebf8283 2019-07-24 stsp }
6806 0ebf8283 2019-07-24 stsp
6807 0ebf8283 2019-07-24 stsp const struct got_error *
6808 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6809 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6810 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6811 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6812 0ebf8283 2019-07-24 stsp {
6813 0ebf8283 2019-07-24 stsp const struct got_error *err;
6814 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6815 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6816 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6817 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6818 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6819 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6820 0ebf8283 2019-07-24 stsp
6821 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6822 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6823 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6824 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6825 0ebf8283 2019-07-24 stsp
6826 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6827 3e3a69f1 2019-07-25 stsp if (err)
6828 3e3a69f1 2019-07-25 stsp return err;
6829 3e3a69f1 2019-07-25 stsp
6830 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6831 3e3a69f1 2019-07-25 stsp if (err)
6832 f032f1f7 2019-08-04 stsp goto done;
6833 f032f1f7 2019-08-04 stsp
6834 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6835 f032f1f7 2019-08-04 stsp &have_staged_files);
6836 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6837 f032f1f7 2019-08-04 stsp goto done;
6838 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6839 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6840 3e3a69f1 2019-07-25 stsp goto done;
6841 f032f1f7 2019-08-04 stsp }
6842 3e3a69f1 2019-07-25 stsp
6843 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6844 0ebf8283 2019-07-24 stsp if (err)
6845 f032f1f7 2019-08-04 stsp goto done;
6846 0ebf8283 2019-07-24 stsp
6847 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
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 = get_histedit_commit_ref_name(&commit_ref_name, worktree);
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 = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6856 0ebf8283 2019-07-24 stsp worktree);
6857 0ebf8283 2019-07-24 stsp if (err)
6858 0ebf8283 2019-07-24 stsp goto done;
6859 0ebf8283 2019-07-24 stsp
6860 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6861 0ebf8283 2019-07-24 stsp if (err)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6865 0ebf8283 2019-07-24 stsp if (err)
6866 0ebf8283 2019-07-24 stsp goto done;
6867 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6868 0ebf8283 2019-07-24 stsp if (err)
6869 0ebf8283 2019-07-24 stsp goto done;
6870 0ebf8283 2019-07-24 stsp
6871 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6872 0ebf8283 2019-07-24 stsp if (err)
6873 0ebf8283 2019-07-24 stsp goto done;
6874 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6875 0ebf8283 2019-07-24 stsp if (err)
6876 0ebf8283 2019-07-24 stsp goto done;
6877 0ebf8283 2019-07-24 stsp
6878 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6879 0ebf8283 2019-07-24 stsp if (err)
6880 0ebf8283 2019-07-24 stsp goto done;
6881 0ebf8283 2019-07-24 stsp done:
6882 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6883 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6884 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6885 0ebf8283 2019-07-24 stsp if (commit_ref)
6886 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6887 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6888 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6889 0ebf8283 2019-07-24 stsp if (err) {
6890 0ebf8283 2019-07-24 stsp free(*commit_id);
6891 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6892 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6893 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6894 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6895 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6896 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6897 0ebf8283 2019-07-24 stsp }
6898 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6899 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6900 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6901 3e3a69f1 2019-07-25 stsp }
6902 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6903 0ebf8283 2019-07-24 stsp }
6904 0ebf8283 2019-07-24 stsp return err;
6905 0ebf8283 2019-07-24 stsp }
6906 0ebf8283 2019-07-24 stsp
6907 0ebf8283 2019-07-24 stsp static const struct got_error *
6908 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6909 0ebf8283 2019-07-24 stsp {
6910 0ebf8283 2019-07-24 stsp const struct got_error *err;
6911 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6912 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6913 0ebf8283 2019-07-24 stsp
6914 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6915 0ebf8283 2019-07-24 stsp if (err)
6916 0ebf8283 2019-07-24 stsp goto done;
6917 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6918 0ebf8283 2019-07-24 stsp if (err)
6919 0ebf8283 2019-07-24 stsp goto done;
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6922 0ebf8283 2019-07-24 stsp worktree);
6923 0ebf8283 2019-07-24 stsp if (err)
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6926 0ebf8283 2019-07-24 stsp if (err)
6927 0ebf8283 2019-07-24 stsp goto done;
6928 0ebf8283 2019-07-24 stsp
6929 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6933 0ebf8283 2019-07-24 stsp if (err)
6934 0ebf8283 2019-07-24 stsp goto done;
6935 0ebf8283 2019-07-24 stsp
6936 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6937 0ebf8283 2019-07-24 stsp if (err)
6938 0ebf8283 2019-07-24 stsp goto done;
6939 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6940 0ebf8283 2019-07-24 stsp if (err)
6941 0ebf8283 2019-07-24 stsp goto done;
6942 0ebf8283 2019-07-24 stsp done:
6943 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6944 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6945 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6946 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6947 0ebf8283 2019-07-24 stsp return err;
6948 0ebf8283 2019-07-24 stsp }
6949 0ebf8283 2019-07-24 stsp
6950 0ebf8283 2019-07-24 stsp const struct got_error *
6951 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6952 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6953 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6954 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6955 0ebf8283 2019-07-24 stsp {
6956 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6957 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6958 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6959 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6960 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6961 0ebf8283 2019-07-24 stsp
6962 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6963 0ebf8283 2019-07-24 stsp if (err)
6964 0ebf8283 2019-07-24 stsp return err;
6965 0ebf8283 2019-07-24 stsp
6966 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6967 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6968 0ebf8283 2019-07-24 stsp if (err)
6969 0ebf8283 2019-07-24 stsp goto done;
6970 0ebf8283 2019-07-24 stsp
6971 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6972 0ebf8283 2019-07-24 stsp if (err)
6973 0ebf8283 2019-07-24 stsp goto done;
6974 0ebf8283 2019-07-24 stsp
6975 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6976 0ebf8283 2019-07-24 stsp if (err)
6977 0ebf8283 2019-07-24 stsp goto done;
6978 0ebf8283 2019-07-24 stsp
6979 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6980 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6981 0ebf8283 2019-07-24 stsp if (err)
6982 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp
6984 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6985 0ebf8283 2019-07-24 stsp if (err)
6986 0ebf8283 2019-07-24 stsp goto done;
6987 0ebf8283 2019-07-24 stsp
6988 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6989 0ebf8283 2019-07-24 stsp if (err)
6990 0ebf8283 2019-07-24 stsp goto done;
6991 0ebf8283 2019-07-24 stsp
6992 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6993 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6994 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6995 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6996 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6997 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6998 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6999 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7000 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7001 0ebf8283 2019-07-24 stsp if (err)
7002 1f1abb7e 2019-08-08 stsp goto sync;
7003 0ebf8283 2019-07-24 stsp
7004 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7005 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7006 0ebf8283 2019-07-24 stsp sync:
7007 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7008 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7009 0ebf8283 2019-07-24 stsp err = sync_err;
7010 0ebf8283 2019-07-24 stsp done:
7011 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7012 0ebf8283 2019-07-24 stsp free(tree_id);
7013 818c7501 2019-07-11 stsp free(fileindex_path);
7014 818c7501 2019-07-11 stsp
7015 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7016 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7017 818c7501 2019-07-11 stsp err = unlockerr;
7018 818c7501 2019-07-11 stsp return err;
7019 818c7501 2019-07-11 stsp }
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp const struct got_error *
7022 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7023 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7024 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7025 0ebf8283 2019-07-24 stsp {
7026 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7027 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7028 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7029 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7030 0ebf8283 2019-07-24 stsp
7031 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7032 0ebf8283 2019-07-24 stsp if (err)
7033 0ebf8283 2019-07-24 stsp return err;
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7036 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7037 e600f124 2021-03-21 stsp if (err)
7038 e600f124 2021-03-21 stsp goto done;
7039 e600f124 2021-03-21 stsp
7040 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7041 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7042 0ebf8283 2019-07-24 stsp if (err)
7043 0ebf8283 2019-07-24 stsp goto done;
7044 0ebf8283 2019-07-24 stsp
7045 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7046 0ebf8283 2019-07-24 stsp if (err)
7047 0ebf8283 2019-07-24 stsp goto done;
7048 0ebf8283 2019-07-24 stsp
7049 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7050 0ebf8283 2019-07-24 stsp if (err)
7051 0ebf8283 2019-07-24 stsp goto done;
7052 0ebf8283 2019-07-24 stsp
7053 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7054 0ebf8283 2019-07-24 stsp if (err)
7055 0ebf8283 2019-07-24 stsp goto done;
7056 0ebf8283 2019-07-24 stsp
7057 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7058 a615e0e7 2020-12-16 stsp if (err)
7059 a615e0e7 2020-12-16 stsp goto done;
7060 a615e0e7 2020-12-16 stsp
7061 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7062 a615e0e7 2020-12-16 stsp if (err)
7063 a615e0e7 2020-12-16 stsp goto done;
7064 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7065 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7066 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7067 a615e0e7 2020-12-16 stsp err = sync_err;
7068 0ebf8283 2019-07-24 stsp done:
7069 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7070 a615e0e7 2020-12-16 stsp free(fileindex_path);
7071 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7072 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7073 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7074 0ebf8283 2019-07-24 stsp err = unlockerr;
7075 0ebf8283 2019-07-24 stsp return err;
7076 0ebf8283 2019-07-24 stsp }
7077 0ebf8283 2019-07-24 stsp
7078 0ebf8283 2019-07-24 stsp const struct got_error *
7079 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7080 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7081 0ebf8283 2019-07-24 stsp {
7082 0ebf8283 2019-07-24 stsp const struct got_error *err;
7083 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7084 0ebf8283 2019-07-24 stsp
7085 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7086 0ebf8283 2019-07-24 stsp if (err)
7087 0ebf8283 2019-07-24 stsp return err;
7088 0ebf8283 2019-07-24 stsp
7089 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7090 0ebf8283 2019-07-24 stsp if (err)
7091 0ebf8283 2019-07-24 stsp goto done;
7092 0ebf8283 2019-07-24 stsp
7093 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7094 0ebf8283 2019-07-24 stsp done:
7095 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7096 2822a352 2019-10-15 stsp return err;
7097 2822a352 2019-10-15 stsp }
7098 2822a352 2019-10-15 stsp
7099 2822a352 2019-10-15 stsp const struct got_error *
7100 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7101 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7102 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7103 2822a352 2019-10-15 stsp struct got_repository *repo)
7104 2822a352 2019-10-15 stsp {
7105 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7106 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7107 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7108 2822a352 2019-10-15 stsp
7109 2822a352 2019-10-15 stsp *fileindex = NULL;
7110 2822a352 2019-10-15 stsp *branch_ref = NULL;
7111 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7112 2822a352 2019-10-15 stsp
7113 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7114 2822a352 2019-10-15 stsp if (err)
7115 2822a352 2019-10-15 stsp return err;
7116 2822a352 2019-10-15 stsp
7117 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7118 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7119 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7120 2822a352 2019-10-15 stsp "update -b or different branch name required");
7121 2822a352 2019-10-15 stsp goto done;
7122 2822a352 2019-10-15 stsp }
7123 2822a352 2019-10-15 stsp
7124 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7125 2822a352 2019-10-15 stsp if (err)
7126 2822a352 2019-10-15 stsp goto done;
7127 2822a352 2019-10-15 stsp
7128 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7129 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7130 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7131 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7132 2822a352 2019-10-15 stsp &ok_arg);
7133 2822a352 2019-10-15 stsp if (err)
7134 2822a352 2019-10-15 stsp goto done;
7135 2822a352 2019-10-15 stsp
7136 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7137 2822a352 2019-10-15 stsp if (err)
7138 2822a352 2019-10-15 stsp goto done;
7139 2822a352 2019-10-15 stsp
7140 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7141 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7142 2822a352 2019-10-15 stsp done:
7143 2822a352 2019-10-15 stsp if (err) {
7144 2822a352 2019-10-15 stsp if (*branch_ref) {
7145 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7146 2822a352 2019-10-15 stsp *branch_ref = NULL;
7147 2822a352 2019-10-15 stsp }
7148 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7149 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7150 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7151 2822a352 2019-10-15 stsp }
7152 2822a352 2019-10-15 stsp if (*fileindex) {
7153 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7154 2822a352 2019-10-15 stsp *fileindex = NULL;
7155 2822a352 2019-10-15 stsp }
7156 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7157 2822a352 2019-10-15 stsp }
7158 0cb83759 2019-08-03 stsp return err;
7159 0cb83759 2019-08-03 stsp }
7160 0cb83759 2019-08-03 stsp
7161 2822a352 2019-10-15 stsp const struct got_error *
7162 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7163 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7164 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7165 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7166 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7167 2822a352 2019-10-15 stsp {
7168 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7169 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7170 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7171 2822a352 2019-10-15 stsp
7172 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7173 2822a352 2019-10-15 stsp if (err)
7174 2822a352 2019-10-15 stsp goto done;
7175 2822a352 2019-10-15 stsp
7176 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7177 2822a352 2019-10-15 stsp if (err)
7178 2822a352 2019-10-15 stsp goto done;
7179 2822a352 2019-10-15 stsp
7180 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7181 2822a352 2019-10-15 stsp worktree->path_prefix);
7182 2822a352 2019-10-15 stsp if (err)
7183 2822a352 2019-10-15 stsp goto done;
7184 2822a352 2019-10-15 stsp
7185 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7186 2822a352 2019-10-15 stsp if (err)
7187 2822a352 2019-10-15 stsp goto done;
7188 2822a352 2019-10-15 stsp
7189 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7190 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7191 2822a352 2019-10-15 stsp if (err)
7192 2822a352 2019-10-15 stsp goto sync;
7193 2822a352 2019-10-15 stsp
7194 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7195 2822a352 2019-10-15 stsp if (err)
7196 2822a352 2019-10-15 stsp goto sync;
7197 2822a352 2019-10-15 stsp
7198 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7199 6e210706 2021-01-22 stsp if (err)
7200 6e210706 2021-01-22 stsp goto sync;
7201 6e210706 2021-01-22 stsp
7202 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7203 2822a352 2019-10-15 stsp sync:
7204 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7205 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7206 2822a352 2019-10-15 stsp err = sync_err;
7207 2822a352 2019-10-15 stsp
7208 2822a352 2019-10-15 stsp done:
7209 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7210 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7211 2822a352 2019-10-15 stsp err = unlockerr;
7212 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7213 2822a352 2019-10-15 stsp
7214 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7215 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7216 2822a352 2019-10-15 stsp err = unlockerr;
7217 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7218 2822a352 2019-10-15 stsp
7219 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7220 2822a352 2019-10-15 stsp free(fileindex_path);
7221 2822a352 2019-10-15 stsp free(tree_id);
7222 2822a352 2019-10-15 stsp
7223 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7224 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7225 2822a352 2019-10-15 stsp err = unlockerr;
7226 2822a352 2019-10-15 stsp return err;
7227 2822a352 2019-10-15 stsp }
7228 2822a352 2019-10-15 stsp
7229 2822a352 2019-10-15 stsp const struct got_error *
7230 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7231 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7232 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7233 2822a352 2019-10-15 stsp {
7234 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7235 8b692cd0 2019-10-21 stsp
7236 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7237 8b692cd0 2019-10-21 stsp
7238 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7239 8b692cd0 2019-10-21 stsp
7240 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7241 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7242 8b692cd0 2019-10-21 stsp err = unlockerr;
7243 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7244 8b692cd0 2019-10-21 stsp
7245 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7246 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7247 8b692cd0 2019-10-21 stsp err = unlockerr;
7248 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7249 8b692cd0 2019-10-21 stsp
7250 8b692cd0 2019-10-21 stsp return err;
7251 2822a352 2019-10-15 stsp }
7252 2822a352 2019-10-15 stsp
7253 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7254 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7255 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7256 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7257 2db2652d 2019-08-07 stsp struct got_repository *repo;
7258 2db2652d 2019-08-07 stsp int have_changes;
7259 2db2652d 2019-08-07 stsp };
7260 2db2652d 2019-08-07 stsp
7261 2db2652d 2019-08-07 stsp const struct got_error *
7262 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7263 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7264 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7265 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7266 735ef5ac 2019-08-03 stsp {
7267 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7268 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7269 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7270 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7271 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7272 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7273 8b13ce36 2019-08-08 stsp
7274 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7275 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7276 8b13ce36 2019-08-08 stsp return NULL;
7277 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7278 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7279 735ef5ac 2019-08-03 stsp
7280 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7281 735ef5ac 2019-08-03 stsp if (ie == NULL)
7282 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7283 735ef5ac 2019-08-03 stsp
7284 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7285 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7286 735ef5ac 2019-08-03 stsp relpath) == -1)
7287 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7288 735ef5ac 2019-08-03 stsp
7289 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7290 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7291 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7292 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7293 735ef5ac 2019-08-03 stsp }
7294 735ef5ac 2019-08-03 stsp
7295 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7296 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7297 3aa5969e 2019-08-06 stsp goto done;
7298 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7299 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7300 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7301 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7302 735ef5ac 2019-08-03 stsp goto done;
7303 3aa5969e 2019-08-06 stsp }
7304 735ef5ac 2019-08-03 stsp
7305 2db2652d 2019-08-07 stsp a->have_changes = 1;
7306 2db2652d 2019-08-07 stsp
7307 735ef5ac 2019-08-03 stsp p = in_repo_path;
7308 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7309 735ef5ac 2019-08-03 stsp p++;
7310 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7311 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7312 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7313 735ef5ac 2019-08-03 stsp done:
7314 735ef5ac 2019-08-03 stsp free(in_repo_path);
7315 dc424a06 2019-08-07 stsp return err;
7316 dc424a06 2019-08-07 stsp }
7317 dc424a06 2019-08-07 stsp
7318 2db2652d 2019-08-07 stsp struct stage_path_arg {
7319 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7320 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7321 2db2652d 2019-08-07 stsp struct got_repository *repo;
7322 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7323 2db2652d 2019-08-07 stsp void *status_arg;
7324 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7325 2db2652d 2019-08-07 stsp void *patch_arg;
7326 7b5dc508 2019-10-28 stsp int staged_something;
7327 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7328 2db2652d 2019-08-07 stsp };
7329 2db2652d 2019-08-07 stsp
7330 2db2652d 2019-08-07 stsp static const struct got_error *
7331 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7332 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7333 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7334 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7335 0cb83759 2019-08-03 stsp {
7336 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7337 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7338 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7339 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7340 0cb83759 2019-08-03 stsp uint32_t stage;
7341 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7342 0aeb8099 2020-07-23 stsp struct stat sb;
7343 8b13ce36 2019-08-08 stsp
7344 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7345 8b13ce36 2019-08-08 stsp return NULL;
7346 0cb83759 2019-08-03 stsp
7347 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7348 d3e7c587 2019-08-03 stsp if (ie == NULL)
7349 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7350 0cb83759 2019-08-03 stsp
7351 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7352 2db2652d 2019-08-07 stsp relpath)== -1)
7353 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7354 0cb83759 2019-08-03 stsp
7355 0cb83759 2019-08-03 stsp switch (status) {
7356 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7357 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7358 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7359 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7360 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7361 0aeb8099 2020-07-23 stsp break;
7362 0aeb8099 2020-07-23 stsp }
7363 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7364 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7365 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7366 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7367 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7368 dc424a06 2019-08-07 stsp if (err)
7369 dc424a06 2019-08-07 stsp break;
7370 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7371 dc424a06 2019-08-07 stsp break;
7372 dc424a06 2019-08-07 stsp } else {
7373 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7374 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7375 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7376 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7377 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7378 dc424a06 2019-08-07 stsp break;
7379 dc424a06 2019-08-07 stsp }
7380 dc424a06 2019-08-07 stsp }
7381 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7382 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7383 0cb83759 2019-08-03 stsp if (err)
7384 dc424a06 2019-08-07 stsp break;
7385 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7386 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7387 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7388 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7389 0cb83759 2019-08-03 stsp else
7390 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7391 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7392 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7393 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7394 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7395 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7396 35213c7c 2020-07-23 stsp ssize_t target_len;
7397 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7398 35213c7c 2020-07-23 stsp sizeof(target_path));
7399 35213c7c 2020-07-23 stsp if (target_len == -1) {
7400 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7401 35213c7c 2020-07-23 stsp ondisk_path);
7402 35213c7c 2020-07-23 stsp break;
7403 35213c7c 2020-07-23 stsp }
7404 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7405 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7406 35213c7c 2020-07-23 stsp a->worktree->root_path);
7407 35213c7c 2020-07-23 stsp if (err)
7408 35213c7c 2020-07-23 stsp break;
7409 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7410 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7411 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7412 35213c7c 2020-07-23 stsp break;
7413 35213c7c 2020-07-23 stsp }
7414 35213c7c 2020-07-23 stsp }
7415 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7416 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7417 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7418 35213c7c 2020-07-23 stsp else
7419 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7420 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7421 0aeb8099 2020-07-23 stsp } else {
7422 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7423 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7424 0aeb8099 2020-07-23 stsp }
7425 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7426 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7427 dc424a06 2019-08-07 stsp break;
7428 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7429 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7430 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7431 0cb83759 2019-08-03 stsp break;
7432 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7433 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7434 d3e7c587 2019-08-03 stsp break;
7435 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7436 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7437 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7438 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7439 dc424a06 2019-08-07 stsp if (err)
7440 dc424a06 2019-08-07 stsp break;
7441 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7442 88f33a19 2019-08-08 stsp break;
7443 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7444 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7445 dc424a06 2019-08-07 stsp break;
7446 88f33a19 2019-08-08 stsp }
7447 dc424a06 2019-08-07 stsp }
7448 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7449 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7450 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7451 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7452 dc424a06 2019-08-07 stsp break;
7453 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7454 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7455 12463d8b 2019-12-13 stsp de_name);
7456 0cb83759 2019-08-03 stsp break;
7457 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7458 d3e7c587 2019-08-03 stsp break;
7459 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7460 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7461 ebf48fd5 2019-08-03 stsp break;
7462 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7463 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7464 2a06fe5f 2019-08-24 stsp break;
7465 0cb83759 2019-08-03 stsp default:
7466 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7467 537ac44b 2019-08-03 stsp break;
7468 0cb83759 2019-08-03 stsp }
7469 dc424a06 2019-08-07 stsp
7470 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7471 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7472 dc424a06 2019-08-07 stsp free(path_content);
7473 2db2652d 2019-08-07 stsp free(ondisk_path);
7474 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7475 0ebf8283 2019-07-24 stsp return err;
7476 0ebf8283 2019-07-24 stsp }
7477 0cb83759 2019-08-03 stsp
7478 0cb83759 2019-08-03 stsp const struct got_error *
7479 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7480 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7481 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7482 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7483 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7484 0cb83759 2019-08-03 stsp {
7485 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7486 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7487 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7488 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7489 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7490 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7491 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7492 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7493 0cb83759 2019-08-03 stsp
7494 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7495 0cb83759 2019-08-03 stsp if (err)
7496 0cb83759 2019-08-03 stsp return err;
7497 0cb83759 2019-08-03 stsp
7498 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7499 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7500 735ef5ac 2019-08-03 stsp if (err)
7501 735ef5ac 2019-08-03 stsp goto done;
7502 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7503 735ef5ac 2019-08-03 stsp if (err)
7504 735ef5ac 2019-08-03 stsp goto done;
7505 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7506 0cb83759 2019-08-03 stsp if (err)
7507 0cb83759 2019-08-03 stsp goto done;
7508 0cb83759 2019-08-03 stsp
7509 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7510 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7511 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7512 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7513 2db2652d 2019-08-07 stsp oka.repo = repo;
7514 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7515 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7516 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7517 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7518 735ef5ac 2019-08-03 stsp if (err)
7519 735ef5ac 2019-08-03 stsp goto done;
7520 735ef5ac 2019-08-03 stsp }
7521 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7522 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7523 2db2652d 2019-08-07 stsp goto done;
7524 2db2652d 2019-08-07 stsp }
7525 735ef5ac 2019-08-03 stsp
7526 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7527 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7528 2db2652d 2019-08-07 stsp spa.repo = repo;
7529 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7530 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7531 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7532 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7533 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7534 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7535 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7536 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7537 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7538 42005733 2019-08-03 stsp if (err)
7539 2db2652d 2019-08-07 stsp goto done;
7540 7b5dc508 2019-10-28 stsp }
7541 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7542 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7543 7b5dc508 2019-10-28 stsp goto done;
7544 0cb83759 2019-08-03 stsp }
7545 0cb83759 2019-08-03 stsp
7546 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7547 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7548 0cb83759 2019-08-03 stsp err = sync_err;
7549 0cb83759 2019-08-03 stsp done:
7550 735ef5ac 2019-08-03 stsp if (head_ref)
7551 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7552 735ef5ac 2019-08-03 stsp free(head_commit_id);
7553 0cb83759 2019-08-03 stsp free(fileindex_path);
7554 0cb83759 2019-08-03 stsp if (fileindex)
7555 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7556 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7557 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7558 0cb83759 2019-08-03 stsp err = unlockerr;
7559 0cb83759 2019-08-03 stsp return err;
7560 0cb83759 2019-08-03 stsp }
7561 ad493afc 2019-08-03 stsp
7562 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7563 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7564 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7565 ad493afc 2019-08-03 stsp struct got_repository *repo;
7566 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7567 ad493afc 2019-08-03 stsp void *progress_arg;
7568 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7569 2e1f37b0 2019-08-08 stsp void *patch_arg;
7570 ad493afc 2019-08-03 stsp };
7571 2e1f37b0 2019-08-08 stsp
7572 2e1f37b0 2019-08-08 stsp static const struct got_error *
7573 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7574 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7575 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7576 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7577 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7578 2e1f37b0 2019-08-08 stsp {
7579 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7580 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7581 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7582 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7583 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7584 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7585 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7586 2e1f37b0 2019-08-08 stsp
7587 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7588 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7589 2e1f37b0 2019-08-08 stsp
7590 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7591 2e1f37b0 2019-08-08 stsp if (err)
7592 2e1f37b0 2019-08-08 stsp return err;
7593 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7594 2e1f37b0 2019-08-08 stsp if (err)
7595 2e1f37b0 2019-08-08 stsp goto done;
7596 2e1f37b0 2019-08-08 stsp
7597 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7598 2e1f37b0 2019-08-08 stsp if (err)
7599 2e1f37b0 2019-08-08 stsp goto done;
7600 2e1f37b0 2019-08-08 stsp
7601 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7602 2e1f37b0 2019-08-08 stsp if (err)
7603 2e1f37b0 2019-08-08 stsp goto done;
7604 2e1f37b0 2019-08-08 stsp
7605 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7606 2e1f37b0 2019-08-08 stsp if (err)
7607 2e1f37b0 2019-08-08 stsp goto done;
7608 2e1f37b0 2019-08-08 stsp
7609 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7610 2e1f37b0 2019-08-08 stsp if (err)
7611 2e1f37b0 2019-08-08 stsp goto done;
7612 ad493afc 2019-08-03 stsp
7613 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7614 2e1f37b0 2019-08-08 stsp if (err)
7615 2e1f37b0 2019-08-08 stsp goto done;
7616 2e1f37b0 2019-08-08 stsp
7617 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7618 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7619 2e1f37b0 2019-08-08 stsp if (err)
7620 2e1f37b0 2019-08-08 stsp goto done;
7621 2e1f37b0 2019-08-08 stsp
7622 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7623 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7624 2e1f37b0 2019-08-08 stsp if (err)
7625 2e1f37b0 2019-08-08 stsp goto done;
7626 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7627 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7628 2e1f37b0 2019-08-08 stsp if (err)
7629 2e1f37b0 2019-08-08 stsp goto done;
7630 2e1f37b0 2019-08-08 stsp
7631 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7632 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7633 2e1f37b0 2019-08-08 stsp goto done;
7634 2e1f37b0 2019-08-08 stsp }
7635 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7636 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7637 2e1f37b0 2019-08-08 stsp goto done;
7638 2e1f37b0 2019-08-08 stsp }
7639 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7640 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7641 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7642 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7643 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7644 fe621944 2020-11-10 stsp nchanges++;
7645 fe621944 2020-11-10 stsp }
7646 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7647 2e1f37b0 2019-08-08 stsp int choice;
7648 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7649 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7650 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7651 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7652 2e1f37b0 2019-08-08 stsp if (err)
7653 2e1f37b0 2019-08-08 stsp goto done;
7654 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7655 2e1f37b0 2019-08-08 stsp have_content = 1;
7656 19e4b907 2019-08-08 stsp else
7657 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7658 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7659 2e1f37b0 2019-08-08 stsp break;
7660 2e1f37b0 2019-08-08 stsp }
7661 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7662 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7663 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7664 2e1f37b0 2019-08-08 stsp done:
7665 2e1f37b0 2019-08-08 stsp free(label1);
7666 2e1f37b0 2019-08-08 stsp if (blob)
7667 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7668 2e1f37b0 2019-08-08 stsp if (staged_blob)
7669 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7670 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7671 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7672 fe621944 2020-11-10 stsp err = free_err;
7673 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7674 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7675 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7676 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7677 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7678 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7679 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7680 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7681 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7682 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7683 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7684 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7685 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7686 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7687 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7688 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7689 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7690 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7691 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7692 2e1f37b0 2019-08-08 stsp }
7693 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7694 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7695 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7696 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7697 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7698 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7699 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7700 2e1f37b0 2019-08-08 stsp }
7701 2e1f37b0 2019-08-08 stsp free(path1);
7702 2e1f37b0 2019-08-08 stsp free(path2);
7703 fda8017d 2020-07-23 stsp return err;
7704 fda8017d 2020-07-23 stsp }
7705 fda8017d 2020-07-23 stsp
7706 fda8017d 2020-07-23 stsp static const struct got_error *
7707 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7708 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7709 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7710 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7711 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7712 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7713 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7714 fda8017d 2020-07-23 stsp {
7715 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7716 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7717 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7718 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
7719 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
7720 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7721 67a66647 2021-05-31 stsp FILE *f = NULL, *f_base = NULL;
7722 fda8017d 2020-07-23 stsp struct stat sb;
7723 fda8017d 2020-07-23 stsp
7724 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7725 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7726 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7727 fda8017d 2020-07-23 stsp if (err)
7728 fda8017d 2020-07-23 stsp return err;
7729 fda8017d 2020-07-23 stsp
7730 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7731 fda8017d 2020-07-23 stsp return NULL;
7732 fda8017d 2020-07-23 stsp
7733 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7734 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7735 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7736 fda8017d 2020-07-23 stsp if (err)
7737 fda8017d 2020-07-23 stsp goto done;
7738 fda8017d 2020-07-23 stsp }
7739 fda8017d 2020-07-23 stsp
7740 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7741 fda8017d 2020-07-23 stsp if (f == NULL) {
7742 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7743 fda8017d 2020-07-23 stsp path_unstaged_content);
7744 fda8017d 2020-07-23 stsp goto done;
7745 fda8017d 2020-07-23 stsp }
7746 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7747 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7748 fda8017d 2020-07-23 stsp goto done;
7749 fda8017d 2020-07-23 stsp }
7750 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7751 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7752 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7753 fda8017d 2020-07-23 stsp size_t r;
7754 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7755 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7756 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7757 fda8017d 2020-07-23 stsp goto done;
7758 fda8017d 2020-07-23 stsp }
7759 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7760 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7761 fda8017d 2020-07-23 stsp goto done;
7762 fda8017d 2020-07-23 stsp }
7763 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7764 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7765 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7766 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7767 fda8017d 2020-07-23 stsp progress_arg);
7768 fda8017d 2020-07-23 stsp } else {
7769 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7770 67a66647 2021-05-31 stsp
7771 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
7772 67a66647 2021-05-31 stsp if (err)
7773 67a66647 2021-05-31 stsp return err;
7774 67a66647 2021-05-31 stsp
7775 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7776 67a66647 2021-05-31 stsp parent) == -1) {
7777 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
7778 67a66647 2021-05-31 stsp base_path = NULL;
7779 67a66647 2021-05-31 stsp goto done;
7780 67a66647 2021-05-31 stsp }
7781 67a66647 2021-05-31 stsp
7782 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7783 67a66647 2021-05-31 stsp if (err)
7784 67a66647 2021-05-31 stsp goto done;
7785 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7786 67a66647 2021-05-31 stsp blob_base);
7787 67a66647 2021-05-31 stsp if (err)
7788 67a66647 2021-05-31 stsp goto done;
7789 67a66647 2021-05-31 stsp
7790 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7791 db590691 2021-06-02 stsp f_base, ondisk_path, ie->path,
7792 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7793 db590691 2021-06-02 stsp f, label_orig, "unstaged",
7794 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7795 fda8017d 2020-07-23 stsp }
7796 fda8017d 2020-07-23 stsp if (err)
7797 fda8017d 2020-07-23 stsp goto done;
7798 fda8017d 2020-07-23 stsp
7799 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7800 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7801 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7802 2ac8aa02 2020-11-09 stsp } else {
7803 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7804 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7805 2ac8aa02 2020-11-09 stsp }
7806 fda8017d 2020-07-23 stsp done:
7807 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7808 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7809 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7810 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7811 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7812 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7813 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7814 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7815 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
7816 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
7817 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7818 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7819 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7820 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7821 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7822 67a66647 2021-05-31 stsp free(blob_base_path);
7823 67a66647 2021-05-31 stsp free(parent);
7824 67a66647 2021-05-31 stsp free(base_path);
7825 2e1f37b0 2019-08-08 stsp return err;
7826 2e1f37b0 2019-08-08 stsp }
7827 2e1f37b0 2019-08-08 stsp
7828 ad493afc 2019-08-03 stsp static const struct got_error *
7829 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7830 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7831 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7832 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7833 ad493afc 2019-08-03 stsp {
7834 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7835 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7836 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7837 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7838 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7839 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7840 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7841 9bc94a15 2019-08-03 stsp struct stat sb;
7842 ad493afc 2019-08-03 stsp
7843 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7844 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7845 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7846 2e1f37b0 2019-08-08 stsp return NULL;
7847 2e1f37b0 2019-08-08 stsp
7848 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7849 ad493afc 2019-08-03 stsp if (ie == NULL)
7850 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7851 9bc94a15 2019-08-03 stsp
7852 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7853 9bc94a15 2019-08-03 stsp == -1)
7854 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7855 ad493afc 2019-08-03 stsp
7856 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7857 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7858 f69721c3 2019-10-21 stsp if (err)
7859 f69721c3 2019-10-21 stsp goto done;
7860 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7861 f69721c3 2019-10-21 stsp id_str) == -1) {
7862 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7863 f69721c3 2019-10-21 stsp goto done;
7864 f69721c3 2019-10-21 stsp }
7865 f69721c3 2019-10-21 stsp
7866 ad493afc 2019-08-03 stsp switch (staged_status) {
7867 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7868 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7869 ad493afc 2019-08-03 stsp blob_id, 8192);
7870 ad493afc 2019-08-03 stsp if (err)
7871 ad493afc 2019-08-03 stsp break;
7872 ad493afc 2019-08-03 stsp /* fall through */
7873 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7874 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7875 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7876 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7877 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7878 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7879 2e1f37b0 2019-08-08 stsp if (err)
7880 2e1f37b0 2019-08-08 stsp break;
7881 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7882 2e1f37b0 2019-08-08 stsp break;
7883 2e1f37b0 2019-08-08 stsp } else {
7884 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7885 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7886 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7887 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7888 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7889 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7890 2e1f37b0 2019-08-08 stsp }
7891 2e1f37b0 2019-08-08 stsp }
7892 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7893 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7894 ad493afc 2019-08-03 stsp if (err)
7895 ad493afc 2019-08-03 stsp break;
7896 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7897 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7898 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7899 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7900 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7901 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7902 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7903 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7904 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7905 ea7786be 2020-07-23 stsp break;
7906 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7907 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7908 36bf999c 2020-07-23 stsp char *staged_target;
7909 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7910 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7911 36bf999c 2020-07-23 stsp if (err)
7912 36bf999c 2020-07-23 stsp goto done;
7913 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7914 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7915 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7916 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7917 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7918 36bf999c 2020-07-23 stsp free(staged_target);
7919 dfe9fba0 2020-07-23 stsp } else {
7920 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7921 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7922 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7923 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7924 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7925 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7926 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7927 dfe9fba0 2020-07-23 stsp }
7928 ea7786be 2020-07-23 stsp break;
7929 ea7786be 2020-07-23 stsp default:
7930 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7931 ea7786be 2020-07-23 stsp break;
7932 ea7786be 2020-07-23 stsp }
7933 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7934 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7935 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7936 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7937 2ac8aa02 2020-11-09 stsp }
7938 ad493afc 2019-08-03 stsp break;
7939 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7940 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7941 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7942 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7943 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7944 2e1f37b0 2019-08-08 stsp if (err)
7945 2e1f37b0 2019-08-08 stsp break;
7946 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7947 2e1f37b0 2019-08-08 stsp break;
7948 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7949 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7950 2e1f37b0 2019-08-08 stsp break;
7951 2e1f37b0 2019-08-08 stsp }
7952 2e1f37b0 2019-08-08 stsp }
7953 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7954 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7955 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7956 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7957 9bc94a15 2019-08-03 stsp if (err)
7958 9bc94a15 2019-08-03 stsp break;
7959 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7960 ad493afc 2019-08-03 stsp break;
7961 ad493afc 2019-08-03 stsp }
7962 f69721c3 2019-10-21 stsp done:
7963 ad493afc 2019-08-03 stsp free(ondisk_path);
7964 ad493afc 2019-08-03 stsp if (blob_base)
7965 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7966 ad493afc 2019-08-03 stsp if (blob_staged)
7967 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7968 f69721c3 2019-10-21 stsp free(id_str);
7969 f69721c3 2019-10-21 stsp free(label_orig);
7970 ad493afc 2019-08-03 stsp return err;
7971 ad493afc 2019-08-03 stsp }
7972 ad493afc 2019-08-03 stsp
7973 ad493afc 2019-08-03 stsp const struct got_error *
7974 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7975 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7976 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7977 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7978 ad493afc 2019-08-03 stsp struct got_repository *repo)
7979 ad493afc 2019-08-03 stsp {
7980 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7981 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7982 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7983 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7984 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7985 ad493afc 2019-08-03 stsp
7986 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7987 ad493afc 2019-08-03 stsp if (err)
7988 ad493afc 2019-08-03 stsp return err;
7989 ad493afc 2019-08-03 stsp
7990 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7991 ad493afc 2019-08-03 stsp if (err)
7992 ad493afc 2019-08-03 stsp goto done;
7993 ad493afc 2019-08-03 stsp
7994 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7995 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7996 ad493afc 2019-08-03 stsp upa.repo = repo;
7997 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7998 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7999 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8000 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8001 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8002 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8003 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8004 ad493afc 2019-08-03 stsp if (err)
8005 ad493afc 2019-08-03 stsp goto done;
8006 ad493afc 2019-08-03 stsp }
8007 ad493afc 2019-08-03 stsp
8008 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8009 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8010 ad493afc 2019-08-03 stsp err = sync_err;
8011 ad493afc 2019-08-03 stsp done:
8012 ad493afc 2019-08-03 stsp free(fileindex_path);
8013 ad493afc 2019-08-03 stsp if (fileindex)
8014 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8015 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8016 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8017 ad493afc 2019-08-03 stsp err = unlockerr;
8018 ad493afc 2019-08-03 stsp return err;
8019 ad493afc 2019-08-03 stsp }
8020 b2118c49 2020-07-28 stsp
8021 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8022 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8023 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8024 b2118c49 2020-07-28 stsp void *info_arg;
8025 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8026 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8027 b2118c49 2020-07-28 stsp void *cancel_arg;
8028 b2118c49 2020-07-28 stsp };
8029 b2118c49 2020-07-28 stsp
8030 b2118c49 2020-07-28 stsp static const struct got_error *
8031 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8032 b2118c49 2020-07-28 stsp {
8033 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8034 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8035 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8036 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8037 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8038 b2118c49 2020-07-28 stsp int stage;
8039 b2118c49 2020-07-28 stsp
8040 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8041 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8042 b2118c49 2020-07-28 stsp
8043 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8044 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8045 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8046 b2118c49 2020-07-28 stsp break;
8047 b2118c49 2020-07-28 stsp }
8048 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8049 b2118c49 2020-07-28 stsp return NULL;
8050 b2118c49 2020-07-28 stsp
8051 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8052 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8053 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8054 b2118c49 2020-07-28 stsp }
8055 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8056 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8057 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8058 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8059 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8060 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8061 b2118c49 2020-07-28 stsp }
8062 b2118c49 2020-07-28 stsp
8063 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8064 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8065 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8066 b2118c49 2020-07-28 stsp }
8067 b2118c49 2020-07-28 stsp
8068 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8069 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8070 b2118c49 2020-07-28 stsp }
8071 b2118c49 2020-07-28 stsp
8072 b2118c49 2020-07-28 stsp const struct got_error *
8073 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8074 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8075 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8076 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8077 b2118c49 2020-07-28 stsp
8078 b2118c49 2020-07-28 stsp {
8079 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8080 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8081 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8082 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8083 b2118c49 2020-07-28 stsp
8084 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8085 b2118c49 2020-07-28 stsp if (err)
8086 b2118c49 2020-07-28 stsp return err;
8087 b2118c49 2020-07-28 stsp
8088 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8089 b2118c49 2020-07-28 stsp if (err)
8090 b2118c49 2020-07-28 stsp goto done;
8091 b2118c49 2020-07-28 stsp
8092 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8093 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8094 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8095 b2118c49 2020-07-28 stsp arg.paths = paths;
8096 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8097 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8098 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8099 b2118c49 2020-07-28 stsp &arg);
8100 b2118c49 2020-07-28 stsp done:
8101 b2118c49 2020-07-28 stsp free(fileindex_path);
8102 b2118c49 2020-07-28 stsp if (fileindex)
8103 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8104 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8105 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8106 b2118c49 2020-07-28 stsp err = unlockerr;
8107 b2118c49 2020-07-28 stsp return err;
8108 b2118c49 2020-07-28 stsp }